public function testAfterExecuteNoVideo()
 {
     $mediaData = ['images' => [['value_id' => '7', 'file' => '/h/d/hd_image.jpg', 'media_type' => 'image', 'entity_id' => '1', 'label' => '', 'position' => '4', 'disabled' => '0', 'label_default' => null, 'position_default' => '4', 'disabled_default' => '0']], 'values' => []];
     $this->product->expects($this->once())->method('getData')->with('media_gallery')->willReturn($mediaData);
     $this->resourceModel->expects($this->never())->method('loadDataFromTableByValueId');
     $this->mediaGalleryReadHandler->expects($this->any())->method('getAttribute')->willReturn($this->attribute);
     $this->subject->afterExecute($this->mediaGalleryReadHandler, $this->product);
 }
 public function testAddMediaDataToProduct()
 {
     $attribute = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)->disableOriginalConstructor()->getMock();
     $attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue('image'));
     $this->attributeRepository->expects($this->once())->method('get')->with('media_gallery')->willReturn($attribute);
     $product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)->disableOriginalConstructor()->getMock();
     $product->expects($this->once())->method('setData')->with('image', ['images' => [10 => ['value_id' => 10]], 'values' => []]);
     $this->model->addMediaDataToProduct($product, [['value_id' => 10]]);
 }
Example #3
0
 /**
  * @param \Magento\Catalog\Model\Product $product
  * @return array
  */
 private function getProductGallery($product)
 {
     $result = [];
     $this->productGalleryReadHandler->execute($product);
     $images = $product->getMediaGalleryImages();
     foreach ($images as $image) {
         $result[] = ['mediaType' => $image->getMediaType(), 'videoUrl' => $image->getVideoUrl(), 'isBase' => $product->getImage() == $image->getFile()];
     }
     return $result;
 }
Example #4
0
 /**
  * @covers \Magento\Catalog\Model\Product\Gallery\ReadHandler::execute
  */
 public function testExecute()
 {
     /** @var \Magento\Catalog\Model\Product $product */
     $product = $this->objectManager->create('Magento\\Catalog\\Model\\Product');
     $product->setId(1);
     $this->readHandler->execute('Magento\\Catalog\\Api\\Data\\ProductInterface', $product);
     $data = $product->getData();
     $this->assertArrayHasKey('media_gallery', $data);
     $this->assertArrayHasKey('images', $data['media_gallery']);
     $this->assertEquals('Image Alt Text', $data['media_gallery']['images'][0]['label']);
 }
Example #5
0
 /**
  * @param \Magento\Catalog\Model\Product\Gallery\ReadHandler $mediaGalleryReadHandler
  * @param \Magento\Catalog\Model\Product $product
  * @return \Magento\Catalog\Model\Product
  */
 public function afterExecute(\Magento\Catalog\Model\Product\Gallery\ReadHandler $mediaGalleryReadHandler, \Magento\Catalog\Model\Product $product)
 {
     $mediaCollection = $this->getMediaEntriesDataCollection($product, $mediaGalleryReadHandler->getAttribute());
     if (!empty($mediaCollection)) {
         $ids = $this->collectVideoEntriesIds($mediaCollection);
         $videoDataCollection = $this->loadVideoDataById($ids, $product->getStoreId());
         $mediaEntriesDataCollection = $this->addVideoDataToMediaEntries($mediaCollection, $videoDataCollection);
         $product->setData($mediaGalleryReadHandler->getAttribute()->getAttributeCode(), $mediaEntriesDataCollection);
     }
     return $product;
 }
 /**
  * @covers \Magento\Catalog\Model\Product\Gallery\ReadHandler::execute
  */
 public function testExecute()
 {
     /** @var \Magento\Catalog\Model\Product $product */
     $product = $this->objectManager->create('Magento\\Catalog\\Model\\Product');
     /**
      * @var $entityMetadata \Magento\Framework\Model\Entity\EntityMetadata
      */
     $entityMetadata = $this->objectManager->get(MetadataPool::class)->getMetadata(ProductInterface::class);
     $productRepository = $this->objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
     $linkFieldId = $productRepository->get('simple')->getData($entityMetadata->getLinkField());
     $product->setData($entityMetadata->getLinkField(), $linkFieldId);
     $this->readHandler->execute('Magento\\Catalog\\Api\\Data\\ProductInterface', $product);
     $data = $product->getData();
     $this->assertArrayHasKey('media_gallery', $data);
     $this->assertArrayHasKey('images', $data['media_gallery']);
     $this->assertEquals('Image Alt Text', $data['media_gallery']['images'][0]['label']);
 }
 /**
  * {@inheritdoc}
  */
 public function getAttribute()
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getAttribute');
     if (!$pluginInfo) {
         return parent::getAttribute();
     } else {
         return $this->___callPlugins('getAttribute', func_get_args(), $pluginInfo);
     }
 }
