Skip to content

Commit fdd1ad8

Browse files
committed
Merge branch 'ACP2E-3126' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-2024-07-11
2 parents d4648e4 + 92ede33 commit fdd1ad8

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function execute($entity, $arguments = [])
6464

6565
$this->addMediaDataToProduct(
6666
$entity,
67-
$this->sortMediaEntriesByPosition($mediaEntries)
67+
$mediaEntries
6868
);
6969

7070
return $entity;
@@ -80,6 +80,7 @@ public function execute($entity, $arguments = [])
8080
*/
8181
public function addMediaDataToProduct(Product $product, array $mediaEntries)
8282
{
83+
$mediaEntries = $this->sortMediaEntriesByPosition($mediaEntries);
8384
$product->setData(
8485
$this->getAttribute()->getAttributeCode(),
8586
[
@@ -111,6 +112,7 @@ public function getAttribute()
111112
* @param string[] $image
112113
* @return string
113114
* @deprecated 101.0.1
115+
* @see \Magento\Catalog\Model\Product\Gallery\ReadHandler::addMediaDataToProduct
114116
* @since 101.0.0
115117
*/
116118
protected function findDefaultValue($key, &$image)

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
namespace Magento\GraphQl\Catalog;
99

10+
use Magento\Catalog\Test\Fixture\Category as CategoryFixture;
11+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\TestFramework\Fixture\DataFixture;
14+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
1015
use Magento\TestFramework\TestCase\GraphQlAbstract;
1116

1217
/**
@@ -214,6 +219,79 @@ public function testProductMediaGalleryEntries()
214219
);
215220
}
216221

222+
/**
223+
* Tests the sorting of media gallery entries by their position attribute for a given product.
224+
*
225+
* @return void
226+
* @throws LocalizedException
227+
*/
228+
#[
229+
DataFixture(CategoryFixture::class, ['name' => 'Category'], 'category'),
230+
DataFixture(
231+
ProductFixture::class,
232+
[
233+
'name' => 'Product 1',
234+
'sku' => 'product-1',
235+
'category_ids' => ['$category.id$'],
236+
'price' => 10,
237+
'media_gallery_entries' => [
238+
[
239+
'label' => 'image',
240+
'media_type' => 'image',
241+
'position' => 0,
242+
'disabled' => false,
243+
'types' => [
244+
'image',
245+
'small_image',
246+
'thumbnail'
247+
],
248+
'file' => '/m/product1.jpg',
249+
],
250+
[
251+
'label' => 'image',
252+
'media_type' => 'image',
253+
'position' => 2,
254+
'disabled' => false,
255+
'file' => '/m/product3.jpg',
256+
],
257+
[
258+
'label' => 'image',
259+
'media_type' => 'image',
260+
'position' => 1,
261+
'disabled' => false,
262+
'file' => '/m/product2.jpg',
263+
],
264+
],
265+
],
266+
'product1'
267+
),
268+
]
269+
public function testProductMediaGalleryEntriesPositionSort()
270+
{
271+
$product = DataFixtureStorageManager::getStorage()->get('product1');
272+
$productSku = $product->getSku();
273+
$query = <<<QUERY
274+
{
275+
products(filter: {sku: {eq: "{$productSku}"}}) {
276+
items {
277+
name
278+
sku
279+
media_gallery_entries {
280+
id
281+
position
282+
file
283+
}
284+
}
285+
}
286+
}
287+
QUERY;
288+
$response = $this->graphQlQuery($query);
289+
self::assertCount(3, $response['products']['items'][0]['media_gallery_entries']);
290+
foreach ($response['products']['items'][0]['media_gallery_entries'] as $key => $mediaGalleryEntry) {
291+
self::assertEquals($key, $mediaGalleryEntry['position']);
292+
}
293+
}
294+
217295
/**
218296
* @param string $url
219297
* @return bool

0 commit comments

Comments
 (0)