Skip to content

Commit a09a428

Browse files
committed
Merge branch '10797' of github.com:nmalevanec/magento2ce into 10797
2 parents 321278b + 93df8bf commit a09a428

File tree

3 files changed

+90
-31
lines changed

3 files changed

+90
-31
lines changed

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

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
488488
* @return $this
489489
* @throws InputException
490490
* @throws StateException
491+
* @throws LocalizedException
491492
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
492493
*/
493494
protected function processMediaGallery(ProductInterface $product, $mediaGalleryEntries)
@@ -497,16 +498,16 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
497498
$entriesById = [];
498499
if (!empty($existingMediaGallery)) {
499500
foreach ($mediaGalleryEntries as $entry) {
500-
if (isset($entry['value_id'])) {
501-
$entriesById[$entry['value_id']] = $entry;
501+
if (isset($entry['id'])) {
502+
$entriesById[$entry['id']] = $entry;
502503
} else {
503504
$newEntries[] = $entry;
504505
}
505506
}
506507
foreach ($existingMediaGallery as $key => &$existingEntry) {
507508
if (isset($entriesById[$existingEntry['value_id']])) {
508509
$updatedEntry = $entriesById[$existingEntry['value_id']];
509-
if ($updatedEntry['file'] === null) {
510+
if (array_key_exists('file', $updatedEntry) && $updatedEntry['file'] === null) {
510511
unset($updatedEntry['file']);
511512
}
512513
$existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
@@ -515,6 +516,7 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
515516
$existingEntry['removed'] = true;
516517
}
517518
}
519+
unset($existingEntry);
518520
$product->setData('media_gallery', ["images" => $existingMediaGallery]);
519521
} else {
520522
$newEntries = $mediaGalleryEntries;
@@ -529,26 +531,8 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
529531
}
530532
}
531533
}
534+
$this->processEntries($product, $newEntries, $entriesById);
532535

533-
foreach ($newEntries as $newEntry) {
534-
if (!isset($newEntry['content'])) {
535-
throw new InputException(__('The image content is not valid.'));
536-
}
537-
/** @var ImageContentInterface $contentDataObject */
538-
$contentDataObject = $this->contentFactory->create()
539-
->setName($newEntry['content']['data'][ImageContentInterface::NAME])
540-
->setBase64EncodedData($newEntry['content']['data'][ImageContentInterface::BASE64_ENCODED_DATA])
541-
->setType($newEntry['content']['data'][ImageContentInterface::TYPE]);
542-
$newEntry['content'] = $contentDataObject;
543-
$this->processNewMediaGalleryEntry($product, $newEntry);
544-
545-
$finalGallery = $product->getData('media_gallery');
546-
$newEntryId = key(array_diff_key($product->getData('media_gallery')['images'], $entriesById));
547-
$newEntry = array_replace_recursive($newEntry, $finalGallery['images'][$newEntryId]);
548-
$entriesById[$newEntryId] = $newEntry;
549-
$finalGallery['images'][$newEntryId] = $newEntry;
550-
$product->setData('media_gallery', $finalGallery);
551-
}
552536
return $this;
553537
}
554538

@@ -587,8 +571,8 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
587571
$product = $this->initializeProductData($productDataArray, empty($existingProduct));
588572

589573
$this->processLinks($product, $productLinks);
590-
if (isset($productDataArray['media_gallery'])) {
591-
$this->processMediaGallery($product, $productDataArray['media_gallery']['images']);
574+
if (isset($productDataArray['media_gallery_entries'])) {
575+
$this->processMediaGallery($product, $productDataArray['media_gallery_entries']);
592576
}
593577

594578
if (!$product->getOptionsReadonly()) {
@@ -786,4 +770,60 @@ private function getCollectionProcessor()
786770
}
787771
return $this->collectionProcessor;
788772
}
773+
774+
/**
775+
* Convert extension attribute for product media gallery.
776+
*
777+
* @param array $newEntry
778+
* @param array $extensionAttributes
779+
* @return void
780+
*/
781+
private function processExtensionAttributes(array &$newEntry, array $extensionAttributes)
782+
{
783+
foreach ($extensionAttributes as $code => $value) {
784+
if (is_array($value)) {
785+
$this->processExtensionAttributes($newEntry, $value);
786+
} else {
787+
$newEntry[$code] = $value;
788+
}
789+
}
790+
unset($newEntry['extension_attributes']);
791+
}
792+
793+
/**
794+
* Convert entries into product media gallery data and set to product.
795+
*
796+
* @param ProductInterface $product
797+
* @param array $newEntries
798+
* @param array $entriesById
799+
* @throws InputException
800+
* @throws LocalizedException
801+
* @throws StateException
802+
* @return void
803+
*/
804+
private function processEntries(ProductInterface $product, array $newEntries, array $entriesById)
805+
{
806+
foreach ($newEntries as $newEntry) {
807+
if (!isset($newEntry['content'])) {
808+
throw new InputException(__('The image content is not valid.'));
809+
}
810+
/** @var ImageContentInterface $contentDataObject */
811+
$contentDataObject = $this->contentFactory->create()
812+
->setName($newEntry['content'][ImageContentInterface::NAME])
813+
->setBase64EncodedData($newEntry['content'][ImageContentInterface::BASE64_ENCODED_DATA])
814+
->setType($newEntry['content'][ImageContentInterface::TYPE]);
815+
$newEntry['content'] = $contentDataObject;
816+
$this->processNewMediaGalleryEntry($product, $newEntry);
817+
818+
$finalGallery = $product->getData('media_gallery');
819+
$newEntryId = key(array_diff_key($product->getData('media_gallery')['images'], $entriesById));
820+
if (isset($newEntry['extension_attributes'])) {
821+
$this->processExtensionAttributes($newEntry, $newEntry['extension_attributes']);
822+
}
823+
$newEntry = array_replace_recursive($newEntry, $finalGallery['images'][$newEntryId]);
824+
$entriesById[$newEntryId] = $newEntry;
825+
$finalGallery['images'][$newEntryId] = $newEntry;
826+
$product->setData('media_gallery', $finalGallery);
827+
}
828+
}
789829
}

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

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

