Skip to content

Commit 899e950

Browse files
authored
Merge pull request #3488 from magento-tango/MAGETWO-91070
[tango] MAGETWO-91070: Missing Swatch Images Break Catalog Pages
2 parents 653d533 + 0bfccfc commit 899e950

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

app/code/Magento/Swatches/Helper/Media.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
/**
1313
* Helper to move images from tmp to catalog directory
14+
*
1415
* @api
1516
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1617
* @since 100.0.2
@@ -19,7 +20,6 @@ class Media extends \Magento\Framework\App\Helper\AbstractHelper
1920
{
2021
/**
2122
* Swatch area inside media folder
22-
*
2323
*/
2424
const SWATCH_MEDIA_PATH = 'attribute/swatch';
2525

@@ -100,6 +100,8 @@ public function __construct(
100100
}
101101

102102
/**
103+
* Get swatch attribute image
104+
*
103105
* @param string $swatchType
104106
* @param string $file
105107
* @return string
@@ -110,13 +112,17 @@ public function getSwatchAttributeImage($swatchType, $file)
110112
$absoluteImagePath = $this->mediaDirectory
111113
->getAbsolutePath($this->getSwatchMediaPath() . '/' . $generationPath);
112114
if (!file_exists($absoluteImagePath)) {
113-
$this->generateSwatchVariations($file);
115+
try {
116+
$this->generateSwatchVariations($file);
117+
} catch (\Exception $e) {
118+
return '';
119+
}
114120
}
115121
return $this->getSwatchMediaUrl() . '/' . $generationPath;
116122
}
117123

118124
/**
119-
* move image from tmp to catalog dir
125+
* Move image from tmp to catalog dir
120126
*
121127
* @param string $file
122128
* @return string path
@@ -152,7 +158,7 @@ public function moveImageFromTmp($file)
152158
/**
153159
* Check whether file to move exists. Getting unique name
154160
*
155-
* @param <type> $file
161+
* @param string $file
156162
* @return string
157163
*/
158164
protected function getUniqueFileName($file)
@@ -176,6 +182,7 @@ protected function getUniqueFileName($file)
176182
*
177183
* @param string $imageUrl
178184
* @return $this
185+
* @throws \Exception
179186
*/
180187
public function generateSwatchVariations($imageUrl)
181188
{
@@ -234,7 +241,7 @@ protected function generateNamePath($imageConfig, $imageUrl, $swatchType)
234241
* Generate folder name WIDTHxHEIGHT based on config in view.xml
235242
*
236243
* @param string $swatchType
237-
* @param null $imageConfig
244+
* @param array|null $imageConfig
238245
* @return string
239246
*/
240247
public function getFolderNameSize($swatchType, $imageConfig = null)
@@ -336,6 +343,8 @@ protected function prepareFile($file)
336343
}
337344

338345
/**
346+
* Get registered themes
347+
*
339348
* @return \Magento\Theme\Model\ResourceModel\Theme\Collection
340349
*/
341350
private function getRegisteredThemes()

app/code/Magento/Swatches/Test/Unit/Helper/MediaTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,15 @@ protected function setUp()
9191

9292
/**
9393
* @dataProvider dataForFullPath
94+
* @param string $swatchType
95+
* @param string $expectedResult
9496
*/
9597
public function testGetSwatchAttributeImage($swatchType, $expectedResult)
9698
{
9799
$this->storeManagerMock
98100
->expects($this->once())
99101
->method('getStore')
100102
->willReturn($this->storeMock);
101-
102103
$this->storeMock
103104
->expects($this->once())
104105
->method('getBaseUrl')
@@ -107,11 +108,41 @@ public function testGetSwatchAttributeImage($swatchType, $expectedResult)
107108

108109
$this->generateImageConfig();
109110

110-
$this->testGenerateSwatchVariations();
111+
$image = $this->createPartialMock(\Magento\Framework\Image::class, [
112+
'resize',
113+
'save',
114+
'keepTransparency',
115+
'constrainOnly',
116+
'keepFrame',
117+
'keepAspectRatio',
118+
'backgroundColor',
119+
'quality'
120+
]);
121+
$this->imageFactoryMock->expects($this->atLeastOnce())
122+
->method('create')
123+
->willReturn($image);
124+
$image->expects($this->atLeastOnce())
125+
->method('resize')
126+
->will($this->returnSelf());
127+
$image->expects($this->atLeastOnce())
128+
->method('backgroundColor')
129+
->with([255, 255, 255])
130+
->willReturnSelf();
111131

112132
$result = $this->mediaHelperObject->getSwatchAttributeImage($swatchType, '/f/i/file.png');
133+
$this->assertEquals($expectedResult, $result);
134+
}
135+
136+
public function testGetSwatchAttributeImageWithMissingFile()
137+
{
138+
$this->generateImageConfig();
139+
140+
$this->imageFactoryMock->expects($this->atLeastOnce())
141+
->method('create')
142+
->willThrowException(new \Exception(''));
113143

114-
$this->assertEquals($result, $expectedResult);
144+
$result = $this->mediaHelperObject->getSwatchAttributeImage('swatch_image', '/f/i/file.png');
145+
$this->assertEquals('', $result);
115146
}
116147

117148
/**
@@ -195,6 +226,9 @@ public function testGetSwatchMediaUrl()
195226

196227
/**
197228
* @dataProvider dataForFolderName
229+
* @param string $swatchType
230+
* @param array|null $imageConfig
231+
* @param string $expectedResult
198232
*/
199233
public function testGetFolderNameSize($swatchType, $imageConfig, $expectedResult)
200234
{
@@ -293,6 +327,8 @@ public function testGetSwatchMediaPath()
293327

294328
/**
295329
* @dataProvider getSwatchTypes
330+
* @param string $swatchType
331+
* @param string $expectedResult
296332
*/
297333
public function testGetSwatchCachePath($swatchType, $expectedResult)
298334
{

0 commit comments

Comments
 (0)