Skip to content

Commit 4e7cd44

Browse files
MC-21715: Storefront: view product images
1 parent 8865386 commit 4e7cd44

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Block/Product/View/GalleryTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
1111
use Magento\Catalog\Model\Product;
12-
use Magento\Catalog\Model\Product\Gallery\Processor;
1312
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
1413
use Magento\Framework\Serialize\Serializer\Json;
1514
use Magento\Framework\View\LayoutInterface;
@@ -44,11 +43,6 @@ class GalleryTest extends \PHPUnit\Framework\TestCase
4443
*/
4544
private $storeRepository;
4645

47-
/**
48-
* @var Processor
49-
*/
50-
private $galleryProcessor;
51-
5246
/**
5347
* @var Json
5448
*/
@@ -115,7 +109,6 @@ protected function setUp()
115109
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
116110
$this->productResource = $this->objectManager->get(ProductResource::class);
117111
$this->storeRepository = $this->objectManager->create(StoreRepositoryInterface::class);
118-
$this->galleryProcessor = $this->objectManager->create(Processor::class);
119112
$this->serializer = $this->objectManager->get(Json::class);
120113
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Gallery::class);
121114
}
@@ -149,7 +142,6 @@ public function testGetGalleryImagesJsonWithDisabledImage(array $images, array $
149142
$this->assertImages(reset($firstImage), $expectation);
150143
}
151144

152-
153145
/**
154146
* @dataProvider galleryDisabledImagesDataProvider
155147
* @magentoDataFixture Magento/Catalog/_files/product_with_multiple_images.php
@@ -315,10 +307,17 @@ private function setGalleryImages(Product $product, array $images, int $storeId
315307
{
316308
$product->setImage(null);
317309
foreach ($images as $file => $data) {
318-
if (!empty($data)) {
319-
$this->galleryProcessor->updateImage($product, $file, $data);
310+
$mediaGalleryData = $product->getData('media_gallery');
311+
foreach ($mediaGalleryData['images'] as &$image) {
312+
if ($image['file'] == $file) {
313+
foreach ($data as $key => $value) {
314+
$image[$key] = $value;
315+
}
316+
}
320317
}
321318

319+
$product->setData('media_gallery', $mediaGalleryData);
320+
322321
if (!empty($data['main'])) {
323322
$product->setImage($file);
324323
}

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Gallery/ReadHandlerTest.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
1212
use Magento\Catalog\Model\Product;
1313
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
14-
use Magento\Framework\EntityManager\EntityMetadata;
1514
use Magento\Framework\EntityManager\MetadataPool;
1615
use Magento\Store\Api\StoreRepositoryInterface;
1716
use Magento\Store\Model\Store;
@@ -49,14 +48,9 @@ class ReadHandlerTest extends \PHPUnit\Framework\TestCase
4948
private $storeRepository;
5049

5150
/**
52-
* @var Processor
51+
* @var string
5352
*/
54-
private $galleryProcessor;
55-
56-
/**
57-
* @var EntityMetadata
58-
*/
59-
private $metadata;
53+
private $productLinkField;
6054

6155
/**
6256
* @inheritdoc
@@ -68,8 +62,9 @@ protected function setUp()
6862
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
6963
$this->productResource = $this->objectManager->get(ProductResource::class);
7064
$this->storeRepository = $this->objectManager->create(StoreRepositoryInterface::class);
71-
$this->galleryProcessor = $this->objectManager->create(Processor::class);
72-
$this->metadata = $this->objectManager->get(MetadataPool::class)->getMetadata(ProductInterface::class);
65+
$this->productLinkField = $this->objectManager->get(MetadataPool::class)
66+
->getMetadata(ProductInterface::class)
67+
->getLinkField();
7368
}
7469

7570
/**
@@ -117,13 +112,7 @@ public function testExecuteWithOneImage(): void
117112
*/
118113
public function testExecuteWithTwoImages(array $images, array $expectation): void
119114
{
120-
$product = $this->getProduct();
121-
foreach ($images as $file => $data) {
122-
if (!empty($data)) {
123-
$this->galleryProcessor->updateImage($product, $file, $data);
124-
}
125-
}
126-
$this->saveProduct($product);
115+
$this->setGalleryImages($this->getProduct(), $images);
127116
$productInstance = $this->getProductInstance();
128117
$this->readHandler->execute($productInstance);
129118
$data = $productInstance->getData();
@@ -215,9 +204,8 @@ public function executeWithTwoImagesDataProvider(): array
215204
public function testExecuteOnStoreView(string $file, string $field, string $value, array $expectation): void
216205
{
217206
$product = $this->getProduct();
218-
$this->galleryProcessor->updateImage($product, $file, [$field => $value]);
219207
$secondStoreId = (int)$this->storeRepository->get('fixture_second_store')->getId();
220-
$this->saveProduct($product, (int)$secondStoreId);
208+
$this->setGalleryImages($product, [$file => [$field => $value]], (int)$secondStoreId);
221209
$productInstance = $this->getProductInstance($secondStoreId);
222210
$this->readHandler->execute($productInstance);
223211
$data = $productInstance->getData();
@@ -281,15 +269,32 @@ private function getProduct(): Product
281269
}
282270

283271
/**
284-
* Saves product via resource model.
285-
* Uses product resource, because saving via repository requires image in base64 format.
272+
* Updates product gallery images and saves product.
286273
*
287274
* @param Product $product
275+
* @param array $images
288276
* @param int|null $storeId
289277
* @return void
290278
*/
291-
private function saveProduct(Product $product, int $storeId = null): void
279+
private function setGalleryImages(Product $product, array $images, int $storeId = null): void
292280
{
281+
$product->setImage(null);
282+
foreach ($images as $file => $data) {
283+
$mediaGalleryData = $product->getData('media_gallery');
284+
foreach ($mediaGalleryData['images'] as &$image) {
285+
if ($image['file'] == $file) {
286+
foreach ($data as $key => $value) {
287+
$image[$key] = $value;
288+
}
289+
}
290+
}
291+
292+
$product->setData('media_gallery', $mediaGalleryData);
293+
if (!empty($data['main'])) {
294+
$product->setImage($file);
295+
}
296+
}
297+
293298
if ($storeId) {
294299
$product->setStoreId($storeId);
295300
}
@@ -308,8 +313,8 @@ private function getProductInstance(int $storeId = null): Product
308313
/** @var Product $product */
309314
$product = $this->objectManager->create(Product::class);
310315
$product->setData(
311-
$this->metadata->getLinkField(),
312-
$this->getProduct()->getData($this->metadata->getLinkField())
316+
$this->productLinkField,
317+
$this->getProduct()->getData($this->productLinkField)
313318
);
314319

315320
if ($storeId) {

0 commit comments

Comments
 (0)