public function testUpdateImage() { $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->setData('media_gallery', ['images' => ['image' => ['file' => 'magento_image.jpg']]]); $this->_model->updateImage($product, 'magento_image.jpg', ['label' => 'test label']); $this->assertEquals('test label', $product->getData('media_gallery/images/image/label')); }
/** * @param ProductInterface $product * @param array $newEntry * @return $this * @throws InputException * @throws StateException * @throws \Magento\Framework\Exception\LocalizedException */ protected function processNewMediaGalleryEntry(ProductInterface $product, array $newEntry) { /** @var ImageContentInterface $contentDataObject */ $contentDataObject = $newEntry['content']; /** @var \Magento\Catalog\Model\Product\Media\Config $mediaConfig */ $mediaConfig = $product->getMediaConfig(); $mediaTmpPath = $mediaConfig->getBaseTmpMediaPath(); $relativeFilePath = $this->imageProcessor->processImageContent($mediaTmpPath, $contentDataObject); $tmpFilePath = $mediaConfig->getTmpMediaShortUrl($relativeFilePath); if (!$product->hasGalleryAttribute()) { throw new StateException(__('Requested product does not support images.')); } $imageFileUri = $this->mediaGalleryProcessor->addImage($product, $tmpFilePath, isset($newEntry['types']) ? $newEntry['types'] : [], true, isset($newEntry['disabled']) ? $newEntry['disabled'] : true); // Update additional fields that are still empty after addImage call $this->mediaGalleryProcessor->updateImage($product, $imageFileUri, ['label' => $newEntry['label'], 'position' => $newEntry['position'], 'disabled' => $newEntry['disabled'], 'media_type' => $newEntry['media_type']]); return $this; }