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')); }
/** * {@inheritdoc} */ public function delete($productSku, $entryId) { $product = $this->productLoader->load($productSku); /** @var $productMediaGallery \Magento\Catalog\Model\Product\Attribute\Backend\Media */ $productMediaGallery = $this->getGalleryAttributeBackend($product); $filePath = $this->entryResolver->getEntryFilePathById($product, $entryId); if (is_null($filePath)) { throw new NoSuchEntityException('There is no image with provided ID.'); } $productMediaGallery->removeImage($product, $filePath); $product->save(); return true; }