Example #8
0
 /**
  * Get all product images
  *
  * @param \Magento\Framework\DataObject $product
  * @param int $storeId
  * @return array
  */
 protected function _getAllProductImages($product, $storeId)
 {
     $product->setStoreId($storeId);
     $gallery = $this->mediaGalleryResourceModel->loadProductGalleryByAttributeId($product, $this->mediaGalleryReadHandler->getAttribute()->getId());
     $imagesCollection = [];
     if ($gallery) {
         $productMediaPath = $this->_getMediaConfig()->getBaseMediaUrlAddition();
         foreach ($gallery as $image) {
             $imagesCollection[] = new \Magento\Framework\DataObject(['url' => $productMediaPath . $image['file'], 'caption' => $image['label'] ? $image['label'] : $image['label_default']]);
         }
     }
     return $imagesCollection;
 }
Example #9
0
 protected function setUp()
 {
     $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
     $this->galleryHandler = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product\\Gallery\\ReadHandler')->disableOriginalConstructor()->setMethods(['execute'])->getMock();
     $this->jsonEncoder = $this->getMock('\\Magento\\Framework\\Json\\EncoderInterface');
     $this->jsonDecoder = $this->getMock('\\Magento\\Framework\\Json\\DecoderInterface');
     $productMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['getTypeId', 'getTypeInstance'])->disableOriginalConstructor()->getMock();
     $variationProduct = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['setMediaGalleryEntries', 'getSku', 'getMediaGalleryImages', 'getImage', 'getData'])->disableOriginalConstructor()->getMock();
     $image = new \Magento\Framework\DataObject(['media_type' => 'type', 'video_url' => 'url', 'file' => 'image.jpg']);
     $variationProduct->expects($this->any())->method('setMediaGalleryEntries')->willReturn([]);
     $variationProduct->expects($this->any())->method('getSku')->willReturn('sku');
     $variationProduct->expects($this->any())->method('getMediaGalleryImages')->willReturn([$image]);
     $variationProduct->expects($this->any())->method('getImage')->willReturn('image.jpg');
     $variationProduct->expects($this->any())->method('getData')->with('configurable_attribute')->willReturn(1);
     $this->galleryHandler->expects($this->once())->method('execute')->with('', $variationProduct);
     $configurableType = $this->getMockBuilder('\\Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable')->disableOriginalConstructor()->setMethods(['getUsedProducts', 'getConfigurableAttributesAsArray'])->getMock();
     $configurableType->expects($this->any())->method('getUsedProducts')->with($productMock)->willReturn([$variationProduct]);
     $configurableType->expects($this->any())->method('getConfigurableAttributesAsArray')->with($productMock)->willReturn([['attribute_code' => 'configurable_attribute']]);
     $productMock->expects($this->any())->method('getTypeId')->willReturn('configurable');
     $productMock->expects($this->any())->method('getTypeInstance')->willReturn($configurableType);
     $this->plugin = $helper->getObject('\\Magento\\ConfigurableProduct\\Block\\Plugin\\Product\\Media\\Gallery', ['productGalleryReadHandler' => $this->galleryHandler, 'jsonEncoder' => $this->jsonEncoder, 'jsonDecoder' => $this->jsonDecoder]);
     $this->plugin->setData('product', $productMock);
 }