Skip to content

Commit b22b232

Browse files
Merge forwardport of #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. 550434e 2. 566cc7f 3. f0be8db 4. de5990a Fixed GitHub Issues in 2.3-develop branch: - #8970: Cannot assign products to categories not under tree root (reported by @marius-bica)
2 parents d37ce08 + d87e316 commit b22b232

File tree

3 files changed

+55
-20
lines changed

3 files changed

+55
-20
lines changed

app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,9 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ
207207
*/
208208
public function isCategoryProperForGenerating(Category $category, $storeId)
209209
{
210-
if ($category->getParentId() != \Magento\Catalog\Model\Category::TREE_ROOT_ID) {
211-
list(, $rootCategoryId) = $category->getParentIds();
210+
$parentIds = $category->getParentIds();
211+
if (count($parentIds) >= 2) {
212+
$rootCategoryId = $parentIds[1];
212213
return $rootCategoryId == $this->storeManager->getStore($storeId)->getRootCategoryId();
213214
}
214215
return false;

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class ProductScopeRewriteGeneratorTest extends \PHPUnit\Framework\TestCase
4747
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
4848
private $serializer;
4949

50+
/** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */
51+
private $categoryMock;
52+
5053
public function setUp()
5154
{
5255
$this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
@@ -83,6 +86,10 @@ function ($value) {
8386
$this->storeViewService = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class)
8487
->disableOriginalConstructor()->getMock();
8588
$this->storeManager = $this->createMock(StoreManagerInterface::class);
89+
$storeRootCategoryId = 2;
90+
$store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
91+
$store->expects($this->any())->method('getRootCategoryId')->will($this->returnValue($storeRootCategoryId));
92+
$this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
8693
$mergeDataProviderFactory = $this->createPartialMock(
8794
\Magento\UrlRewrite\Model\MergeDataProviderFactory::class,
8895
['create']
@@ -103,6 +110,7 @@ function ($value) {
103110
'mergeDataProviderFactory' => $mergeDataProviderFactory
104111
]
105112
);
113+
$this->categoryMock = $this->getMockBuilder(Category::class)->disableOriginalConstructor()->getMock();
106114
}
107115

108116
public function testGenerationForGlobalScope()
@@ -112,12 +120,6 @@ public function testGenerationForGlobalScope()
112120
$product->expects($this->any())->method('getStoreIds')->will($this->returnValue([1]));
113121
$this->storeViewService->expects($this->once())->method('doesEntityHaveOverriddenUrlKeyForStore')
114122
->will($this->returnValue(false));
115-
$categoryMock = $this->getMockBuilder(Category::class)
116-
->disableOriginalConstructor()
117-
->getMock();
118-
$categoryMock->expects($this->once())
119-
->method('getParentId')
120-
->willReturn(1);
121123
$this->initObjectRegistryFactory([]);
122124
$canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer);
123125
$canonical->setRequestPath('category-1')
@@ -149,25 +151,21 @@ public function testGenerationForGlobalScope()
149151
'category-3_3' => $current,
150152
'category-4_4' => $anchorCategories
151153
],
152-
$this->productScopeGenerator->generateForGlobalScope([$categoryMock], $product, 1)
154+
$this->productScopeGenerator->generateForGlobalScope([$this->categoryMock], $product, 1)
153155
);
154156
}
155157

156158
public function testGenerationForSpecificStore()
157159
{
160+
$storeRootCategoryId = 2;
161+
$category_id = 4;
158162
$product = $this->createMock(\Magento\Catalog\Model\Product::class);
159163
$product->expects($this->any())->method('getStoreId')->will($this->returnValue(1));
160164
$product->expects($this->never())->method('getStoreIds');
161-
$storeRootCategoryId = 'root-for-store-id';
162-
$category = $this->createMock(\Magento\Catalog\Model\Category::class);
163-
$category->expects($this->any())->method('getParentIds')
165+
$this->categoryMock->expects($this->any())->method('getParentIds')
164166
->will($this->returnValue(['root-id', $storeRootCategoryId]));
165-
$category->expects($this->any())->method('getParentId')->will($this->returnValue('parent_id'));
166-
$category->expects($this->any())->method('getId')->will($this->returnValue('category_id'));
167-
$store = $this->getMockBuilder(\Magento\Store\Model\Store::class)->disableOriginalConstructor()->getMock();
168-
$store->expects($this->any())->method('getRootCategoryId')->will($this->returnValue($storeRootCategoryId));
169-
$this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
170-
$this->initObjectRegistryFactory([$category]);
167+
$this->categoryMock->expects($this->any())->method('getId')->will($this->returnValue($category_id));
168+
$this->initObjectRegistryFactory([$this->categoryMock]);
171169
$canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer);
172170
$canonical->setRequestPath('category-1')
173171
->setStoreId(1);
@@ -184,7 +182,7 @@ public function testGenerationForSpecificStore()
184182

185183
$this->assertEquals(
186184
['category-1_1' => $canonical],
187-
$this->productScopeGenerator->generateForSpecificStoreView(1, [$category], $product, 1)
185+
$this->productScopeGenerator->generateForSpecificStoreView(1, [$this->categoryMock], $product, 1)
188186
);
189187
}
190188

@@ -212,4 +210,40 @@ protected function initObjectRegistryFactory($entities)
212210
->with(['entities' => $entities])
213211
->will($this->returnValue($objectRegistry));
214212
}
213+
214+
/**
215+
* Test the possibility of url rewrite generation.
216+
*
217+
* @param array $parentIds
218+
* @param bool $expectedResult
219+
* @dataProvider isCategoryProperForGeneratingDataProvider
220+
*/
221+
public function testIsCategoryProperForGenerating($parentIds, $expectedResult)
222+
{
223+
$storeId = 1;
224+
$this->categoryMock->expects(self::any())->method('getParentIds')->willReturn($parentIds);
225+
$result = $this->productScopeGenerator->isCategoryProperForGenerating(
226+
$this->categoryMock,
227+
$storeId
228+
);
229+
self::assertEquals(
230+
$expectedResult,
231+
$result
232+
);
233+
}
234+
235+
/**
236+
* Data provider for testIsCategoryProperForGenerating.
237+
*
238+
* @return array
239+
*/
240+
public function isCategoryProperForGeneratingDataProvider()
241+
{
242+
return [
243+
[['0'], false],
244+
[['1'], false],
245+
[['1', '2'], true],
246+
[['1', '3'], false],
247+
];
248+
}
215249
}

dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/product_with_category.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@
7979

8080
/** @var CategoryLinkManagementInterface $linkManagement */
8181
$linkManagement = $objectManager->get(CategoryLinkManagementInterface::class);
82-
$linkManagement->assignProductToCategories($product->getSku(), [$category->getEntityId()]);
82+
$linkManagement->assignProductToCategories($product->getSku(), [Category::TREE_ROOT_ID, $category->getEntityId()]);

0 commit comments

Comments
 (0)