2008-04-06 10:20:13 +02:00
|
|
|
<?php
|
2008-06-15 15:33:53 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-06-15 15:33:53 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2008-04-06 10:20:13 +02:00
|
|
|
class UploadTest extends SapphireTest {
|
2011-03-30 08:49:11 +02:00
|
|
|
static $fixture_file = 'UploadTest.yml';
|
2008-04-08 08:08:31 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
function testUpload() {
|
|
|
|
// create tmp file
|
2011-08-26 17:57:05 +02:00
|
|
|
$tmpFileName = 'UploadTest-testUpload.txt';
|
2008-04-06 10:20:13 +02:00
|
|
|
$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,
|
|
|
|
);
|
|
|
|
|
2010-05-25 05:42:52 +02:00
|
|
|
$v = new UploadTest_Validator();
|
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
// test upload into default folder
|
|
|
|
$u1 = new Upload();
|
2010-05-25 05:42:52 +02:00
|
|
|
$u1->setValidator($v);
|
2008-04-06 10:20:13 +02:00
|
|
|
$u1->load($tmpFile);
|
|
|
|
$file1 = $u1->getFile();
|
|
|
|
$this->assertTrue(
|
|
|
|
file_exists($file1->getFullPath()),
|
|
|
|
'File upload to standard directory in /assets'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
2010-10-04 06:29:58 +02:00
|
|
|
(strpos($file1->getFullPath(), Director::baseFolder() . '/assets/' . Upload::$uploads_folder) !== false),
|
2008-04-06 10:20:13 +02:00
|
|
|
'File upload to standard directory in /assets'
|
|
|
|
);
|
|
|
|
$file1->delete();
|
|
|
|
|
|
|
|
// test upload into custom folder
|
2011-08-26 17:57:05 +02:00
|
|
|
$customFolder = 'UploadTest-testUpload';
|
2008-04-06 10:20:13 +02:00
|
|
|
$u2 = new Upload();
|
|
|
|
$u2->load($tmpFile, $customFolder);
|
|
|
|
$file2 = $u2->getFile();
|
|
|
|
$this->assertTrue(
|
|
|
|
file_exists($file2->getFullPath()),
|
|
|
|
'File upload to custom directory in /assets'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
2010-10-04 06:29:58 +02:00
|
|
|
(strpos($file2->getFullPath(), Director::baseFolder() . '/assets/' . $customFolder) !== false),
|
2008-04-06 10:20:13 +02:00
|
|
|
'File upload to custom directory in /assets'
|
|
|
|
);
|
|
|
|
$file2->delete();
|
|
|
|
|
|
|
|
unlink($tmpFilePath);
|
2010-10-04 06:30:14 +02:00
|
|
|
rmdir(Director::baseFolder() . '/assets/' . $customFolder);
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAllowedFilesize() {
|
2010-10-04 06:44:03 +02:00
|
|
|
// create tmp file
|
2011-08-26 17:57:05 +02:00
|
|
|
$tmpFileName = 'UploadTest-testUpload.txt';
|
2010-10-04 06:44:03 +02:00
|
|
|
$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
|
2011-08-26 17:57:05 +02:00
|
|
|
$tmpFileName = 'UploadTest-testUpload';
|
2010-10-04 06:44:03 +02:00
|
|
|
$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');
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
|
|
|
|
2010-10-04 06:29:58 +02:00
|
|
|
function testUploadDoesNotAllowUnknownExtension() {
|
|
|
|
// create tmp file
|
2011-08-26 17:57:05 +02:00
|
|
|
$tmpFileName = 'UploadTest-testUpload.php';
|
2010-10-04 06:29:58 +02:00
|
|
|
$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,
|
2010-10-04 06:44:03 +02:00
|
|
|
'extension' => 'php',
|
2010-10-04 06:29:58 +02:00
|
|
|
'error' => UPLOAD_ERR_OK,
|
|
|
|
);
|
|
|
|
|
|
|
|
$v = new UploadTest_Validator();
|
|
|
|
$v->setAllowedExtensions(array('txt'));
|
|
|
|
|
|
|
|
// test upload into default folder
|
|
|
|
$u = new Upload();
|
|
|
|
$u->setValidator($v);
|
|
|
|
$result = $u->load($tmpFile);
|
|
|
|
|
|
|
|
$this->assertFalse($result, 'Load failed because extension was not accepted');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testUploadAcceptsAllowedExtension() {
|
|
|
|
// create tmp file
|
2011-08-26 17:57:05 +02:00
|
|
|
$tmpFileName = 'UploadTest-testUpload.txt';
|
2010-10-04 06:29:58 +02:00
|
|
|
$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->setAllowedExtensions(array('txt'));
|
|
|
|
|
|
|
|
// test upload into default folder
|
|
|
|
$u = new Upload();
|
|
|
|
$u->setValidator($v);
|
|
|
|
$u->load($tmpFile);
|
|
|
|
$file = $u->getFile();
|
|
|
|
$this->assertTrue(
|
|
|
|
file_exists($file->getFullPath()),
|
|
|
|
'File upload to custom directory in /assets'
|
|
|
|
);
|
|
|
|
$file->delete();
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
|
|
|
|
2010-10-04 06:42:13 +02:00
|
|
|
function testUploadDeniesNoExtensionFilesIfNoEmptyStringSetForValidatorExtensions() {
|
|
|
|
// create tmp file
|
2011-08-26 17:57:05 +02:00
|
|
|
$tmpFileName = 'UploadTest-testUpload';
|
2010-10-04 06:42:13 +02:00
|
|
|
$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,
|
2010-10-04 06:44:03 +02:00
|
|
|
'extension' => '',
|
2010-10-04 06:42:13 +02:00
|
|
|
'error' => UPLOAD_ERR_OK,
|
|
|
|
);
|
|
|
|
|
|
|
|
$v = new UploadTest_Validator();
|
|
|
|
$v->setAllowedExtensions(array('txt'));
|
|
|
|
|
|
|
|
// test upload into default folder
|
|
|
|
$u = new Upload();
|
|
|
|
$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');
|
|
|
|
|
|
|
|
}
|
2010-10-15 01:55:31 +02:00
|
|
|
|
|
|
|
// Delete files in the default uploads directory that match the name pattern.
|
|
|
|
// @param String $namePattern A regular expression applied to files in the directory. If the name matches
|
|
|
|
// the pattern, it is deleted. Directories, . and .. are excluded.
|
|
|
|
function deleteTestUploadFiles($namePattern) {
|
|
|
|
$tmpFolder = ASSETS_PATH . "/" . Upload::$uploads_folder;
|
|
|
|
$files = scandir($tmpFolder);
|
|
|
|
foreach ($files as $f) {
|
|
|
|
if ($f == "." || $f == ".." || is_dir("$tmpFolder/$f")) continue;
|
|
|
|
if (preg_match($namePattern, $f)) unlink("$tmpFolder/$f");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-04 06:42:13 +02:00
|
|
|
function testUploadTarGzFileTwiceAppendsNumber() {
|
|
|
|
// create tmp file
|
2011-08-26 17:57:05 +02:00
|
|
|
$tmpFileName = 'UploadTest-testUpload.tar.gz';
|
2010-10-04 06:42:13 +02:00
|
|
|
$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,
|
2010-10-04 06:44:03 +02:00
|
|
|
'extension' => 'tar.gz',
|
2010-10-04 06:42:13 +02:00
|
|
|
'error' => UPLOAD_ERR_OK,
|
|
|
|
);
|
2010-10-15 01:55:31 +02:00
|
|
|
|
|
|
|
// Make sure there are none here, otherwise they get renamed incorrectly for the test.
|
2011-08-26 17:57:05 +02:00
|
|
|
$this->deleteTestUploadFiles("/UploadTest-testUpload.*tar\.gz/");
|
2010-10-15 01:55:31 +02:00
|
|
|
|
2010-10-04 06:42:13 +02:00
|
|
|
// test upload into default folder
|
|
|
|
$u = new Upload();
|
|
|
|
$u->load($tmpFile);
|
|
|
|
$file = $u->getFile();
|
|
|
|
$this->assertEquals(
|
2011-08-26 17:57:05 +02:00
|
|
|
'UploadTest-testUpload.tar.gz',
|
2010-10-04 06:42:13 +02:00
|
|
|
$file->Name,
|
|
|
|
'File has a name without a number because it\'s not a duplicate'
|
|
|
|
);
|
|
|
|
|
|
|
|
$u = new Upload();
|
|
|
|
$u->load($tmpFile);
|
|
|
|
$file2 = $u->getFile();
|
|
|
|
$this->assertEquals(
|
2011-08-26 17:57:05 +02:00
|
|
|
'UploadTest-testUpload2.tar.gz',
|
2010-10-04 06:42:13 +02:00
|
|
|
$file2->Name,
|
|
|
|
'File receives a number attached to the end before the extension'
|
|
|
|
);
|
|
|
|
|
|
|
|
$file->delete();
|
|
|
|
$file2->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
function testUploadFileWithNoExtensionTwiceAppendsNumber() {
|
|
|
|
// create tmp file
|
2011-08-26 17:57:05 +02:00
|
|
|
$tmpFileName = 'UploadTest-testUpload';
|
2010-10-04 06:42:13 +02:00
|
|
|
$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,
|
|
|
|
);
|
|
|
|
|
2010-10-15 01:55:31 +02:00
|
|
|
// Make sure there are none here, otherwise they get renamed incorrectly for the test.
|
2011-08-26 17:57:05 +02:00
|
|
|
$this->deleteTestUploadFiles("/UploadTest-testUpload.*/");
|
2010-10-15 01:55:31 +02:00
|
|
|
|
2010-10-04 06:42:13 +02:00
|
|
|
$v = new UploadTest_Validator();
|
|
|
|
$v->setAllowedExtensions(array(''));
|
2010-10-15 01:55:31 +02:00
|
|
|
|
2010-10-04 06:42:13 +02:00
|
|
|
// test upload into default folder
|
|
|
|
$u = new Upload();
|
|
|
|
$u->setValidator($v);
|
|
|
|
$u->load($tmpFile);
|
|
|
|
$file = $u->getFile();
|
2010-10-15 01:55:31 +02:00
|
|
|
|
2010-10-04 06:42:13 +02:00
|
|
|
$this->assertEquals(
|
2011-08-26 17:57:05 +02:00
|
|
|
'UploadTest-testUpload',
|
2010-10-04 06:42:13 +02:00
|
|
|
$file->Name,
|
|
|
|
'File is uploaded without extension'
|
|
|
|
);
|
|
|
|
|
|
|
|
$u = new Upload();
|
|
|
|
$u->setValidator($v);
|
|
|
|
$u->load($tmpFile);
|
|
|
|
$file2 = $u->getFile();
|
|
|
|
$this->assertEquals(
|
2011-08-26 17:57:05 +02:00
|
|
|
'UploadTest-testUpload-2',
|
2010-10-04 06:42:13 +02:00
|
|
|
$file2->Name,
|
|
|
|
'File receives a number attached to the end'
|
|
|
|
);
|
|
|
|
|
|
|
|
$file->delete();
|
|
|
|
$file2->delete();
|
|
|
|
}
|
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2010-05-25 05:42:52 +02:00
|
|
|
class UploadTest_Validator extends Upload_Validator implements TestOnly {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Looser check validation that doesn't do is_upload_file()
|
|
|
|
* checks as we're faking a POST request that PHP didn't generate
|
|
|
|
* itself.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function validate() {
|
|
|
|
$pathInfo = pathinfo($this->tmpFile['name']);
|
|
|
|
// filesize validation
|
|
|
|
|
|
|
|
if(!$this->isValidSize()) {
|
2010-10-04 06:44:03 +02:00
|
|
|
$ext = (isset($pathInfo['extension'])) ? $pathInfo['extension'] : '';
|
|
|
|
$arg = File::format_size($this->getAllowedMaxFileSize($ext));
|
2012-05-01 21:44:54 +02:00
|
|
|
$this->errors[] = _t(
|
|
|
|
'File.TOOLARGE',
|
2012-06-01 03:13:06 +02:00
|
|
|
'Filesize is too large, maximum {size} allowed',
|
2012-05-01 21:44:54 +02:00
|
|
|
'Argument 1: Filesize (e.g. 1MB)',
|
|
|
|
array('size' => $arg)
|
2010-05-25 05:42:52 +02:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// extension validation
|
|
|
|
if(!$this->isValidExtension()) {
|
2012-05-01 21:44:54 +02:00
|
|
|
$this->errors[] = _t(
|
|
|
|
'File.INVALIDEXTENSION',
|
|
|
|
'Extension is not allowed (valid: {extensions})',
|
|
|
|
'Argument 1: Comma-separated list of valid extensions',
|
|
|
|
array('extensions' => implode(',', $this->allowedExtensions))
|
2010-05-25 05:42:52 +02:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|