From 6df1405c2f96f5fa33b0e0818e08f4685b390c48 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Fri, 16 Feb 2024 11:14:04 +0100 Subject: [PATCH] Fix;: return correct php.ini value w/o unit set (#16) * Return numeric php.ini value * Fixed comparison --- app/code/core/Mage/Uploader/Helper/File.php | 25 +++- .../unit/Mage/Uploader/Helper/FileTest.php | 137 ++++++++++++++++++ 2 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 dev/tests/unit/Mage/Uploader/Helper/FileTest.php diff --git a/app/code/core/Mage/Uploader/Helper/File.php b/app/code/core/Mage/Uploader/Helper/File.php index 4ed940445d8..6070831fd11 100644 --- a/app/code/core/Mage/Uploader/Helper/File.php +++ b/app/code/core/Mage/Uploader/Helper/File.php @@ -703,11 +703,16 @@ public function getUploadMaxSize() /** * Get max upload size * - * @return mixed + * @return string */ public function getDataMaxSize() { - return min($this->getPostMaxSize(), $this->getUploadMaxSize()); + $postMaxSize = $this->getPostMaxSize(); + $uploadMaxSize = $this->getUploadMaxSize(); + $postMaxSizeBytes = $this->convertToBytes($postMaxSize); + $uploadMaxSizeBytes = $this->convertToBytes($uploadMaxSize); + + return min($postMaxSizeBytes, $uploadMaxSizeBytes) === $postMaxSizeBytes ? $postMaxSize : $uploadMaxSize; } /** @@ -717,10 +722,18 @@ public function getDataMaxSize() */ public function getDataMaxSizeInBytes() { - $iniSize = $this->getDataMaxSize(); - $size = (int)substr($iniSize, 0, -1); - $parsedSize = 0; - switch (strtolower(substr($iniSize, strlen($iniSize) - 1))) { + return $this->convertToBytes($this->getDataMaxSize()); + } + + protected function convertToBytes(string $input): int + { + if (is_numeric($input)) { + return (int)$input; + } + + $size = (int)substr($input, 0, -1); + $unit = strtolower(substr($input, strlen($input) - 1)); + switch ($unit) { case 't': $parsedSize = $size * (1024 * 1024 * 1024 * 1024); break; diff --git a/dev/tests/unit/Mage/Uploader/Helper/FileTest.php b/dev/tests/unit/Mage/Uploader/Helper/FileTest.php new file mode 100644 index 00000000000..fad36ced384 --- /dev/null +++ b/dev/tests/unit/Mage/Uploader/Helper/FileTest.php @@ -0,0 +1,137 @@ +setNode('global/mime/types/test-new-node', 'application/octet-stream'); + $this->subject = Mage::helper('uploader/file'); + } + + /** + * @dataProvider provideGetMimeTypeFromExtensionListData + * @param array $expectedResult + * @param string|array $extensionsList + * @return void + */ + public function testGetMimeTypeFromExtensionList(array $expectedResult, $extensionsList): void + { + self::assertSame($expectedResult, $this->subject->getMimeTypeFromExtensionList($extensionsList)); + } + + /** + * @return string[][] + */ + public function provideGetMimeTypeFromExtensionListData(): array + { + return [ + 'string exists' => [ + [ + 0 => 'application/vnd.lotus-1-2-3' + ], + '123' + ], + 'string not exists' => [ + [ + 0 => 'application/octet-stream' + ], + 'not-exists' + ], + 'array' => [ + [ + 0 => 'application/vnd.lotus-1-2-3', + 1 => 'application/octet-stream', + 2 => 'application/octet-stream', + ], + [ + '123', + 'not-exists', + 'test-new-node', + ] + ], + ]; + } + + public function testGetPostMaxSize(): void + { + self::assertIsString($this->subject->getPostMaxSize()); + } + + public function testGetUploadMaxSize(): void + { + self::assertIsString($this->subject->getUploadMaxSize()); + } + + public function testGetDataMaxSize(): void + { + $mock = $this->getMockBuilder(Mage_Uploader_Helper_File::class) + ->setMethods(['getPostMaxSize', 'getUploadMaxSize']) + ->getMock(); + + $mock->expects($this->once())->method('getPostMaxSize')->willReturn('1G'); + $mock->expects($this->once())->method('getUploadMaxSize')->willReturn('1M'); + self::assertSame('1M', $mock->getDataMaxSize()); + } + + /** + * @dataProvider provideGetDataMaxSizeInBytesData + * @param int $expectedResult + * @param string $maxSize + * @return void + */ + public function testGetDataMaxSizeInBytes(int $expectedResult, string $maxSize): void + { + $mock = $this->getMockBuilder(Mage_Uploader_Helper_File::class) + ->setMethods(['getDataMaxSize']) + ->getMock(); + + $mock->expects($this->once())->method('getDataMaxSize')->willReturn($maxSize); + self::assertSame($expectedResult, $mock->getDataMaxSizeInBytes()); + } + + /** + * @return string[][] + */ + public function provideGetDataMaxSizeInBytesData(): array + { + return [ + 'no unit' => [ + 1024, + '1024' + ], + 'byte' => [ + 1, + '1B' + ], + 'kilobyte' => [ + 1024, + '1K' + ], + 'megabyte' => [ + 1048576, + '1M' + ], + 'gigabyte' => [ + 1073741824, + '1G' + ], + 'terabyte' => [ + 1099511627776, + '1T' + ], + ]; + } +}