Skip to content

Commit 6e1e7d6

Browse files
committed
MAGETWO-81368: Defaulting missing alt-text for a product to use the product name. #11323
- Merge Pull Request #11323 from brobie/magento2:2.2-develop - Merged commits: 1. 836cb30 2. 3381ec0 3. 0f1b58f 4. 7f44e66 5. 8aeb71b 6. 1511377 7. bab1a38 8. d6fa9db 9. ae5454a
2 parents 6862632 + ae5454a commit 6e1e7d6

File tree

3 files changed

+191
-17
lines changed

3 files changed

+191
-17
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function getGalleryImagesJson()
116116
'thumb' => $image->getData('small_image_url'),
117117
'img' => $image->getData('medium_image_url'),
118118
'full' => $image->getData('large_image_url'),
119-
'caption' => $image->getLabel(),
119+
'caption' => ($image->getLabel() ?: $this->getProduct()->getName()),
120120
'position' => $image->getPosition(),
121121
'isMain' => $this->isMainImage($image),
122122
'type' => str_replace('external-', '', $image->getMediaType()),

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

Lines changed: 85 additions & 16 deletions
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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,85 @@ protected function mockContext()
7777
->willReturn($this->registry);
7878
}
7979

80+
public function testGetGalleryImagesJsonWithLabel()
81+
{
82+
$this->prepareGetGalleryImagesJsonMocks();
83+
$json = $this->model->getGalleryImagesJson();
84+
$decodedJson = json_decode($json, true);
85+
$this->assertEquals('product_page_image_small_url', $decodedJson[0]['thumb']);
86+
$this->assertEquals('product_page_image_medium_url', $decodedJson[0]['img']);
87+
$this->assertEquals('product_page_image_large_url', $decodedJson[0]['full']);
88+
$this->assertEquals('test_label', $decodedJson[0]['caption']);
89+
$this->assertEquals('2', $decodedJson[0]['position']);
90+
$this->assertEquals(false, $decodedJson[0]['isMain']);
91+
$this->assertEquals('test_media_type', $decodedJson[0]['type']);
92+
$this->assertEquals('test_video_url', $decodedJson[0]['videoUrl']);
93+
}
94+
95+
public function testGetGalleryImagesJsonWithoutLabel()
96+
{
97+
$this->prepareGetGalleryImagesJsonMocks(false);
98+
$json = $this->model->getGalleryImagesJson();
99+
$decodedJson = json_decode($json, true);
100+
$this->assertEquals('test_product_name', $decodedJson[0]['caption']);
101+
}
102+
103+
private function prepareGetGalleryImagesJsonMocks($hasLabel = true)
104+
{
105+
$storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
106+
->disableOriginalConstructor()
107+
->getMock();
108+
109+
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
110+
->disableOriginalConstructor()
111+
->getMock();
112+
113+
$productTypeMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Type\AbstractType::class)
114+
->disableOriginalConstructor()
115+
->getMock();
116+
$productTypeMock->expects($this->any())
117+
->method('getStoreFilter')
118+
->with($productMock)
119+
->willReturn($storeMock);
120+
121+
$productMock->expects($this->any())
122+
->method('getTypeInstance')
123+
->willReturn($productTypeMock);
124+
$productMock->expects($this->any())
125+
->method('getMediaGalleryImages')
126+
->willReturn($this->getImagesCollectionWithPopulatedDataObject($hasLabel));
127+
$productMock->expects($this->any())
128+
->method('getName')
129+
->willReturn('test_product_name');
130+
131+
$this->registry->expects($this->any())
132+
->method('registry')
133+
->with('product')
134+
->willReturn($productMock);
135+
136+
$this->imageHelper->expects($this->any())
137+
->method('init')
138+
->willReturnMap([
139+
[$productMock, 'product_page_image_small', [], $this->imageHelper],
140+
[$productMock, 'product_page_image_medium_no_frame', [], $this->imageHelper],
141+
[$productMock, 'product_page_image_large_no_frame', [], $this->imageHelper],
142+
])
143+
->willReturnSelf();
144+
$this->imageHelper->expects($this->any())
145+
->method('setImageFile')
146+
->with('test_file')
147+
->willReturnSelf();
148+
$this->imageHelper->expects($this->at(2))
149+
->method('getUrl')
150+
->willReturn('product_page_image_small_url');
151+
$this->imageHelper->expects($this->at(5))
152+
->method('getUrl')
153+
->willReturn('product_page_image_medium_url');
154+
$this->imageHelper->expects($this->at(8))
155+
->method('getUrl')
156+
->willReturn('product_page_image_large_url');
157+
}
158+
80159
public function testGetGalleryImages()
81160
{
82161
$storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
@@ -154,4 +233,30 @@ private function getImagesCollection()
154233

155234
return $collectionMock;
156235
}
236+
237+
/**
238+
* @return \Magento\Framework\Data\Collection
239+
*/
240+
private function getImagesCollectionWithPopulatedDataObject($hasLabel)
241+
{
242+
$collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
243+
->disableOriginalConstructor()
244+
->getMock();
245+
246+
$items = [
247+
new \Magento\Framework\DataObject([
248+
'file' => 'test_file',
249+
'label' => ($hasLabel ? 'test_label' : ''),
250+
'position' => '2',
251+
'media_type' => 'external-test_media_type',
252+
"video_url" => 'test_video_url'
253+
]),
254+
];
255+
256+
$collectionMock->expects($this->any())
257+
->method('getIterator')
258+
->willReturn(new \ArrayIterator($items));
259+
260+
return $collectionMock;
261+
}
157262
}

0 commit comments

Comments
 (0)