Skip to content

Commit de5990a

Browse files
committed
GITHUB-8970: Cannot assign products to categories not under tree root.
1 parent f0be8db commit de5990a

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

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

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

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ public function testGenerationForGlobalScope()
120120
$product->expects($this->any())->method('getStoreIds')->will($this->returnValue([1]));
121121
$this->storeViewService->expects($this->once())->method('doesEntityHaveOverriddenUrlKeyForStore')
122122
->will($this->returnValue(false));
123-
$this->categoryMock->expects($this->once())
124-
->method('getParentId')
125-
->willReturn(2);
126123
$this->initObjectRegistryFactory([]);
127124
$canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer);
128125
$canonical->setRequestPath('category-1')
@@ -161,14 +158,12 @@ public function testGenerationForGlobalScope()
161158
public function testGenerationForSpecificStore()
162159
{
163160
$storeRootCategoryId = 2;
164-
$parent_id = 3;
165161
$category_id = 4;
166162
$product = $this->createMock(\Magento\Catalog\Model\Product::class);
167163
$product->expects($this->any())->method('getStoreId')->will($this->returnValue(1));
168164
$product->expects($this->never())->method('getStoreIds');
169165
$this->categoryMock->expects($this->any())->method('getParentIds')
170166
->will($this->returnValue(['root-id', $storeRootCategoryId]));
171-
$this->categoryMock->expects($this->any())->method('getParentId')->will($this->returnValue($parent_id));
172167
$this->categoryMock->expects($this->any())->method('getId')->will($this->returnValue($category_id));
173168
$this->initObjectRegistryFactory([$this->categoryMock]);
174169
$canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer);
@@ -219,15 +214,13 @@ protected function initObjectRegistryFactory($entities)
219214
/**
220215
* Test the possibility of url rewrite generation.
221216
*
222-
* @param int $parentId
223217
* @param array $parentIds
224218
* @param bool $expectedResult
225219
* @dataProvider isCategoryProperForGeneratingDataProvider
226220
*/
227-
public function testIsCategoryProperForGenerating($parentId, $parentIds, $expectedResult)
221+
public function testIsCategoryProperForGenerating($parentIds, $expectedResult)
228222
{
229223
$storeId = 1;
230-
$this->categoryMock->expects(self::any())->method('getParentId')->willReturn($parentId);
231224
$this->categoryMock->expects(self::any())->method('getParentIds')->willReturn($parentIds);
232225
$result = $this->productScopeGenerator->isCategoryProperForGenerating(
233226
$this->categoryMock,
@@ -247,10 +240,10 @@ public function testIsCategoryProperForGenerating($parentId, $parentIds, $expect
247240
public function isCategoryProperForGeneratingDataProvider()
248241
{
249242
return [
250-
['0', ['0'], false],
251-
['1', ['1'], false],
252-
['2', ['1', '2'], true],
253-
['2', ['1', '3'], false],
243+
[['0'], false],
244+
[['1'], false],
245+
[['1', '2'], true],
246+
[['1', '3'], false],
254247
];
255248
}
256249
}

0 commit comments

Comments
 (0)