11
11
use Magento \Framework \Api \Data \ImageContentInterface ;
12
12
use Magento \TestFramework \Helper \Bootstrap ;
13
13
use Magento \Catalog \Model \ProductFactory ;
14
- use Magento \TestFramework \ObjectManager ;
15
14
use Magento \Catalog \Model \Product \Attribute \Backend \Media \ImageEntryConverter ;
16
15
use Magento \Catalog \Model \ProductRepository ;
17
16
use Magento \Framework \Webapi \Rest \Request ;
18
17
use Magento \TestFramework \TestCase \WebapiAbstract ;
18
+ use Magento \Framework \ObjectManagerInterface ;
19
19
20
20
/**
21
21
* Class ProductAttributeMediaGalleryManagementInterfaceTest
@@ -48,11 +48,18 @@ class ProductAttributeMediaGalleryManagementInterfaceTest extends WebapiAbstract
48
48
*/
49
49
protected $ testImagePath ;
50
50
51
+ /**
52
+ * @var ObjectManagerInterface
53
+ */
54
+ private $ objectManager ;
55
+
51
56
/**
52
57
* @inheritDoc
53
58
*/
54
59
protected function setUp ()
55
60
{
61
+ $ this ->objectManager = Bootstrap::getObjectManager ();
62
+
56
63
$ this ->createServiceInfo = [
57
64
'rest ' => [
58
65
'resourcePath ' => '/V1/products/simple/media ' ,
@@ -98,9 +105,7 @@ protected function setUp()
98
105
*/
99
106
protected function getTargetSimpleProduct ()
100
107
{
101
- $ objectManager = Bootstrap::getObjectManager ();
102
-
103
- return $ objectManager ->get (ProductFactory::class)->create ()->load (1 );
108
+ return $ this ->objectManager ->get (ProductFactory::class)->create ()->load (1 );
104
109
}
105
110
106
111
/**
@@ -241,6 +246,10 @@ public function testCreateWithNotDefaultStoreId()
241
246
*/
242
247
public function testUpdate ()
243
248
{
249
+ $ productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
250
+ /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
251
+ $ product = $ productRepository ->get ('simple ' );
252
+ $ imageId = (int )$ product ->getMediaGalleryImages ()->getFirstItem ()->getValueId ();
244
253
$ requestData = [
245
254
'sku ' => 'simple ' ,
246
255
'entry ' => [
@@ -257,19 +266,48 @@ public function testUpdate()
257
266
. '/ ' . $ this ->getTargetGalleryEntryId ();
258
267
259
268
$ this ->assertTrue ($ this ->_webApiCall ($ this ->updateServiceInfo , $ requestData , null , 'all ' ));
269
+ $ updatedImage = $ this ->assertMediaGalleryData ($ imageId , '/m/a/magento_image.jpg ' , 'Updated Image Text ' );
270
+ $ this ->assertEquals (10 , $ updatedImage ['position_default ' ]);
271
+ $ this ->assertEquals (1 , $ updatedImage ['disabled_default ' ]);
272
+ }
260
273
261
- $ targetProduct = $ this ->getTargetSimpleProduct ();
262
- $ this ->assertEquals ('/m/a/magento_image.jpg ' , $ targetProduct ->getData ('thumbnail ' ));
263
- $ this ->assertEquals ('no_selection ' , $ targetProduct ->getData ('image ' ));
264
- $ this ->assertEquals ('no_selection ' , $ targetProduct ->getData ('small_image ' ));
265
- $ mediaGallery = $ targetProduct ->getData ('media_gallery ' );
266
- $ this ->assertCount (1 , $ mediaGallery ['images ' ]);
267
- $ updatedImage = array_shift ($ mediaGallery ['images ' ]);
268
- $ this ->assertEquals ('Updated Image Text ' , $ updatedImage ['label ' ]);
269
- $ this ->assertEquals ('/m/a/magento_image.jpg ' , $ updatedImage ['file ' ]);
270
- $ this ->assertEquals (10 , $ updatedImage ['position ' ]);
271
- $ this ->assertEquals (1 , $ updatedImage ['disabled ' ]);
272
- $ this ->assertEquals ('Updated Image Text ' , $ updatedImage ['label_default ' ]);
274
+ /**
275
+ * Update media gallery entity with new image.
276
+ *
277
+ * @magentoApiDataFixture Magento/Catalog/_files/product_with_image.php
278
+ * @return void
279
+ */
280
+ public function testUpdateWithNewImage (): void
281
+ {
282
+ $ productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
283
+ /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
284
+ $ product = $ productRepository ->get ('simple ' );
285
+ $ imageId = (int )$ product ->getMediaGalleryImages ()->getFirstItem ()->getValueId ();
286
+
287
+ $ requestData = [
288
+ 'sku ' => 'simple ' ,
289
+ 'entry ' => [
290
+ 'id ' => $ this ->getTargetGalleryEntryId (),
291
+ 'label ' => 'Updated Image Text ' ,
292
+ 'position ' => 10 ,
293
+ 'types ' => ['thumbnail ' ],
294
+ 'disabled ' => true ,
295
+ 'media_type ' => 'image ' ,
296
+ 'content ' => [
297
+ 'base64_encoded_data ' => 'iVBORw0KGgoAAAANSUhEUgAAAP8AAADGCAMAAAAqo6adAAAAA1BMVEUAAP79f '
298
+ . '+LBAAAASElEQVR4nO3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA '
299
+ . 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAA+BsYAAAF7hZJ0AAAAAElFTkSuQmCC ' ,
300
+ 'type ' => 'image/png ' ,
301
+ 'name ' => 'testname_updated.png ' ,
302
+ ],
303
+ ],
304
+ ];
305
+
306
+ $ this ->updateServiceInfo ['rest ' ]['resourcePath ' ] = $ this ->updateServiceInfo ['rest ' ]['resourcePath ' ]
307
+ . '/ ' . $ this ->getTargetGalleryEntryId ();
308
+
309
+ $ this ->assertTrue ($ this ->_webApiCall ($ this ->updateServiceInfo , $ requestData , null , 'all ' ));
310
+ $ updatedImage = $ this ->assertMediaGalleryData ($ imageId , '/t/e/testname_updated.png ' , 'Updated Image Text ' );
273
311
$ this ->assertEquals (10 , $ updatedImage ['position_default ' ]);
274
312
$ this ->assertEquals (1 , $ updatedImage ['disabled_default ' ]);
275
313
}
@@ -281,6 +319,11 @@ public function testUpdate()
281
319
*/
282
320
public function testUpdateWithNotDefaultStoreId ()
283
321
{
322
+ $ productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
323
+ /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
324
+ $ product = $ productRepository ->get ('simple ' );
325
+ $ imageId = (int )$ product ->getMediaGalleryImages ()->getFirstItem ()->getValueId ();
326
+
284
327
$ requestData = [
285
328
'sku ' => 'simple ' ,
286
329
'entry ' => [
@@ -297,21 +340,36 @@ public function testUpdateWithNotDefaultStoreId()
297
340
. '/ ' . $ this ->getTargetGalleryEntryId ();
298
341
299
342
$ this ->assertTrue ($ this ->_webApiCall ($ this ->updateServiceInfo , $ requestData , null , 'default ' ));
343
+ $ updatedImage = $ this ->assertMediaGalleryData ($ imageId , '/m/a/magento_image.jpg ' , 'Image Alt Text ' );
344
+ $ this ->assertEquals (1 , $ updatedImage ['position_default ' ]);
345
+ $ this ->assertEquals (0 , $ updatedImage ['disabled_default ' ]);
346
+ }
300
347
348
+ /**
349
+ * Check that Media Gallery data is correct.
350
+ *
351
+ * @param int $imageId
352
+ * @param string $file
353
+ * @param string $label
354
+ * @return array
355
+ */
356
+ private function assertMediaGalleryData (int $ imageId , string $ file , string $ label ): array
357
+ {
301
358
$ targetProduct = $ this ->getTargetSimpleProduct ();
302
- $ this ->assertEquals ('/m/a/magento_image.jpg ' , $ targetProduct ->getData ('thumbnail ' ));
359
+ $ this ->assertEquals ($ file , $ targetProduct ->getData ('thumbnail ' ));
360
+ $ this ->assertEquals ('no_selection ' , $ targetProduct ->getData ('image ' ));
361
+ $ this ->assertEquals ('no_selection ' , $ targetProduct ->getData ('small_image ' ));
303
362
$ mediaGallery = $ targetProduct ->getData ('media_gallery ' );
304
363
$ this ->assertCount (1 , $ mediaGallery ['images ' ]);
305
364
$ updatedImage = array_shift ($ mediaGallery ['images ' ]);
306
- // Not default store view values were updated
365
+ $ this -> assertEquals ( $ imageId , $ updatedImage [ ' value_id ' ]);
307
366
$ this ->assertEquals ('Updated Image Text ' , $ updatedImage ['label ' ]);
308
- $ this ->assertEquals (' /m/a/magento_image.jpg ' , $ updatedImage ['file ' ]);
367
+ $ this ->assertEquals ($ file , $ updatedImage ['file ' ]);
309
368
$ this ->assertEquals (10 , $ updatedImage ['position ' ]);
310
369
$ this ->assertEquals (1 , $ updatedImage ['disabled ' ]);
311
- // Default store view values were not updated
312
- $ this ->assertEquals ('Image Alt Text ' , $ updatedImage ['label_default ' ]);
313
- $ this ->assertEquals (1 , $ updatedImage ['position_default ' ]);
314
- $ this ->assertEquals (0 , $ updatedImage ['disabled_default ' ]);
370
+ $ this ->assertEquals ($ label , $ updatedImage ['label_default ' ]);
371
+
372
+ return $ updatedImage ;
315
373
}
316
374
317
375
/**
@@ -564,9 +622,8 @@ public function testGet()
564
622
{
565
623
$ productSku = 'simple ' ;
566
624
567
- $ objectManager = ObjectManager::getInstance ();
568
625
/** @var ProductRepository $repository */
569
- $ repository = $ objectManager ->create (ProductRepository::class);
626
+ $ repository = $ this -> objectManager ->create (ProductRepository::class);
570
627
$ product = $ repository ->get ($ productSku );
571
628
$ image = current ($ product ->getMediaGallery ('images ' ));
572
629
$ imageId = $ image ['value_id ' ];
0 commit comments