Skip to content

Commit f312045

Browse files
committed
Merge remote-tracking branch 'mpi/MC-18790' into BUGFIX-2.2-0823
2 parents 5562d10 + d4cf0fc commit f312045

File tree

2 files changed

+138
-130
lines changed

2 files changed

+138
-130
lines changed

app/code/Magento/CatalogUrlRewrite/Observer/CategoryUrlPathAutogeneratorObserver.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
*/
66
namespace Magento\CatalogUrlRewrite\Observer;
77

8-
use Magento\Catalog\Api\CategoryRepositoryInterface;
98
use Magento\Catalog\Model\Category;
10-
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
119
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
1210
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
11+
use Magento\Catalog\Api\CategoryRepositoryInterface;
12+
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
1313
use Magento\Framework\Event\ObserverInterface;
1414
use Magento\Store\Model\Store;
1515

@@ -67,13 +67,15 @@ public function execute(\Magento\Framework\Event\Observer $observer)
6767
{
6868
/** @var Category $category */
6969
$category = $observer->getEvent()->getCategory();
70-
$useDefaultAttribute = !$category->isObjectNew() && !empty($category->getData('use_default')['url_key']);
70+
$useDefaultAttribute = !empty($category->getData('use_default')['url_key']);
7171
if ($category->getUrlKey() !== false && !$useDefaultAttribute) {
7272
$resultUrlKey = $this->categoryUrlPathGenerator->getUrlKey($category);
7373
$this->updateUrlKey($category, $resultUrlKey);
74-
} else if ($useDefaultAttribute) {
75-
$resultUrlKey = $category->formatUrlKey($category->getOrigData('name'));
76-
$this->updateUrlKey($category, $resultUrlKey);
74+
} elseif ($useDefaultAttribute) {
75+
if (!$category->isObjectNew()) {
76+
$resultUrlKey = $category->formatUrlKey($category->getOrigData('name'));
77+
$this->updateUrlKey($category, $resultUrlKey);
78+
}
7779
$category->setUrlKey(null)->setUrlPath(null);
7880
}
7981
}

app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/CategoryUrlPathAutogeneratorObserverTest.php

Lines changed: 130 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,67 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\CatalogUrlRewrite\Test\Unit\Observer;
78

89
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
910

1011
class CategoryUrlPathAutogeneratorObserverTest extends \PHPUnit\Framework\TestCase
1112
{
12-
/** @var \Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver */
13-
protected $categoryUrlPathAutogeneratorObserver;
13+
/**
14+
* @var \Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver
15+
*/
16+
private $categoryUrlPathAutogeneratorObserver;
1417

15-
/** @var \PHPUnit_Framework_MockObject_MockObject */
16-
protected $categoryUrlPathGenerator;
18+
/**
19+
* @var \PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
private $categoryUrlPathGenerator;
1722

18-
/** @var \PHPUnit_Framework_MockObject_MockObject */
19-
protected $childrenCategoriesProvider;
23+
/**
24+
* @var \PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $childrenCategoriesProvider;
2027

21-
/** @var \PHPUnit_Framework_MockObject_MockObject */
22-
protected $observer;
28+
/**
29+
* @var \PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $observer;
2332

24-
/** @var \PHPUnit_Framework_MockObject_MockObject */
25-
protected $category;
33+
/**
34+
* @var \PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
private $category;
2637

2738
/**
2839
* @var \Magento\CatalogUrlRewrite\Service\V1\StoreViewService|\PHPUnit_Framework_MockObject_MockObject
2940
*/
30-
protected $storeViewService;
41+
private $storeViewService;
3142

3243
/**
3344
* @var \Magento\Catalog\Model\ResourceModel\Category|\PHPUnit_Framework_MockObject_MockObject
3445
*/
35-
protected $categoryResource;
46+
private $categoryResource;
3647

48+
/**
49+
* @inheritDoc
50+
*/
3751
protected function setUp()
3852
{
3953
$this->observer = $this->createPartialMock(
4054
\Magento\Framework\Event\Observer::class,
4155
['getEvent', 'getCategory']
4256
);
4357
$this->categoryResource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category::class);
44-
$this->category = $this->createPartialMock(\Magento\Catalog\Model\Category::class, [
45-
'setUrlKey',
46-
'setUrlPath',
58+
$this->category = $this->createPartialMock(
59+
\Magento\Catalog\Model\Category::class,
60+
[
4761
'dataHasChangedFor',
48-
'isObjectNew',
4962
'getResource',
50-
'getUrlKey',
5163
'getStoreId',
52-
'getData',
53-
'getOrigData',
54-
'formatUrlKey',
55-
]);
64+
'formatUrlKey'
65+
]
66+
);
5667
$this->category->expects($this->any())->method('getResource')->willReturn($this->categoryResource);
5768
$this->observer->expects($this->any())->method('getEvent')->willReturnSelf();
5869
$this->observer->expects($this->any())->method('getCategory')->willReturn($this->category);
@@ -75,134 +86,127 @@ protected function setUp()
7586
);
7687
}
7788

