diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php index 81bbb08b0f39e..9c5c37b51f0b2 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php @@ -207,8 +207,9 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ */ public function isCategoryProperForGenerating(Category $category, $storeId) { - if ($category->getParentId() != \Magento\Catalog\Model\Category::TREE_ROOT_ID) { - list(, $rootCategoryId) = $category->getParentIds(); + $parentIds = $category->getParentIds(); + if (count($parentIds) >= 2) { + $rootCategoryId = $parentIds[1]; return $rootCategoryId == $this->storeManager->getStore($storeId)->getRootCategoryId(); } return false; diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php index 48399d5ef612b..06be01445df4c 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php @@ -47,6 +47,9 @@ class ProductScopeRewriteGeneratorTest extends \PHPUnit\Framework\TestCase /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */ private $serializer; + /** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */ + private $categoryMock; + public function setUp() { $this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class); @@ -83,6 +86,10 @@ function ($value) { $this->storeViewService = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class) ->disableOriginalConstructor()->getMock(); $this->storeManager = $this->createMock(StoreManagerInterface::class); + $storeRootCategoryId = 2; + $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock(); + $store->expects($this->any())->method('getRootCategoryId')->will($this->returnValue($storeRootCategoryId)); + $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store)); $mergeDataProviderFactory = $this->createPartialMock( \Magento\UrlRewrite\Model\MergeDataProviderFactory::class, ['create'] @@ -103,6 +110,7 @@ function ($value) { 'mergeDataProviderFactory' => $mergeDataProviderFactory ] ); + $this->categoryMock = $this->getMockBuilder(Category::class)->disableOriginalConstructor()->getMock(); } public function testGenerationForGlobalScope() @@ -112,12 +120,6 @@ public function testGenerationForGlobalScope() $product->expects($this->any())->method('getStoreIds')->will($this->returnValue([1])); $this->storeViewService->expects($this->once())->method('doesEntityHaveOverriddenUrlKeyForStore') ->will($this->returnValue(false)); - $categoryMock = $this->getMockBuilder(Category::class) - ->disableOriginalConstructor() - ->getMock(); - $categoryMock->expects($this->once()) - ->method('getParentId') - ->willReturn(1); $this->initObjectRegistryFactory([]); $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer); $canonical->setRequestPath('category-1') @@ -149,25 +151,21 @@ public function testGenerationForGlobalScope() 'category-3_3' => $current, 'category-4_4' => $anchorCategories ], - $this->productScopeGenerator->generateForGlobalScope([$categoryMock], $product, 1) + $this->productScopeGenerator->generateForGlobalScope([$this->categoryMock], $product, 1) ); } public function testGenerationForSpecificStore() { + $storeRootCategoryId = 2; + $category_id = 4; $product = $this->createMock(\Magento\Catalog\Model\Product::class); $product->expects($this->any())->method('getStoreId')->will($this->returnValue(1)); $product->expects($this->never())->method('getStoreIds'); - $storeRootCategoryId = 'root-for-store-id'; - $category = $this->createMock(\Magento\Catalog\Model\Category::class); - $category->expects($this->any())->method('getParentIds') + $this->categoryMock->expects($this->any())->method('getParentIds') ->will($this->returnValue(['root-id', $storeRootCategoryId])); - $category->expects($this->any())->method('getParentId')->will($this->returnValue('parent_id')); - $category->expects($this->any())->method('getId')->will($this->returnValue('category_id')); - $store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock(); - $store->expects($this->any())->method('getRootCategoryId')->will($this->returnValue($storeRootCategoryId)); - $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store)); - $this->initObjectRegistryFactory([$category]); + $this->categoryMock->expects($this->any())->method('getId')->will($this->returnValue($category_id)); + $this->initObjectRegistryFactory([$this->categoryMock]); $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer); $canonical->setRequestPath('category-1') ->setStoreId(1); @@ -184,7 +182,7 @@ public function testGenerationForSpecificStore() $this->assertEquals( ['category-1_1' => $canonical], - $this->productScopeGenerator->generateForSpecificStoreView(1, [$category], $product, 1) + $this->productScopeGenerator->generateForSpecificStoreView(1, [$this->categoryMock], $product, 1) ); } @@ -212,4 +210,40 @@ protected function initObjectRegistryFactory($entities) ->with(['entities' => $entities]) ->will($this->returnValue($objectRegistry)); } + + /** + * Test the possibility of url rewrite generation. + * + * @param array $parentIds + * @param bool $expectedResult + * @dataProvider isCategoryProperForGeneratingDataProvider + */ + public function testIsCategoryProperForGenerating($parentIds, $expectedResult) + { + $storeId = 1; + $this->categoryMock->expects(self::any())->method('getParentIds')->willReturn($parentIds); + $result = $this->productScopeGenerator->isCategoryProperForGenerating( + $this->categoryMock, + $storeId + ); + self::assertEquals( + $expectedResult, + $result + ); + } + + /** + * Data provider for testIsCategoryProperForGenerating. + * + * @return array + */ + public function isCategoryProperForGeneratingDataProvider() + { + return [ + [['0'], false], + [['1'], false], + [['1', '2'], true], + [['1', '3'], false], + ]; + } } diff --git a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category.php b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category.php index 2dc1713a5bb0d..aa753a3509987 100644 --- a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category.php +++ b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category.php @@ -79,4 +79,4 @@ /** @var CategoryLinkManagementInterface $linkManagement */ $linkManagement = $objectManager->get(CategoryLinkManagementInterface::class); -$linkManagement->assignProductToCategories($product->getSku(), [$category->getEntityId()]); +$linkManagement->assignProductToCategories($product->getSku(), [Category::TREE_ROOT_ID, $category->getEntityId()]);