From d87e316a2d6ee55debe2e712359adba8cb70fdb2 Mon Sep 17 00:00:00 2001 From: Magento EngCom Team Date: Tue, 23 Jan 2018 10:57:06 -0600 Subject: [PATCH] :arrow_double_up: Forwardport of magento/magento2#11817 to 2.3-develop branch Applied pull request patch https://github.com/magento/magento2/pull/11817.patch (created by @p-bystritsky) based on commit(s): 1. 550434e94fa3b8d5ab5ece8064fee3ab539bf4f0 2. 566cc7fef2ab33d2b2ea296b2e4e8a83abf0838e 3. f0be8db3a5e35c8ab69748e0990790fc0fb146a0 4. de5990a2ef32135c2b774806373c45f5e5c43550 Fixed GitHub Issues in 2.3-develop branch: - magento/magento2#8970: Cannot assign products to categories not under tree root (reported by @marius-bica) --- .../Model/ProductScopeRewriteGenerator.php | 5 +- .../ProductScopeRewriteGeneratorTest.php | 68 ++++++++++++++----- .../_files/product_with_category.php | 2 +- 3 files changed, 55 insertions(+), 20 deletions(-) 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()]);