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

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@101061 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2010-03-15 20:49:35 +00:00 committed by Sam Minnee
parent 8bdb806180
commit 4f1b504cf5
2 changed files with 67 additions and 10 deletions

View File

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

View File

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