Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductAttributeMediaGalleryManagementInterfaceTest.php
Go to the documentation of this file.
1 <?php
8 namespace Magento\Catalog\Api;
9 
12 
17 {
23  protected $createServiceInfo;
24 
30  protected $updateServiceInfo;
31 
37  protected $deleteServiceInfo;
38 
42  protected $testImagePath;
43 
44  protected function setUp()
45  {
46  $this->createServiceInfo = [
47  'rest' => [
48  'resourcePath' => '/V1/products/simple/media',
50  ],
51  'soap' => [
52  'service' => 'catalogProductAttributeMediaGalleryManagementV1',
53  'serviceVersion' => 'V1',
54  'operation' => 'catalogProductAttributeMediaGalleryManagementV1Create',
55  ],
56  ];
57 
58  $this->updateServiceInfo = [
59  'rest' => [
60  'resourcePath' => '/V1/products/simple/media',
62  ],
63  'soap' => [
64  'service' => 'catalogProductAttributeMediaGalleryManagementV1',
65  'serviceVersion' => 'V1',
66  'operation' => 'catalogProductAttributeMediaGalleryManagementV1Update',
67  ],
68  ];
69  $this->deleteServiceInfo = [
70  'rest' => [
72  ],
73  'soap' => [
74  'service' => 'catalogProductAttributeMediaGalleryManagementV1',
75  'serviceVersion' => 'V1',
76  'operation' => 'catalogProductAttributeMediaGalleryManagementV1Remove',
77  ],
78  ];
79  $this->testImagePath = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test_image.jpg';
80  }
81 
87  protected function getTargetSimpleProduct()
88  {
90  return $objectManager->get(\Magento\Catalog\Model\ProductFactory::class)->create()->load(1);
91  }
92 
100  protected function getTargetGalleryEntryId()
101  {
102  $mediaGallery = $this->getTargetSimpleProduct()->getData('media_gallery');
103  $image = array_shift($mediaGallery['images']);
104  return (int)$image['value_id'];
105  }
106 
110  public function testCreate()
111  {
112  $requestData = [
113  'id' => null,
114  'media_type' => \Magento\Catalog\Model\Product\Attribute\Backend\Media\ImageEntryConverter::MEDIA_TYPE_CODE,
115  'label' => 'Image Text',
116  'position' => 1,
117  'types' => ['image'],
118  'disabled' => false,
119  'content' => [
120  ImageContentInterface::BASE64_ENCODED_DATA => base64_encode(file_get_contents($this->testImagePath)),
121  ImageContentInterface::TYPE => 'image/jpeg',
122  ImageContentInterface::NAME => 'test_image.jpg'
123  ]
124  ];
125 
126  $actualResult = $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]);
127  $targetProduct = $this->getTargetSimpleProduct();
128  $mediaGallery = $targetProduct->getData('media_gallery');
129 
130  $this->assertCount(1, $mediaGallery['images']);
131  $updatedImage = array_shift($mediaGallery['images']);
132  $this->assertEquals($actualResult, $updatedImage['value_id']);
133  $this->assertEquals('Image Text', $updatedImage['label']);
134  $this->assertEquals(1, $updatedImage['position']);
135  $this->assertEquals(0, $updatedImage['disabled']);
136  $this->assertStringStartsWith('/t/e/test_image', $updatedImage['file']);
137  $this->assertEquals($updatedImage['file'], $targetProduct->getData('image'));
138  }
139 
144  {
145  $requestData = [
146  'id' => null,
147  'media_type' => \Magento\Catalog\Model\Product\Attribute\Backend\Media\ImageEntryConverter::MEDIA_TYPE_CODE,
148  'label' => 'Image Text',
149  'position' => 1,
150  'types' => ['image'],
151  'disabled' => false,
152  'content' => [
153  ImageContentInterface::BASE64_ENCODED_DATA => base64_encode(file_get_contents($this->testImagePath)),
154  ImageContentInterface::TYPE => 'image/jpeg',
155  ImageContentInterface::NAME => 'test_image'
156  ]
157  ];
158 
159  $actualResult = $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]);
160  $targetProduct = $this->getTargetSimpleProduct();
161  $mediaGallery = $targetProduct->getData('media_gallery');
162 
163  $this->assertCount(1, $mediaGallery['images']);
164  $updatedImage = array_shift($mediaGallery['images']);
165  $this->assertEquals($actualResult, $updatedImage['value_id']);
166  $this->assertEquals('Image Text', $updatedImage['label']);
167  $this->assertEquals(1, $updatedImage['position']);
168  $this->assertEquals(0, $updatedImage['disabled']);
169  $this->assertStringStartsWith('/t/e/test_image', $updatedImage['file']);
170  $this->assertEquals($updatedImage['file'], $targetProduct->getData('image'));
171  }
172 
177  {
178  $requestData = [
179  'id' => null,
180  'media_type' => \Magento\Catalog\Model\Product\Attribute\Backend\Media\ImageEntryConverter::MEDIA_TYPE_CODE,
181  'label' => 'Image Text',
182  'position' => 1,
183  'types' => ['image'],
184  'disabled' => false,
185  'content' => [
186  'base64_encoded_data' => base64_encode(file_get_contents($this->testImagePath)),
187  'type' => 'image/jpeg',
188  'name' => 'test_image.jpg',
189  ]
190  ];
191 
192  $actualResult = $this->_webApiCall(
193  $this->createServiceInfo,
194  [
195  'sku' => 'simple',
196  'entry' => $requestData,
197  'storeId' => 1,
198  ]
199  );
200  $targetProduct = $this->getTargetSimpleProduct();
201  $mediaGallery = $targetProduct->getData('media_gallery');
202  $this->assertCount(1, $mediaGallery['images']);
203  $updatedImage = array_shift($mediaGallery['images']);
204  // Values for not default store view were provided
205  $this->assertEquals('Image Text', $updatedImage['label']);
206  $this->assertEquals($actualResult, $updatedImage['value_id']);
207  $this->assertEquals(1, $updatedImage['position']);
208  $this->assertEquals(0, $updatedImage['disabled']);
209  $this->assertStringStartsWith('/t/e/test_image', $updatedImage['file']);
210  $this->assertEquals($updatedImage['file'], $targetProduct->getData('image'));
211  // No values for default store view were provided
212  $this->assertNull($updatedImage['label_default']);
213  $this->assertNull($updatedImage['position_default']);
214  $this->assertNull($updatedImage['disabled_default']);
215  }
216 
220  public function testUpdate()
221  {
222  $requestData = [
223  'sku' => 'simple',
224  'entry' => [
225  'id' => $this->getTargetGalleryEntryId(),
226  'label' => 'Updated Image Text',
227  'position' => 10,
228  'types' => ['thumbnail'],
229  'disabled' => true,
230  'media_type' => 'image',
231  ],
232  ];
233 
234  $this->updateServiceInfo['rest']['resourcePath'] = $this->updateServiceInfo['rest']['resourcePath']
235  . '/' . $this->getTargetGalleryEntryId();
236 
237  $this->assertTrue($this->_webApiCall($this->updateServiceInfo, $requestData, null, 'all'));
238 
239  $targetProduct = $this->getTargetSimpleProduct();
240  $this->assertEquals('/m/a/magento_image.jpg', $targetProduct->getData('thumbnail'));
241  $this->assertEquals('no_selection', $targetProduct->getData('image'));
242  $this->assertEquals('no_selection', $targetProduct->getData('small_image'));
243  $mediaGallery = $targetProduct->getData('media_gallery');
244  $this->assertCount(1, $mediaGallery['images']);
245  $updatedImage = array_shift($mediaGallery['images']);
246  $this->assertEquals('Updated Image Text', $updatedImage['label']);
247  $this->assertEquals('/m/a/magento_image.jpg', $updatedImage['file']);
248  $this->assertEquals(10, $updatedImage['position']);
249  $this->assertEquals(1, $updatedImage['disabled']);
250  $this->assertEquals('Updated Image Text', $updatedImage['label_default']);
251  $this->assertEquals(10, $updatedImage['position_default']);
252  $this->assertEquals(1, $updatedImage['disabled_default']);
253  }
254 
259  {
260  $requestData = [
261  'sku' => 'simple',
262  'entry' => [
263  'id' => $this->getTargetGalleryEntryId(),
264  'label' => 'Updated Image Text',
265  'position' => 10,
266  'types' => ['thumbnail'],
267  'disabled' => true,
268  'media_type' => 'image',
269  ]
270  ];
271 
272  $this->updateServiceInfo['rest']['resourcePath'] = $this->updateServiceInfo['rest']['resourcePath']
273  . '/' . $this->getTargetGalleryEntryId();
274 
275  $this->assertTrue($this->_webApiCall($this->updateServiceInfo, $requestData, null, 'default'));
276 
277  $targetProduct = $this->getTargetSimpleProduct();
278  $this->assertEquals('/m/a/magento_image.jpg', $targetProduct->getData('thumbnail'));
279  $mediaGallery = $targetProduct->getData('media_gallery');
280  $this->assertCount(1, $mediaGallery['images']);
281  $updatedImage = array_shift($mediaGallery['images']);
282  // Not default store view values were updated
283  $this->assertEquals('Updated Image Text', $updatedImage['label']);
284  $this->assertEquals('/m/a/magento_image.jpg', $updatedImage['file']);
285  $this->assertEquals(10, $updatedImage['position']);
286  $this->assertEquals(1, $updatedImage['disabled']);
287  // Default store view values were not updated
288  $this->assertEquals('Image Alt Text', $updatedImage['label_default']);
289  $this->assertEquals(1, $updatedImage['position_default']);
290  $this->assertEquals(0, $updatedImage['disabled_default']);
291  }
292 
296  public function testDelete()
297  {
298  $entryId = $this->getTargetGalleryEntryId();
299  $this->deleteServiceInfo['rest']['resourcePath'] = "/V1/products/simple/media/{$entryId}";
300  $requestData = [
301  'sku' => 'simple',
302  'entryId' => $this->getTargetGalleryEntryId(),
303  ];
304 
305  $this->assertTrue($this->_webApiCall($this->deleteServiceInfo, $requestData));
306  $targetProduct = $this->getTargetSimpleProduct();
307  $mediaGallery = $targetProduct->getData('media_gallery');
308  $this->assertCount(0, $mediaGallery['images']);
309  }
310 
317  {
318  $encodedContent = 'not_a_base64_encoded_content';
319  $requestData = [
320  'id' => null,
321  'media_type' => 'image',
322  'label' => 'Image Text',
323  'position' => 1,
324  'types' => ['image'],
325  'disabled' => false,
326  'content' => [
327  'base64_encoded_data' => $encodedContent,
328  'type' => 'image/jpeg',
329  'name' => 'test_image.jpg',
330  ]
331  ];
332 
333  $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]);
334  }
335 
342  {
343  $encodedContent = base64_encode('not_an_image');
344  $requestData = [
345  'id' => null,
346  'media_type' => 'image',
347  'label' => 'Image Text',
348  'position' => 1,
349  'types' => ['image'],
350  'disabled' => false,
351  'content' => [
352  'base64_encoded_data' => $encodedContent,
353  'type' => 'image/jpeg',
354  'name' => 'test_image.jpg',
355  ]
356  ];
357 
358  $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]);
359  }
360 
367  {
368  $encodedContent = base64_encode(file_get_contents($this->testImagePath));
369  $requestData = [
370  'id' => null,
371  'media_type' => 'image',
372  'label' => 'Image Text',
373  'position' => 1,
374  'types' => ['image'],
375  'disabled' => false,
376  'content' => [
377  'base64_encoded_data' => $encodedContent,
378  'type' => 'wrong_mime_type',
379  'name' => 'test_image.jpg',
380  ]
381  ];
382 
383  $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]);
384  }
385 
391  {
392  $this->createServiceInfo['rest']['resourcePath'] = '/V1/products/wrong_product_sku/media';
393 
394  $requestData = [
395  'id' => null,
396  'media_type' => 'image',
397  'label' => 'Image Text',
398  'position' => 1,
399  'types' => ['image'],
400  'disabled' => false,
401  'content' => [
402  'base64_encoded_data' => base64_encode(file_get_contents($this->testImagePath)),
403  'type' => 'image/jpeg',
404  'name' => 'test_image.jpg',
405  ]
406  ];
407 
408  $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]);
409  }
410 
417  {
418  $requestData = [
419  'id' => null,
420  'media_type' => 'image',
421  'label' => 'Image Text',
422  'position' => 1,
423  'types' => ['image'],
424  'disabled' => false,
425  'content' => [
426  'base64_encoded_data' => base64_encode(file_get_contents($this->testImagePath)),
427  'type' => 'image/jpeg',
428  'name' => 'test/\\{}|:"<>', // Cannot contain \ / : * ? " < > |
429  ]
430  ];
431 
432  $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]);
433  }
434 
440  {
441  $this->updateServiceInfo['rest']['resourcePath'] = '/V1/products/wrong_product_sku/media'
442  . '/' . 'wrong-sku';
443  $requestData = [
444  'sku' => 'wrong_product_sku',
445  'entry' => [
446  'id' => 9999,
447  'media_type' => 'image',
448  'label' => 'Updated Image Text',
449  'position' => 1,
450  'types' => ['thumbnail'],
451  'disabled' => true,
452  ],
453  ];
454 
455  $this->_webApiCall($this->updateServiceInfo, $requestData, null, 'all');
456  }
457 
464  {
465  $requestData = [
466  'sku' => 'simple',
467  'entry' => [
468  'id' => 9999,
469  'media_type' => 'image',
470  'label' => 'Updated Image Text',
471  'position' => 1,
472  'types' => ['thumbnail'],
473  'disabled' => true,
474  ],
475  ];
476 
477  $this->updateServiceInfo['rest']['resourcePath'] = $this->updateServiceInfo['rest']['resourcePath']
478  . '/' . $this->getTargetGalleryEntryId();
479 
480  $this->_webApiCall($this->updateServiceInfo, $requestData, null, 'all');
481  }
482 
488  {
489  $this->deleteServiceInfo['rest']['resourcePath'] = '/V1/products/wrong_product_sku/media/9999';
490  $requestData = [
491  'sku' => 'wrong_product_sku',
492  'entryId' => 9999,
493  ];
494 
495  $this->_webApiCall($this->deleteServiceInfo, $requestData);
496  }
497 
504  {
505  $this->deleteServiceInfo['rest']['resourcePath'] = '/V1/products/simple/media/9999';
506  $requestData = [
507  'sku' => 'simple',
508  'entryId' => 9999,
509  ];
510 
511  $this->_webApiCall($this->deleteServiceInfo, $requestData);
512  }
513 
517  public function testGet()
518  {
519  $productSku = 'simple';
520 
523  $repository = $objectManager->create(\Magento\Catalog\Model\ProductRepository::class);
524  $product = $repository->get($productSku);
525  $image = current($product->getMediaGallery('images'));
526  $imageId = $image['value_id'];
527 
528  $expected = [
529  'label' => $image['label'],
530  'media_type' => $image['media_type'],
531  'position' => $image['position'],
532  'disabled' => (bool)$image['disabled'],
533  'file' => $image['file'],
534  'types' => ['image', 'small_image', 'thumbnail'],
535  ];
536 
537  $serviceInfo = [
538  'rest' => [
539  'resourcePath' => '/V1/products/' . $productSku . '/media/' . $imageId,
541  ],
542  'soap' => [
543  'service' => 'catalogProductAttributeMediaGalleryManagementV1',
544  'serviceVersion' => 'V1',
545  'operation' => 'catalogProductAttributeMediaGalleryManagementV1Get',
546  ],
547  ];
548  $requestData = [
549  'sku' => $productSku,
550  'entryId' => $imageId,
551  ];
552  $data = $this->_webApiCall($serviceInfo, $requestData);
553  $actual = (array)$data;
554  $this->assertEquals($expected['label'], $actual['label']);
555  $this->assertEquals($expected['position'], $actual['position']);
556  $this->assertEquals($expected['file'], $actual['file']);
557  $this->assertEquals($expected['types'], $actual['types']);
558  $this->assertEquals($expected['media_type'], $actual['media_type']);
559  $this->assertEquals($expected['disabled'], (bool)$actual['disabled']);
560  }
561 
565  public function testGetList()
566  {
567  $productSku = 'simple'; //from fixture
568  $serviceInfo = [
569  'rest' => [
570  'resourcePath' => '/V1/products/' . urlencode($productSku) . '/media',
572  ],
573  'soap' => [
574  'service' => 'catalogProductAttributeMediaGalleryManagementV1',
575  'serviceVersion' => 'V1',
576  'operation' => 'catalogProductAttributeMediaGalleryManagementV1GetList',
577  ],
578  ];
579 
580  $requestData = [
581  'sku' => $productSku,
582  ];
583  $imageList = $this->_webApiCall($serviceInfo, $requestData);
584 
585  $image = reset($imageList);
586  $this->assertEquals('/m/a/magento_image.jpg', $image['file']);
587  $this->assertNotEmpty($image['types']);
588  $imageTypes = $image['types'];
589  $this->assertContains('image', $imageTypes);
590  $this->assertContains('small_image', $imageTypes);
591  $this->assertContains('thumbnail', $imageTypes);
592  }
593 
594  public function testGetListForAbsentSku()
595  {
596  $productSku = 'absent_sku_' . time();
597  $serviceInfo = [
598  'rest' => [
599  'resourcePath' => '/V1/products/' . urlencode($productSku) . '/media',
601  ],
602  'soap' => [
603  'service' => 'catalogProductAttributeMediaGalleryManagementV1',
604  'serviceVersion' => 'V1',
605  'operation' => 'catalogProductAttributeMediaGalleryManagementV1GetList',
606  ],
607  ];
608 
609  $requestData = [
610  'sku' => $productSku,
611  ];
612  if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
613  $this->expectException('SoapFault');
614  $this->expectExceptionMessage(
615  "The product that was requested doesn't exist. Verify the product and try again."
616  );
617  } else {
618  $this->expectException('Exception');
619  $this->expectExceptionCode(404);
620  }
621  $this->_webApiCall($serviceInfo, $requestData);
622  }
623 
627  public function testAddProductVideo()
628  {
629  $videoContent = [
630  'media_type' => 'external-video',
631  'video_provider' => 'vimeo',
632  'video_url' => 'https://vimeo.com/testUrl',
633  'video_title' => 'Vimeo Test Title',
634  'video_description' => 'test description',
635  'video_metadata' => 'video meta data'
636  ];
637 
638  $requestData = [
639  'id' => null,
640  'media_type' => 'external-video',
641  'label' => 'Image Text',
642  'position' => 1,
643  'types' => null,
644  'disabled' => false,
645  'content' => [
646  ImageContentInterface::BASE64_ENCODED_DATA => base64_encode(file_get_contents($this->testImagePath)),
647  ImageContentInterface::TYPE => 'image/jpeg',
648  ImageContentInterface::NAME => 'test_image.jpg'
649  ],
650  'extension_attributes' => [
651  'video_content' => $videoContent
652  ]
653  ];
654 
655  $actualResult = $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]);
656  $targetProduct = $this->getTargetSimpleProduct();
657  $mediaGallery = $targetProduct->getData('media_gallery');
658 
659  $this->assertCount(1, $mediaGallery['images']);
660  $updatedImage = array_shift($mediaGallery['images']);
661  $this->assertEquals($actualResult, $updatedImage['value_id']);
662  $this->assertEquals('Image Text', $updatedImage['label']);
663  $this->assertEquals(1, $updatedImage['position']);
664  $this->assertEquals(0, $updatedImage['disabled']);
665  $this->assertStringStartsWith('/t/e/test_image', $updatedImage['file']);
666  $this->assertEquals($videoContent, array_intersect($updatedImage, $videoContent));
667  }
668 }
$objectManager
Definition: bootstrap.php:17
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60