1010
namespace Magento\Catalog\Test\Unit\Model;
1111

12-
use Magento\Catalog\Api\Data\ProductAttributeInterface;
1312
use Magento\Framework\Api\Data\ImageContentInterface;
1413
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
1514
use Magento\Framework\DB\Adapter\ConnectionException;
@@ -1178,7 +1177,21 @@ public function testSaveExistingWithNewMediaGalleryEntries()
11781177

11791178
$this->setupProductMocksForSave();
11801179
//media gallery data
1181-
$this->productData['media_gallery'] = $newEntriesData;
1180+
$this->productData['media_gallery_entries'] = [
1181+
[
1182+
'id' => null,
1183+
'label' => "label_text",
1184+
'position' => 10,
1185+
'disabled' => false,
1186+
'types' => ['image', 'small_image'],
1187+
'content' => [
1188+
ImageContentInterface::NAME => 'filename',
1189+
ImageContentInterface::TYPE => 'image/jpeg',
1190+
ImageContentInterface::BASE64_ENCODED_DATA => 'encoded_content',
1191+
],
1192+
'media_type' => 'media_type',
1193+
]
1194+
];
11821195
$this->extensibleDataObjectConverterMock
11831196
->expects($this->once())
11841197
->method('toNestedArray')
@@ -1288,7 +1301,7 @@ public function testSaveExistingWithMediaGalleryEntries()
12881301
//update one entry, delete one entry
12891302
$newEntries = [
12901303
[
1291-
'value_id' => 5,
1304+
'id' => 5,
12921305
"label" => "new_label_text",
12931306
'file' => 'filename1',
12941307
'position' => 10,
@@ -1316,7 +1329,7 @@ public function testSaveExistingWithMediaGalleryEntries()
13161329
$expectedResult = [
13171330
[
13181331
'value_id' => 5,
1319-
'value_id' => 5,
1332+
'id' => 5,
13201333
"label" => "new_label_text",
13211334
'file' => 'filename1',
13221335
'position' => 10,
@@ -1332,7 +1345,7 @@ public function testSaveExistingWithMediaGalleryEntries()
13321345

13331346
$this->setupProductMocksForSave();
13341347
//media gallery data
1335-
$this->productData['media_gallery']['images'] = $newEntries;
1348+
$this->productData['media_gallery_entries'] = $newEntries;
13361349
$this->extensibleDataObjectConverterMock
13371350
->expects($this->once())
13381351
->method('toNestedArray')

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ public function getListDataProvider()
6060
/**
6161
* @param string|int $customerGroupId
6262
* @param int $qty
63-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
63+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_image.php
6464
* @dataProvider deleteDataProvider
6565
*/
6666
public function testDelete($customerGroupId, $qty)
6767
{
6868
$productSku = 'simple';
69+
$objectManager = \Magento\TestFramework\ObjectManager::getInstance();
70+
$productBefore = $objectManager->get(ProductRepositoryInterface::class)->get($productSku, false, null, true);
6971
$serviceInfo = [
7072
'rest' => [
71-
'resourcePath' => self::RESOURCE_PATH
73+
'resourcePath' => self::RESOURCE_PATH
7274
. $productSku . "/group-prices/" . $customerGroupId . "/tiers/" . $qty,
7375
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
7476
],
@@ -80,6 +82,10 @@ public function testDelete($customerGroupId, $qty)
8082
];
8183
$requestData = ['sku' => $productSku, 'customerGroupId' => $customerGroupId, 'qty' => $qty];
8284
$this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
85+
$productAfter = $objectManager->get(ProductRepositoryInterface::class)->get($productSku, false, null, true);
86+
$this->assertSame($productBefore->getImage(), $productAfter->getImage());
87+
$this->assertSame($productBefore->getSmallImage(), $productAfter->getSmallImage());
88+
$this->assertSame($productBefore->getThumbnail(), $productAfter->getThumbnail());
8389
}
8490

8591
public function deleteDataProvider()

0 commit comments

Comments
 (0)