Skip to content

Commit 6a1773e

Browse files
Merge remote-tracking branch 'remotes/github/MAGETWO-63599' into EPAM-PR-59
2 parents e258d48 + 346edac commit 6a1773e

File tree

5 files changed

+172
-20
lines changed

5 files changed

+172
-20
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaStorage\Console\Command;
9+
10+
use Magento\Catalog\Model\Product\Gallery\Processor;
11+
use Magento\Catalog\Model\ProductRepository;
12+
use Magento\Framework\App\Filesystem\DirectoryList;
13+
use Magento\Framework\Filesystem;
14+
use Magento\Framework\Filesystem\Directory\WriteInterface;
15+
use Magento\Framework\ObjectManagerInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Symfony\Component\Console\Tester\CommandTester;
18+
19+
/**
20+
* Test for \Magento\MediaStorage\Console\Command\ImagesResizeCommand.
21+
*
22+
*/
23+
class ImageResizeCommandTest extends \PHPUnit\Framework\TestCase
24+
{
25+
/**
26+
* @var CommandTester
27+
*/
28+
private $tester;
29+
30+
/**
31+
* @var ImagesResizeCommand
32+
*/
33+
private $command;
34+
35+
/**
36+
* @var ObjectManagerInterface
37+
*/
38+
private $objectManager;
39+
40+
/**
41+
* @var WriteInterface
42+
*/
43+
private $mediaDirectory;
44+
45+
/**
46+
* @var Filesystem
47+
*/
48+
private $filesystem;
49+
50+
/**
51+
* @var string
52+
*/
53+
private $fileName;
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
public function setUp()
59+
{
60+
$this->fileName = 'image.jpg';
61+
$this->objectManager = Bootstrap::getObjectManager();
62+
$this->command = $this->objectManager->get(ImagesResizeCommand::class);
63+
$this->tester = new CommandTester($this->command);
64+
$this->filesystem = $this->objectManager->get(Filesystem::class);
65+
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
66+
}
67+
68+
/**
69+
* Test command with zero byte file
70+
*
71+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
72+
* @magentoDataFixture Magento/Catalog/_files/product_image.php
73+
*
74+
* @return void
75+
*/
76+
public function testExecuteWithZeroByteImage()
77+
{
78+
$this->mediaDirectory->writeFile($this->fileName, '');
79+
80+
/** @var ProductRepository $productRepository */
81+
$productRepository = $this->objectManager->create(ProductRepository::class);
82+
$product = $productRepository->getById(1);
83+
84+
/** @var Processor $mediaGalleryProcessor */
85+
$mediaGalleryProcessor = $this->objectManager->get(Processor::class);
86+
$mediaGalleryProcessor->addImage(
87+
$product,
88+
$this->mediaDirectory->getAbsolutePath($this->fileName),
89+
['image','thumbnail','small_image'],
90+
false,
91+
false
92+
);
93+
94+
$product->save();
95+
96+
$this->tester->execute([]);
97+
$this->assertContains('Wrong file', $this->tester->getDisplay());
98+
}
99+
100+
/**
101+
* @inheritDoc
102+
*/
103+
public function tearDown()
104+
{
105+
$this->mediaDirectory->getDriver()->deleteFile($this->mediaDirectory->getAbsolutePath($this->fileName));
106+
}
107+
}

lib/internal/Magento/Framework/Image/Adapter/AbstractAdapter.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Framework\Image\Adapter;
710

811
use Magento\Framework\App\Filesystem\DirectoryList;
912

1013
/**
14+
* Image abstract adapter
15+
*
1116
* @file Abstract.php
1217
* @author Magento Core Team <core@magentocommerce.com>
1318
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -169,6 +174,7 @@ abstract public function open($fileName);
169174

170175
/**
171176
* Save image to specific path.
177+
*
172178
* If some folders of path does not exist they will be created
173179
*
174180
* @param null|string $destination
@@ -620,7 +626,7 @@ protected function _checkDimensions($frameWidth, $frameHeight)
620626
$frameHeight !== null && $frameHeight <= 0 ||
621627
empty($frameWidth) && empty($frameHeight)
622628
) {
623-
throw new \Exception('Invalid image dimensions.');
629+
throw new \InvalidArgumentException('Invalid image dimensions.');
624630
}
625631
}
626632

@@ -687,7 +693,9 @@ protected function _prepareDestination($destination = null, $newName = null)
687693
$this->directoryWrite->create($this->directoryWrite->getRelativePath($destination));
688694
} catch (\Magento\Framework\Exception\FileSystemException $e) {
689695
$this->logger->critical($e);
690-
throw new \Exception('Unable to write file into directory ' . $destination . '. Access forbidden.');
696+
throw new \DomainException(
697+
'Unable to write file into directory ' . $destination . '. Access forbidden.'
698+
);
691699
}
692700
}
693701

@@ -701,7 +709,7 @@ protected function _prepareDestination($destination = null, $newName = null)
701709
*/
702710
protected function _canProcess()
703711
{
704-
return !empty($this->_fileName);
712+
return !empty($this->_fileName) && filesize($this->_fileName) > 0;
705713
}
706714

