mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Upload: retrieve existing File if an object without an ID is given and replaceFile=true
This commit is contained in:
parent
1cc366fe23
commit
3c1e82b42c
@ -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)
|
||||
|
@ -378,6 +378,93 @@ class UploadTest extends SapphireTest {
|
||||
$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 {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user