Skip to content

Commit d3afaa9

Browse files
committed
MAGETWO-61018: [Github] Unable to add video to product via REST api #7153
2 parents ff9bca9 + ddb16a5 commit d3afaa9

File tree

3 files changed

+95
-29
lines changed

3 files changed

+95
-29
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ protected function getCacheKey($data)
289289
*/
290290
protected function initializeProductData(array $productData, $createNew)
291291
{
292+
unset($productData['media_gallery']);
292293
if ($createNew) {
293294
$product = $this->productFactory->create();
294295
if ($this->storeManager->hasSingleStore()) {
@@ -437,8 +438,15 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
437438
}
438439

439440
/**
440-
* @param ProductInterface $product
441-
* @param array $mediaGalleryEntries
441+
* Process Media gallery data before save product.
442+
*
443+
* Compare Media Gallery Entries Data with existing Media Gallery
444+
* * If Media entry has not value_id set it as new
445+
* * If Existing entry 'value_id' absent in Media Gallery set 'removed' flag
446+
* * Merge Existing and new media gallery
447+
*
448+
* @param ProductInterface $product contains only existing media gallery items
449+
* @param array $mediaGalleryEntries array which contains all media gallery items
442450
* @return $this
443451
* @throws InputException
444452
* @throws StateException
@@ -448,11 +456,10 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
448456
{
449457
$existingMediaGallery = $product->getMediaGallery('images');
450458
$newEntries = [];
459+
$entriesById = [];
451460
if (!empty($existingMediaGallery)) {
452-
$entriesById = [];
453461
foreach ($mediaGalleryEntries as $entry) {
454-
if (isset($entry['id'])) {
455-
$entry['value_id'] = $entry['id'];
462+
if (isset($entry['value_id'])) {
456463
$entriesById[$entry['value_id']] = $entry;
457464
} else {
458465
$newEntries[] = $entry;
@@ -461,6 +468,9 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
461468
foreach ($existingMediaGallery as $key => &$existingEntry) {
462469
if (isset($entriesById[$existingEntry['value_id']])) {
463470
$updatedEntry = $entriesById[$existingEntry['value_id']];
471+
if ($updatedEntry['file'] === null) {
472+
unset($updatedEntry['file']);
473+
}
464474
$existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
465475
} else {
466476
//set the removed flag
@@ -488,11 +498,18 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
488498
}
489499
/** @var ImageContentInterface $contentDataObject */
490500
$contentDataObject = $this->contentFactory->create()
491-
->setName($newEntry['content'][ImageContentInterface::NAME])
492-
->setBase64EncodedData($newEntry['content'][ImageContentInterface::BASE64_ENCODED_DATA])
493-
->setType($newEntry['content'][ImageContentInterface::TYPE]);
501+
->setName($newEntry['content']['data'][ImageContentInterface::NAME])
502+
->setBase64EncodedData($newEntry['content']['data'][ImageContentInterface::BASE64_ENCODED_DATA])
503+
->setType($newEntry['content']['data'][ImageContentInterface::TYPE]);
494504
$newEntry['content'] = $contentDataObject;
495505
$this->processNewMediaGalleryEntry($product, $newEntry);
506+
507+
$finalGallery = $product->getData('media_gallery');
508+
$newEntryId = key(array_diff_key($product->getData('media_gallery')['images'], $entriesById));
509+
$newEntry = array_replace_recursive($newEntry, $finalGallery['images'][$newEntryId]);
510+
$entriesById[$newEntryId] = $newEntry;
511+
$finalGallery['images'][$newEntryId] = $newEntry;
512+
$product->setData('media_gallery', $finalGallery);
496513
}
497514
return $this;
498515
}
@@ -520,8 +537,6 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
520537
$productDataArray = $this->extensibleDataObjectConverter
521538
->toNestedArray($product, [], \Magento\Catalog\Api\Data\ProductInterface::class);
522539
$productDataArray = array_replace($productDataArray, $product->getData());
523-
unset($productDataArray['media_gallery']);
524-
525540
$ignoreLinksFlag = $product->getData('ignore_links_flag');
526541
$productLinks = null;
527542
if (!$ignoreLinksFlag && $ignoreLinksFlag !== null) {
@@ -531,8 +546,8 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
531546
$product = $this->initializeProductData($productDataArray, empty($existingProduct));
532547

533548
$this->processLinks($product, $productLinks);
534-
if (isset($productDataArray['media_gallery_entries'])) {
535-
$this->processMediaGallery($product, $productDataArray['media_gallery_entries']);
549+
if (isset($productDataArray['media_gallery'])) {
550+
$this->processMediaGallery($product, $productDataArray['media_gallery']['images']);
536551
}
537552

538553
if (!$product->getOptionsReadonly()) {

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,29 +1154,35 @@ public function testSaveExistingWithNewMediaGalleryEntries()
11541154
{
11551155
$this->storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']);
11561156
$newEntriesData = [
1157-
[
1158-
"label" => "label_text",
1159-
'position' => 10,
1160-
'disabled' => false,
1161-
'types' => ['image', 'small_image'],
1162-
'content' => [
1163-
ImageContentInterface::NAME => 'filename',
1164-
ImageContentInterface::TYPE => 'image/jpeg',
1165-
ImageContentInterface::BASE64_ENCODED_DATA => 'encoded_content',
1166-
],
1167-
'media_type' => 'media_type',
1168-
],
1157+
'images' =>
1158+
[
1159+
[
1160+
'value_id' => null,
1161+
'label' => "label_text",
1162+
'position' => 10,
1163+
'disabled' => false,
1164+
'types' => ['image', 'small_image'],
1165+
'content' => [
1166+
'data' => [
1167+
ImageContentInterface::NAME => 'filename',
1168+
ImageContentInterface::TYPE => 'image/jpeg',
1169+
ImageContentInterface::BASE64_ENCODED_DATA => 'encoded_content',
1170+
],
1171+
],
1172+
'media_type' => 'media_type',
1173+
]
1174+
]
11691175
];
11701176

11711177
$this->setupProductMocksForSave();
11721178
//media gallery data
1173-
$this->productData['media_gallery_entries'] = $newEntriesData;
1179+
$this->productData['media_gallery'] = $newEntriesData;
11741180
$this->extensibleDataObjectConverterMock
11751181
->expects($this->once())
11761182
->method('toNestedArray')
11771183
->will($this->returnValue($this->productData));
11781184

1179-
$this->initializedProductMock->setData('media_gallery', []);
1185+
$this->initializedProductMock->setData('media_gallery', $newEntriesData);
11801186
$this->initializedProductMock->expects($this->any())
11811187
->method('getMediaAttributes')
11821188
->willReturn(["image" => "imageAttribute", "small_image" => "small_image_attribute"]);
@@ -1277,7 +1283,7 @@ public function testSaveExistingWithMediaGalleryEntries()
12771283
//update one entry, delete one entry
12781284
$newEntries = [
12791285
[
1280-
'id' => 5,
1286+
'value_id' => 5,
12811287
"label" => "new_label_text",
12821288
'file' => 'filename1',
12831289
'position' => 10,
@@ -1304,7 +1310,7 @@ public function testSaveExistingWithMediaGalleryEntries()
13041310

13051311
$expectedResult = [
13061312
[
1307-
'id' => 5,
1313+
'value_id' => 5,
13081314
'value_id' => 5,
13091315
"label" => "new_label_text",
13101316
'file' => 'filename1',
@@ -1321,7 +1327,7 @@ public function testSaveExistingWithMediaGalleryEntries()
13211327

13221328
$this->setupProductMocksForSave();
13231329
//media gallery data
1324-
$this->productData['media_gallery_entries'] = $newEntries;
1330+
$this->productData['media_gallery']['images'] = $newEntries;
13251331
$this->extensibleDataObjectConverterMock
13261332
->expects($this->once())
13271333
->method('toNestedArray')

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeMediaGalleryManagementInterfaceTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,4 +615,49 @@ public function testGetListForAbsentSku()
615615
}
616616
$this->_webApiCall($serviceInfo, $requestData);
617617
}
618+
619+
/**
620+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
621+
*/
622+
public function testAddProductVideo()
623+
{
624+
$videoContent = [
625+
'media_type' => 'external-video',
626+
'video_provider' => 'vimeo',
627+
'video_url' => 'https://vimeo.com/testUrl',
628+
'video_title' => 'Vimeo Test Title',
629+
'video_description' => 'test description',
630+
'video_metadata' => 'video meta data'
631+
];
632+
633+
$requestData = [
634+
'id' => null,
635+
'media_type' => 'external-video',
636+
'label' => 'Image Text',
637+
'position' => 1,
638+
'types' => null,
639+
'disabled' => false,
640+
'content' => [
641+
ImageContentInterface::BASE64_ENCODED_DATA => base64_encode(file_get_contents($this->testImagePath)),
642+
ImageContentInterface::TYPE => 'image/jpeg',
643+
ImageContentInterface::NAME => 'test_image.jpg'
644+
],
645+
'extension_attributes' => [
646+
'video_content' => $videoContent
647+
]
648+
];
649+
650+
$actualResult = $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]);
651+
$targetProduct = $this->getTargetSimpleProduct();
652+
$mediaGallery = $targetProduct->getData('media_gallery');
653+
654+
$this->assertCount(1, $mediaGallery['images']);
655+
$updatedImage = array_shift($mediaGallery['images']);
656+
$this->assertEquals($actualResult, $updatedImage['value_id']);
657+
$this->assertEquals('Image Text', $updatedImage['label']);
658+
$this->assertEquals(1, $updatedImage['position']);
659+
$this->assertEquals(0, $updatedImage['disabled']);
660+
$this->assertStringStartsWith('/t/e/test_image', $updatedImage['file']);
661+
$this->assertEquals($videoContent, array_intersect($updatedImage, $videoContent));
662+
}
618663
}

0 commit comments

Comments
 (0)