707715
/**
@@ -710,12 +718,19 @@ protected function _canProcess()
710718
* @param string $filePath
711719
* @return bool
712720
* @throws \InvalidArgumentException
721+
* @throws \DomainException
722+
* @throws \BadFunctionCallException
723+
* @throws \RuntimeException
724+
* @throws \OverflowException
713725
*/
714726
public function validateUploadFile($filePath)
715727
{
716728
if (!file_exists($filePath)) {
717729
throw new \InvalidArgumentException("File '{$filePath}' does not exists.");
718730
}
731+
if (filesize($filePath) === 0) {
732+
throw new \InvalidArgumentException('Wrong file size.');
733+
}
719734
if (!getimagesize($filePath)) {
720735
throw new \InvalidArgumentException('Disallowed file type.');
721736
}

lib/internal/Magento/Framework/Image/Adapter/Gd2.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Framework\Image\Adapter;
78

89
/**
@@ -59,6 +60,9 @@ protected function _reset()
5960
*/
6061
public function open($filename)
6162
{
63+
if (!$filename || filesize($filename) === 0) {
64+
throw new \InvalidArgumentException('Wrong file');
65+
}
6266
$this->_fileName = $filename;
6367
$this->_reset();
6468
$this->getMimeType();
@@ -225,18 +229,19 @@ public function getImage()
225229
* @param null|int $fileType
226230
* @param string $unsupportedText
227231
* @return string
228-
* @throws \Exception
232+
* @throws \InvalidArgumentException
233+
* @throws \BadFunctionCallException
229234
*/
230235
private function _getCallback($callbackType, $fileType = null, $unsupportedText = 'Unsupported image format.')
231236
{
232237
if (null === $fileType) {
233238
$fileType = $this->_fileType;
234239
}
235240
if (empty(self::$_callbacks[$fileType])) {
236-
throw new \Exception($unsupportedText);
241+
throw new \InvalidArgumentException($unsupportedText);
237242
}
238243
if (empty(self::$_callbacks[$fileType][$callbackType])) {
239-
throw new \Exception('Callback not found.');
244+
throw new \BadFunctionCallException('Callback not found.');
240245
}
241246
return self::$_callbacks[$fileType][$callbackType];
242247
}
@@ -248,7 +253,7 @@ private function _getCallback($callbackType, $fileType = null, $unsupportedText
248253
*
249254
* @param resource &$imageResourceTo
250255
* @return int
251-
* @throws \Exception
256+
* @throws \InvalidArgumentException
252257
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
253258
*/
254259
private function _fillBackgroundColor(&$imageResourceTo)
@@ -261,17 +266,17 @@ private function _fillBackgroundColor(&$imageResourceTo)
261266
// fill truecolor png with alpha transparency
262267
if ($isAlpha) {
263268
if (!imagealphablending($imageResourceTo, false)) {
264-
throw new \Exception('Failed to set alpha blending for PNG image.');
269+
throw new \InvalidArgumentException('Failed to set alpha blending for PNG image.');
265270
}
266271
$transparentAlphaColor = imagecolorallocatealpha($imageResourceTo, 0, 0, 0, 127);
267272
if (false === $transparentAlphaColor) {
268-
throw new \Exception('Failed to allocate alpha transparency for PNG image.');
273+
throw new \InvalidArgumentException('Failed to allocate alpha transparency for PNG image.');
269274
}
270275
if (!imagefill($imageResourceTo, 0, 0, $transparentAlphaColor)) {
271-
throw new \Exception('Failed to fill PNG image with alpha transparency.');
276+
throw new \InvalidArgumentException('Failed to fill PNG image with alpha transparency.');
272277
}
273278
if (!imagesavealpha($imageResourceTo, true)) {
274-
throw new \Exception('Failed to save alpha transparency into PNG image.');
279+
throw new \InvalidArgumentException('Failed to save alpha transparency into PNG image.');
275280
}
276281

277282
return $transparentAlphaColor;
@@ -283,22 +288,22 @@ private function _fillBackgroundColor(&$imageResourceTo)
283288
$transparentColor = imagecolorallocate($imageResourceTo, $r, $g, $b);
284289
}
285290
if (false === $transparentColor) {
286-
throw new \Exception('Failed to allocate transparent color for image.');
291+
throw new \InvalidArgumentException('Failed to allocate transparent color for image.');
287292
}
288293
if (!imagefill($imageResourceTo, 0, 0, $transparentColor)) {
289-
throw new \Exception('Failed to fill image with transparency.');
294+
throw new \InvalidArgumentException('Failed to fill image with transparency.');
290295
}
291296
imagecolortransparent($imageResourceTo, $transparentColor);
292297
return $transparentColor;
293298
}
294299
} catch (\Exception $e) {
295-
// fallback to default background color
300+
throw new \DomainException('Failed to fill image.');
296301
}
297302
}
298303
list($r, $g, $b) = $this->_backgroundColor;
299304
$color = imagecolorallocate($imageResourceTo, $r, $g, $b);
300305
if (!imagefill($imageResourceTo, 0, 0, $color)) {
301-
throw new \Exception("Failed to fill image background with color {$r} {$g} {$b}.");
306+
throw new \InvalidArgumentException("Failed to fill image background with color {$r} {$g} {$b}.");
302307
}
303308

