diff --git a/filesystem/Upload.php b/filesystem/Upload.php index 399f10801d1..c5d9eae7481 100644 --- a/filesystem/Upload.php +++ b/filesystem/Upload.php @@ -466,6 +466,8 @@ 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', @@ -473,7 +475,7 @@ public function validate() { PR_MEDIUM, 'Argument 1: Filesize (e.g. 1MB)' ), - File::format_size($this->getAllowedMaxFileSize($pathInfo['extension'])) + $arg ); return false; } diff --git a/tests/filesystem/UploadTest.php b/tests/filesystem/UploadTest.php index e21bea865d5..68b695be107 100644 --- a/tests/filesystem/UploadTest.php +++ b/tests/filesystem/UploadTest.php @@ -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() { @@ -78,7 +132,7 @@ function testUploadDoesNotAllowUnknownExtension() { 'type' => 'text/plaintext', 'size' => filesize($tmpFilePath), 'tmp_name' => $tmpFilePath, - 'extension' => 'txt', + 'extension' => 'php', 'error' => UPLOAD_ERR_OK, ); @@ -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() { @@ -141,7 +194,7 @@ function testUploadDeniesNoExtensionFilesIfNoEmptyStringSetForValidatorExtension 'type' => 'text/plaintext', 'size' => filesize($tmpFilePath), 'tmp_name' => $tmpFilePath, - 'extension' => 'txt', + 'extension' => '', 'error' => UPLOAD_ERR_OK, ); @@ -171,7 +224,7 @@ function testUploadTarGzFileTwiceAppendsNumber() { 'type' => 'text/plaintext', 'size' => filesize($tmpFilePath), 'tmp_name' => $tmpFilePath, - 'extension' => 'txt', + 'extension' => 'tar.gz', 'error' => UPLOAD_ERR_OK, ); @@ -260,6 +313,8 @@ 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', @@ -267,7 +322,7 @@ public function validate() { PR_MEDIUM, 'Argument 1: Filesize (e.g. 1MB)' ), - File::format_size($this->getAllowedMaxFileSize($pathInfo['extension'])) + $arg ); return false; }