From 3c1e82b42c282ab64dfe7f5a68a50f59d8ebcc69 Mon Sep 17 00:00:00 2001 From: Devlin Date: Tue, 4 Mar 2014 13:10:48 +0100 Subject: [PATCH] Upload: retrieve existing File if an object without an ID is given and replaceFile=true --- filesystem/Upload.php | 19 ++++--- tests/filesystem/UploadTest.php | 87 +++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 7 deletions(-) diff --git a/filesystem/Upload.php b/filesystem/Upload.php index 90cb343dd..a6ab0beba 100644 --- a/filesystem/Upload.php +++ b/filesystem/Upload.php @@ -139,14 +139,19 @@ class Upload extends Controller { // Create a new file record (or try to retrieve an existing one) if(!$this->file) { $fileClass = File::get_class_for_file_extension(pathinfo($tmpFile['name'], PATHINFO_EXTENSION)); - if($this->replaceFile) { - $this->file = File::get() - ->filter(array( - 'Name' => $fileName, - 'ParentID' => $parentFolder ? $parentFolder->ID : 0 - ))->First(); + $this->file = new $fileClass(); + } + if(!$this->file->ID && $this->replaceFile) { + $fileClass = $this->file->class; + $file = File::get() + ->filter(array( + 'ClassName' => $fileClass, + 'Name' => $fileName, + 'ParentID' => $parentFolder ? $parentFolder->ID : 0 + ))->First(); + if($file) { + $this->file = $file; } - if(!$this->file) $this->file = new $fileClass(); } // if filename already exists, version the filename (e.g. test.gif to test1.gif) diff --git a/tests/filesystem/UploadTest.php b/tests/filesystem/UploadTest.php index d5b255d35..48a833027 100644 --- a/tests/filesystem/UploadTest.php +++ b/tests/filesystem/UploadTest.php @@ -377,6 +377,93 @@ class UploadTest extends SapphireTest { $file->delete(); $file2->delete(); } + + public function testReplaceFileWithLoadIntoFile() { + // create tmp file + $tmpFileName = 'UploadTest-testUpload.txt'; + $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; + $tmpFileContent = ''; + for ($i = 0; $i < 10000; $i++) + $tmpFileContent .= '0'; + file_put_contents($tmpFilePath, $tmpFileContent); + + // emulates the $_FILES array + $tmpFile = array( + 'name' => $tmpFileName, + 'type' => 'text/plaintext', + 'size' => filesize($tmpFilePath), + 'tmp_name' => $tmpFilePath, + 'extension' => 'txt', + 'error' => UPLOAD_ERR_OK, + ); + + // Make sure there are none here, otherwise they get renamed incorrectly for the test. + $this->deleteTestUploadFiles("/UploadTest-testUpload.*/"); + + $v = new UploadTest_Validator(); + + // test upload into default folder + $u = new Upload(); + $u->setValidator($v); + $u->load($tmpFile); + $file = $u->getFile(); + + $this->assertEquals( + 'UploadTest-testUpload.txt', + $file->Name, + 'File is uploaded without extension' + ); + $this->assertFileExists( + BASE_PATH . '/' . $file->getFilename(), + 'File exists' + ); + + // replace=true + $u = new Upload(); + $u->setValidator($v); + $u->setReplaceFile(true); + $u->loadIntoFile($tmpFile, new File()); + $file2 = $u->getFile(); + $this->assertEquals( + 'UploadTest-testUpload.txt', + $file2->Name, + 'File does not receive new name' + ); + $this->assertFileExists( + BASE_PATH . '/' . $file2->getFilename(), + 'File exists' + ); + $this->assertEquals( + $file->ID, + $file2->ID, + 'File database record is the same' + ); + + // replace=false + $u = new Upload(); + $u->setValidator($v); + $u->setReplaceFile(false); + $u->loadIntoFile($tmpFile, new File()); + $file3 = $u->getFile(); + $this->assertEquals( + 'UploadTest-testUpload2.txt', + $file3->Name, + 'File does receive new name' + ); + $this->assertFileExists( + BASE_PATH . '/' . $file3->getFilename(), + 'File exists' + ); + $this->assertGreaterThan( + $file2->ID, + $file3->ID, + 'File database record is not the same' + ); + + $file->delete(); + $file2->delete(); + $file3->delete(); + } } class UploadTest_Validator extends Upload_Validator implements TestOnly {