Consolidate temporary file creation and deletion logic.

This commit is contained in:
Robbie Averill 2016-12-29 22:21:28 +13:00
parent 7448622a1a
commit 37a009289c
3 changed files with 130 additions and 125 deletions

View File

@ -1043,7 +1043,7 @@ class File extends DataObject implements ShortcodeHandler, AssetContainer, Thumb
*/ */
public static function ini2bytes($iniValue) public static function ini2bytes($iniValue)
{ {
$iniValues = array_filter(str_split(trim($iniValue))); $iniValues = str_split(trim($iniValue));
$unit = strtolower(array_pop($iniValues)); $unit = strtolower(array_pop($iniValues));
$quantity = (int) implode($iniValues); $quantity = (int) implode($iniValues);
switch ($unit) { switch ($unit) {

View File

@ -615,11 +615,10 @@ class FileTest extends SapphireTest
public function ini2BytesProvider() public function ini2BytesProvider()
{ {
return [ return [
['2k', 2048], ['2k', 2 * 1024],
['512M', 524288], ['512M', 512 * 1024 * 1024],
['512 M', 524288], ['1024g', 1024 * 1024 * 1024 * 1024],
['1024g', 1048576], ['1024G', 1024 * 1024 * 1024 * 1024]
['1024G', 1048576]
]; ];
} }

View File

@ -11,9 +11,21 @@ use SilverStripe\ORM\Versioning\Versioned;
class UploadTest extends SapphireTest class UploadTest extends SapphireTest
{ {
/**
* {@inheritDoc}
* @var bool
*/
protected $usesDatabase = true; protected $usesDatabase = true;
/**
* The temporary file path used for upload tests
* @var string
*/
protected $tmpFilePath;
/**
* {@inheritDoc}
*/
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
@ -21,29 +33,33 @@ class UploadTest extends SapphireTest
TestAssetStore::activate('UploadTest'); TestAssetStore::activate('UploadTest');
} }
/**
* {@inheritDoc}
*/
public function tearDown() public function tearDown()
{ {
TestAssetStore::reset(); TestAssetStore::reset();
parent::tearDown(); parent::tearDown();
if (file_exists($this->tmpFilePath)) {
unlink($this->tmpFilePath);
}
} }
public function testUpload() public function testUpload()
{ {
// create tmp file // create tmp file
$tmpFileName = 'UploadTest-testUpload.txt'; $tmpFileName = 'UploadTest-testUpload.txt';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = $this->getTemporaryFileContent();
for ($i = 0; $i < 10000; $i++) { file_put_contents($this->tmpFilePath, $tmpFileContent);
$tmpFileContent .= '0';
}
file_put_contents($tmpFilePath, $tmpFileContent);
// emulates the $_FILES array // emulates the $_FILES array
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'text/plaintext', 'type' => 'text/plaintext',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => 'txt', 'extension' => 'txt',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -91,19 +107,16 @@ class UploadTest extends SapphireTest
{ {
// create tmp file // create tmp file
$tmpFileName = 'UploadTest-testUpload.txt'; $tmpFileName = 'UploadTest-testUpload.txt';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = $this->getTemporaryFileContent();
for ($i = 0; $i < 10000; $i++) { file_put_contents($this->tmpFilePath, $tmpFileContent);
$tmpFileContent .= '0';
}
file_put_contents($tmpFilePath, $tmpFileContent);
// emulates the $_FILES array // emulates the $_FILES array
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'text/plaintext', 'type' => 'text/plaintext',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => 'txt', 'extension' => 'txt',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -129,14 +142,14 @@ class UploadTest extends SapphireTest
// check max file size set by app category // check max file size set by app category
$tmpFileName = 'UploadTest-testUpload.jpg'; $tmpFileName = 'UploadTest-testUpload.jpg';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
file_put_contents($tmpFilePath, $tmpFileContent . $tmpFileContent); file_put_contents($this->tmpFilePath, $tmpFileContent . $tmpFileContent);
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'image/jpeg', 'type' => 'image/jpeg',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => 'jpg', 'extension' => 'jpg',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -167,21 +180,17 @@ class UploadTest extends SapphireTest
); );
// create tmp file // create tmp file
$tmpFileName = 'myfile.jpg'; $tmpFileName = 'myfile.jpg';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = $this->getTemporaryFileContent(100);
for ($i=0; $i<100; file_put_contents($this->tmpFilePath, $tmpFileContent);
$i++) {
$tmpFileContent .= '0';
}
file_put_contents($tmpFilePath, $tmpFileContent);
// Build file // Build file
$upload = new Upload(); $upload = new Upload();
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => '', 'type' => '',
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -248,10 +257,18 @@ class UploadTest extends SapphireTest
$v = new UploadTest\Validator(); $v = new UploadTest\Validator();
$retrievedSize = $v->getAllowedMaxFileSize('[image]'); $retrievedSize = $v->getAllowedMaxFileSize('[image]');
$this->assertEquals(1024, $retrievedSize, 'Max file size check on default values failed (config category set check)'); $this->assertEquals(
1024,
$retrievedSize,
'Max file size check on default values failed (config category set check)'
);
$retrievedSize = $v->getAllowedMaxFileSize('txt'); $retrievedSize = $v->getAllowedMaxFileSize('txt');
$this->assertEquals(1000, $retrievedSize, 'Max file size check on default values failed (config extension set check)'); $this->assertEquals(
1000,
$retrievedSize,
'Max file size check on default values failed (config extension set check)'
);
// Check instance values for max file size // Check instance values for max file size
$maxFileSizes = array( $maxFileSizes = array(
@ -262,7 +279,11 @@ class UploadTest extends SapphireTest
$v->setAllowedMaxFileSize($maxFileSizes); $v->setAllowedMaxFileSize($maxFileSizes);
$retrievedSize = $v->getAllowedMaxFileSize('[document]'); $retrievedSize = $v->getAllowedMaxFileSize('[document]');
$this->assertEquals(2000, $retrievedSize, 'Max file size check on instance values failed (instance category set check)'); $this->assertEquals(
2000,
$retrievedSize,
'Max file size check on instance values failed (instance category set check)'
);
// Check that the instance values overwrote the default values // Check that the instance values overwrote the default values
// ie. The max file size will not exist for [image] // ie. The max file size will not exist for [image]
@ -278,14 +299,22 @@ class UploadTest extends SapphireTest
$this->assertFalse($retrievedSize, 'Max file size check on instance values failed (extension not set check)'); $this->assertFalse($retrievedSize, 'Max file size check on instance values failed (extension not set check)');
$retrievedSize = $v->getAllowedMaxFileSize('txt'); $retrievedSize = $v->getAllowedMaxFileSize('txt');
$this->assertEquals(4096, $retrievedSize, 'Max file size check on instance values failed (instance extension set check)'); $this->assertEquals(
4096,
$retrievedSize,
'Max file size check on instance values failed (instance extension set check)'
);
// Check a wildcard max file size against a file with an extension // Check a wildcard max file size against a file with an extension
$v = new UploadTest\Validator(); $v = new UploadTest\Validator();
$v->setAllowedMaxFileSize(2000); $v->setAllowedMaxFileSize(2000);
$retrievedSize = $v->getAllowedMaxFileSize('.jpg'); $retrievedSize = $v->getAllowedMaxFileSize('.jpg');
$this->assertEquals(2000, $retrievedSize, 'Max file size check on instance values failed (wildcard max file size)'); $this->assertEquals(
2000,
$retrievedSize,
'Max file size check on instance values failed (wildcard max file size)'
);
Config::unnest(); Config::unnest();
} }
@ -294,20 +323,16 @@ class UploadTest extends SapphireTest
{ {
// create tmp file // create tmp file
$tmpFileName = 'UploadTest-testUpload'; $tmpFileName = 'UploadTest-testUpload';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = $this->getTemporaryFileContent();
for ($i=0; $i<10000; file_put_contents($this->tmpFilePath, $tmpFileContent);
$i++) {
$tmpFileContent .= '0';
}
file_put_contents($tmpFilePath, $tmpFileContent);
// emulates the $_FILES array // emulates the $_FILES array
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'text/plaintext', 'type' => 'text/plaintext',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => '', 'extension' => '',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -327,20 +352,16 @@ class UploadTest extends SapphireTest
{ {
// create tmp file // create tmp file
$tmpFileName = 'UploadTest-testUpload.php'; $tmpFileName = 'UploadTest-testUpload.php';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = $this->getTemporaryFileContent();
for ($i=0; $i<10000; file_put_contents($this->tmpFilePath, $tmpFileContent);
$i++) {
$tmpFileContent .= '0';
}
file_put_contents($tmpFilePath, $tmpFileContent);
// emulates the $_FILES array // emulates the $_FILES array
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'text/plaintext', 'type' => 'text/plaintext',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => 'php', 'extension' => 'php',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -360,20 +381,16 @@ class UploadTest extends SapphireTest
{ {
// create tmp file // create tmp file
$tmpFileName = 'UploadTest-testUpload.txt'; $tmpFileName = 'UploadTest-testUpload.txt';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = $this->getTemporaryFileContent();
for ($i=0; $i<10000; file_put_contents($this->tmpFilePath, $tmpFileContent);
$i++) {
$tmpFileContent .= '0';
}
file_put_contents($tmpFilePath, $tmpFileContent);
// emulates the $_FILES array // emulates the $_FILES array
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'text/plaintext', 'type' => 'text/plaintext',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => 'txt', 'extension' => 'txt',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -396,20 +413,16 @@ class UploadTest extends SapphireTest
{ {
// create tmp file // create tmp file
$tmpFileName = 'UploadTest-testUpload'; $tmpFileName = 'UploadTest-testUpload';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = $this->getTemporaryFileContent();
for ($i=0; $i<10000; file_put_contents($this->tmpFilePath, $tmpFileContent);
$i++) {
$tmpFileContent .= '0';
}
file_put_contents($tmpFilePath, $tmpFileContent);
// emulates the $_FILES array // emulates the $_FILES array
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'text/plaintext', 'type' => 'text/plaintext',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => '', 'extension' => '',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -429,20 +442,16 @@ class UploadTest extends SapphireTest
{ {
// create tmp file // create tmp file
$tmpFileName = 'UploadTest-testUpload.tar.gz'; $tmpFileName = 'UploadTest-testUpload.tar.gz';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = $this->getTemporaryFileContent();
for ($i=0; $i<10000; file_put_contents($this->tmpFilePath, $tmpFileContent);
$i++) {
$tmpFileContent .= '0';
}
file_put_contents($tmpFilePath, $tmpFileContent);
// emulates the $_FILES array // emulates the $_FILES array
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'text/plaintext', 'type' => 'text/plaintext',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => 'tar.gz', 'extension' => 'tar.gz',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -502,20 +511,16 @@ class UploadTest extends SapphireTest
{ {
// create tmp file // create tmp file
$tmpFileName = 'UploadTest-testUpload'; $tmpFileName = 'UploadTest-testUpload';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = $this->getTemporaryFileContent();
for ($i=0; $i<10000; file_put_contents($this->tmpFilePath, $tmpFileContent);
$i++) {
$tmpFileContent .= '0';
}
file_put_contents($tmpFilePath, $tmpFileContent);
// emulates the $_FILES array // emulates the $_FILES array
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'text/plaintext', 'type' => 'text/plaintext',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => 'txt', 'extension' => 'txt',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -563,20 +568,16 @@ class UploadTest extends SapphireTest
{ {
// create tmp file // create tmp file
$tmpFileName = 'UploadTest-testUpload'; $tmpFileName = 'UploadTest-testUpload';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = $this->getTemporaryFileContent();
for ($i=0; $i<10000; file_put_contents($this->tmpFilePath, $tmpFileContent);
$i++) {
$tmpFileContent .= '0';
}
file_put_contents($tmpFilePath, $tmpFileContent);
// emulates the $_FILES array // emulates the $_FILES array
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'text/plaintext', 'type' => 'text/plaintext',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => 'txt', 'extension' => 'txt',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -625,19 +626,16 @@ class UploadTest extends SapphireTest
{ {
// create tmp file // create tmp file
$tmpFileName = 'UploadTest-testUpload.txt'; $tmpFileName = 'UploadTest-testUpload.txt';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = $this->getTemporaryFileContent();
for ($i = 0; $i < 10000; $i++) { file_put_contents($this->tmpFilePath, $tmpFileContent);
$tmpFileContent .= '0';
}
file_put_contents($tmpFilePath, $tmpFileContent);
// emulates the $_FILES array // emulates the $_FILES array
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'text/plaintext', 'type' => 'text/plaintext',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => 'txt', 'extension' => 'txt',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -706,17 +704,17 @@ class UploadTest extends SapphireTest
public function testDeleteResampledImagesOnUpload() public function testDeleteResampledImagesOnUpload()
{ {
$tmpFileName = 'UploadTest-testUpload.jpg'; $tmpFileName = 'UploadTest-testUpload.jpg';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$uploadImage = function () use ($tmpFileName, $tmpFilePath) { $uploadImage = function () use ($tmpFileName) {
copy(__DIR__ . '/GDTest/images/test_jpg.jpg', $tmpFilePath); copy(__DIR__ . '/GDTest/images/test_jpg.jpg', $this->tmpFilePath);
// emulates the $_FILES array // emulates the $_FILES array
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'text/plaintext', 'type' => 'text/plaintext',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => 'jpg', 'extension' => 'jpg',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -747,19 +745,16 @@ class UploadTest extends SapphireTest
{ {
$upload = function ($tmpFileName) { $upload = function ($tmpFileName) {
// create tmp file // create tmp file
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $this->tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = $this->getTemporaryFileContent();
for ($i = 0; $i < 10000; $i++) { file_put_contents($this->tmpFilePath, $tmpFileContent);
$tmpFileContent .= '0';
}
file_put_contents($tmpFilePath, $tmpFileContent);
// emulates the $_FILES array // emulates the $_FILES array
$tmpFile = array( $tmpFile = array(
'name' => $tmpFileName, 'name' => $tmpFileName,
'type' => 'text/plaintext', 'type' => 'text/plaintext',
'size' => filesize($tmpFilePath), 'size' => filesize($this->tmpFilePath),
'tmp_name' => $tmpFilePath, 'tmp_name' => $this->tmpFilePath,
'extension' => 'jpg', 'extension' => 'jpg',
'error' => UPLOAD_ERR_OK, 'error' => UPLOAD_ERR_OK,
); );
@ -841,4 +836,15 @@ class UploadTest extends SapphireTest
'File does receive new name' 'File does receive new name'
); );
} }
/**
* Generate some dummy file content
*
* @param int $reps How many zeros to return
* @return string
*/
protected function getTemporaryFileContent($reps = 10000)
{
return str_repeat('0', $reps);
}
} }