/** * Test for method addNewItemXmlCallback */ public function testAddNewItemXmlCallback() { $priceHtmlForTest = '<div class="price">Price is 10 for example</div>'; $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', ['getProductUrl', 'getDescription', 'getAllowedInRss', 'getName', '__wakeup'], [], '', false); $rssObjMock = $this->getMock('Magento\\Rss\\Model\\Rss', [], [], '', false); $layoutMock = $this->getMockForAbstractClass('Magento\\Framework\\View\\LayoutInterface', [], '', true, true, true, ['getBlock']); $priceRendererMock = $this->getMock('Magento\\Framework\\Pricing\\Render', ['render'], [], '', false); $productTitle = 'Product title'; $productUrl = '<a href="http://product.url">Product Url</a>'; $imgThumbSrc = 'http://source-for-thumbnail'; $productDescription = 'Product description'; $description = '<table><tr><td><a href="' . $productUrl . '"><img src="' . $imgThumbSrc . '" border="0" align="left" height="75" width="75"></a></td><td style="text-decoration:none;">' . $productDescription . $priceHtmlForTest . '</td></tr></table>'; $productMock->expects($this->exactly(2))->method('getProductUrl')->will($this->returnValue($productUrl)); $productMock->expects($this->once())->method('getDescription')->will($this->returnValue($productDescription)); $productMock->expects($this->any())->method('getAllowedInRss')->will($this->returnValue(true)); $productMock->expects($this->once())->method('getName')->will($this->returnValue($productTitle)); $this->imageHelperMock->expects($this->once())->method('init')->will($this->returnSelf()); $this->imageHelperMock->expects($this->once())->method('resize')->will($this->returnValue($imgThumbSrc)); $layoutMock->expects($this->once())->method('getBlock')->will($this->returnValue($priceRendererMock)); $priceRendererMock->expects($this->once())->method('render')->will($this->returnValue($priceHtmlForTest)); $expectedData = ['title' => $productTitle, 'link' => $productUrl, 'description' => $description]; $rssObjMock->expects($this->once())->method('_addEntry')->with($expectedData)->will($this->returnSelf()); $args = ['product' => $productMock, 'rssObj' => $rssObjMock, 'row' => '']; $this->block->setLayout($layoutMock); $this->assertNull($this->block->addNewItemXmlCallback($args)); }
/** * @param $products * @return array */ public function getListValues($ids) { $values = []; $searchCriteria = $this->_criteriaBuilder->addFilter('entity_id', $ids, 'in')->create(); $products = $this->_productRepository->getList($searchCriteria); foreach ($products->getItems() as $product) { $image = $this->_imageHelper->init($product, 'product_page_image_small')->getUrl(); $price = $product->getFinalPrice(); if ($price == 0 && $product->getTypeId() == 'grouped') { $children = $product->getTypeInstance()->getAssociatedProducts($product); foreach ($children as $child) { if ($child->getPrice() < $price || $price == 0) { $price = $child->getPrice(); } } } $value = array(); $value['escape_name'] = $this->escapeHtml($product->getName()); $value['name'] = $product->getName(); $value['url'] = $product->getProductUrl(); $value['price'] = $this->_priceCurrency->format($price, false); $value['image'] = $image; $values[] = $value; } return $values; }
/** * @return array */ public function getCartItems() { if (!isset(self::$quoteItemsData)) { self::$quoteItemsData = []; $displayPriceWithTax = $this->rejoinerHelper->getTrackPriceWithTax(); if ($quote = $this->getQuote()) { $categories = []; /** @var \Magento\Quote\Model\Quote $quote */ /** @var \Magento\Quote\Model\Quote\Item $item */ foreach ($quote->getAllItems() as $item) { $categories = array_merge($categories, $item->getProduct()->getCategoryIds()); } /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection */ $categoryCollection = $this->categoryCollectionFactory->create(); $categoriesArray = $categoryCollection->addAttributeToSelect('name')->addFieldToFilter('entity_id', ['in' => $categories])->load()->getItems(); $imageWidth = $this->rejoinerHelper->getImageWidth(); $imageHeight = $this->rejoinerHelper->getImageHeight(); foreach ($quote->getAllVisibleItems() as $item) { $product = $item->getProduct(); $productCategories = $this->rejoinerHelper->getProductCategories($product, $categoriesArray); $imageUrl = $this->imageHelper->init($product, 'category_page_grid')->resize($imageWidth, $imageHeight)->getUrl(); if ($displayPriceWithTax) { $productPrice = $item->getPriceInclTax(); $rowTotal = $item->getRowTotalInclTax(); } else { $productPrice = $item->getPrice(); $rowTotal = $item->getRowTotal(); } $newItem = ['name' => $item->getName(), 'image_url' => $imageUrl, 'price' => (string) $this->rejoinerHelper->convertPriceToCents($productPrice), 'product_id' => (string) $item->getSku(), 'item_qty' => (string) $item->getQty(), 'qty_price' => (string) $this->rejoinerHelper->convertPriceToCents($rowTotal), 'product_url' => (string) $product->getProductUrl(), 'category' => $productCategories]; self::$quoteItemsData[] = $newItem; } } } return self::$quoteItemsData; }
public function testGetProductThumbnail() { $productForThumbnail = $this->_initProduct(); /** Ensure that image helper was initialized with correct arguments */ $this->_imageHelper->expects($this->once())->method('init')->with($productForThumbnail, 'thumbnail')->will($this->returnSelf()); $productThumbnail = $this->_renderer->getProductThumbnail(); $this->assertSame($this->_imageHelper, $productThumbnail, 'Invalid product thumbnail is returned.'); }
/** * @param array $expected * @param array $data * @dataProvider getOptionsDataProvider */ public function testGetOptions(array $expected, array $data) { $this->_imageHelperMock->expects($this->at(0))->method('init')->will($this->returnValue('http://example.com/base_img_url')); for ($i = 1; $i <= count($data['allowed_products']); $i++) { $this->_imageHelperMock->expects($this->at($i))->method('init')->will($this->returnValue('http://example.com/base_img_url_' . $i)); } $this->assertEquals($expected, $this->_model->getOptions($data['current_product_mock'], $data['allowed_products'])); }
/** * @param array $expected * @param array $data * @dataProvider getOptionsDataProvider */ public function testGetOptions(array $expected, array $data) { if (count($data['allowed_products'])) { $imageHelper1 = $this->getMockBuilder('Magento\\Catalog\\Helper\\Image')->disableOriginalConstructor()->getMock(); $imageHelper1->expects($this->any())->method('getUrl')->willReturn('http://example.com/base_img_url'); $imageHelper2 = $this->getMockBuilder('Magento\\Catalog\\Helper\\Image')->disableOriginalConstructor()->getMock(); $imageHelper2->expects($this->any())->method('getUrl')->willReturn('http://example.com/base_img_url_2'); $this->_imageHelperMock->expects($this->any())->method('init')->willReturnMap([[$data['current_product_mock'], 'product_page_image_large', [], $imageHelper1], [$data['allowed_products'][0], 'product_page_image_large', [], $imageHelper1], [$data['allowed_products'][1], 'product_page_image_large', [], $imageHelper2]]); } $this->assertEquals($expected, $this->_model->getOptions($data['current_product_mock'], $data['allowed_products'])); }
/** * Retrieve collection of gallery images * * @param \Magento\Catalog\Api\Data\ProductInterface $product * @return \Magento\Catalog\Model\Product\Image[]|null */ public function getGalleryImages(\Magento\Catalog\Api\Data\ProductInterface $product) { $images = $product->getMediaGalleryImages(); if ($images instanceof \Magento\Framework\Data\Collection) { foreach ($images as $image) { /** @var $image \Magento\Catalog\Model\Product\Image */ $image->setData('small_image_url', $this->imageHelper->init($product, 'product_page_image_small')->setImageFile($image->getFile())->getUrl()); $image->setData('medium_image_url', $this->imageHelper->init($product, 'product_page_image_medium')->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->setImageFile($image->getFile())->getUrl()); $image->setData('large_image_url', $this->imageHelper->init($product, 'product_page_image_large')->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->setImageFile($image->getFile())->getUrl()); } } return $images; }
public function testGenerate() { $imageFile = 'image.jpg'; $imageItem = $this->objectManager->getObject('Magento\\Framework\\Object', ['data' => ['file' => $imageFile]]); $this->mediaGalleryCollection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$imageItem])); $this->product->expects($this->any())->method('getMediaGalleryImages')->willReturn($this->mediaGalleryCollection); $this->config->expects($this->once())->method('getVars')->with('Magento_Catalog')->willReturn($this->getTestData()); $this->viewConfig->expects($this->once())->method('getViewConfig')->with(['area' => Area::AREA_FRONTEND, 'themeModel' => 'Magento\\theme'])->willReturn($this->config); $this->themeCollection->expects($this->once())->method('loadRegisteredThemes')->willReturn(['Magento\\theme']); $this->imageHelper->expects($this->exactly(3))->method('init')->will($this->returnValueMap([[$this->product, 'image', $imageFile, $this->imageHelper], [$this->product, 'small_image', $imageFile, $this->imageHelper], [$this->product, 'thumbnail', $imageFile, $this->imageHelper]])); $this->imageHelper->expects($this->exactly(3))->method('resize')->will($this->returnValueMap([[300, 300, $this->imageHelper], [200, 200, $this->imageHelper], [100, 100, $this->imageHelper]])); $this->imageHelper->expects($this->exactly(3))->method('save')->will($this->returnSelf()); $this->model->generate($this->product); }
public function testGetRssData() { $category = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Category')->setMethods(['__sleep', '__wakeup', 'load', 'getId', 'getUrl', 'getName'])->disableOriginalConstructor()->getMock(); $category->expects($this->once())->method('getName')->will($this->returnValue('Category Name')); $category->expects($this->once())->method('getUrl')->will($this->returnValue('http://magento.com/category-name.html')); $this->categoryRepository->expects($this->once())->method('get')->will($this->returnValue($category)); $product = $this->getMockBuilder('\\Magento\\catalog\\Model\\Product')->setMethods(['__sleep', '__wakeup', 'getName', 'getAllowedInRss', 'getProductUrl', 'getDescription', 'getAllowedPriceInRss'])->disableOriginalConstructor()->getMock(); $product->expects($this->once())->method('getName')->will($this->returnValue('Product Name')); $product->expects($this->once())->method('getAllowedInRss')->will($this->returnValue(true)); $product->expects($this->exactly(2))->method('getProductUrl')->will($this->returnValue('http://magento.com/product.html')); $product->expects($this->once())->method('getDescription')->will($this->returnValue('Product Description')); $product->expects($this->once())->method('getAllowedPriceInRss')->will($this->returnValue(true)); $this->rssModel->expects($this->once())->method('getProductCollection')->will($this->returnValue([$product])); $this->imageHelper->expects($this->once())->method('init')->with($product, 'thumbnail')->will($this->returnSelf()); $this->imageHelper->expects($this->once())->method('resize')->with(75, 75)->will($this->returnValue('image_link')); $data = $this->block->getRssData(); $this->assertEquals($this->rssFeed['link'], $data['link']); $this->assertEquals($this->rssFeed['title'], $data['title']); $this->assertEquals($this->rssFeed['description'], $data['description']); $this->assertEquals($this->rssFeed['entries'][0]['title'], $data['entries'][0]['title']); $this->assertEquals($this->rssFeed['entries'][0]['link'], $data['entries'][0]['link']); $this->assertContains('<a href="http://magento.com/product.html">', $data['entries'][0]['description']); $this->assertContains('<img src="image_link" border="0" align="left" height="75" width="75">', $data['entries'][0]['description']); $this->assertContains('<td style="text-decoration:none;">Product Description </td>', $data['entries'][0]['description']); }
/** * Get RSS feed items * * @return array */ public function getRssData() { $wishlist = $this->getWishlist(); if ($wishlist->getId()) { $data = $this->getHeader(); /** @var $wishlistItem \Magento\Wishlist\Model\Item */ foreach ($wishlist->getItemCollection() as $wishlistItem) { /* @var $product \Magento\Catalog\Model\Product */ $product = $wishlistItem->getProduct(); $productUrl = $this->wishlistBlock->getProductUrl($product, ['_rss' => true]); $product->setAllowedInRss(true); $product->setAllowedPriceInRss(true); $product->setProductUrl($productUrl); $args = ['product' => $product]; $this->eventManager->dispatch('rss_wishlist_xml_callback', $args); if (!$product->getAllowedInRss()) { continue; } $description = '<table><tr><td><a href="' . $productUrl . '"><img src="' . $this->imageHelper->init($product, 'rss_thumbnail')->getUrl() . '" border="0" align="left" height="75" width="75"></a></td>' . '<td style="text-decoration:none;">' . $this->outputHelper->productAttribute($product, $product->getShortDescription(), 'short_description') . '<p>'; if ($product->getAllowedPriceInRss()) { $description .= $this->getProductPriceHtml($product); } $description .= '</p>'; if (trim($product->getDescription()) != '') { $description .= '<p>' . __('Comment:') . ' ' . $this->outputHelper->productAttribute($product, $product->getDescription(), 'description') . '<p>'; } $description .= '</td></tr></table>'; $data['entries'][] = ['title' => $product->getName(), 'link' => $productUrl, 'description' => $description]; } } else { $data = ['title' => __('We cannot retrieve the Wish List.'), 'description' => __('We cannot retrieve the Wish List.'), 'link' => $this->urlBuilder->getUrl(), 'charset' => 'UTF-8']; } return $data; }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getItems() { Profiler::start(__METHOD__); $items = []; /** @var \Magento\Catalog\Model\Product $product */ foreach ($this->collection as $product) { $item = ['sku' => $product->getSku(), 'name' => $product->getName(), 'url' => $product->getProductUrl(), 'description' => null, 'image' => null, 'price' => null, 'rating' => null]; if ($this->config->isShowShortDescription()) { $item['description'] = html_entity_decode(strip_tags($product->getDataUsingMethod('description'))); } $image = false; if ($product->getImage() && $product->getImage() != 'no_selection') { $image = $product->getImage(); } elseif ($product->getSmallImage() && $product->getSmallImage() != 'no_selection') { $image = $product->getSmallImage(); } if ($this->config->isShowImage() && $image) { $item['image'] = $this->imageHelper->init($product, false)->setImageFile($image)->resize(65 * 2, 80 * 2)->getUrl(); } if ($this->config->isShowPrice()) { $item['price'] = $this->catalogHelper->getTaxPrice($product, $product->getFinalPrice()); $item['price'] = $this->pricingHelper->currency($item['price'], false, false); } if ($this->config->isShowRating()) { $item['rating'] = $this->reviewRenderer->getReviewsSummaryHtml($product, ReviewRendererInterface::SHORT_VIEW); } $items[] = $item; } Profiler::stop(__METHOD__); return $items; }
/** * Additional function to process forming description for wishlist item * * @param \Magento\Wishlist\Model\Wishlist $wishlistModelMock * @param array $staticArgs * @return string */ protected function processWishlistItemDescription($wishlistModelMock, $staticArgs) { $imgThumbSrc = 'http://source-for-thumbnail'; $priceHtmlForTest = '<div class="price">Price is 10 for example</div>'; $productDescription = 'Product description'; $productShortDescription = 'Product short description'; $wishlistItem = $this->getMock('Magento\\Wishlist\\Model\\Item', [], [], '', false); $wishlistItemsCollection = [$wishlistItem]; $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', ['getAllowedInRss', 'getAllowedPriceInRss', 'getDescription', 'getShortDescription', 'getName', 'getVisibleInSiteVisibilities', 'getUrlModel', '__wakeup'], [], '', false); $urlModelMock = $this->getMock('Magento\\Catalog\\Model\\Product\\Url', [], [], '', false); $layoutMock = $this->getMockForAbstractClass('Magento\\Framework\\View\\LayoutInterface', [], '', true, true, true, ['getBlock']); $wishlistModelMock->expects($this->once())->method('getItemCollection')->will($this->returnValue($wishlistItemsCollection)); $wishlistItem->expects($this->once())->method('getProduct')->will($this->returnValue($productMock)); $productMock->expects($this->once())->method('getUrlModel')->will($this->returnValue($urlModelMock)); $productMock->expects($this->once())->method('getAllowedPriceInRss')->will($this->returnValue($urlModelMock)); $urlModelMock->expects($this->once())->method('getUrl')->will($this->returnValue($staticArgs['productUrl'])); $productMock->expects($this->once())->method('getName')->will($this->returnValue($staticArgs['productName'])); $productMock->expects($this->once())->method('getAllowedInRss')->will($this->returnValue(true)); $productMock->expects($this->once())->method('getVisibleInSiteVisibilities')->will($this->returnValue(true)); $this->imageHelperMock->expects($this->once())->method('init')->will($this->returnSelf()); $this->imageHelperMock->expects($this->once())->method('resize')->will($this->returnValue($imgThumbSrc)); $priceRendererMock = $this->getMock('Magento\\Framework\\Pricing\\Render', ['render'], [], '', false); $layoutMock->expects($this->once())->method('getBlock')->will($this->returnValue($priceRendererMock)); $priceRendererMock->expects($this->once())->method('render')->will($this->returnValue($priceHtmlForTest)); $productMock->expects($this->any())->method('getDescription')->will($this->returnValue($productDescription)); $productMock->expects($this->any())->method('getShortDescription')->will($this->returnValue($productShortDescription)); $this->catalogOutputMock->expects($this->any())->method('productAttribute')->will($this->returnArgument(1)); $this->block->setLayout($layoutMock); $description = '<table><tr><td><a href="' . $staticArgs['productUrl'] . '"><img src="' . $imgThumbSrc . '" border="0" align="left" height="75" width="75"></a></td><td style="text-decoration:none;">' . $productShortDescription . '<p>' . $priceHtmlForTest . '</p><p>Comment: ' . $productDescription . '<p>' . '</td></tr></table>'; return $description; }
/** * {@inheritdoc} */ public function getRssData() { try { $category = $this->categoryRepository->get($this->getRequest()->getParam('cid')); } catch (NoSuchEntityException $e) { return ['title' => 'Category Not Found', 'description' => 'Category Not Found', 'link' => $this->getUrl(''), 'charset' => 'UTF-8']; } $category->setIsAnchor(true); $newUrl = $category->getUrl(); $title = $category->getName(); $data = ['title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8']; /** @var $product \Magento\Catalog\Model\Product */ foreach ($this->rssModel->getProductCollection($category, $this->getStoreId()) as $product) { $product->setAllowedInRss(true); $product->setAllowedPriceInRss(true); $this->_eventManager->dispatch('rss_catalog_category_xml_callback', ['product' => $product]); if (!$product->getAllowedInRss()) { continue; } $description = ' <table><tr> <td><a href="%s"><img src="%s" border="0" align="left" height="75" width="75"></a></td> <td style="text-decoration:none;">%s %s</td> </tr></table> '; $description = sprintf($description, $product->getProductUrl(), $this->imageHelper->init($product, 'rss_thumbnail')->getUrl(), $product->getDescription(), $product->getAllowedPriceInRss() ? $this->renderPriceHtml($product) : ''); $data['entries'][] = ['title' => $product->getName(), 'link' => $product->getProductUrl(), 'description' => $description]; } return $data; }
/** * {@inheritdoc} */ public function getRssData() { $storeModel = $this->storeManager->getStore($this->getStoreId()); $newUrl = $this->rssUrlBuilder->getUrl(['store_id' => $this->getStoreId(), 'type' => 'new_products']); $title = __('New Products from %1', $storeModel->getFrontendName()); $lang = $this->_scopeConfig->getValue('general/locale/code', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeModel); $data = ['title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8', 'language' => $lang]; foreach ($this->rssModel->getProductsCollection($this->getStoreId()) as $item) { /** @var $item \Magento\Catalog\Model\Product */ $item->setAllowedInRss(true); $item->setAllowedPriceInRss(true); $this->_eventManager->dispatch('rss_catalog_new_xml_callback', ['row' => $item->getData(), 'product' => $item]); if (!$item->getAllowedInRss()) { continue; } $allowedPriceInRss = $item->getAllowedPriceInRss(); $description = ' <table><tr> <td><a href="%s"><img src="%s" border="0" align="left" height="75" width="75"></a></td> <td style="text-decoration:none;">%s %s</td> </tr></table> '; $description = sprintf($description, $item->getProductUrl(), $this->imageHelper->init($item, 'thumbnail')->resize(75, 75), $item->getDescription(), $allowedPriceInRss ? $this->renderPriceHtml($item) : ''); $data['entries'][] = ['title' => $item->getName(), 'link' => $item->getProductUrl(), 'description' => $description]; } return $data; }
/** * Get Options for Configurable Product Options * * @param \Magento\Catalog\Model\Product $currentProduct * @param array $allowedProducts * @return array */ public function getOptions($currentProduct, $allowedProducts) { $options = []; $baseImageUrl = (string) $this->imageHelper->init($currentProduct, 'image'); foreach ($allowedProducts as $product) { $productId = $product->getId(); $image = (string) $this->imageHelper->init($product, 'image'); foreach ($this->getAllowAttributes($currentProduct) as $attribute) { $productAttribute = $attribute->getProductAttribute(); $productAttributeId = $productAttribute->getId(); $attributeValue = $product->getData($productAttribute->getAttributeCode()); $options[$productAttributeId][$attributeValue][] = $productId; $imageUrl = !$product->getImage() || $product->getImage() === 'no_selection' ? $baseImageUrl : (string) $image; $options['images'][$productAttributeId][$attributeValue][$productId] = $imageUrl; } } return $options; }
/** * Return array with data of items of order * * @param $order * * @return array */ protected function getItems($order) { $items = []; foreach ($order->getAllVisibleItems() as $item) { $product = $item->getProduct(); $image = $this->_helperImage->init($product, 'image'); $items[] = ["id" => $item->getSku(), "title" => $product->getName(), "description" => $product->getName(), "picture_url" => $image->getUrl(), "category_id" => $this->_scopeConfig->getValue('payment/mercadopago/category_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE), "quantity" => (int) number_format($item->getQtyOrdered(), 0, '.', ''), "unit_price" => (double) number_format($item->getPrice(), 2, '.', '')]; } return $items; }
/** * {@inheritdoc} */ protected function createModel() { $this->currencyMock = $this->getMockBuilder(CurrencyInterface::class)->setMethods(['getCurrency', 'toCurrency'])->getMockForAbstractClass(); $this->currencyMock->expects($this->any())->method('getCurrency')->willReturn($this->currencyMock); $this->imageHelperMock = $this->getMockBuilder(ImageHelper::class)->setMethods(['init', 'getUrl'])->disableOriginalConstructor()->getMock(); $this->imageHelperMock->expects($this->any())->method('init')->willReturn($this->imageHelperMock); $this->attributeSetRepositoryMock = $this->getMockBuilder(AttributeSetRepositoryInterface::class)->setMethods(['get'])->getMockForAbstractClass(); $attributeSetMock = $this->getMockBuilder(AttributeSetInterface::class)->setMethods(['getAttributeSetName'])->getMockForAbstractClass(); $this->attributeSetRepositoryMock->expects($this->any())->method('get')->willReturn($attributeSetMock); return $this->objectManager->getObject(Grouped::class, ['locator' => $this->locatorMock, 'productLinkRepository' => $this->linkRepositoryMock, 'productRepository' => $this->productRepositoryMock, 'localeCurrency' => $this->currencyMock, 'imageHelper' => $this->imageHelperMock, 'attributeSetRepository' => $this->attributeSetRepositoryMock]); }
/** * Additional function to break up mocks initialization * * @return void */ protected function addMocks() { $resIteratorcallback = function () { $arguments = func_get_args(); $arguments[2]['results'] = [['use_special' => false, 'price' => 10, 'final_price' => 20]]; }; $this->storeMock->expects($this->once())->method('getWebsiteId')->will($this->returnValue(0)); $this->storeMock->expects($this->once())->method('getFrontendName')->will($this->returnValue('store name')); $this->catalogHelperMock->expects($this->once())->method('canApplyMsrp')->will($this->returnValue(false)); $this->resourceIteratorMock->expects($this->once())->method('walk')->will($this->returnCallback($resIteratorcallback)); $this->imageHelperMock->expects($this->once())->method('init')->will($this->returnSelf()); }
/** * Resize product images and save results to image cache * * @param Product $product * @return $this */ public function generate(Product $product) { $galleryImages = $product->getMediaGalleryImages(); if ($galleryImages) { foreach ($galleryImages as $image) { foreach ($this->getData() as $params) { $this->imageHelper->init($product, $params['type'], $image->getFile())->resize($params['width'], $params['height'])->save(); } } } return $this; }
public function testGetItemData() { $urlModel = $this->getMockBuilder(\Magento\Catalog\Model\Product\Url::class)->disableOriginalConstructor()->getMock(); $product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)->setMethods(['getUrlModel', 'isVisibleInSiteVisibility', 'getSku'])->disableOriginalConstructor()->getMock(); $product->expects($this->any())->method('getUrlModel')->willReturn($urlModel); $product->expects($this->any())->method('isVisibleInSiteVisibility')->willReturn(true); $product->expects($this->any())->method('getSku')->willReturn('simple'); /** @var \Magento\Quote\Model\Quote\Item $item */ $item = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)->setMethods(['getProductType', 'getProduct', 'getCalculationPrice'])->disableOriginalConstructor()->getMock(); $item->expects($this->any())->method('getProduct')->willReturn($product); $item->expects($this->any())->method('getProductType')->willReturn('simple'); $item->expects($this->any())->method('getCalculationPrice')->willReturn(5); $this->imageHelper->expects($this->any())->method('init')->with($product)->willReturnSelf(); $this->imageHelper->expects($this->any())->method('getUrl')->willReturn('url'); $this->imageHelper->expects($this->any())->method('getLabel')->willReturn('label'); $this->imageHelper->expects($this->any())->method('getWidth')->willReturn(100); $this->imageHelper->expects($this->any())->method('getHeight')->willReturn(100); $this->configurationPool->expects($this->any())->method('getByProductType')->willReturn($product); $itemData = $this->model->getItemData($item); $this->assertArrayHasKey('options', $itemData); $this->assertArrayHasKey('qty', $itemData); $this->assertArrayHasKey('item_id', $itemData); $this->assertArrayHasKey('configure_url', $itemData); $this->assertArrayHasKey('is_visible_in_site_visibility', $itemData); $this->assertArrayHasKey('product_type', $itemData); $this->assertArrayHasKey('product_name', $itemData); $this->assertArrayHasKey('product_sku', $itemData); $this->assertArrayHasKey('product_url', $itemData); $this->assertArrayHasKey('product_has_url', $itemData); $this->assertArrayHasKey('product_price', $itemData); $this->assertArrayHasKey('product_price_value', $itemData); $this->assertArrayHasKey('product_image', $itemData); $this->assertArrayHasKey('canApplyMsrp', $itemData); }
/** * Trigger pusher event with latest order information * * @param \Magento\Framework\Event\Observer $observer * @return \Jahvi\NewOrderNotification\Observer\DisplayNotification */ public function execute(Observer $observer) { if ($this->globalConfig->getValue('checkout/newordernotification/enabled')) { $appId = $this->globalConfig->getValue('checkout/newordernotification/app_id'); $appKey = $this->globalConfig->getValue('checkout/newordernotification/app_key'); $appSecret = $this->globalConfig->getValue('checkout/newordernotification/app_secret'); $pusher = new \Pusher($appKey, $appSecret, $appId, ['encrypted' => true]); // Get latest order $orderId = $observer->getEvent()->getOrderIds()[0]; $order = $this->orderFactory->create()->load($orderId); // Get last product in order data $product = $order->getAllVisibleItems()[0]->getProduct(); $shippingCity = $order->getShippingAddress()->getCity(); $productImage = $this->imageHelper->init($product, 'product_thumbnail_image'); // Get shipping city and country $shippingCountryCode = $order->getShippingAddress()->getCountryId(); $shippingCountry = $this->countryFactory->create()->loadByCode($shippingCountryCode); // Trigger pusher event with collected data $pusher->trigger('non_channel', 'new_order', ['product_name' => $product->getName(), 'product_image' => $productImage->getUrl(), 'product_url' => $product->getProductUrl(), 'shipping_city' => $shippingCity, 'shipping_country' => $shippingCountry->getName()]); } return $this; }
public function testGetJsonSwatchConfigWithoutVisualImageType() { $this->configurable->setProduct($this->product); $this->swatchHelper->expects($this->once())->method('getSwatchAttributesAsArray')->with($this->product)->willReturn([1 => ['options' => [1 => 'testA', 3 => 'testB'], 'use_product_image_for_swatch' => true, 'attribute_code' => 'code']]); $this->swatchHelper->expects($this->once())->method('getSwatchesByOptionsId')->with([1, 3])->willReturn([3 => ['type' => Swatch::SWATCH_TYPE_VISUAL_IMAGE, 'value' => 'hello']]); $this->swatchHelper->expects($this->once())->method('loadFirstVariationWithSwatchImage')->with($this->product, ['code' => 3])->willReturn($this->product); $this->swatchMediaHelper->expects($this->exactly(2))->method('getSwatchAttributeImage')->withConsecutive(['swatch_image', 'hello'], ['swatch_thumb', 'hello'])->willReturn('/path'); $this->product->expects($this->exactly(4))->method('getData')->withConsecutive(['swatch_image'], ['image'], ['swatch_image'], ['image'])->will($this->onConsecutiveCalls(null, null, null, null)); $this->imageHelper->expects($this->never())->method('init'); $this->imageHelper->expects($this->never())->method('resize'); $this->jsonEncoder->expects($this->once())->method('encode'); $this->configurable->getJsonSwatchConfig(); }
/** * Get wishlist items * * @return array */ protected function getItems() { $this->view->loadLayout(); $collection = $this->wishlistHelper->getWishlistItemCollection(); $collection->clear()->setPageSize(self::SIDEBAR_ITEMS_NUMBER)->setInStockFilter(true)->setOrder('added_at'); $items = []; /** @var \Magento\Wishlist\Model\Item $wishlistItem */ foreach ($collection as $wishlistItem) { $product = $wishlistItem->getProduct(); $this->imageHelper->init($product, 'wishlist_sidebar_block'); $items[] = ['image' => ['src' => $this->imageHelper->getUrl(), 'alt' => $this->imageHelper->getLabel(), 'width' => $this->imageHelper->getWidth(), 'height' => $this->imageHelper->getHeight()], 'product_url' => $this->wishlistHelper->getProductUrl($wishlistItem), 'product_name' => $product->getName(), 'product_price' => $this->block->getProductPriceHtml($product, \Magento\Catalog\Pricing\Price\ConfiguredPriceInterface::CONFIGURED_PRICE_CODE, \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST, ['item' => $wishlistItem]), 'product_is_saleable_and_visible' => $product->isSaleable() && $product->isVisibleInSiteVisibility(), 'product_has_required_options' => $product->getTypeInstance()->hasRequiredOptions($product), 'add_to_cart_params' => $this->wishlistHelper->getAddToCartParams($wishlistItem, true), 'delete_item_params' => $this->wishlistHelper->getRemoveParams($wishlistItem, true)]; } return $items; }
public function testGetGalleryImages() { $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $productMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock(); $productTypeMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Type\\AbstractType')->disableOriginalConstructor()->getMock(); $productTypeMock->expects($this->once())->method('getStoreFilter')->with($productMock)->willReturn($storeMock); $productMock->expects($this->once())->method('getTypeInstance')->willReturn($productTypeMock); $productMock->expects($this->once())->method('getMediaGalleryImages')->willReturn($this->getImagesCollection()); $this->registry->expects($this->once())->method('registry')->with('product')->willReturn($productMock); $this->imageHelper->expects($this->exactly(3))->method('init')->willReturnMap([[$productMock, 'product_page_image_small', [], $this->imageHelper], [$productMock, 'product_page_image_medium', [], $this->imageHelper], [$productMock, 'product_page_image_large', [], $this->imageHelper]])->willReturnSelf(); $this->imageHelper->expects($this->exactly(3))->method('setImageFile')->with('test_file')->willReturnSelf(); $this->imageHelper->expects($this->at(0))->method('getUrl')->willReturn('product_page_image_small_url'); $this->imageHelper->expects($this->at(1))->method('getUrl')->willReturn('product_page_image_medium_url'); $this->imageHelper->expects($this->at(2))->method('getUrl')->willReturn('product_page_image_large_url'); $this->imageHelper->expects($this->exactly(2))->method('constrainOnly')->with(true)->willReturnSelf(); $this->imageHelper->expects($this->exactly(2))->method('keepAspectRatio')->with(true)->willReturnSelf(); $this->imageHelper->expects($this->exactly(2))->method('keepFrame')->with(false)->willReturnSelf(); $images = $this->model->getGalleryImages(); $this->assertInstanceOf('Magento\\Framework\\Data\\Collection', $images); }
public function testGetProductThumbnailUrl() { $url = 'pub/media/catalog/product/cache/1/thumbnail/75x/9df78eab33525d08d6e5fb8d27136e95/_/_/__green.gif'; $objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this); $configView = $this->getMock('Magento\\Framework\\Config\\View', array('getVarValue'), array(), '', false); $configView->expects($this->any())->method('getVarValue')->will($this->returnValue(75)); $this->_configManager->expects($this->any())->method('getViewConfig')->will($this->returnValue($configView)); $product = $this->getMock('Magento\\Catalog\\Model\\Product', array('isConfigurable', '__wakeup'), array(), '', false); $product->expects($this->any())->method('isConfigurable')->will($this->returnValue(true)); $childProduct = $this->getMock('Magento\\Catalog\\Model\\Product', array('getThumbnail', 'getDataByKey', '__wakeup'), array(), '', false); $childProduct->expects($this->any())->method('getThumbnail')->will($this->returnValue('/_/_/__green.gif')); $this->_imageHelper->expects($this->any())->method('init')->will($this->returnValue($this->_imageHelper)); $this->_imageHelper->expects($this->any())->method('resize')->will($this->returnValue($this->_imageHelper)); $this->_imageHelper->expects($this->any())->method('__toString')->will($this->returnValue($url)); $arguments = array('statusListFactory' => $this->getMock('Magento\\Sales\\Model\\Status\\ListFactory', array(), array(), '', false), 'productFactory' => $this->getMock('Magento\\Catalog\\Model\\ProductFactory', array(), array(), '', false), 'itemOptionFactory' => $this->getMock('Magento\\Sales\\Model\\Quote\\Item\\OptionFactory', array(), array(), '', false)); $childItem = $objectManagerHelper->getObject('Magento\\Sales\\Model\\Quote\\Item', $arguments); $childItem->setData('product', $childProduct); $item = $objectManagerHelper->getObject('Magento\\Sales\\Model\\Quote\\Item', $arguments); $item->setData('product', $product); $item->addChild($childItem); $this->_renderer->setItem($item); $configurableUrl = $this->_renderer->getProductThumbnailUrl(); $this->assertNotNull($configurableUrl); }
public function testGetRssData() { $this->rssUrlBuilder->expects($this->once())->method('getUrl')->with(['type' => 'new_products', 'store_id' => 1])->will($this->returnValue('http://magento.com/rss/feed/index/type/new_products/store_id/1')); $item = $this->getItemMock(); $this->newProducts->expects($this->once())->method('getProductsCollection')->will($this->returnValue([$item])); $this->imageHelper->expects($this->once())->method('init')->with($item, 'thumbnail')->will($this->returnSelf()); $this->imageHelper->expects($this->once())->method('resize')->with(75, 75)->will($this->returnValue('image_link')); $data = ['title' => 'New Products from Store 1', 'description' => 'New Products from Store 1', 'link' => 'http://magento.com/rss/feed/index/type/new_products/store_id/1', 'charset' => 'UTF-8', 'language' => null, 'entries' => [['title' => 'Product Name', 'link' => 'http://magento.com/product-name.html']]]; $rssData = $this->block->getRssData(); $description = $rssData['entries'][0]['description']; unset($rssData['entries'][0]['description']); $this->assertEquals($data, $rssData); $this->assertContains('<a href="http://magento.com/product-name.html">', $description); $this->assertContains('<img src="image_link" border="0" align="left" height="75" width="75">', $description); $this->assertContains('<td style="text-decoration:none;">Product Description </td>', $description); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGenerate() { $imageFile = 'image.jpg'; $imageItem = $this->objectManager->getObject('Magento\\Framework\\DataObject', ['data' => ['file' => $imageFile]]); $this->mediaGalleryCollection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$imageItem])); $this->product->expects($this->any())->method('getMediaGalleryImages')->willReturn($this->mediaGalleryCollection); $data = $this->getTestData(); $this->config->expects($this->once())->method('getMediaEntities')->with('Magento_Catalog')->willReturn($data); $themeMock = $this->getMockBuilder('Magento\\Theme\\Model\\Theme')->disableOriginalConstructor()->getMock(); $themeMock->expects($this->exactly(3))->method('getCode')->willReturn('Magento\\theme'); $this->themeCollection->expects($this->once())->method('loadRegisteredThemes')->willReturn([$themeMock]); $this->viewConfig->expects($this->once())->method('getViewConfig')->with(['area' => Area::AREA_FRONTEND, 'themeModel' => $themeMock])->willReturn($this->config); $this->imageHelper->expects($this->exactly(3))->method('init')->will($this->returnValueMap([[$this->product, 'product_image', $this->getImageData('product_image'), $this->imageHelper], [$this->product, 'product_small_image', $this->getImageData('product_small_image'), $this->imageHelper], [$this->product, 'product_thumbnail', $this->getImageData('product_thumbnail'), $this->imageHelper]])); $this->imageHelper->expects($this->exactly(3))->method('setImageFile')->with($imageFile)->willReturnSelf(); $this->imageHelper->expects($this->any())->method('keepAspectRatio')->with($data['product_image']['aspect_ratio'])->willReturnSelf(); $this->imageHelper->expects($this->any())->method('keepFrame')->with($data['product_image']['frame'])->willReturnSelf(); $this->imageHelper->expects($this->any())->method('keepTransparency')->with($data['product_image']['transparency'])->willReturnSelf(); $this->imageHelper->expects($this->any())->method('constrainOnly')->with($data['product_image']['constrain'])->willReturnSelf(); $this->imageHelper->expects($this->any())->method('backgroundColor')->with($data['product_image']['background'])->willReturnSelf(); $this->imageHelper->expects($this->exactly(3))->method('save')->will($this->returnSelf()); $this->model->generate($this->product); }
/** * @return string */ protected function _toHtml() { //store id is store view id $storeId = $this->_getStoreId(); $websiteId = $this->_storeManager->getStore($storeId)->getWebsiteId(); //customer group id $customerGroupId = $this->_getCustomerGroupId(); /** @var $product \Magento\Catalog\Model\Product */ $product = $this->_productFactory->create(); $product->setStoreId($storeId); $specials = $product->getResourceCollection()->addPriceDataFieldFilter('%s < %s', array('final_price', 'price'))->addPriceData($customerGroupId, $websiteId)->addAttributeToSelect(array('name', 'short_description', 'description', 'price', 'thumbnail', 'special_price', 'special_to_date', 'msrp_enabled', 'msrp_display_actual_price_type', 'msrp'), 'left')->addAttributeToSort('name', 'asc'); $newUrl = $this->_urlBuilder->getUrl('rss/catalog/special/store_id/' . $storeId); $title = __('%1 - Special Products', $this->_storeManager->getStore()->getFrontendName()); $lang = $this->_scopeConfig->getValue('general/locale/code', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); /** @var $rssObj \Magento\Rss\Model\Rss */ $rssObj = $this->_rssFactory->create(); $rssObj->_addHeader(array('title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8', 'language' => $lang)); $results = array(); /* using resource iterator to load the data one by one instead of loading all at the same time. loading all data at the same time can cause the big memory allocation. */ $this->_resourceIterator->walk($specials->getSelect(), array(array($this, 'addSpecialXmlCallback')), array('rssObj' => $rssObj, 'results' => &$results)); if (sizeof($results) > 0) { foreach ($results as $result) { // render a row for RSS feed $product->setData($result); $html = sprintf('<table><tr>' . '<td><a href="%s"><img src="%s" alt="" border="0" align="left" height="75" width="75" /></a></td>' . '<td style="text-decoration:none;">%s', $product->getProductUrl(), $this->_imageHelper->init($product, 'thumbnail')->resize(75, 75), $this->_outputHelper->productAttribute($product, $product->getDescription(), 'description')); // add price data if needed if ($product->getAllowedPriceInRss()) { if ($this->_catalogData->canApplyMsrp($product)) { $html .= '<br/><a href="' . $product->getProductUrl() . '">' . __('Click for price') . '</a>'; } else { $special = ''; if ($result['use_special']) { $special = '<br />' . __('Special Expires On: %1', $this->formatDate($result['special_to_date'], \Magento\Framework\Stdlib\DateTime\TimezoneInterface::FORMAT_TYPE_MEDIUM)); } $html .= sprintf('<p>%s %s%s</p>', __('Price: %1', $this->_priceCurrency->convertAndFormat($result['price'])), __('Special Price: %1', $this->_priceCurrency->convertAndFormat($result['final_price'])), $special); } } $html .= '</td></tr></table>'; $rssObj->_addEntry(array('title' => $product->getName(), 'link' => $product->getProductUrl(), 'description' => $html)); } } return $rssObj->createRssXml(); }
/** * @dataProvider dataForMediaGallery */ public function testGetProductMediaGallery($mediaGallery, $image) { $this->productMock->expects($this->once())->method('getMediaAttributeValues')->willReturn($mediaGallery); $this->imageHelperMock->expects($this->any())->method('init')->willReturnMap([[$this->productMock, 'product_page_image_large', [], $this->imageHelperMock], [$this->productMock, 'product_page_image_medium', [], $this->imageHelperMock], [$this->productMock, 'product_page_image_small', [], $this->imageHelperMock]]); $this->imageHelperMock->expects($this->any())->method('setImageFile')->with($image)->willReturnSelf(); $this->imageHelperMock->expects($this->any())->method('constrainOnly')->willReturnSelf(); $this->imageHelperMock->expects($this->any())->method('keepAspectRatio')->willReturnSelf(); $this->imageHelperMock->expects($this->any())->method('keepFrame')->willReturnSelf(); $this->imageHelperMock->expects($this->any())->method('getUrl')->willReturn('http://full_path_to_image/magento1.png'); $mediaObject = $this->getMock('\\Magento\\Framework\\DataObject', [], [], '', false); $iterator = new \ArrayIterator([$mediaObject]); $mediaCollectionMock = $this->getMock('\\Magento\\Framework\\Data\\Collection', [], [], '', false); $mediaCollectionMock->expects($this->any())->method('getIterator')->willReturn($iterator); $mediaObject->method('getData')->withConsecutive(['value_id'], ['file'])->willReturnOnConsecutiveCalls(0, $image); $this->productMock->method('getMediaGalleryImages')->willReturn($mediaCollectionMock); $this->swatchHelperObject->getProductMediaGallery($this->productMock); }
/** * getItems method * * @return array */ public function getItems() { $result = []; $query = $this->queryFactory->get()->getQueryText(); $productIds = $this->searchProductsFullText($query); // Check if products are found if ($productIds) { $searchCriteria = $this->searchCriteriaBuilder->addFilter('entity_id', $productIds, 'in')->create(); $products = $this->productRepository->getList($searchCriteria); foreach ($products->getItems() as $product) { $image = $this->imageHelper->init($product, 'product_page_image_small')->getUrl(); $resultItem = $this->itemFactory->create(['title' => $product->getName(), 'price' => $this->priceCurrency->format($product->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue(), false), 'special_price' => $this->priceCurrency->format($product->getPriceInfo()->getPrice('special_price')->getAmount()->getValue(), false), 'has_special_price' => $product->getSpecialPrice() > 0 ? true : false, 'image' => $image, 'url' => $product->getProductUrl()]); $result[] = $resultItem; } } return $result; }