mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-09 03:01:18 +02:00
Merge branch 'pr/3570' into 3.1
This commit is contained in:
commit
d474671d09
@ -173,6 +173,8 @@ class Upload extends Controller {
|
|||||||
}
|
}
|
||||||
while(file_exists("$base/$relativeFilePath")) {
|
while(file_exists("$base/$relativeFilePath")) {
|
||||||
$i = isset($i) ? ($i+1) : 2;
|
$i = isset($i) ? ($i+1) : 2;
|
||||||
|
$oldFilePath = $relativeFilePath;
|
||||||
|
|
||||||
$pattern = '/([0-9]+$)/';
|
$pattern = '/([0-9]+$)/';
|
||||||
if(preg_match($pattern, $fileTitle)) {
|
if(preg_match($pattern, $fileTitle)) {
|
||||||
$fileTitle = preg_replace($pattern, $i, $fileTitle);
|
$fileTitle = preg_replace($pattern, $i, $fileTitle);
|
||||||
@ -180,6 +182,7 @@ class Upload extends Controller {
|
|||||||
$fileTitle .= $i;
|
$fileTitle .= $i;
|
||||||
}
|
}
|
||||||
$relativeFilePath = $relativeFolderPath . $fileTitle . $fileSuffix;
|
$relativeFilePath = $relativeFolderPath . $fileTitle . $fileSuffix;
|
||||||
|
|
||||||
if($oldFilePath == $relativeFilePath && $i > 2) {
|
if($oldFilePath == $relativeFilePath && $i > 2) {
|
||||||
user_error("Couldn't fix $relativeFilePath with $i tries", E_USER_ERROR);
|
user_error("Couldn't fix $relativeFilePath with $i tries", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
@ -560,6 +560,70 @@ class UploadTest extends SapphireTest {
|
|||||||
$image->delete();
|
$image->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFileVersioningWithAnExistingFile() {
|
||||||
|
$upload = function($tmpFileName) {
|
||||||
|
// create tmp file
|
||||||
|
$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' => 'jpg',
|
||||||
|
'error' => UPLOAD_ERR_OK,
|
||||||
|
);
|
||||||
|
|
||||||
|
$v = new UploadTest_Validator();
|
||||||
|
|
||||||
|
// test upload into default folder
|
||||||
|
$u = new Upload();
|
||||||
|
$u->setReplaceFile(false);
|
||||||
|
$u->setValidator($v);
|
||||||
|
$u->load($tmpFile);
|
||||||
|
return $u->getFile();
|
||||||
|
};
|
||||||
|
|
||||||
|
$file1 = $upload('UploadTest-testUpload.jpg');
|
||||||
|
$this->assertEquals(
|
||||||
|
'UploadTest-testUpload.jpg',
|
||||||
|
$file1->Name,
|
||||||
|
'File does not receive new name'
|
||||||
|
);
|
||||||
|
|
||||||
|
$file2 = $upload('UploadTest-testUpload.jpg');
|
||||||
|
$this->assertEquals(
|
||||||
|
'UploadTest-testUpload2.jpg',
|
||||||
|
$file2->Name,
|
||||||
|
'File does receive new name'
|
||||||
|
);
|
||||||
|
|
||||||
|
$file3 = $upload('UploadTest-testUpload.jpg');
|
||||||
|
$this->assertEquals(
|
||||||
|
'UploadTest-testUpload3.jpg',
|
||||||
|
$file3->Name,
|
||||||
|
'File does receive new name'
|
||||||
|
);
|
||||||
|
|
||||||
|
$file4 = $upload('UploadTest-testUpload3.jpg');
|
||||||
|
$this->assertEquals(
|
||||||
|
'UploadTest-testUpload4.jpg',
|
||||||
|
$file4->Name,
|
||||||
|
'File does receive new name'
|
||||||
|
);
|
||||||
|
|
||||||
|
$file1->delete();
|
||||||
|
$file2->delete();
|
||||||
|
$file3->delete();
|
||||||
|
$file4->delete();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
class UploadTest_Validator extends Upload_Validator implements TestOnly {
|
class UploadTest_Validator extends Upload_Validator implements TestOnly {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user