304309
return $color;
@@ -319,10 +324,12 @@ public function checkAlpha($fileName)
319324
* Checks if image has alpha transparency
320325
*
321326
* @param resource $imageResource
322-
* @param int $fileType one of the constants IMAGETYPE_*
327+
* @param int $fileType
323328
* @param bool &$isAlpha
324329
* @param bool &$isTrueColor
330+
*
325331
* @return boolean
332+
*
326333
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
327334
*/
328335
private function _getTransparency($imageResource, $fileType, &$isAlpha = false, &$isTrueColor = false)
@@ -637,13 +644,13 @@ public function crop($top = 0, $left = 0, $right = 0, $bottom = 0)
637644
* Checks required dependencies
638645
*
639646
* @return void
640-
* @throws \Exception If some of dependencies are missing
647+
* @throws \RuntimeException If some of dependencies are missing
641648
*/
642649
public function checkDependencies()
643650
{
644651
foreach ($this->_requiredExtensions as $value) {
645652
if (!extension_loaded($value)) {
646-
throw new \Exception("Required PHP extension '{$value}' was not loaded.");
653+
throw new \RuntimeException("Required PHP extension '{$value}' was not loaded.");
647654
}
648655
}
649656
}
@@ -755,7 +762,7 @@ protected function _createImageFromText($text)
755762
* @param string $text
756763
* @param string $font
757764
* @return void
758-
* @throws \Exception
765+
* @throws \InvalidArgumentException
759766
*/
760767
protected function _createImageFromTtfText($text, $font)
761768
{
@@ -777,7 +784,7 @@ protected function _createImageFromTtfText($text, $font)
777784
$text
778785
);
779786
if ($result === false) {
780-
throw new \Exception('Unable to create TTF text');
787+
throw new \InvalidArgumentException('Unable to create TTF text');
781788
}
782789
}
783790

lib/internal/Magento/Framework/Image/Test/Unit/Adapter/Gd2Test.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Framework\Image\Test\Unit\Adapter;
710

811
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -24,6 +27,13 @@ class Gd2Test extends \PHPUnit\Framework\TestCase
2427
*/
2528
public static $imageData = [];
2629

30+
/**
31+
* Simulation of filesize() function
32+
*
33+
* @var int
34+
*/
35+
public static $imageSize = 1;
36+
2737
/**
2838
* Adapter for testing
2939
* @var \Magento\Framework\Image\Adapter\Gd2

lib/internal/Magento/Framework/Image/Test/Unit/Adapter/_files/global_php_mock.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Framework\Image\Adapter;
710

811
use Magento\Framework\Image\Test\Unit\Adapter\Gd2Test;
@@ -35,6 +38,16 @@ function getimagesize($file)
3538
return Gd2Test::$imageData;
3639
}
3740

41+
/**
42+
* @param $file
43+
* @return mixed
44+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
45+
*/
46+
function filesize($file)
47+
{
48+
return Gd2Test::$imageSize;
49+
}
50+
3851
/**
3952
* @param $real
4053
* @return int

0 commit comments

Comments
 (0)