BUGFIX Fixed Upload and checking for size with files that don't have any extension (from r101061)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@111580 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-10-04 04:44:03 +00:00
parent 37629a9c89
commit 81ce1e358d
2 changed files with 67 additions and 10 deletions

View File

@ -466,6 +466,8 @@ class Upload_Validator {
$pathInfo = pathinfo($this->tmpFile['name']);
// filesize validation
if(!$this->isValidSize()) {
$ext = (isset($pathInfo['extension'])) ? $pathInfo['extension'] : '';
$arg = File::format_size($this->getAllowedMaxFileSize($ext));
$this->errors[] = sprintf(
_t(
'File.TOOLARGE',
@ -473,7 +475,7 @@ class Upload_Validator {
PR_MEDIUM,
'Argument 1: Filesize (e.g. 1MB)'
),
File::format_size($this->getAllowedMaxFileSize($pathInfo['extension']))
$arg
);
return false;
}

View File

@ -61,12 +61,8 @@ class UploadTest extends SapphireTest {
}
function testAllowedFilesize() {
// @todo
}
function testUploadDoesNotAllowUnknownExtension() {
// create tmp file
$tmpFileName = 'UploadTest_testUpload.php';
$tmpFileName = 'UploadTest_testUpload.txt';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = '';
for($i=0; $i<10000; $i++) $tmpFileContent .= '0';
@ -82,6 +78,64 @@ class UploadTest extends SapphireTest {
'error' => UPLOAD_ERR_OK,
);
$v = new UploadTest_Validator();
$v->setAllowedMaxFileSize(array('txt' => 10));
// test upload into default folder
$u1 = new Upload();
$u1->setValidator($v);
$result = $u1->load($tmpFile);
$this->assertFalse($result, 'Load failed because size was too big');
}
function testAllowedSizeOnFileWithNoExtension() {
// create tmp file
$tmpFileName = 'UploadTest_testUpload';
$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' => '',
'error' => UPLOAD_ERR_OK,
);
$v = new UploadTest_Validator();
$v->setAllowedMaxFileSize(array('' => 10));
// test upload into default folder
$u1 = new Upload();
$u1->setValidator($v);
$result = $u1->load($tmpFile);
$this->assertFalse($result, 'Load failed because size was too big');
}
function testUploadDoesNotAllowUnknownExtension() {
// create tmp file
$tmpFileName = 'UploadTest_testUpload.php';
$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' => 'php',
'error' => UPLOAD_ERR_OK,
);
$v = new UploadTest_Validator();
$v->setAllowedExtensions(array('txt'));
@ -91,7 +145,6 @@ class UploadTest extends SapphireTest {
$result = $u->load($tmpFile);
$this->assertFalse($result, 'Load failed because extension was not accepted');
$this->assertEquals(1, count($u->getErrors()), 'There is a single error of the file extension');
}
function testUploadAcceptsAllowedExtension() {
@ -141,7 +194,7 @@ class UploadTest extends SapphireTest {
'type' => 'text/plaintext',
'size' => filesize($tmpFilePath),
'tmp_name' => $tmpFilePath,
'extension' => 'txt',
'extension' => '',
'error' => UPLOAD_ERR_OK,
);
@ -171,7 +224,7 @@ class UploadTest extends SapphireTest {
'type' => 'text/plaintext',
'size' => filesize($tmpFilePath),
'tmp_name' => $tmpFilePath,
'extension' => 'txt',
'extension' => 'tar.gz',
'error' => UPLOAD_ERR_OK,
);
@ -260,6 +313,8 @@ class UploadTest_Validator extends Upload_Validator implements TestOnly {
// filesize validation
if(!$this->isValidSize()) {
$ext = (isset($pathInfo['extension'])) ? $pathInfo['extension'] : '';
$arg = File::format_size($this->getAllowedMaxFileSize($ext));
$this->errors[] = sprintf(
_t(
'File.TOOLARGE',
@ -267,7 +322,7 @@ class UploadTest_Validator extends Upload_Validator implements TestOnly {
PR_MEDIUM,
'Argument 1: Filesize (e.g. 1MB)'
),
File::format_size($this->getAllowedMaxFileSize($pathInfo['extension']))
$arg
);
return false;
}