Skip to content

Commit dfec69b

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-2343' into SEP292023_PR_pradeep
2 parents 8f75578 + ba4ccf4 commit dfec69b

File tree

2 files changed

+96
-38
lines changed
  • app/code/Magento/CatalogUrlRewrite

2 files changed

+96
-38
lines changed

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

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Magento\Catalog\Model\CategoryFactory;
1010
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
1111
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
12-
use Magento\Store\Model\Store;
12+
use Magento\Store\Model\StoreManagerInterface;
1313

1414
/**
1515
* Perform url updating for children categories.
@@ -31,19 +31,27 @@ class Move
3131
*/
3232
private $categoryFactory;
3333

34+
/**
35+
* @var StoreManagerInterface
36+
*/
37+
private $storeManager;
38+
3439
/**
3540
* @param CategoryUrlPathGenerator $categoryUrlPathGenerator
3641
* @param ChildrenCategoriesProvider $childrenCategoriesProvider
3742
* @param CategoryFactory $categoryFactory
43+
* @param StoreManagerInterface $storeManager
3844
*/
3945
public function __construct(
4046
CategoryUrlPathGenerator $categoryUrlPathGenerator,
4147
ChildrenCategoriesProvider $childrenCategoriesProvider,
42-
CategoryFactory $categoryFactory
48+
CategoryFactory $categoryFactory,
49+
StoreManagerInterface $storeManager
4350
) {
4451
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
4552
$this->childrenCategoriesProvider = $childrenCategoriesProvider;
4653
$this->categoryFactory = $categoryFactory;
54+
$this->storeManager = $storeManager;
4755
}
4856

4957
/**
@@ -67,6 +75,7 @@ public function afterChangeParent(
6775
$categoryStoreId = $category->getStoreId();
6876
foreach ($category->getStoreIds() as $storeId) {
6977
$category->setStoreId($storeId);
78+
$this->removeObsoleteUrlPathEntries($category);
7079
$this->updateCategoryUrlKeyForStore($category);
7180
$category->unsUrlPath();
7281
$category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
@@ -92,24 +101,13 @@ private function updateCategoryUrlKeyForStore(Category $category)
92101
$category->setUrlKey($item->getUrlKey());
93102
}
94103

95-
/**
96-
* Check is global scope.
97-
*
98-
* @param int|null $storeId
99-
* @return bool
100-
*/
101-
private function isGlobalScope($storeId)
102-
{
103-
return null === $storeId || $storeId == Store::DEFAULT_STORE_ID;
104-
}
105-
106104
/**
107105
* Updates url_path for child categories.
108106
*
109107
* @param Category $category
110108
* @return void
111109
*/
112-
private function updateUrlPathForChildren($category)
110+
private function updateUrlPathForChildren(Category $category): void
113111
{
114112
foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) {
115113
$childCategory->setStoreId($category->getStoreId());
@@ -118,4 +116,27 @@ private function updateUrlPathForChildren($category)
118116
$childCategory->getResource()->saveAttribute($childCategory, 'url_path');
119117
}
120118
}
119+
120+
/**
121+
* Clean obsolete entries
122+
*
123+
* @param Category $category
124+
* @return void
125+
*/
126+
private function removeObsoleteUrlPathEntries(Category $category): void
127+
{
128+
if ($this->storeManager->hasSingleStore()) {
129+
return;
130+
}
131+
$origPath = $category->getOrigData('path');
132+
$path = $category->getData('path');
133+
if ($origPath != null && $path != null && $origPath != $path) {
134+
$category->unsUrlPath();
135+
$category->getResource()->saveAttribute($category, 'url_path');
136+
foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) {
137+
$childCategory->unsUrlPath();
138+
$childCategory->getResource()->saveAttribute($childCategory, 'url_path');
139+
}
140+
}
141+
}
121142
}

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/MoveTest.php

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Move as CategoryMovePlugin;
1515
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
1616
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
17+
use Magento\Store\Model\StoreManagerInterface;
1718
use PHPUnit\Framework\MockObject\MockObject;
1819
use PHPUnit\Framework\TestCase;
1920

@@ -49,6 +50,11 @@ class MoveTest extends TestCase
4950
*/
5051
private $categoryFactory;
5152

