FIX Replace ini casting to int with explicit split and cast for PHP 7.1. Add tests.

This commit is contained in:
Robbie Averill 2016-12-29 21:21:00 +13:00
parent 664c0eafbe
commit 7448622a1a
3 changed files with 51 additions and 23 deletions

View File

@ -1012,8 +1012,6 @@ class File extends DataObject implements ShortcodeHandler, AssetContainer, Thumb
/** /**
* Formats a file size (eg: (int)42 becomes string '42 bytes') * Formats a file size (eg: (int)42 becomes string '42 bytes')
* *
* @todo unit tests
*
* @param int $size * @param int $size
* @return string * @return string
*/ */
@ -1040,22 +1038,29 @@ class File extends DataObject implements ShortcodeHandler, AssetContainer, Thumb
/** /**
* Convert a php.ini value (eg: 512M) to bytes * Convert a php.ini value (eg: 512M) to bytes
* *
* @todo unit tests
*
* @param string $iniValue * @param string $iniValue
* @return int * @return int
*/ */
public static function ini2bytes($iniValue) public static function ini2bytes($iniValue)
{ {
switch (strtolower(substr(trim($iniValue), -1))) { $iniValues = array_filter(str_split(trim($iniValue)));
$unit = strtolower(array_pop($iniValues));
$quantity = (int) implode($iniValues);
switch ($unit) {
case 'g': case 'g':
$iniValue *= 1024; $quantity *= 1024;
// deliberate no break
case 'm': case 'm':
$iniValue *= 1024; $quantity *= 1024;
// deliberate no break
case 'k': case 'k':
$iniValue *= 1024; $quantity *= 1024;
// deliberate no break
default:
// no-op: pre-existing behaviour
break;
} }
return $iniValue; return $quantity;
} }
/** /**

View File

@ -143,7 +143,7 @@ class FileTest extends SapphireTest
// because the parent folders don't exist in the database // because the parent folders don't exist in the database
$folder = Folder::find_or_make('/FileTest/'); $folder = Folder::find_or_make('/FileTest/');
$testfilePath = BASE_PATH . '/assets/FileTest/CreateWithFilenameHasCorrectPath.txt'; // Important: No leading slash $testfilePath = BASE_PATH . '/assets/FileTest/CreateWithFilenameHasCorrectPath.txt'; // Important: No leading slash
$fh = fopen($testfilePath, "w"); $fh = fopen($testfilePath, 'w');
fwrite($fh, str_repeat('x', 1000000)); fwrite($fh, str_repeat('x', 1000000));
fclose($fh); fclose($fh);
@ -502,7 +502,7 @@ class FileTest extends SapphireTest
//get folder again and see if the filename has changed //get folder again and see if the filename has changed
$folder = DataObject::get_by_id(Folder::class, $folderID); $folder = DataObject::get_by_id(Folder::class, $folderID);
$this->assertEquals( $this->assertEquals(
$newTitle . "/", $newTitle . '/',
$folder->Filename, $folder->Filename,
"Folder Filename updated after rename of Title" "Folder Filename updated after rename of Title"
); );
@ -587,7 +587,6 @@ class FileTest extends SapphireTest
$this->assertTrue($file->canEdit(), "Admins can edit files"); $this->assertTrue($file->canEdit(), "Admins can edit files");
} }
public function testJoinPaths() public function testJoinPaths()
{ {
$this->assertEquals('name/file.jpg', File::join_paths('/name', 'file.jpg')); $this->assertEquals('name/file.jpg', File::join_paths('/name', 'file.jpg'));
@ -598,6 +597,32 @@ class FileTest extends SapphireTest
$this->assertEquals('', File::join_paths('/', '/')); $this->assertEquals('', File::join_paths('/', '/'));
} }
/**
* Test that ini2bytes returns the number of bytes for a PHP ini style size declaration
*
* @param string $iniValue
* @param int $expected
* @dataProvider ini2BytesProvider
*/
public function testIni2Bytes($iniValue, $expected)
{
$this->assertSame($expected, File::ini2bytes($iniValue));
}
/**
* @return array
*/
public function ini2BytesProvider()
{
return [
['2k', 2048],
['512M', 524288],
['512 M', 524288],
['1024g', 1048576],
['1024G', 1048576]
];
}
/** /**
* @return AssetStore * @return AssetStore
*/ */

View File

@ -33,8 +33,7 @@ class UploadTest extends SapphireTest
$tmpFileName = 'UploadTest-testUpload.txt'; $tmpFileName = 'UploadTest-testUpload.txt';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = '';
for ($i=0; $i<10000; for ($i = 0; $i < 10000; $i++) {
$i++) {
$tmpFileContent .= '0'; $tmpFileContent .= '0';
} }
file_put_contents($tmpFilePath, $tmpFileContent); file_put_contents($tmpFilePath, $tmpFileContent);
@ -94,8 +93,7 @@ class UploadTest extends SapphireTest
$tmpFileName = 'UploadTest-testUpload.txt'; $tmpFileName = 'UploadTest-testUpload.txt';
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
$tmpFileContent = ''; $tmpFileContent = '';
for ($i=0; $i<10000; for ($i = 0; $i < 10000; $i++) {
$i++) {
$tmpFileContent .= '0'; $tmpFileContent .= '0';
} }
file_put_contents($tmpFilePath, $tmpFileContent); file_put_contents($tmpFilePath, $tmpFileContent);