Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Mage_Uploader_Helper_File::getDataMaxSize() when checking against different size units #4039

Merged
merged 5 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions app/code/core/Mage/Catalog/Model/Product/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ protected function _checkMemory($file = null)
}

/**
* @return int
* @deprecated
* @return float|int|string
*/
protected function _getMemoryLimit()
{
Expand All @@ -251,18 +251,7 @@ protected function _getMemoryLimit()
$memoryLimit = "128M";
}

$value = (int)substr($memoryLimit, 0, -1);

if (substr($memoryLimit, -1) == 'K') {
return $value * 1024;
}
if (substr($memoryLimit, -1) == 'M') {
return $value * 1024 * 1024;
}
if (substr($memoryLimit, -1) == 'G') {
return $value * 1024 * 1024 * 1024;
}
return $memoryLimit;
return ini_parse_quantity($memoryLimit);
}

/**
Expand Down
14 changes: 2 additions & 12 deletions app/code/core/Mage/Catalog/Model/Product/Option/Type/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -801,21 +801,11 @@ protected function _getBytesIniValue($ini_key)
{
$_bytes = @ini_get($ini_key);

if (stristr($_bytes, 'k')) {
// kilobytes
$_bytes = (int) $_bytes * 1024;
} elseif (stristr($_bytes, 'm')) {
// megabytes
$_bytes = (int) $_bytes * 1024 * 1024;
} elseif (stristr($_bytes, 'g')) {
// gigabytes
$_bytes = (int) $_bytes * 1024 * 1024 * 1024;
}
return (int)$_bytes;
return ini_parse_quantity($_bytes);
}

/**
* Simple converrt bytes to Megabytes
* Simple convert bytes to Megabytes
*
* @param int $bytes
* @return float
Expand Down
30 changes: 7 additions & 23 deletions app/code/core/Mage/Uploader/Helper/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,12 @@ public function getUploadMaxSize()
*/
public function getDataMaxSize()
{
return min($this->getPostMaxSize(), $this->getUploadMaxSize());
$postMaxSize = $this->getPostMaxSize();
$uploadMaxSize = $this->getUploadMaxSize();
$postMaxSizeBytes = ini_parse_quantity($postMaxSize);
$uploadMaxSizeBytes = ini_parse_quantity($uploadMaxSize);

return min($postMaxSizeBytes, $uploadMaxSizeBytes) === $postMaxSizeBytes ? $postMaxSize : $uploadMaxSize;
}

/**
Expand All @@ -717,27 +722,6 @@ public function getDataMaxSize()
*/
public function getDataMaxSizeInBytes()
{
$iniSize = $this->getDataMaxSize();
$size = (int)substr($iniSize, 0, -1);
$parsedSize = 0;
switch (strtolower(substr($iniSize, strlen($iniSize) - 1))) {
case 't':
$parsedSize = $size * (1024 * 1024 * 1024 * 1024);
break;
case 'g':
$parsedSize = $size * (1024 * 1024 * 1024);
break;
case 'm':
$parsedSize = $size * (1024 * 1024);
break;
case 'k':
$parsedSize = $size * 1024;
break;
case 'b':
default:
$parsedSize = $size;
break;
}
return (int)$parsedSize;
return ini_parse_quantity($this->getDataMaxSize());
}
}
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
"phpseclib/mcrypt_compat": "^2.0.3",
"phpseclib/phpseclib": "^3.0.14",
"shardj/zf1-future": "1.24.0",
"symfony/polyfill-php74": "^1.27",
"symfony/polyfill-php80": "^1.27",
"symfony/polyfill-php81": "^1.27"
"symfony/polyfill-php74": "^1.29",
"symfony/polyfill-php80": "^1.29",
"symfony/polyfill-php81": "^1.29",
"symfony/polyfill-php82": "^1.29"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
Expand Down
78 changes: 77 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

130 changes: 130 additions & 0 deletions dev/tests/unit/Mage/Uploader/Helper/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

namespace OpenMage\Tests\Unit\Mage\Uploader\Helper;

use Mage;
use Mage_Uploader_Helper_File;
use PHPUnit\Framework\TestCase;

class FileTest extends TestCase
{
/**
* @var Mage_Uploader_Helper_File
*/
public Mage_Uploader_Helper_File $subject;

public function setUp(): void
{
Mage::app();
Mage::getConfig()->setNode('global/mime/types/test-new-node', 'application/octet-stream');
$this->subject = Mage::helper('uploader/file');
}

/**
* @dataProvider provideGetMimeTypeFromExtensionListData
* @param array<int, string> $expectedResult
* @param string|array<int, string> $extensionsList
* @return void
*/
public function testGetMimeTypeFromExtensionList(array $expectedResult, $extensionsList): void
{
self::assertSame($expectedResult, $this->subject->getMimeTypeFromExtensionList($extensionsList));
}

/**
* @return array<string, array<int, array<int, string>|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');
F1Red5 marked this conversation as resolved.
Show resolved Hide resolved
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 array<string, array<int, int|string>>
*/
public function provideGetDataMaxSizeInBytesData(): array
{
return [
'no unit' => [
1024,
'1024'
],
'kilobyte' => [
1024,
'1K'
],
'megabyte' => [
1048576,
'1M'
],
'gigabyte' => [
1073741824,
'1G'
]
];
}
}
Loading