78-
public function testSetCategoryUrlAndCategoryPath()
89+
/**
90+
* @param $isObjectNew
91+
* @throws \Magento\Framework\Exception\LocalizedException
92+
* @dataProvider shouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValueDataProvider
93+
*/
94+
public function testShouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValue($isObjectNew)
7995
{
80-
$this->category->expects($this->once())->method('getUrlKey')->willReturn('category');
81-
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlKey')->willReturn('urk_key');
82-
$this->category->expects($this->once())->method('setUrlKey')->with('urk_key')->willReturnSelf();
83-
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')->willReturn('url_path');
84-
$this->category->expects($this->once())->method('setUrlPath')->with('url_path')->willReturnSelf();
85-
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
86-
96+
$expectedUrlKey = 'formatted_url_key';
97+
$expectedUrlPath = 'generated_url_path';
98+
$categoryData = ['use_default' => ['url_key' => 0], 'url_key' => 'some_key', 'url_path' => ''];
99+
$this->category->setData($categoryData);
100+
$this->category->isObjectNew($isObjectNew);
101+
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlKey')->willReturn($expectedUrlKey);
102+
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')->willReturn($expectedUrlPath);
103+
$this->assertEquals($categoryData['url_key'], $this->category->getUrlKey());
104+
$this->assertEquals($categoryData['url_path'], $this->category->getUrlPath());
87105
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
106+
$this->assertEquals($expectedUrlKey, $this->category->getUrlKey());
107+
$this->assertEquals($expectedUrlPath, $this->category->getUrlPath());
108+
$this->categoryResource->expects($this->never())->method('saveAttribute');
88109
}
89110

90-
public function testExecuteWithoutUrlKeyAndUrlPathUpdating()
111+
/**
112+
* @return array
113+
*/
114+
public function shouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValueDataProvider()
91115
{
92-
$this->category->expects($this->once())->method('getUrlKey')->willReturn(false);
93-
$this->category->expects($this->never())->method('setUrlKey');
94-
$this->category->expects($this->never())->method('setUrlPath');
95-
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
116+
return [
117+
[true],
118+
[false],
119+
];
96120
}
97121

98122
/**
99-
* @expectedException \Magento\Framework\Exception\LocalizedException
100-
* @expectedExceptionMessage Invalid URL key
123+
* @param $isObjectNew
124+
* @throws \Magento\Framework\Exception\LocalizedException
125+
* @dataProvider shouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValueDataProvider
101126
*/
102-
public function testExecuteWithException()
127+
public function testShouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValue($isObjectNew)
103128
{
104-
$categoryName = 'test';
105-
$categoryData = ['url_key' => 0];
106-
$this->category->expects($this->once())->method('getUrlKey')->willReturn($categoryName);
107-
$this->category->expects($this->once())
108-
->method('getData')
109-
->with('use_default')
110-
->willReturn($categoryData);
111-
$this->categoryUrlPathGenerator->expects($this->once())
112-
->method('getUrlKey')
113-
->with($this->category)
114-
->willReturn('');
129+
$categoryData = ['use_default' => ['url_key' => 1], 'url_key' => 'some_key', 'url_path' => 'some_path'];
130+
$this->category->setData($categoryData);
131+
$this->category->isObjectNew($isObjectNew);
132+
$this->category->expects($this->any())->method('formatUrlKey')->willReturn('formatted_key');
133+
$this->assertEquals($categoryData['url_key'], $this->category->getUrlKey());
134+
$this->assertEquals($categoryData['url_path'], $this->category->getUrlPath());
115135
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
136+
$this->assertNull($this->category->getUrlKey());
137+
$this->assertNull($this->category->getUrlPath());
116138
}
117139

118140
/**
119-
* Test execute method when option use default url key is true.
141+
* @return array
120142
*/
121-
public function testExecuteWithUseDefault()
143+
public function shouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValueDataProvider()
122144
{
123-
$categoryName = 'test';
124-
$categoryData = ['url_key' => 1];
125-
$this->category->expects($this->once())->method('getUrlKey')->willReturn($categoryName);
126-
$this->category->expects($this->atLeastOnce())
127-
->method('getData')
128-
->with('use_default')
129-
->willReturn($categoryData);
130-
$this->category->expects($this->once())->method('getOrigData')->with('name')->willReturn('some_name');
131-
$this->category->expects($this->once())->method('formatUrlKey')->with('some_name')->willReturn('url_key');
132-
$this->category->expects($this->any())->method('setUrlKey')
133-
->willReturnMap([
134-
['url_key', $this->category],
135-
[null, $this->category],
136-
]);
137-
$this->category->expects($this->any())->method('setUrlPath')
138-
->willReturnMap([
139-
['url_path', $this->category],
140-
[null, $this->category],
141-
]);
142-
143-
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
145+
return [
146+
[true],
147+
[false],
148+
];
144149
}
145150

