Skip to content

Commit

Permalink
BUGFIX Fixed Upload and checking for size with files that don't have …
Browse files Browse the repository at this point in the history
…any extension (from r101061)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@111580 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
Sam Minnee committed Oct 4, 2010
1 parent 37629a9 commit 81ce1e3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
4 changes: 3 additions & 1 deletion filesystem/Upload.php
Expand Up @@ -466,14 +466,16 @@ public function validate() {
$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',
'Filesize is too large, maximum %s allowed.',
PR_MEDIUM,
'Argument 1: Filesize (e.g. 1MB)'
),
File::format_size($this->getAllowedMaxFileSize($pathInfo['extension']))
$arg
);
return false;
}
Expand Down
67 changes: 61 additions & 6 deletions tests/filesystem/UploadTest.php
Expand Up @@ -61,7 +61,61 @@ function testUpload() {
}

function testAllowedFilesize() {
// @todo
// 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,
);

$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() {
Expand All @@ -78,7 +132,7 @@ function testUploadDoesNotAllowUnknownExtension() {
'type' => 'text/plaintext',
'size' => filesize($tmpFilePath),
'tmp_name' => $tmpFilePath,
'extension' => 'txt',
'extension' => 'php',
'error' => UPLOAD_ERR_OK,
);

Expand All @@ -91,7 +145,6 @@ function testUploadDoesNotAllowUnknownExtension() {
$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() {
Expand Down Expand Up @@ -141,7 +194,7 @@ function testUploadDeniesNoExtensionFilesIfNoEmptyStringSetForValidatorExtension
'type' => 'text/plaintext',
'size' => filesize($tmpFilePath),
'tmp_name' => $tmpFilePath,
'extension' => 'txt',
'extension' => '',
'error' => UPLOAD_ERR_OK,
);

Expand Down Expand Up @@ -171,7 +224,7 @@ function testUploadTarGzFileTwiceAppendsNumber() {
'type' => 'text/plaintext',
'size' => filesize($tmpFilePath),
'tmp_name' => $tmpFilePath,
'extension' => 'txt',
'extension' => 'tar.gz',
'error' => UPLOAD_ERR_OK,
);

Expand Down Expand Up @@ -260,14 +313,16 @@ public function validate() {
// filesize validation

if(!$this->isValidSize()) {
$ext = (isset($pathInfo['extension'])) ? $pathInfo['extension'] : '';
$arg = File::format_size($this->getAllowedMaxFileSize($ext));
$this->errors[] = sprintf(
_t(
'File.TOOLARGE',
'Filesize is too large, maximum %s allowed.',
PR_MEDIUM,
'Argument 1: Filesize (e.g. 1MB)'
),
File::format_size($this->getAllowedMaxFileSize($pathInfo['extension']))
$arg
);
return false;
}
Expand Down

0 comments on commit 81ce1e3

Please sign in to comment.