Ejemplo n.º 1
0
 public function testGetEntryIdByFilePath()
 {
     $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', array(), array(), '', false);
     $productMock->expects($this->any())->method('getData')->with('media_gallery')->will($this->returnValue(array('images' => array(array('file' => '/i/m/image2.jpg', 'value_id' => 2), array('file' => '/i/m/image.jpg', 'value_id' => 1)))));
     $this->assertEquals(1, $this->entryResolver->getEntryIdByFilePath($productMock, '/i/m/image.jpg'));
     $this->assertNull($this->entryResolver->getEntryIdByFilePath($productMock, '/i/m/non_existent_image.jpg'));
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function create($productSku, GalleryEntry $entry, GalleryEntryContent $entryContent, $storeId = 0)
 {
     $store = $this->storeFactory->create()->load($storeId);
     if ($store->getId() != $storeId) {
         throw new NoSuchEntityException('There is no store with provided ID.');
     }
     if (!$this->contentValidator->isValid($entryContent)) {
         throw new InputException('The image content is not valid.');
     }
     $product = $this->productLoader->load($productSku);
     $fileContent = @base64_decode($entryContent->getData(), true);
     $mediaTmpPath = $this->mediaConfig->getBaseTmpMediaPath();
     $mediaDirectory = $this->filesystem->getDirectoryWrite(Filesystem::MEDIA_DIR);
     $mediaDirectory->create($mediaTmpPath);
     $fileName = $entryContent->getName() . '.' . $this->mimeTypeExtensionMap[$entryContent->getMimeType()];
     $relativeFilePath = $mediaTmpPath . DIRECTORY_SEPARATOR . $fileName;
     $absoluteFilePath = $mediaDirectory->getAbsolutePath($relativeFilePath);
     $mediaDirectory->writeFile($relativeFilePath, $fileContent);
     /** @var $productMediaGallery \Magento\Catalog\Model\Product\Attribute\Backend\Media */
     $productMediaGallery = $this->getGalleryAttributeBackend($product);
     $imageFileUri = $productMediaGallery->addImage($product, $absoluteFilePath, $entry->getTypes(), true, $entry->isDisabled());
     // Update additional fields that are still empty after addImage call
     $productMediaGallery->updateImage($product, $imageFileUri, array('label' => $entry->getLabel(), 'position' => $entry->getPosition(), 'disabled' => $entry->isDisabled()));
     $product->setStoreId($storeId);
     $product->save();
     // Remove all temporary files
     $mediaDirectory->delete($relativeFilePath);
     // File could change its name during the move from tmp dir
     return $this->entryResolver->getEntryIdByFilePath($product, $productMediaGallery->getRenamedImage($imageFileUri));
 }