Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit bcd05dd

Browse files
⏫ Forwardport of magento/magento2#11323 to 2.3-develop branch
1 parent c7207f6 commit bcd05dd

File tree

3 files changed

+195
-17
lines changed

3 files changed

+195
-17
lines changed

app/code/Magento/Catalog/Block/Product/View/Gallery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function getGalleryImagesJson()
132132
'thumb' => $image->getData('small_image_url'),
133133
'img' => $image->getData('medium_image_url'),
134134
'full' => $image->getData('large_image_url'),
135-
'caption' => $image->getData('label'),
135+
'caption' => ($image->getLabel() ?: $this->getProduct()->getName()),
136136
'position' => $image->getData('position'),
137137
'isMain' => $this->isMainImage($image),
138138
'type' => str_replace('external-', '', $image->getMediaType()),

app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php

+85-16
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,19 @@ public function execute($product, $arguments = [])
167167
if (empty($attrData) && empty($clearImages) && empty($newImages) && empty($existImages)) {
168168
continue;
169169
}
170-
if (in_array($attrData, $clearImages)) {
171-
$product->setData($mediaAttrCode, 'no_selection');
172-
}
173-
174-
if (in_array($attrData, array_keys($newImages))) {
175-
$product->setData($mediaAttrCode, $newImages[$attrData]['new_file']);
176-
$product->setData($mediaAttrCode . '_label', $newImages[$attrData]['label']);
177-
}
178-
179-
if (in_array($attrData, array_keys($existImages)) && isset($existImages[$attrData]['label'])) {
180-
$product->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']);
181-
}
182-
if (!empty($product->getData($mediaAttrCode))) {
183-
$product->addAttributeUpdate(
170+
$this->processMediaAttribute(
171+
$product,
172+
$mediaAttrCode,
173+
$clearImages,
174+
$newImages
175+
);
176+
if (in_array($mediaAttrCode, ['image', 'small_image', 'thumbnail'])) {
177+
$this->processMediaAttributeLabel(
178+
$product,
184179
$mediaAttrCode,
185-
$product->getData($mediaAttrCode),
186-
$product->getStoreId()
180+
$clearImages,
181+
$newImages,
182+
$existImages
187183
);
188184
}
189185
}
@@ -448,4 +444,77 @@ private function getMediaAttributeCodes()
448444
}
449445
return $this->mediaAttributeCodes;
450446
}
447+
448+
/**
449+
* @param \Magento\Catalog\Model\Product $product
450+
* @param $mediaAttrCode
451+
* @param array $clearImages
452+
* @param array $newImages
453+
*/
454+
private function processMediaAttribute(
455+
\Magento\Catalog\Model\Product $product,
456+
$mediaAttrCode,
457+
array $clearImages,
458+
array $newImages
459+
) {
460+
$attrData = $product->getData($mediaAttrCode);
461+
if (in_array($attrData, $clearImages)) {
462+
$product->setData($mediaAttrCode, 'no_selection');
463+
}
464+
465+
if (in_array($attrData, array_keys($newImages))) {
466+
$product->setData($mediaAttrCode, $newImages[$attrData]['new_file']);
467+
}
468+
if (!empty($product->getData($mediaAttrCode))) {
469+
$product->addAttributeUpdate(
470+
$mediaAttrCode,
471+
$product->getData($mediaAttrCode),
472+
$product->getStoreId()
473+
);
474+
}
475+
}
476+
477+
/**
478+
* @param \Magento\Catalog\Model\Product $product
479+
* @param $mediaAttrCode
480+
* @param array $clearImages
481+
* @param array $newImages
482+
* @param array $existImages
483+
*/
484+
private function processMediaAttributeLabel(
485+
\Magento\Catalog\Model\Product $product,
486+
$mediaAttrCode,
487+
array $clearImages,
488+
array $newImages,
489+
array $existImages
490+
) {
491+
$resetLabel = false;
492+
$attrData = $product->getData($mediaAttrCode);
493+
if (in_array($attrData, $clearImages)) {
494+
$product->setData($mediaAttrCode . '_label', null);
495+
$resetLabel = true;
496+
}
497+
498+
if (in_array($attrData, array_keys($newImages))) {
499+
$product->setData($mediaAttrCode . '_label', $newImages[$attrData]['label']);
500+
}
501+
502+
if (in_array($attrData, array_keys($existImages)) && isset($existImages[$attrData]['label'])) {
503+
$product->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']);
504+
}
505+
506+
if ($attrData === 'no_selection' && !empty($product->getData($mediaAttrCode . '_label'))) {
507+
$product->setData($mediaAttrCode . '_label', null);
508+
$resetLabel = true;
509+
}
510+
if (!empty($product->getData($mediaAttrCode . '_label'))
511+
|| $resetLabel === true
512+
) {
513+
$product->addAttributeUpdate(
514+
$mediaAttrCode . '_label',
515+
$product->getData($mediaAttrCode . '_label'),
516+
$product->getStoreId()
517+
);
518+
}
519+
}
451520
}

