Skip to content

Commit

Permalink
Merge branch '10797' of github.com:nmalevanec/magento2ce into 10797
Browse files Browse the repository at this point in the history
  • Loading branch information
ishakhsuvarov committed Dec 12, 2017
2 parents 321278b + 93df8bf commit a09a428
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 31 deletions.
88 changes: 64 additions & 24 deletions app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
* @return $this
* @throws InputException
* @throws StateException
* @throws LocalizedException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function processMediaGallery(ProductInterface $product, $mediaGalleryEntries)
Expand All @@ -497,16 +498,16 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
$entriesById = [];
if (!empty($existingMediaGallery)) {
foreach ($mediaGalleryEntries as $entry) {
if (isset($entry['value_id'])) {
$entriesById[$entry['value_id']] = $entry;
if (isset($entry['id'])) {
$entriesById[$entry['id']] = $entry;
} else {
$newEntries[] = $entry;
}
}
foreach ($existingMediaGallery as $key => &$existingEntry) {
if (isset($entriesById[$existingEntry['value_id']])) {
$updatedEntry = $entriesById[$existingEntry['value_id']];
if ($updatedEntry['file'] === null) {
if (array_key_exists('file', $updatedEntry) && $updatedEntry['file'] === null) {
unset($updatedEntry['file']);
}
$existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
Expand All @@ -515,6 +516,7 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
$existingEntry['removed'] = true;
}
}
unset($existingEntry);
$product->setData('media_gallery', ["images" => $existingMediaGallery]);
} else {
$newEntries = $mediaGalleryEntries;
Expand All @@ -529,26 +531,8 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
}
}
}
$this->processEntries($product, $newEntries, $entriesById);

foreach ($newEntries as $newEntry) {
if (!isset($newEntry['content'])) {
throw new InputException(__('The image content is not valid.'));
}
/** @var ImageContentInterface $contentDataObject */
$contentDataObject = $this->contentFactory->create()
->setName($newEntry['content']['data'][ImageContentInterface::NAME])
->setBase64EncodedData($newEntry['content']['data'][ImageContentInterface::BASE64_ENCODED_DATA])
->setType($newEntry['content']['data'][ImageContentInterface::TYPE]);
$newEntry['content'] = $contentDataObject;
$this->processNewMediaGalleryEntry($product, $newEntry);

$finalGallery = $product->getData('media_gallery');
$newEntryId = key(array_diff_key($product->getData('media_gallery')['images'], $entriesById));
$newEntry = array_replace_recursive($newEntry, $finalGallery['images'][$newEntryId]);
$entriesById[$newEntryId] = $newEntry;
$finalGallery['images'][$newEntryId] = $newEntry;
$product->setData('media_gallery', $finalGallery);
}
return $this;
}

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

$this->processLinks($product, $productLinks);
if (isset($productDataArray['media_gallery'])) {
$this->processMediaGallery($product, $productDataArray['media_gallery']['images']);
if (isset($productDataArray['media_gallery_entries'])) {
$this->processMediaGallery($product, $productDataArray['media_gallery_entries']);
}

if (!$product->getOptionsReadonly()) {
Expand Down Expand Up @@ -786,4 +770,60 @@ private function getCollectionProcessor()
}
return $this->collectionProcessor;
}

/**
* Convert extension attribute for product media gallery.
*
* @param array $newEntry
* @param array $extensionAttributes
* @return void
*/
private function processExtensionAttributes(array &$newEntry, array $extensionAttributes)
{
foreach ($extensionAttributes as $code => $value) {
if (is_array($value)) {
$this->processExtensionAttributes($newEntry, $value);
} else {
$newEntry[$code] = $value;
}
}
unset($newEntry['extension_attributes']);
}

