Skip to content

Commit

Permalink
Merge pull request magento#3488 from magento-tango/MAGETWO-91070
Browse files Browse the repository at this point in the history
[tango] MAGETWO-91070: Missing Swatch Images Break Catalog Pages
  • Loading branch information
dhorytskyi authored Nov 28, 2018
2 parents 653d533 + 0bfccfc commit 899e950
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
19 changes: 14 additions & 5 deletions app/code/Magento/Swatches/Helper/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

/**
* Helper to move images from tmp to catalog directory
*
* @api
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @since 100.0.2
Expand All @@ -19,7 +20,6 @@ class Media extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* Swatch area inside media folder
*
*/
const SWATCH_MEDIA_PATH = 'attribute/swatch';

Expand Down Expand Up @@ -100,6 +100,8 @@ public function __construct(
}

/**
* Get swatch attribute image
*
* @param string $swatchType
* @param string $file
* @return string
Expand All @@ -110,13 +112,17 @@ public function getSwatchAttributeImage($swatchType, $file)
$absoluteImagePath = $this->mediaDirectory
->getAbsolutePath($this->getSwatchMediaPath() . '/' . $generationPath);
if (!file_exists($absoluteImagePath)) {
$this->generateSwatchVariations($file);
try {
$this->generateSwatchVariations($file);
} catch (\Exception $e) {
return '';
}
}
return $this->getSwatchMediaUrl() . '/' . $generationPath;
}

/**
* move image from tmp to catalog dir
* Move image from tmp to catalog dir
*
* @param string $file
* @return string path
Expand Down Expand Up @@ -152,7 +158,7 @@ public function moveImageFromTmp($file)
/**
* Check whether file to move exists. Getting unique name
*
* @param <type> $file
* @param string $file
* @return string
*/
protected function getUniqueFileName($file)
Expand All @@ -176,6 +182,7 @@ protected function getUniqueFileName($file)
*
* @param string $imageUrl
* @return $this
* @throws \Exception
*/
public function generateSwatchVariations($imageUrl)
{
Expand Down Expand Up @@ -234,7 +241,7 @@ protected function generateNamePath($imageConfig, $imageUrl, $swatchType)
* Generate folder name WIDTHxHEIGHT based on config in view.xml
*
* @param string $swatchType
* @param null $imageConfig
* @param array|null $imageConfig
* @return string
*/
public function getFolderNameSize($swatchType, $imageConfig = null)
Expand Down Expand Up @@ -336,6 +343,8 @@ protected function prepareFile($file)
}

/**
* Get registered themes
*
* @return \Magento\Theme\Model\ResourceModel\Theme\Collection
*/
private function getRegisteredThemes()
Expand Down
42 changes: 39 additions & 3 deletions app/code/Magento/Swatches/Test/Unit/Helper/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ protected function setUp()

/**
* @dataProvider dataForFullPath
* @param string $swatchType
* @param string $expectedResult
*/
public function testGetSwatchAttributeImage($swatchType, $expectedResult)
{
$this->storeManagerMock
->expects($this->once())
->method('getStore')
->willReturn($this->storeMock);

$this->storeMock
->expects($this->once())
->method('getBaseUrl')
Expand All @@ -107,11 +108,41 @@ public function testGetSwatchAttributeImage($swatchType, $expectedResult)

$this->generateImageConfig();

$this->testGenerateSwatchVariations();
$image = $this->createPartialMock(\Magento\Framework\Image::class, [
'resize',
'save',
'keepTransparency',
'constrainOnly',
'keepFrame',
'keepAspectRatio',
'backgroundColor',
'quality'
]);
$this->imageFactoryMock->expects($this->atLeastOnce())
->method('create')
->willReturn($image);
$image->expects($this->atLeastOnce())
->method('resize')
->will($this->returnSelf());
$image->expects($this->atLeastOnce())
->method('backgroundColor')
->with([255, 255, 255])
->willReturnSelf();

$result = $this->mediaHelperObject->getSwatchAttributeImage($swatchType, '/f/i/file.png');
$this->assertEquals($expectedResult, $result);
}

public function testGetSwatchAttributeImageWithMissingFile()
{
$this->generateImageConfig();

$this->imageFactoryMock->expects($this->atLeastOnce())
->method('create')
->willThrowException(new \Exception(''));

$this->assertEquals($result, $expectedResult);
$result = $this->mediaHelperObject->getSwatchAttributeImage('swatch_image', '/f/i/file.png');
$this->assertEquals('', $result);
}

/**
Expand Down Expand Up @@ -195,6 +226,9 @@ public function testGetSwatchMediaUrl()

/**
* @dataProvider dataForFolderName
* @param string $swatchType
* @param array|null $imageConfig
* @param string $expectedResult
*/
public function testGetFolderNameSize($swatchType, $imageConfig, $expectedResult)
{
Expand Down Expand Up @@ -293,6 +327,8 @@ public function testGetSwatchMediaPath()

/**
* @dataProvider getSwatchTypes
* @param string $swatchType
* @param string $expectedResult
*/
public function testGetSwatchCachePath($swatchType, $expectedResult)
{
Expand Down

0 comments on commit 899e950

Please sign in to comment.