app/code/Magento/Catalog/Test/Unit/Block/Product/View/GalleryTest.php

+109
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,89 @@ protected function mockContext()
9191
->willReturn($this->registry);
9292
}
9393

94+
public function testGetGalleryImagesJsonWithLabel()
95+
{
96+
$this->prepareGetGalleryImagesJsonMocks();
97+
$json = $this->model->getGalleryImagesJson();
98+
$decodedJson = json_decode($json, true);
99+
$this->assertEquals('product_page_image_small_url', $decodedJson[0]['thumb']);
100+
$this->assertEquals('product_page_image_medium_url', $decodedJson[0]['img']);
101+
$this->assertEquals('product_page_image_large_url', $decodedJson[0]['full']);
102+
$this->assertEquals('test_label', $decodedJson[0]['caption']);
103+
$this->assertEquals('2', $decodedJson[0]['position']);
104+
$this->assertEquals(false, $decodedJson[0]['isMain']);
105+
$this->assertEquals('test_media_type', $decodedJson[0]['type']);
106+
$this->assertEquals('test_video_url', $decodedJson[0]['videoUrl']);
107+
}
108+
109+
public function testGetGalleryImagesJsonWithoutLabel()
110+
{
111+
$this->prepareGetGalleryImagesJsonMocks(false);
112+
$json = $this->model->getGalleryImagesJson();
113+
$decodedJson = json_decode($json, true);
114+
$this->assertEquals('test_product_name', $decodedJson[0]['caption']);
115+
}
116+
117+
private function prepareGetGalleryImagesJsonMocks($hasLabel = true)
118+
{
119+
$storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
120+
->disableOriginalConstructor()
121+
->getMock();
122+
123+
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
124+
->disableOriginalConstructor()
125+
->getMock();
126+
127+
$productTypeMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Type\AbstractType::class)
128+
->disableOriginalConstructor()
129+
->getMock();
130+
$productTypeMock->expects($this->any())
131+
->method('getStoreFilter')
132+
->with($productMock)
133+
->willReturn($storeMock);
134+
135+
$productMock->expects($this->any())
136+
->method('getTypeInstance')
137+
->willReturn($productTypeMock);
138+
$productMock->expects($this->any())
139+
->method('getMediaGalleryImages')
140+
->willReturn($this->getImagesCollectionWithPopulatedDataObject($hasLabel));
141+
$productMock->expects($this->any())
142+
->method('getName')
143+
->willReturn('test_product_name');
144+
145+
$this->registry->expects($this->any())
146+
->method('registry')
147+
->with('product')
148+
->willReturn($productMock);
149+
150+
$this->imageHelper->expects($this->any())
151+
->method('init')
152+
->willReturnMap([
153+
[$productMock, 'product_page_image_small', [], $this->imageHelper],
154+
[$productMock, 'product_page_image_medium_no_frame', [], $this->imageHelper],
155+
[$productMock, 'product_page_image_large_no_frame', [], $this->imageHelper],
156+
])
157+
->willReturnSelf();
158+
$this->imageHelper->expects($this->any())
159+
->method('setImageFile')
160+
->with('test_file')
161+
->willReturnSelf();
162+
$this->imageHelper->expects($this->at(2))
163+
->method('getUrl')
164+
->willReturn('product_page_image_small_url');
165+
$this->imageHelper->expects($this->at(5))
166+
->method('getUrl')
167+
->willReturn('product_page_image_medium_url');
168+
$this->imageHelper->expects($this->at(8))
169+
->method('getUrl')
170+
->willReturn('product_page_image_large_url');
171+
172+
$this->galleryImagesConfigMock->expects($this->exactly(2))
173+
->method('getItems')
174+
->willReturn($this->getGalleryImagesConfigItems());
175+
}
176+
94177
public function testGetGalleryImages()
95178
{
96179
$storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
@@ -225,4 +308,30 @@ private function getGalleryImagesConfigItems()
225308
])
226309
];
227310
}
311+
312+
/**
313+
* @return \Magento\Framework\Data\Collection
314+
*/
315+
private function getImagesCollectionWithPopulatedDataObject($hasLabel)
316+
{
317+
$collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
318+
->disableOriginalConstructor()
319+
->getMock();
320+
321+
$items = [
322+
new \Magento\Framework\DataObject([
323+
'file' => 'test_file',
324+
'label' => ($hasLabel ? 'test_label' : ''),
325+
'position' => '2',
326+
'media_type' => 'external-test_media_type',
327+
"video_url" => 'test_video_url'
328+
]),
329+
];
330+
331+
$collectionMock->expects($this->any())
332+
->method('getIterator')
333+
->willReturn(new \ArrayIterator($items));
334+
335+
return $collectionMock;
336+
}
228337
}

0 commit comments

Comments
 (0)