/**
* Convert entries into product media gallery data and set to product.
*
* @param ProductInterface $product
* @param array $newEntries
* @param array $entriesById
* @throws InputException
* @throws LocalizedException
* @throws StateException
* @return void
*/
private function processEntries(ProductInterface $product, array $newEntries, array $entriesById)
{
foreach ($newEntries as $newEntry) {
if (!isset($newEntry['content'])) {
throw new InputException(__('The image content is not valid.'));
}
/** @var ImageContentInterface $contentDataObject */
$contentDataObject = $this->contentFactory->create()
->setName($newEntry['content'][ImageContentInterface::NAME])
->setBase64EncodedData($newEntry['content'][ImageContentInterface::BASE64_ENCODED_DATA])
->setType($newEntry['content'][ImageContentInterface::TYPE]);
$newEntry['content'] = $contentDataObject;
$this->processNewMediaGalleryEntry($product, $newEntry);

$finalGallery = $product->getData('media_gallery');
$newEntryId = key(array_diff_key($product->getData('media_gallery')['images'], $entriesById));
if (isset($newEntry['extension_attributes'])) {
$this->processExtensionAttributes($newEntry, $newEntry['extension_attributes']);
}
$newEntry = array_replace_recursive($newEntry, $finalGallery['images'][$newEntryId]);
$entriesById[$newEntryId] = $newEntry;
$finalGallery['images'][$newEntryId] = $newEntry;
$product->setData('media_gallery', $finalGallery);
}
}
}
23 changes: 18 additions & 5 deletions app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Magento\Catalog\Test\Unit\Model;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Framework\Api\Data\ImageContentInterface;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\DB\Adapter\ConnectionException;
Expand Down Expand Up @@ -1178,7 +1177,21 @@ public function testSaveExistingWithNewMediaGalleryEntries()

$this->setupProductMocksForSave();
//media gallery data
$this->productData['media_gallery'] = $newEntriesData;
$this->productData['media_gallery_entries'] = [
[
'id' => null,
'label' => "label_text",
'position' => 10,
'disabled' => false,
'types' => ['image', 'small_image'],
'content' => [
ImageContentInterface::NAME => 'filename',
ImageContentInterface::TYPE => 'image/jpeg',
ImageContentInterface::BASE64_ENCODED_DATA => 'encoded_content',
],
'media_type' => 'media_type',
]
];
$this->extensibleDataObjectConverterMock
->expects($this->once())
->method('toNestedArray')
Expand Down Expand Up @@ -1288,7 +1301,7 @@ public function testSaveExistingWithMediaGalleryEntries()
//update one entry, delete one entry
$newEntries = [
[
'value_id' => 5,
'id' => 5,
"label" => "new_label_text",
'file' => 'filename1',
'position' => 10,
Expand Down Expand Up @@ -1316,7 +1329,7 @@ public function testSaveExistingWithMediaGalleryEntries()
$expectedResult = [
[
'value_id' => 5,
'value_id' => 5,
'id' => 5,
"label" => "new_label_text",
'file' => 'filename1',
'position' => 10,
Expand All @@ -1332,7 +1345,7 @@ public function testSaveExistingWithMediaGalleryEntries()

$this->setupProductMocksForSave();
//media gallery data
$this->productData['media_gallery']['images'] = $newEntries;
$this->productData['media_gallery_entries'] = $newEntries;
$this->extensibleDataObjectConverterMock
->expects($this->once())
->method('toNestedArray')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ public function getListDataProvider()
/**
* @param string|int $customerGroupId
* @param int $qty
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/Catalog/_files/product_with_image.php
* @dataProvider deleteDataProvider
*/
public function testDelete($customerGroupId, $qty)
{
$productSku = 'simple';
$objectManager = \Magento\TestFramework\ObjectManager::getInstance();
$productBefore = $objectManager->get(ProductRepositoryInterface::class)->get($productSku, false, null, true);
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH
'resourcePath' => self::RESOURCE_PATH
. $productSku . "/group-prices/" . $customerGroupId . "/tiers/" . $qty,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
],
Expand All @@ -80,6 +82,10 @@ public function testDelete($customerGroupId, $qty)
];
$requestData = ['sku' => $productSku, 'customerGroupId' => $customerGroupId, 'qty' => $qty];
$this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
$productAfter = $objectManager->get(ProductRepositoryInterface::class)->get($productSku, false, null, true);
$this->assertSame($productBefore->getImage(), $productAfter->getImage());
$this->assertSame($productBefore->getSmallImage(), $productAfter->getSmallImage());
$this->assertSame($productBefore->getThumbnail(), $productAfter->getThumbnail());
}

public function deleteDataProvider()
Expand Down

0 comments on commit a09a428

Please sign in to comment.