Skip to content

Commit daf6dbb

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-2128' into AUG102023_PR_pradeep
2 parents 3cf0949 + ee3a7f5 commit daf6dbb

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/MediaGallery.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public function resolve(
5151
$mediaGalleryEntries = [];
5252
foreach ($product->getMediaGalleryEntries() ?? [] as $key => $entry) {
5353
$mediaGalleryEntries[$key] = $entry->getData();
54+
if ($mediaGalleryEntries[$key]['label'] === null) {
55+
$mediaGalleryEntries[$key]['label'] = $product->getName();
56+
}
5457
$mediaGalleryEntries[$key]['model'] = $product;
5558
if ($entry->getExtensionAttributes() && $entry->getExtensionAttributes()->getVideoContent()) {
5659
$mediaGalleryEntries[$key]['video_content']
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Test\Unit\Model\Resolver\Product;
9+
10+
use Exception;
11+
use Magento\Catalog\Model\Product;
12+
use Magento\Catalog\Model\Product\Gallery\Entry;
13+
use Magento\CatalogGraphQl\Model\Resolver\Product\MediaGallery;
14+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
use Magento\Framework\GraphQl\Config\Element\Field;
19+
20+
class MediaGalleryTest extends TestCase
21+
{
22+
/**
23+
* @var Field|MockObject
24+
*/
25+
private Field|MockObject $fieldMock;
26+
27+
/**
28+
* @var ContextInterface|MockObject
29+
*/
30+
private ContextInterface|MockObject $contextMock;
31+
32+
/**
33+
* @var ResolveInfo|MockObject
34+
*/
35+
private ResolveInfo|MockObject $infoMock;
36+
37+
/**
38+
* @var Product|MockObject
39+
*/
40+
private Product|MockObject $productMock;
41+
42+
/**
43+
* @var MediaGallery
44+
*/
45+
private MediaGallery $mediaGallery;
46+
47+
protected function setUp(): void
48+
{
49+
$this->fieldMock = $this->createMock(Field::class);
50+
$this->contextMock = $this->getMockForAbstractClass(ContextInterface::class);
51+
$this->infoMock = $this->createMock(ResolveInfo::class);
52+
$this->productMock = $this->getMockBuilder(Product::class)
53+
->disableOriginalConstructor()
54+
->getMock();
55+
$this->mediaGallery = new MediaGallery();
56+
}
57+
58+
/**
59+
* @dataProvider dataProviderForResolve
60+
* @param $expected
61+
* @param $productName
62+
* @return void
63+
* @throws Exception
64+
*/
65+
public function testResolve($expected, $productName): void
66+
{
67+
$existingEntryMock = $this->getMockBuilder(Entry::class)
68+
->disableOriginalConstructor()
69+
->setMethods(['getName', 'getData', 'getExtensionAttributes'])
70+
->getMock();
71+
$existingEntryMock->expects($this->any())->method('getData')->willReturn($expected);
72+
$existingEntryMock->expects($this->any())->method(
73+
'getExtensionAttributes'
74+
)->willReturn(false);
75+
$this->productMock->expects($this->any())->method('getName')->willReturn($productName);
76+
$this->productMock->expects($this->any())->method('getMediaGalleryEntries')
77+
->willReturn([$existingEntryMock]);
78+
$result = $this->mediaGallery->resolve(
79+
$this->fieldMock,
80+
$this->contextMock,
81+
$this->infoMock,
82+
[
83+
'model' => $this->productMock
84+
],
85+
[]
86+
);
87+
$this->assertNotEmpty($result);
88+
$this->assertEquals($productName, $result[0]['label']);
89+
}
90+
91+
/**
92+
* @return array
93+
*/
94+
public function dataProviderForResolve(): array
95+
{
96+
return [
97+
[
98+
[
99+
"file" => "/w/b/wb01-black-0.jpg",
100+
"media_type" => "image",
101+
"label" => null,
102+
"position" => "1",
103+
"disabled" => "0",
104+
"types" => [
105+
"image",
106+
"small_image"
107+
],
108+
"id" => "11"
109+
],
110+
"TestImage"
111+
],
112+
[
113+
[
114+
"file" => "/w/b/wb01-black-0.jpg",
115+
"media_type" => "image",
116+
"label" => "HelloWorld",
117+
"position" => "1",
118+
"disabled" => "0",
119+
"types" => [
120+
"image",
121+
"small_image"
122+
],
123+
"id" => "11"
124+
],
125+
"HelloWorld"
126+
]
127+
];
128+
}
129+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ResolverCache/MediaGalleryTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,10 @@ public function testResolverCacheRecordIsCreatedForEachStoreView()
237237
)
238238
);
239239

240-
// The entry's label in second store view is null by default; assert the cache record has the same value
241-
$this->assertNull(json_decode($cacheEntryInSecondStoreView, true)[0]['label']);
240+
// The entry's label in second store view is not null by default and has product name;
241+
// assert the cache record has the same value
242+
$this->assertNotNull(json_decode($cacheEntryInSecondStoreView, true)[0]['label']);
243+
$this->assertEquals($product->getName(), json_decode($cacheEntryInSecondStoreView, true)[0]['label']);
242244

243245
// Assert cache keys are different
244246
$this->assertNotEquals(

0 commit comments

Comments
 (0)