Skip to content

Commit 6496f4e

Browse files
committed
Correct image params - allow to disable image frame, fix image params hash
1 parent e7e3377 commit 6496f4e

File tree

4 files changed

+129
-31
lines changed

4 files changed

+129
-31
lines changed

app/code/Magento/Catalog/Helper/Image.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,31 +213,29 @@ protected function setImageProperties()
213213

214214
// Set 'keep frame' flag
215215
$frame = $this->getFrame();
216-
if (!empty($frame)) {
217-
$this->_getModel()->setKeepFrame($frame);
218-
}
216+
$this->_getModel()->setKeepFrame($frame);
219217

220218
// Set 'constrain only' flag
221219
$constrain = $this->getAttribute('constrain');
222-
if (!empty($constrain)) {
220+
if (null !== $constrain) {
223221
$this->_getModel()->setConstrainOnly($constrain);
224222
}
225223

226224
// Set 'keep aspect ratio' flag
227225
$aspectRatio = $this->getAttribute('aspect_ratio');
228-
if (!empty($aspectRatio)) {
226+
if (null !== $aspectRatio) {
229227
$this->_getModel()->setKeepAspectRatio($aspectRatio);
230228
}
231229

232230
// Set 'transparency' flag
233231
$transparency = $this->getAttribute('transparency');
234-
if (!empty($transparency)) {
232+
if (null !== $transparency) {
235233
$this->_getModel()->setKeepTransparency($transparency);
236234
}
237235

238236
// Set background color
239237
$background = $this->getAttribute('background');
240-
if (!empty($background)) {
238+
if (null !== $background) {
241239
$this->_getModel()->setBackgroundColor($background);
242240
}
243241

app/code/Magento/Catalog/Model/View/Asset/Image.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ private function convertToReadableFormat($miscParams)
200200
$miscParams['image_width'] = 'w:' . ($miscParams['image_width'] ?? 'empty');
201201
$miscParams['quality'] = 'q:' . ($miscParams['quality'] ?? 'empty');
202202
$miscParams['angle'] = 'r:' . ($miscParams['angle'] ?? 'empty');
203-
$miscParams['keep_aspect_ratio'] = (isset($miscParams['keep_aspect_ratio']) ? '' : 'non') . 'proportional';
204-
$miscParams['keep_frame'] = (isset($miscParams['keep_frame']) ? '' : 'no') . 'frame';
205-
$miscParams['keep_transparency'] = (isset($miscParams['keep_transparency']) ? '' : 'no') . 'transparency';
206-
$miscParams['constrain_only'] = (isset($miscParams['constrain_only']) ? 'do' : 'not') . 'constrainonly';
207-
$miscParams['background'] = isset($miscParams['background'])
203+
$miscParams['keep_aspect_ratio'] = (!empty($miscParams['keep_aspect_ratio']) ? '' : 'non') . 'proportional';
204+
$miscParams['keep_frame'] = (!empty($miscParams['keep_frame']) ? '' : 'no') . 'frame';
205+
$miscParams['keep_transparency'] = (!empty($miscParams['keep_transparency']) ? '' : 'no') . 'transparency';
206+
$miscParams['constrain_only'] = (!empty($miscParams['constrain_only']) ? 'do' : 'not') . 'constrainonly';
207+
$miscParams['background'] = !empty($miscParams['background'])
208208
? 'rgb' . implode(',', $miscParams['background'])
209209
: 'nobackground';
210210
return $miscParams;

app/code/Magento/Catalog/Test/Unit/Helper/ImageTest.php

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class ImageTest extends \PHPUnit\Framework\TestCase
2929
*/
3030
protected $assetRepository;
3131

32+
/**
33+
* @var \Magento\Framework\Config\View|\PHPUnit\Framework\MockObject\MockObject
34+
*/
35+
protected $configView;
36+
3237
/**
3338
* @var \Magento\Framework\View\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
3439
*/
@@ -58,6 +63,10 @@ protected function setUp()
5863
->disableOriginalConstructor()
5964
->getMock();
6065

66+
$this->configView = $this->getMockBuilder(\Magento\Framework\Config\View::class)
67+
->disableOriginalConstructor()
68+
->getMock();
69+
6170
$this->viewConfig = $this->getMockBuilder(\Magento\Framework\View\ConfigInterface::class)
6271
->getMockForAbstractClass();
6372

@@ -151,23 +160,89 @@ public function initDataProvider()
151160
];
152161
}
153162

163+
/**
164+
* @param array $data - optional 'frame' key
165+
* @param bool $whiteBorders view config
166+
* @param bool $expectedKeepFrame
167+
* @dataProvider initKeepFrameDataProvider
168+
*/
169+
public function testInitKeepFrame($data, $whiteBorders, $expectedKeepFrame)
170+
{
171+
$imageId = 'test_image_id';
172+
$attributes = [];
173+
174+
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
175+
->disableOriginalConstructor()
176+
->getMock();
177+
178+
$this->prepareAttributes($data, $imageId);
179+
180+
$this->configView->expects(isset($data['frame']) ? $this->never() : $this->once())
181+
->method('getVarValue')
182+
->with('Magento_Catalog', 'product_image_white_borders')
183+
->willReturn($whiteBorders);
184+
185+
$this->viewConfig->expects($this->once())
186+
->method('getViewConfig')
187+
->willReturn($this->configView);
188+
189+
$this->image->expects($this->once())
190+
->method('setKeepFrame')
191+
->with($expectedKeepFrame)
192+
->willReturnSelf();
193+
194+
$this->helper->init($productMock, $imageId, $attributes);
195+
}
196+
197+
/**
198+
* @return array
199+
*/
200+
public function initKeepFrameDataProvider()
201+
{
202+
return [
203+
// when frame defined explicitly, it wins
204+
[
205+
'mediaImage' => [
206+
'frame' => 1,
207+
],
208+
'whiteBorders' => true,
209+
'expected' => true,
210+
],
211+
[
212+
'mediaImage' => [
213+
'frame' => 0,
214+
],
215+
'whiteBorders' => true,
216+
'expected' => false,
217+
],
218+
// when frame is not defined, var is used
219+
[
220+
'mediaImage' => [],
221+
'whiteBorders' => true,
222+
'expected' => true,
223+
],
224+
[
225+
'mediaImage' => [],
226+
'whiteBorders' => false,
227+
'expected' => false,
228+
],
229+
];
230+
}
231+
154232
/**
155233
* @param $data
156234
* @param $imageId
157235
*/
158236
protected function prepareAttributes($data, $imageId)
159237
{
160-
$configViewMock = $this->getMockBuilder(\Magento\Framework\Config\View::class)
161-
->disableOriginalConstructor()
162-
->getMock();
163-
$configViewMock->expects($this->once())
238+
$this->configView->expects($this->once())
164239
->method('getMediaAttributes')
165240
->with('Magento_Catalog', Image::MEDIA_TYPE_CONFIG_NODE, $imageId)
166241
->willReturn($data);
167242

168243
$this->viewConfig->expects($this->once())
169244
->method('getViewConfig')
170-
->willReturn($configViewMock);
245+
->willReturn($this->configView);
171246
}
172247

173248
/**

app/code/Magento/Catalog/Test/Unit/Model/View/Asset/ImageTest.php

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Catalog\Model\Product\Media\ConfigInterface;
99
use Magento\Catalog\Model\View\Asset\Image;
10+
use Magento\Framework\Encryption\Encryptor;
1011
use Magento\Framework\Encryption\EncryptorInterface;
1112
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1213
use Magento\Framework\View\Asset\ContextInterface;
@@ -103,9 +104,10 @@ public function testGetContext()
103104
/**
104105
* @param string $filePath
105106
* @param array $miscParams
107+
* @param string $readableParams
106108
* @dataProvider getPathDataProvider
107109
*/
108-
public function testGetPath($filePath, $miscParams)
110+
public function testGetPath($filePath, $miscParams, $readableParams)
109111
{
110112
$imageModel = $this->objectManager->getObject(
111113
Image::class,
@@ -118,11 +120,13 @@ public function testGetPath($filePath, $miscParams)
118120
'miscParams' => $miscParams
119121
]
120122
);
121-
$miscParams['background'] = isset($miscParams['background']) ? implode(',', $miscParams['background']) : '';
122123
$absolutePath = '/var/www/html/magento2ce/pub/media/catalog/product';
123-
$hashPath = md5(implode('_', $miscParams));
124+
$hashPath = 'somehash';
124125
$this->context->method('getPath')->willReturn($absolutePath);
125-
$this->encryptor->method('hash')->willReturn($hashPath);
126+
$this->encryptor->expects(static::once())
127+
->method('hash')
128+
->with($readableParams, $this->anything())
129+
->willReturn($hashPath);
126130
static::assertEquals(
127131
$absolutePath . '/cache/'. $hashPath . $filePath,
128132
$imageModel->getPath()
@@ -132,9 +136,10 @@ public function testGetPath($filePath, $miscParams)
132136
/**
133137
* @param string $filePath
134138
* @param array $miscParams
139+
* @param string $readableParams
135140
* @dataProvider getPathDataProvider
136141
*/
137-
public function testGetUrl($filePath, $miscParams)
142+
public function testGetUrl($filePath, $miscParams, $readableParams)
138143
{
139144
$imageModel = $this->objectManager->getObject(
140145
Image::class,
@@ -147,11 +152,13 @@ public function testGetUrl($filePath, $miscParams)
147152
'miscParams' => $miscParams
148153
]
149154
);
150-
$miscParams['background'] = isset($miscParams['background']) ? implode(',', $miscParams['background']) : '';
151155
$absolutePath = 'http://localhost/pub/media/catalog/product';
152-
$hashPath = md5(implode('_', $miscParams));
156+
$hashPath = 'somehash';
153157
$this->context->expects(static::once())->method('getBaseUrl')->willReturn($absolutePath);
154-
$this->encryptor->expects(static::once())->method('hash')->willReturn($hashPath);
158+
$this->encryptor->expects(static::once())
159+
->method('hash')
160+
->with($readableParams, $this->anything())
161+
->willReturn($hashPath);
155162
static::assertEquals(
156163
$absolutePath . '/cache/' . $hashPath . $filePath,
157164
$imageModel->getUrl()
@@ -166,23 +173,41 @@ public function getPathDataProvider()
166173
return [
167174
[
168175
'/some_file.png',
169-
[], //default value for miscParams
176+
[], //default value for miscParams,
177+
'h:empty_w:empty_q:empty_r:empty_nonproportional_noframe_notransparency_notconstrainonly_nobackground',
170178
],
171179
[
172180
'/some_file_2.png',
173181
[
174182
'image_type' => 'thumbnail',
175183
'image_height' => 75,
176184
'image_width' => 75,
177-
'keep_aspect_ratio' => 'proportional',
178-
'keep_frame' => 'frame',
179-
'keep_transparency' => 'transparency',
180-
'constrain_only' => 'doconstrainonly',
185+
'keep_aspect_ratio' => true,
186+
'keep_frame' => true,
187+
'keep_transparency' => true,
188+
'constrain_only' => true,
181189
'background' => [233,1,0],
182190
'angle' => null,
183191
'quality' => 80,
184192
],
185-
]
193+
'h:75_w:75_proportional_frame_transparency_doconstrainonly_rgb233,1,0_r:empty_q:80',
194+
],
195+
[
196+
'/some_file_3.png',
197+
[
198+
'image_type' => 'thumbnail',
199+
'image_height' => 75,
200+
'image_width' => 75,
201+
'keep_aspect_ratio' => false,
202+
'keep_frame' => false,
203+
'keep_transparency' => false,
204+
'constrain_only' => false,
205+
'background' => [233,1,0],
206+
'angle' => 90,
207+
'quality' => 80,
208+
],
209+
'h:75_w:75_nonproportional_noframe_notransparency_notconstrainonly_rgb233,1,0_r:90_q:80',
210+
],
186211
];
187212
}
188213
}

0 commit comments

Comments
 (0)