diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/Plugin/Import.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/Plugin/Import.php index 62adfeb58d529..39be81d852264 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Product/Plugin/Import.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/Plugin/Import.php @@ -320,12 +320,14 @@ protected function canonicalUrlRewriteGenerate() $urls = []; foreach ($this->products as $productId => $productsByStores) { foreach ($productsByStores as $storeId => $product) { - $urls[] = $this->urlRewriteFactory->create() - ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) - ->setEntityId($productId) - ->setRequestPath($this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId)) - ->setTargetPath($this->productUrlPathGenerator->getCanonicalUrlPath($product)) - ->setStoreId($storeId); + if ($this->productUrlPathGenerator->getUrlPath($product)) { + $urls[] = $this->urlRewriteFactory->create() + ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) + ->setEntityId($productId) + ->setRequestPath($this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId)) + ->setTargetPath($this->productUrlPathGenerator->getCanonicalUrlPath($product)) + ->setStoreId($storeId); + } } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/Plugin/ImportTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/Plugin/ImportTest.php index b13bf56a5eaf3..3a1d4bc4573b5 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/Plugin/ImportTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/Plugin/ImportTest.php @@ -683,7 +683,7 @@ public function testClearProductUrls() /** * Cover canonicalUrlRewriteGenerate(). */ - public function testCanonicalUrlRewriteGenerate() + public function testCanonicalUrlRewriteGenerateWithUrlPath() { $productId = 'product_id'; $requestPath = 'simple-product.html'; @@ -704,6 +704,10 @@ public function testCanonicalUrlRewriteGenerate() ->expects($this->once()) ->method('getUrlPathWithSuffix') ->will($this->returnValue($requestPath)); + $this->productUrlPathGenerator + ->expects($this->once()) + ->method('getUrlPath') + ->will($this->returnValue('urlPath')); $this->productUrlPathGenerator ->expects($this->once()) ->method('getCanonicalUrlPath') @@ -747,6 +751,36 @@ public function testCanonicalUrlRewriteGenerate() ); } + /** + * Cover canonicalUrlRewriteGenerate(). + */ + public function testCanonicalUrlRewriteGenerateWithEmptyUrlPath() + { + $productId = 'product_id'; + $storeId = 10; + $product = $this + ->getMockBuilder('Magento\Catalog\Model\Product') + ->disableOriginalConstructor() + ->getMock(); + $productsByStores = [$storeId => $product]; + $products = [ + $productId => $productsByStores, + ]; + + $this->setPropertyValue($this->import, 'products', $products); + + $this->productUrlPathGenerator + ->expects($this->once()) + ->method('getUrlPath') + ->will($this->returnValue('')); + $this->urlRewriteFactory + ->expects($this->never()) + ->method('create'); + + $actualResult = $this->invokeMethod($this->import, 'canonicalUrlRewriteGenerate'); + $this->assertEquals([], $actualResult); + } + /** * Cover categoriesUrlRewriteGenerate(). */