Skip to content

Commit 71e8f77

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 f961681 commit 71e8f77

File tree

4 files changed

+28
-42
lines changed

4 files changed

+28
-42
lines changed

app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Save.php renamed to app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/UpdateUrlPath.php

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
use \Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
1212
use \Magento\UrlRewrite\Model\UrlPersistInterface;
1313
use \Magento\Store\Model\Store;
14+
use \Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
1415

1516
/**
1617
* Generate and save url-rewrites for category if its parent have specified url-key for different store views
1718
*/
18-
class Save
19+
class UpdateUrlPath
1920
{
2021
/**
2122
* @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator
@@ -58,34 +59,31 @@ public function __construct(
5859
/**
5960
* Perform url updating for different stores
6061
*
61-
* @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource
62-
* @param \Closure $proceed
63-
* @param \Magento\Catalog\Model\Category $category
64-
* @return \Magento\Catalog\Model\Category
62+
* @param CategoryResource $subject
63+
* @param CategoryResource $result
64+
* @param Category $category
65+
* @return CategoryResource
6566
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6667
*/
67-
public function aroundSave(
68-
\Magento\Catalog\Model\ResourceModel\Category $categoryResource,
69-
\Closure $proceed,
70-
\Magento\Catalog\Model\Category $category
68+
public function afterSave(
69+
CategoryResource $subject,
70+
CategoryResource $result,
71+
Category $category
7172
) {
72-
$result = $proceed($category);
73-
74-
$currentStoreId = $category->getStoreId();
7573
$parentCategoryId = $category->getParentId();
7674
if ($category->isObjectNew()
77-
&& $this->isGlobalScope($currentStoreId)
7875
&& !$category->isInRootCategoryList()
7976
&& !empty($parentCategoryId)) {
8077
foreach ($category->getStoreIds() as $storeId) {
8178
if (!$this->isGlobalScope($storeId)
8279
&& $this->storeViewService->doesEntityHaveOverriddenUrlPathForStore(
83-
$storeId,
84-
$parentCategoryId,
85-
Category::ENTITY
86-
)) {
80+
$storeId,
81+
$parentCategoryId,
82+
Category::ENTITY
83+
)
84+
) {
8785
$category->setStoreId($storeId);
88-
$this->updateUrlPathForCategory($category, $categoryResource);
86+
$this->updateUrlPathForCategory($category, $subject);
8987
$this->urlPersist->replace($this->categoryUrlRewriteGenerator->generate($category));
9088
}
9189
}
@@ -99,18 +97,16 @@ public function aroundSave(
9997
* @param int|null $storeId
10098
* @return bool
10199
*/
102-
private function isGlobalScope($storeId)
100+
private function isGlobalScope(int $storeId): bool
103101
{
104-
return null === $storeId || $storeId == Store::DEFAULT_STORE_ID;
102+
return null === $storeId || $storeId === Store::DEFAULT_STORE_ID;
105103
}
106104

107105
/**
108106
* @param Category $category
109107
* @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource
110-
*
111-
* @return void
112108
*/
113-
private function updateUrlPathForCategory(Category $category, $categoryResource)
109+
private function updateUrlPathForCategory(Category $category, CategoryResource $categoryResource)
114110
{
115111
$category->unsUrlPath();
116112
$category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/SaveTest.php renamed to app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/UpdateUrlPathTest.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category\Plugin\Category;
77

88
/**
9-
* Unit test for Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Save class
9+
* Unit test for Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\UpdateUrlPath class
1010
*/
11-
class SaveTest extends \PHPUnit\Framework\TestCase
11+
class UpdateUrlPathTest extends \PHPUnit\Framework\TestCase
1212
{
1313
/**
1414
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
@@ -46,9 +46,9 @@ class SaveTest extends \PHPUnit\Framework\TestCase
4646
private $category;
4747

4848
/**
49-
* @var \Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Save
49+
* @var \Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\UpdateUrlPath
5050
*/
51-
private $categorySavePlugin;
51+
private $updateUrlPathPlugin;
5252

5353
/**
5454
* @inheritdoc
@@ -96,8 +96,8 @@ protected function setUp()
9696
->setMethods(['replace'])
9797
->getMockForAbstractClass();
9898

99-
$this->categorySavePlugin = $this->objectManager->getObject(
100-
\Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Save::class,
99+
$this->updateUrlPathPlugin = $this->objectManager->getObject(
100+
\Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\UpdateUrlPath::class,
101101
[
102102
'categoryUrlPathGenerator' => $this->categoryUrlPathGenerator,
103103
'categoryUrlRewriteGenerator' => $this->categoryUrlRewriteGenerator,
@@ -109,19 +109,14 @@ protected function setUp()
109109

110110
public function testAroundSaveWithoutRootCategory()
111111
{
112-
$proceed = function () {
113-
return $this->categoryResource;
114-
};
115-
116-
$this->category->expects($this->atLeastOnce())->method('getStoreId')->willReturn(0);
117112
$this->category->expects($this->atLeastOnce())->method('getParentId')->willReturn(0);
118113
$this->category->expects($this->atLeastOnce())->method('isObjectNew')->willReturn(true);
119114
$this->category->expects($this->atLeastOnce())->method('isInRootCategoryList')->willReturn(false);
120115
$this->category->expects($this->never())->method('getStoreIds');
121116

122117
$this->assertEquals(
123118
$this->categoryResource,
124-
$this->categorySavePlugin->aroundSave($this->categoryResource, $proceed, $this->category)
119+
$this->updateUrlPathPlugin->afterSave($this->categoryResource, $this->categoryResource, $this->category)
125120
);
126121
}
127122

@@ -130,13 +125,9 @@ public function testAroundSaveWithRootCategory()
130125
$parentId = 1;
131126
$categoryStoreIds = [0,1,2];
132127
$generatedUrlPath = 'parent_category/child_category';
133-
$proceed = function () {
134-
return $this->categoryResource;
135-
};
136128

137129
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')->with($this->category)
138130
->willReturn($generatedUrlPath);
139-
$this->category->expects($this->atLeastOnce())->method('getStoreId')->willReturn(0);
140131
$this->category->expects($this->atLeastOnce())->method('getParentId')->willReturn($parentId);
141132
$this->category->expects($this->atLeastOnce())->method('isObjectNew')->willReturn(true);
142133
$this->category->expects($this->atLeastOnce())->method('isInRootCategoryList')->willReturn(false);
@@ -166,7 +157,7 @@ public function testAroundSaveWithRootCategory()
166157

167158
$this->assertEquals(
168159
$this->categoryResource,
169-
$this->categorySavePlugin->aroundSave($this->categoryResource, $proceed, $this->category)
160+
$this->updateUrlPathPlugin->afterSave($this->categoryResource, $this->categoryResource, $this->category)
170161
);
171162
}
172163
}

app/code/Magento/CatalogUrlRewrite/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<type name="Magento\Catalog\Model\ResourceModel\Category">
2020
<plugin name="category_move_plugin" type="Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Move"/>
2121
<plugin name="category_delete_plugin" type="Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Remove"/>
22-
<plugin name="update_url_path_for_different_stores" type="Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Save"/>
22+
<plugin name="update_url_path_for_different_stores" type="Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\UpdateUrlPath"/>
2323
</type>
2424
<type name="Magento\UrlRewrite\Model\StorageInterface">
2525
<plugin name="storage_plugin" type="Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage"/>

dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ public function testCreateSubcategoryWithMultipleStores()
355355

356356
$this->assertEquals($parentAllStoresPath . '/test-category-100', $childCategory->getUrlPath());
357357
$this->assertEquals($parentSecondStoreKey . '/test-category-100', $childCategorySecondStore->getUrlPath());
358-
359358
}
360359

361360
protected function getCategoryByName($categoryName)

0 commit comments

Comments
 (0)