diff --git a/app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php b/app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php index d718c661fb2e8..7aef621a84bac 100644 --- a/app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php +++ b/app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php @@ -3,6 +3,9 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + +// @codingStandardsIgnoreFile + namespace Magento\Sitemap\Model\ResourceModel\Catalog; use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; @@ -13,6 +16,9 @@ * * @author Magento Core Team * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.LongVariable) + * @SuppressWarnings(PHPMD.CamelCasePropertyName) + * @SuppressWarnings(PHPMD.CamelCaseMethodName) */ class Product extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb { @@ -71,9 +77,20 @@ class Product extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb /** * @var \Magento\Catalog\Model\Product\Media\Config + * @deprecated unused */ protected $_mediaConfig; + /** + * @var \Magento\Catalog\Model\Product + */ + protected $productModel; + + /** + * @var \Magento\Catalog\Helper\Image + */ + protected $catalogImageHelper; + /** * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param \Magento\Sitemap\Helper\Data $sitemapData @@ -85,6 +102,8 @@ class Product extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb * @param \Magento\Catalog\Model\Product\Gallery\ReadHandler $mediaGalleryReadHandler * @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig * @param string $connectionName + * @param \Magento\Catalog\Model\Product $productModel + * @param \Magento\Catalog\Helper\Image $catalogImageHelper * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -97,7 +116,9 @@ public function __construct( \Magento\Catalog\Model\ResourceModel\Product\Gallery $mediaGalleryResourceModel, \Magento\Catalog\Model\Product\Gallery\ReadHandler $mediaGalleryReadHandler, \Magento\Catalog\Model\Product\Media\Config $mediaConfig, - $connectionName = null + $connectionName = null, + \Magento\Catalog\Model\Product $productModel = null, + \Magento\Catalog\Helper\Image $catalogImageHelper = null ) { $this->_productResource = $productResource; $this->_storeManager = $storeManager; @@ -107,6 +128,8 @@ public function __construct( $this->mediaGalleryReadHandler = $mediaGalleryReadHandler; $this->_mediaConfig = $mediaConfig; $this->_sitemapData = $sitemapData; + $this->productModel = $productModel ?: \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Catalog\Model\Product::class); + $this->catalogImageHelper = $catalogImageHelper ?: \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Catalog\Helper\Image::class); parent::__construct($context, $connectionName); } @@ -339,7 +362,7 @@ protected function _loadProductImages($product, $storeId) ) { $imagesCollection = [ new \Magento\Framework\DataObject( - ['url' => $this->_getMediaConfig()->getBaseMediaUrlAddition() . $product->getImage()] + ['url' => $this->getProductImageUrl($product->getImage())] ), ]; } @@ -348,7 +371,7 @@ protected function _loadProductImages($product, $storeId) // Determine thumbnail path $thumbnail = $product->getThumbnail(); if ($thumbnail && $product->getThumbnail() != self::NOT_SELECTED_IMAGE) { - $thumbnail = $this->_getMediaConfig()->getBaseMediaUrlAddition() . $thumbnail; + $thumbnail = $this->getProductImageUrl($thumbnail); } else { $thumbnail = $imagesCollection[0]->getUrl(); } @@ -378,11 +401,10 @@ protected function _getAllProductImages($product, $storeId) $imagesCollection = []; if ($gallery) { - $productMediaPath = $this->_getMediaConfig()->getBaseMediaUrlAddition(); foreach ($gallery as $image) { $imagesCollection[] = new \Magento\Framework\DataObject( [ - 'url' => $productMediaPath . $image['file'], + 'url' => $this->getProductImageUrl($image['file']), 'caption' => $image['label'] ? $image['label'] : $image['label_default'], ] ); @@ -396,9 +418,28 @@ protected function _getAllProductImages($product, $storeId) * Get media config * * @return \Magento\Catalog\Model\Product\Media\Config + * @deprecated No longer used, as we're getting full image URL from getProductImageUrl method + * @see getProductImageUrl() */ protected function _getMediaConfig() { return $this->_mediaConfig; } + + /** + * Get product image URL from image filename and path + * + * @param string $image + * @return mixed + */ + private function getProductImageUrl($image) + { + $productObject = $this->productModel; + $imgUrl = $this->catalogImageHelper + ->init($productObject, 'product_page_image_large') + ->setImageFile($image) + ->getUrl(); + + return $imgUrl; + } } diff --git a/app/code/Magento/Sitemap/Model/Sitemap.php b/app/code/Magento/Sitemap/Model/Sitemap.php index 13a90cc627769..ac93ceabc167e 100644 --- a/app/code/Magento/Sitemap/Model/Sitemap.php +++ b/app/code/Magento/Sitemap/Model/Sitemap.php @@ -27,6 +27,9 @@ * @method \Magento\Sitemap\Model\Sitemap setStoreId(int $value) * @SuppressWarnings(PHPMD.TooManyFields) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) + * @SuppressWarnings(PHPMD.CamelCasePropertyName) + * @SuppressWarnings(PHPMD.CamelCaseMethodName) */ class Sitemap extends \Magento\Framework\Model\AbstractModel { @@ -448,7 +451,7 @@ protected function _isSplitRequired($row) * @param null|string $lastmod * @param null|string $changefreq * @param null|string $priority - * @param null|array $images + * @param null|array|\Magento\Framework\DataObject $images * @return string * Sitemap images * @see http://support.google.com/webmasters/bin/answer.py?hl=en&answer=178636 @@ -473,7 +476,7 @@ protected function _getSitemapRow($url, $lastmod = null, $changefreq = null, $pr // Add Images to sitemap foreach ($images->getCollection() as $image) { $row .= ''; - $row .= '' . htmlspecialchars($this->_getMediaUrl($image->getUrl())) . ''; + $row .= '' . htmlspecialchars($image->getUrl()) . ''; $row .= '' . htmlspecialchars($images->getTitle()) . ''; if ($image->getCaption()) { $row .= '' . htmlspecialchars($image->getCaption()) . ''; @@ -483,9 +486,7 @@ protected function _getSitemapRow($url, $lastmod = null, $changefreq = null, $pr // Add PageMap image for Google web search $row .= ''; $row .= ''; - $row .= ''; + $row .= ''; $row .= ''; } @@ -591,6 +592,7 @@ protected function _getBaseDir() */ protected function _getStoreBaseUrl($type = \Magento\Framework\UrlInterface::URL_TYPE_LINK) { + /** @var \Magento\Store\Model\Store $store */ $store = $this->_storeManager->getStore($this->getStoreId()); $isSecure = $store->isUrlSecure(); return rtrim($store->getBaseUrl($type, $isSecure), '/') . '/'; @@ -613,6 +615,8 @@ protected function _getUrl($url, $type = \Magento\Framework\UrlInterface::URL_TY * * @param string $url * @return string + * @deprecated No longer used, as we're generating product image URLs inside collection instead + * @see \Magento\Sitemap\Model\ResourceModel\Catalog\Product::_loadProductImages() */ protected function _getMediaUrl($url) { diff --git a/app/code/Magento/Sitemap/Test/Unit/Model/SitemapTest.php b/app/code/Magento/Sitemap/Test/Unit/Model/SitemapTest.php index 241e394187147..fb1379ecfca28 100644 --- a/app/code/Magento/Sitemap/Test/Unit/Model/SitemapTest.php +++ b/app/code/Magento/Sitemap/Test/Unit/Model/SitemapTest.php @@ -535,6 +535,8 @@ protected function _getModelMock($mockBeforeSave = false) ] ) ); + + $storeBaseMediaUrl = 'http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/'; $this->_sitemapProductMock->expects( $this->any() )->method( @@ -553,13 +555,16 @@ protected function _getModelMock($mockBeforeSave = false) [ 'collection' => [ new \Magento\Framework\DataObject( - ['url' => 'image1.png', 'caption' => 'caption & > title < "'] + [ + 'url' => $storeBaseMediaUrl.'i/m/image1.png', + 'caption' => 'caption & > title < "' + ] ), new \Magento\Framework\DataObject( - ['url' => 'image_no_caption.png', 'caption' => null] + ['url' => $storeBaseMediaUrl.'i/m/image_no_caption.png', 'caption' => null] ), ], - 'thumbnail' => 'thumbnail.jpg', + 'thumbnail' => $storeBaseMediaUrl.'t/h/thumbnail.jpg', 'title' => 'Product & > title < "', ] ), diff --git a/app/code/Magento/Sitemap/Test/Unit/Model/_files/sitemap-1-4.xml b/app/code/Magento/Sitemap/Test/Unit/Model/_files/sitemap-1-4.xml index e9fd2b00a909c..7111154efbf85 100644 --- a/app/code/Magento/Sitemap/Test/Unit/Model/_files/sitemap-1-4.xml +++ b/app/code/Magento/Sitemap/Test/Unit/Model/_files/sitemap-1-4.xml @@ -14,18 +14,18 @@ monthly 0.5 - http://store.com/image1.png + http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/i/m/image1.png Product & > title < " caption & > title < " - http://store.com/image_no_caption.png + http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/i/m/image_no_caption.png Product & > title < " - + diff --git a/app/code/Magento/Sitemap/Test/Unit/Model/_files/sitemap-single.xml b/app/code/Magento/Sitemap/Test/Unit/Model/_files/sitemap-single.xml index d4801a014aabc..e79e022c98995 100644 --- a/app/code/Magento/Sitemap/Test/Unit/Model/_files/sitemap-single.xml +++ b/app/code/Magento/Sitemap/Test/Unit/Model/_files/sitemap-single.xml @@ -32,18 +32,18 @@ monthly 0.5 - http://store.com/image1.png + http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/i/m/image1.png Product & > title < " caption & > title < " - http://store.com/image_no_caption.png + http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/i/m/image_no_caption.png Product & > title < " - + diff --git a/dev/tests/integration/testsuite/Magento/Sitemap/Model/ResourceModel/Catalog/ProductTest.php b/dev/tests/integration/testsuite/Magento/Sitemap/Model/ResourceModel/Catalog/ProductTest.php index 62b97e54bf0f0..e4ab0aa99fa44 100644 --- a/dev/tests/integration/testsuite/Magento/Sitemap/Model/ResourceModel/Catalog/ProductTest.php +++ b/dev/tests/integration/testsuite/Magento/Sitemap/Model/ResourceModel/Catalog/ProductTest.php @@ -14,6 +14,11 @@ */ class ProductTest extends \PHPUnit_Framework_TestCase { + /** + * Base product image path + */ + const BASE_IMAGE_PATH = 'http://localhost/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff'; + /** * Test getCollection None images * 1) Check that image attributes were not loaded @@ -72,7 +77,7 @@ public function testGetCollectionAll() $this->assertNotEmpty($products[4]->getImages(), 'Images were not loaded'); $this->assertEquals('Simple Images', $products[4]->getImages()->getTitle(), 'Incorrect title'); $this->assertEquals( - 'catalog/product/m/a/magento_image_sitemap.png', + self::BASE_IMAGE_PATH.'/m/a/magento_image_sitemap.png', $products[4]->getImages()->getThumbnail(), 'Incorrect thumbnail' ); @@ -80,12 +85,12 @@ public function testGetCollectionAll() $imagesCollection = $products[4]->getImages()->getCollection(); $this->assertEquals( - 'catalog/product/m/a/magento_image_sitemap.png', + self::BASE_IMAGE_PATH.'/m/a/magento_image_sitemap.png', $imagesCollection[0]->getUrl(), 'Incorrect image url' ); $this->assertEquals( - 'catalog/product/s/e/second_image.png', + self::BASE_IMAGE_PATH.'/s/e/second_image.png', $imagesCollection[1]->getUrl(), 'Incorrect image url' ); @@ -97,12 +102,12 @@ public function testGetCollectionAll() $imagesCollection = $products[5]->getImages()->getCollection(); $this->assertCount(1, $imagesCollection); $this->assertEquals( - 'catalog/product/s/e/second_image_1.png', + self::BASE_IMAGE_PATH.'/s/e/second_image_1.png', $imagesCollection[0]->getUrl(), 'Image url is incorrect' ); $this->assertEquals( - 'catalog/product/s/e/second_image_1.png', + self::BASE_IMAGE_PATH.'/s/e/second_image_1.png', $products[5]->getImages()->getThumbnail(), 'Product thumbnail is incorrect' ); @@ -140,7 +145,7 @@ public function testGetCollectionBase() $this->assertNotEmpty($products[4]->getImages(), 'Images were not loaded'); $this->assertEquals('Simple Images', $products[4]->getImages()->getTitle(), 'Incorrect title'); $this->assertEquals( - 'catalog/product/s/e/second_image.png', + self::BASE_IMAGE_PATH.'/s/e/second_image.png', $products[4]->getImages()->getThumbnail(), 'Incorrect thumbnail' ); @@ -148,7 +153,7 @@ public function testGetCollectionBase() $imagesCollection = $products[4]->getImages()->getCollection(); $this->assertEquals( - 'catalog/product/s/e/second_image.png', + self::BASE_IMAGE_PATH.'/s/e/second_image.png', $imagesCollection[0]->getUrl(), 'Incorrect image url' );