146-
public function testUrlKeyAndUrlPathUpdating()
151+
/**
152+
* @param $useDefaultUrlKey
153+
* @param $isObjectNew
154+
* @throws \Magento\Framework\Exception\LocalizedException
155+
* @dataProvider shouldThrowExceptionIfUrlKeyIsEmptyDataProvider
156+
*/
157+
public function testShouldThrowExceptionIfUrlKeyIsEmpty($useDefaultUrlKey, $isObjectNew)
147158
{
148-
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlKey')->with($this->category)
149-
->willReturn('url_key');
150-
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')->with($this->category)
151-
->willReturn('url_path');
152-
153-
$this->category->expects($this->once())->method('getUrlKey')->willReturn('not_formatted_url_key');
154-
$this->category->expects($this->once())->method('setUrlKey')->with('url_key')->willReturnSelf();
155-
$this->category->expects($this->once())->method('setUrlPath')->with('url_path')->willReturnSelf();
156-
// break code execution
157-
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
158-
159+
$this->expectExceptionMessage('Invalid URL key');
160+
$categoryData = ['use_default' => ['url_key' => $useDefaultUrlKey], 'url_key' => '', 'url_path' => ''];
161+
$this->category->setData($categoryData);
162+
$this->category->isObjectNew($isObjectNew);
163+
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('');
164+
$this->category->expects($this->any())->method('formatUrlKey')->willReturn('');
165+
$this->assertEquals($isObjectNew, $this->category->isObjectNew());
166+
$this->assertEquals($categoryData['url_key'], $this->category->getUrlKey());
167+
$this->assertEquals($categoryData['url_path'], $this->category->getUrlPath());
159168
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
169+
$this->assertEquals($categoryData['url_key'], $this->category->getUrlKey());
170+
$this->assertEquals($categoryData['url_path'], $this->category->getUrlPath());
160171
}
161172

162-
public function testUrlPathAttributeNoUpdatingIfCategoryIsNew()
173+
/**
174+
* @return array
175+
*/
176+
public function shouldThrowExceptionIfUrlKeyIsEmptyDataProvider()
163177
{
164-
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('url_key');
165-
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('url_path');
166-
167-
$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
168-
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
169-
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
170-
171-
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
172-
$this->categoryResource->expects($this->never())->method('saveAttribute');
173-
174-
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
178+
return [
179+
[0, false],
180+
[0, true],
181+
[1, false],
182+
];
175183
}
176184

177185
public function testUrlPathAttributeUpdating()
178186
{
179-
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('url_key');
180-
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('url_path');
181-
182-
$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
183-
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
184-
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
185-
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(false);
186-
187+
$categoryData = ['url_key' => 'some_key', 'url_path' => ''];
188+
$this->category->setData($categoryData);
189+
$this->category->isObjectNew(false);
190+
$expectedUrlKey = 'formatted_url_key';
191+
$expectedUrlPath = 'generated_url_path';
192+
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn($expectedUrlKey);
193+
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn($expectedUrlPath);
187194
$this->categoryResource->expects($this->once())->method('saveAttribute')->with($this->category, 'url_path');
188-
189-
// break code execution
190195
$this->category->expects($this->once())->method('dataHasChangedFor')->with('url_path')->willReturn(false);
191-
192196
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
193197
}
194198

195199
public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChanged()
196200
{
201+
$categoryData = ['url_key' => 'some_key', 'url_path' => ''];
202+
$this->category->setData($categoryData);
203+
$this->category->isObjectNew(false);
204+
197205
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('url_key');
198206
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('url_path');
199207

200208
$this->categoryResource->expects($this->once())->method('saveAttribute')->with($this->category, 'url_path');
201209

202-
$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
203-
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
204-
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
205-
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(false);
206210
// break code execution
207211
$this->category->expects($this->once())->method('dataHasChangedFor')->with('url_path')->willReturn(false);
208212

@@ -211,29 +215,31 @@ public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChange
211215

212216
public function testChildrenUrlPathAttributeUpdatingForSpecificStore()
213217
{
218+
$categoryData = ['url_key' => 'some_key', 'url_path' => ''];
219+
$this->category->setData($categoryData);
220+
$this->category->isObjectNew(false);
221+
214222
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('generated_url_key');
215223
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('generated_url_path');
216-
217-
$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
218-
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
219-
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
220-
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(false);
221224
$this->category->expects($this->any())->method('dataHasChangedFor')->willReturn(true);
222225
// only for specific store
223226
$this->category->expects($this->atLeastOnce())->method('getStoreId')->willReturn(1);
224227

225228
$childCategoryResource = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Category::class)
226229
->disableOriginalConstructor()->getMock();
227230
$childCategory = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
228-
->setMethods([
229-
'getUrlPath',
230-
'setUrlPath',
231-
'getResource',
232-
'getStore',
233-
'getStoreId',
234-
'setStoreId'
235-
])
236-
->disableOriginalConstructor()->getMock();
231+
->setMethods(
232+
[
233+
'getUrlPath',
234+
'setUrlPath',
235+
'getResource',
236+
'getStore',
237+
'getStoreId',
238+
'setStoreId'
239+
]
240+
)
241+
->disableOriginalConstructor()
242+
->getMock();
237243
$childCategory->expects($this->any())->method('getResource')->willReturn($childCategoryResource);
238244
$childCategory->expects($this->once())->method('setStoreId')->with(1);
239245

0 commit comments

Comments
 (0)