Skip to content

Commit

Permalink
Fix;: return correct php.ini value w/o unit set (#16)
Browse files Browse the repository at this point in the history
* Return numeric php.ini value

* Fixed comparison
  • Loading branch information
sreichel authored Feb 16, 2024
1 parent 6647256 commit 6df1405
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 6 deletions.
25 changes: 19 additions & 6 deletions app/code/core/Mage/Uploader/Helper/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
Expand Down
137 changes: 137 additions & 0 deletions dev/tests/unit/Mage/Uploader/Helper/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?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
*/
private 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 $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'
],
];
}
}

0 comments on commit 6df1405

Please sign in to comment.