53+
/**
54+
* @var StoreManagerInterface|MockObject
55+
*/
56+
private $storeManagerMock;
57+
5258
/**
5359
* @var CategoryMovePlugin
5460
*/
@@ -59,28 +65,45 @@ protected function setUp(): void
5965
$this->objectManager = new ObjectManager($this);
6066
$this->categoryUrlPathGeneratorMock = $this->getMockBuilder(CategoryUrlPathGenerator::class)
6167
->disableOriginalConstructor()
62-
->setMethods(['getUrlPath'])
68+
->onlyMethods(['getUrlPath'])
6369
->getMock();
6470
$this->childrenCategoriesProviderMock = $this->getMockBuilder(ChildrenCategoriesProvider::class)
6571
->disableOriginalConstructor()
66-
->setMethods(['getChildren'])
72+
->onlyMethods(['getChildren'])
6773
->getMock();
6874
$this->categoryFactory = $this->getMockBuilder(CategoryFactory::class)
6975
->disableOriginalConstructor()
7076
->getMock();
7177
$this->subjectMock = $this->getMockBuilder(CategoryResourceModel::class)
7278
->disableOriginalConstructor()
79+
->onlyMethods(['saveAttribute'])
7380
->getMock();
81+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
82+
->disableOriginalConstructor()
83+
->getMockForAbstractClass();
7484
$this->categoryMock = $this->getMockBuilder(Category::class)
7585
->disableOriginalConstructor()
76-
->setMethods(['getResource', 'setUrlPath', 'getStoreIds', 'getStoreId', 'setStoreId'])
86+
->onlyMethods(
87+
[
88+
'getResource',
89+
'getStoreIds',
90+
'getStoreId',
91+
'setStoreId',
92+
'getData',
93+
'getOrigData',
94+
'getId',
95+
'getUrlKey'
96+
]
97+
)
98+
->addMethods(['setUrlPath', 'unsUrlPath', 'setUrlKey'])
7799
->getMock();
78100
$this->plugin = $this->objectManager->getObject(
79101
CategoryMovePlugin::class,
80102
[
81103
'categoryUrlPathGenerator' => $this->categoryUrlPathGeneratorMock,
82104
'childrenCategoriesProvider' => $this->childrenCategoriesProviderMock,
83-
'categoryFactory' => $this->categoryFactory
105+
'categoryFactory' => $this->categoryFactory,
106+
'storeManager' => $this->storeManagerMock
84107
]
85108
);
86109
}
@@ -92,27 +115,41 @@ public function testAfterChangeParent()
92115
{
93116
$urlPath = 'test/path';
94117
$storeIds = [0, 1];
95-
$originalCategory = $this->getMockBuilder(Category::class)
96-
->disableOriginalConstructor()
97-
->getMock();
98-
$this->categoryFactory->method('create')
99-
->willReturn($originalCategory);
100-
$this->categoryMock->method('getResource')
101-
->willReturn($this->subjectMock);
102-
$this->categoryMock->expects($this->once())
103-
->method('getStoreIds')
104-
->willReturn($storeIds);
105-
$this->childrenCategoriesProviderMock->expects($this->exactly(2))
106-
->method('getChildren')
118+
119+
$this->storeManagerMock->expects($this->exactly(2))->method('hasSingleStore')->willReturn(false);
120+
$this->categoryMock->expects($this->exactly(4))->method('getStoreId')
121+
->willReturnOnConsecutiveCalls(0, 0, 1, 0);
122+
$this->categoryMock->expects($this->once())->method('getStoreIds')->willReturn($storeIds);
123+
$this->categoryMock->expects($this->exactly(4))->method('setStoreId')
124+
->willReturnOnConsecutiveCalls(0, 0, 1, 0);
125+
126+
$this->categoryMock->expects($this->exactly(2))->method('getData')
127+
->willReturnOnConsecutiveCalls('1/3/5', '1/3/5');
128+
$this->categoryMock->expects($this->exactly(2))->method('getOrigData')
129+
->willReturnOnConsecutiveCalls('1/2/5', '1/2/5');
130+
$this->categoryMock->expects($this->exactly(6))->method('unsUrlPath')->willReturnSelf();
131+
$this->childrenCategoriesProviderMock->expects($this->exactly(4))->method('getChildren')
107132
->with($this->categoryMock, true)
108-
->willReturn([]);
109-
$this->categoryUrlPathGeneratorMock->expects($this->exactly(2))
110-
->method('getUrlPath')
111-
->with($this->categoryMock)
112-
->willReturn($urlPath);
113-
$this->categoryMock->expects($this->exactly(2))
114-
->method('setUrlPath')
115-
->with($urlPath);
133+
->willReturnOnConsecutiveCalls([$this->categoryMock], [$this->categoryMock], [], []);
134+
135+
$this->categoryMock->expects($this->exactly(6))->method('getResource')->willReturn($this->subjectMock);
136+
$this->subjectMock->expects($this->exactly(6))->method('saveAttribute')
137+
->with($this->categoryMock, 'url_path')->willReturnSelf();
138+
$this->categoryMock->expects($this->exactly(2))->method('getId')->willReturnSelf();
139+
140+
$originalCategory = $this->getMockBuilder(Category::class)->disableOriginalConstructor()->getMock();
141+
$originalCategory->expects($this->exactly(2))->method('getUrlKey')->willReturn('url-key');
142+
$originalCategory->expects($this->exactly(2))->method('setStoreId')->willReturnSelf();
143+
$originalCategory->expects($this->exactly(2))->method('load')->willReturnSelf();
144+
$this->categoryFactory->expects($this->exactly(2))->method('create')
145+
->willReturn($originalCategory);
146+
$this->categoryMock->expects($this->exactly(2))->method('setUrlKey')->with('url-key')
147+
->willReturnSelf();
148+
149+
$this->categoryUrlPathGeneratorMock->expects($this->exactly(3))->method('getUrlPath')
150+
->with($this->categoryMock)->willReturn($urlPath);
151+
$this->categoryMock->expects($this->exactly(3))->method('setUrlPath')->with($urlPath);
152+
116153
$this->assertSame(
117154
$this->subjectMock,
118155
$this->plugin->afterChangeParent(

0 commit comments

Comments
 (0)