diff --git a/src/Assets/File.php b/src/Assets/File.php index ee2d6ca72..95c6dc31f 100644 --- a/src/Assets/File.php +++ b/src/Assets/File.php @@ -1012,8 +1012,6 @@ class File extends DataObject implements ShortcodeHandler, AssetContainer, Thumb /** * Formats a file size (eg: (int)42 becomes string '42 bytes') * - * @todo unit tests - * * @param int $size * @return string */ @@ -1040,22 +1038,29 @@ class File extends DataObject implements ShortcodeHandler, AssetContainer, Thumb /** * Convert a php.ini value (eg: 512M) to bytes * - * @todo unit tests - * - * @param string $iniValue + * @param string $iniValue * @return int */ 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': - $iniValue *= 1024; + $quantity *= 1024; + // deliberate no break case 'm': - $iniValue *= 1024; + $quantity *= 1024; + // deliberate no break case 'k': - $iniValue *= 1024; + $quantity *= 1024; + // deliberate no break + default: + // no-op: pre-existing behaviour + break; } - return $iniValue; + return $quantity; } /** diff --git a/tests/php/Assets/FileTest.php b/tests/php/Assets/FileTest.php index d5ca613ef..d7abc21b5 100644 --- a/tests/php/Assets/FileTest.php +++ b/tests/php/Assets/FileTest.php @@ -54,8 +54,8 @@ class FileTest extends SapphireTest $fileIDs = $this->allFixtureIDs(File::class); foreach ($fileIDs as $fileID) { /** - * @var File $file -*/ + * @var File $file + */ $file = DataObject::get_by_id(File::class, $fileID); $root = ASSETS_PATH . '/FileTest/'; if ($folder = $file->Parent()) { @@ -143,7 +143,7 @@ class FileTest extends SapphireTest // because the parent folders don't exist in the database $folder = Folder::find_or_make('/FileTest/'); $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)); fclose($fh); @@ -285,8 +285,8 @@ class FileTest extends SapphireTest public function testSetNameChangesFilesystemOnWrite() { /** - * @var File $file -*/ + * @var File $file + */ $file = $this->objFromFixture(File::class, 'asdf'); $this->logInWithPermission('ADMIN'); $file->publishRecursive(); @@ -461,8 +461,8 @@ class FileTest extends SapphireTest public function testDeleteFile() { /** - * @var File $file -*/ + * @var File $file + */ $file = $this->objFromFixture(File::class, 'asdf'); $this->logInWithPermission('ADMIN'); $file->publishSingle(); @@ -502,7 +502,7 @@ class FileTest extends SapphireTest //get folder again and see if the filename has changed $folder = DataObject::get_by_id(Folder::class, $folderID); $this->assertEquals( - $newTitle . "/", + $newTitle . '/', $folder->Filename, "Folder Filename updated after rename of Title" ); @@ -587,7 +587,6 @@ class FileTest extends SapphireTest $this->assertTrue($file->canEdit(), "Admins can edit files"); } - public function testJoinPaths() { $this->assertEquals('name/file.jpg', File::join_paths('/name', 'file.jpg')); @@ -598,6 +597,32 @@ class FileTest extends SapphireTest $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 */ diff --git a/tests/php/Assets/UploadTest.php b/tests/php/Assets/UploadTest.php index 174a3cfeb..b24cda475 100644 --- a/tests/php/Assets/UploadTest.php +++ b/tests/php/Assets/UploadTest.php @@ -33,8 +33,7 @@ class UploadTest extends SapphireTest $tmpFileName = 'UploadTest-testUpload.txt'; $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $tmpFileContent = ''; - for ($i=0; $i<10000; - $i++) { + for ($i = 0; $i < 10000; $i++) { $tmpFileContent .= '0'; } file_put_contents($tmpFilePath, $tmpFileContent); @@ -94,8 +93,7 @@ class UploadTest extends SapphireTest $tmpFileName = 'UploadTest-testUpload.txt'; $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName; $tmpFileContent = ''; - for ($i=0; $i<10000; - $i++) { + for ($i = 0; $i < 10000; $i++) { $tmpFileContent .= '0'; } file_put_contents($tmpFilePath, $tmpFileContent);