11
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
12
use Magento \Catalog \Model \Product ;
13
13
use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
14
- use Magento \Framework \EntityManager \EntityMetadata ;
15
14
use Magento \Framework \EntityManager \MetadataPool ;
16
15
use Magento \Store \Api \StoreRepositoryInterface ;
17
16
use Magento \Store \Model \Store ;
@@ -49,14 +48,9 @@ class ReadHandlerTest extends \PHPUnit\Framework\TestCase
49
48
private $ storeRepository ;
50
49
51
50
/**
52
- * @var Processor
51
+ * @var string
53
52
*/
54
- private $ galleryProcessor ;
55
-
56
- /**
57
- * @var EntityMetadata
58
- */
59
- private $ metadata ;
53
+ private $ productLinkField ;
60
54
61
55
/**
62
56
* @inheritdoc
@@ -68,8 +62,9 @@ protected function setUp()
68
62
$ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
69
63
$ this ->productResource = $ this ->objectManager ->get (ProductResource::class);
70
64
$ 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 ();
73
68
}
74
69
75
70
/**
@@ -117,13 +112,7 @@ public function testExecuteWithOneImage(): void
117
112
*/
118
113
public function testExecuteWithTwoImages (array $ images , array $ expectation ): void
119
114
{
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 );
127
116
$ productInstance = $ this ->getProductInstance ();
128
117
$ this ->readHandler ->execute ($ productInstance );
129
118
$ data = $ productInstance ->getData ();
@@ -215,9 +204,8 @@ public function executeWithTwoImagesDataProvider(): array
215
204
public function testExecuteOnStoreView (string $ file , string $ field , string $ value , array $ expectation ): void
216
205
{
217
206
$ product = $ this ->getProduct ();
218
- $ this ->galleryProcessor ->updateImage ($ product , $ file , [$ field => $ value ]);
219
207
$ secondStoreId = (int )$ this ->storeRepository ->get ('fixture_second_store ' )->getId ();
220
- $ this ->saveProduct ($ product , (int )$ secondStoreId );
208
+ $ this ->setGalleryImages ($ product, [ $ file => [ $ field => $ value ]] , (int )$ secondStoreId );
221
209
$ productInstance = $ this ->getProductInstance ($ secondStoreId );
222
210
$ this ->readHandler ->execute ($ productInstance );
223
211
$ data = $ productInstance ->getData ();
@@ -281,15 +269,32 @@ private function getProduct(): Product
281
269
}
282
270
283
271
/**
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.
286
273
*
287
274
* @param Product $product
275
+ * @param array $images
288
276
* @param int|null $storeId
289
277
* @return void
290
278
*/
291
- private function saveProduct (Product $ product , int $ storeId = null ): void
279
+ private function setGalleryImages (Product $ product, array $ images , int $ storeId = null ): void
292
280
{
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
+
293
298
if ($ storeId ) {
294
299
$ product ->setStoreId ($ storeId );
295
300
}
@@ -308,8 +313,8 @@ private function getProductInstance(int $storeId = null): Product
308
313
/** @var Product $product */
309
314
$ product = $ this ->objectManager ->create (Product::class);
310
315
$ product ->setData (
311
- $ this ->metadata -> getLinkField () ,
312
- $ this ->getProduct ()->getData ($ this ->metadata -> getLinkField () )
316
+ $ this ->productLinkField ,
317
+ $ this ->getProduct ()->getData ($ this ->productLinkField )
313
318
);
314
319
315
320
if ($ storeId ) {
0 commit comments