Ejemplo n.º 1
0
 public function testSetMediaAttribute()
 {
     /** @var $product \Magento\Catalog\Model\Product */
     $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product');
     $this->_model->setMediaAttribute($product, 'image', 'test1');
     $this->assertEquals('test1', $product->getData('image'));
     $this->_model->setMediaAttribute($product, ['non-exist-image-attribute', 'small_image'], 'test');
     $this->assertNull($product->getData('non-exist-image-attribute'));
     $this->assertEquals('test', $product->getData('small_image'));
 }
 public function testSetMediaAttribute()
 {
     /** @var $product \Magento\Catalog\Model\Product */
     $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product');
     $product->setMediaAttributes(['test_media1', 'test_media2', 'test_media3']);
     $this->_model->setMediaAttribute($product, 'test_media1', 'test1');
     $this->assertEquals('test1', $product->getData('test_media1'));
     $this->_model->setMediaAttribute($product, ['test_media2', 'test_media3'], 'test');
     $this->assertEquals('test', $product->getData('test_media2'));
     $this->assertEquals('test', $product->getData('test_media3'));
 }
 /**
  * @param ProductInterface $product
  * @param array $mediaGalleryEntries
  * @return $this
  * @throws InputException
  * @throws StateException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function processMediaGallery(ProductInterface $product, $mediaGalleryEntries)
 {
     $existingMediaGallery = $product->getMediaGallery('images');
     $newEntries = [];
     if (!empty($existingMediaGallery)) {
         $entriesById = [];
         foreach ($mediaGalleryEntries as $entry) {
             if (isset($entry['id'])) {
                 $entry['value_id'] = $entry['id'];
                 $entriesById[$entry['value_id']] = $entry;
             } else {
                 $newEntries[] = $entry;
             }
         }
         foreach ($existingMediaGallery as $key => &$existingEntry) {
             if (isset($entriesById[$existingEntry['value_id']])) {
                 $updatedEntry = $entriesById[$existingEntry['value_id']];
                 $existingMediaGallery[$key] = array_merge($existingEntry, $updatedEntry);
             } else {
                 //set the removed flag
                 $existingEntry['removed'] = true;
             }
         }
         $product->setData('media_gallery', ["images" => $existingMediaGallery]);
     } else {
         $newEntries = $mediaGalleryEntries;
     }
     $this->mediaGalleryProcessor->clearMediaAttribute($product, array_keys($product->getMediaAttributes()));
     $images = $product->getMediaGallery('images');
     if ($images) {
         foreach ($images as $image) {
             if (!isset($image['removed']) && !empty($image['types'])) {
                 $this->mediaGalleryProcessor->setMediaAttribute($product, $image['types'], $image['file']);
             }
         }
     }
     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);
     }
     return $this;
 }