Skip to content

Commit dcb00cc

Browse files
committed
MAGETWO-87526: URL rewrite for store is not created for child if parent category has non-default URL key on store
1 parent 010adf8 commit dcb00cc

File tree

2 files changed

+173
-2
lines changed
  • app/code/Magento/CatalogUrlRewrite

2 files changed

+173
-2
lines changed

app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Save.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function aroundSave(
7878
&& !$category->isInRootCategoryList()
7979
&& !empty($parentCategoryId)) {
8080
foreach ($category->getStoreIds() as $storeId) {
81-
if ($storeId != $this->isGlobalScope($currentStoreId)
81+
if (!$this->isGlobalScope($storeId)
8282
&& $this->storeViewService->doesEntityHaveOverriddenUrlPathForStore(
8383
$storeId,
8484
$parentCategoryId,
@@ -89,7 +89,6 @@ public function aroundSave(
8989
$this->urlPersist->replace($this->categoryUrlRewriteGenerator->generate($category));
9090
}
9191
}
92-
$category->setStoreId($currentStoreId);
9392
}
9493
return $result;
9594
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category\Plugin\Category;
7+
8+
/**
9+
* Unit test for Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Save class
10+
*/
11+
class SaveTest extends \PHPUnit\Framework\TestCase
12+
{
13+
/**
14+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
15+
*/
16+
private $objectManager;
17+
18+
/**
19+
* @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
private $categoryUrlPathGenerator;
22+
23+
/**
24+
* @var \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $categoryUrlRewriteGenerator;
27+
28+
/**
29+
* @var \Magento\CatalogUrlRewrite\Service\V1\StoreViewService|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $storeViewService;
32+
33+
/**
34+
* @var \Magento\UrlRewrite\Model\UrlPersistInterface|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
private $urlPersist;
37+
38+
/**
39+
* @var \Magento\Catalog\Model\ResourceModel\Category|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
private $categoryResource;
42+
43+
/**
44+
* @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
private $category;
47+
48+
/**
49+
* @var \Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Save
50+
*/
51+
private $categorySavePlugin;
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
protected function setUp()
57+
{
58+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
59+
$this->categoryUrlPathGenerator = $this->getMockBuilder(
60+
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::class
61+
)
62+
->disableOriginalConstructor()
63+
->setMethods(['getUrlPath'])
64+
->getMock();
65+
$this->categoryUrlRewriteGenerator = $this->getMockBuilder(
66+
\Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator::class
67+
)
68+
->disableOriginalConstructor()
69+
->setMethods(['generate'])
70+
->getMock();
71+
$this->categoryResource = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Category::class)
72+
->disableOriginalConstructor()
73+
->setMethods(['saveAttribute'])
74+
->getMock();
75+
$this->category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
76+
->disableOriginalConstructor()
77+
->setMethods(
78+
[
79+
'getStoreId',
80+
'getParentId',
81+
'isObjectNew',
82+
'isInRootCategoryList',
83+
'getStoreIds',
84+
'setStoreId',
85+
'unsUrlPath',
86+
'setUrlPath'
87+
]
88+
)
89+
->getMock();
90+
$this->storeViewService = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Service\V1\StoreViewService::class)
91+
->disableOriginalConstructor()
92+
->setMethods(['doesEntityHaveOverriddenUrlPathForStore'])
93+
->getMock();
94+
$this->urlPersist = $this->getMockBuilder(\Magento\UrlRewrite\Model\UrlPersistInterface::class)
95+
->disableOriginalConstructor()
96+
->setMethods(['replace'])
97+
->getMockForAbstractClass();
98+
99+
$this->categorySavePlugin = $this->objectManager->getObject(
100+
\Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Save::class,
101+
[
102+
'categoryUrlPathGenerator' => $this->categoryUrlPathGenerator,
103+
'categoryUrlRewriteGenerator' => $this->categoryUrlRewriteGenerator,
104+
'urlPersist' => $this->urlPersist,
105+
'storeViewService' => $this->storeViewService
106+
]
107+
);
108+
}
109+
110+
public function testAroundSaveWithoutRootCategory()
111+
{
112+
$proceed = function () {
113+
return $this->categoryResource;
114+
};
115+
116+
$this->category->expects($this->atLeastOnce())->method('getStoreId')->willReturn(0);
117+
$this->category->expects($this->atLeastOnce())->method('getParentId')->willReturn(0);
118+
$this->category->expects($this->atLeastOnce())->method('isObjectNew')->willReturn(true);
119+
$this->category->expects($this->atLeastOnce())->method('isInRootCategoryList')->willReturn(false);
120+
$this->category->expects($this->never())->method('getStoreIds');
121+
122+
$this->assertEquals(
123+
$this->categoryResource,
124+
$this->categorySavePlugin->aroundSave($this->categoryResource, $proceed, $this->category)
125+
);
126+
}
127+
128+
public function testAroundSaveWithRootCategory()
129+
{
130+
$parentId = 1;
131+
$categoryStoreIds = [0,1,2];
132+
$generatedUrlPath = 'parent_category/child_category';
133+
$proceed = function () {
134+
return $this->categoryResource;
135+
};
136+
137+
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')->with($this->category)
138+
->willReturn($generatedUrlPath);
139+
$this->category->expects($this->atLeastOnce())->method('getStoreId')->willReturn(0);
140+
$this->category->expects($this->atLeastOnce())->method('getParentId')->willReturn($parentId);
141+
$this->category->expects($this->atLeastOnce())->method('isObjectNew')->willReturn(true);
142+
$this->category->expects($this->atLeastOnce())->method('isInRootCategoryList')->willReturn(false);
143+
$this->category->expects($this->atLeastOnce())->method('getStoreIds')->willReturn($categoryStoreIds);
144+
$this->category->expects($this->once())->method('setStoreId')->with($categoryStoreIds[2])->willReturnSelf();
145+
$this->category->expects($this->once())->method('unsUrlPath')->willReturnSelf();
146+
$this->category->expects($this->once())->method('setUrlPath')->with($generatedUrlPath)->willReturnSelf();
147+
$this->storeViewService->expects($this->exactly(2))->method('doesEntityHaveOverriddenUrlPathForStore')
148+
->willReturnMap(
149+
[
150+
[
151+
$categoryStoreIds[1], $parentId, 'catalog_category', false
152+
],
153+
[
154+
$categoryStoreIds[2], $parentId, 'catalog_category', true
155+
]
156+
]
157+
);
158+
$this->categoryResource->expects($this->once())->method('saveAttribute')->with($this->category, 'url_path')
159+
->willReturnSelf();
160+
$generatedUrlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
161+
->disableOriginalConstructor()
162+
->getMock();
163+
$this->categoryUrlRewriteGenerator->expects($this->once())->method('generate')->with($this->category)
164+
->willReturn([$generatedUrlRewrite]);
165+
$this->urlPersist->expects($this->once())->method('replace')->with([$generatedUrlRewrite])->willReturnSelf();
166+
167+
$this->assertEquals(
168+
$this->categoryResource,
169+
$this->categorySavePlugin->aroundSave($this->categoryResource, $proceed, $this->category)
170+
);
171+
}
172+
}

0 commit comments

Comments
 (0)