3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \CatalogUrlRewrite \Test \Unit \Observer ;
7
8
8
9
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
9
10
10
11
class CategoryUrlPathAutogeneratorObserverTest extends \PHPUnit \Framework \TestCase
11
12
{
12
- /** @var \Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver */
13
- protected $ categoryUrlPathAutogeneratorObserver ;
13
+ /**
14
+ * @var \Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver
15
+ */
16
+ private $ categoryUrlPathAutogeneratorObserver ;
14
17
15
- /** @var \PHPUnit_Framework_MockObject_MockObject */
16
- protected $ categoryUrlPathGenerator ;
18
+ /**
19
+ * @var \PHPUnit_Framework_MockObject_MockObject
20
+ */
21
+ private $ categoryUrlPathGenerator ;
17
22
18
- /** @var \PHPUnit_Framework_MockObject_MockObject */
19
- protected $ childrenCategoriesProvider ;
23
+ /**
24
+ * @var \PHPUnit_Framework_MockObject_MockObject
25
+ */
26
+ private $ childrenCategoriesProvider ;
20
27
21
- /** @var \PHPUnit_Framework_MockObject_MockObject */
22
- protected $ observer ;
28
+ /**
29
+ * @var \PHPUnit_Framework_MockObject_MockObject
30
+ */
31
+ private $ observer ;
23
32
24
- /** @var \PHPUnit_Framework_MockObject_MockObject */
25
- protected $ category ;
33
+ /**
34
+ * @var \PHPUnit_Framework_MockObject_MockObject
35
+ */
36
+ private $ category ;
26
37
27
38
/**
28
39
* @var \Magento\CatalogUrlRewrite\Service\V1\StoreViewService|\PHPUnit_Framework_MockObject_MockObject
29
40
*/
30
- protected $ storeViewService ;
41
+ private $ storeViewService ;
31
42
32
43
/**
33
44
* @var \Magento\Catalog\Model\ResourceModel\Category|\PHPUnit_Framework_MockObject_MockObject
34
45
*/
35
- protected $ categoryResource ;
46
+ private $ categoryResource ;
36
47
48
+ /**
49
+ * @inheritDoc
50
+ */
37
51
protected function setUp ()
38
52
{
39
53
$ this ->observer = $ this ->createPartialMock (
40
54
\Magento \Framework \Event \Observer::class,
41
55
['getEvent ' , 'getCategory ' ]
42
56
);
43
57
$ 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
+ [
47
61
'dataHasChangedFor ' ,
48
- 'isObjectNew ' ,
49
62
'getResource ' ,
50
- 'getUrlKey ' ,
51
63
'getStoreId ' ,
52
- 'getData ' ,
53
- 'getOrigData ' ,
54
- 'formatUrlKey ' ,
55
- ]);
64
+ 'formatUrlKey '
65
+ ]
66
+ );
56
67
$ this ->category ->expects ($ this ->any ())->method ('getResource ' )->willReturn ($ this ->categoryResource );
57
68
$ this ->observer ->expects ($ this ->any ())->method ('getEvent ' )->willReturnSelf ();
58
69
$ this ->observer ->expects ($ this ->any ())->method ('getCategory ' )->willReturn ($ this ->category );
@@ -75,134 +86,127 @@ protected function setUp()
75
86
);
76
87
}
77
88
78
- public function testSetCategoryUrlAndCategoryPath ()
89
+ /**
90
+ * @param $isObjectNew
91
+ * @throws \Magento\Framework\Exception\LocalizedException
92
+ * @dataProvider shouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValueDataProvider
93
+ */
94
+ public function testShouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValue ($ isObjectNew )
79
95
{
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 ());
87
105
$ 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 ' );
88
109
}
89
110
90
- public function testExecuteWithoutUrlKeyAndUrlPathUpdating ()
111
+ /**
112
+ * @return array
113
+ */
114
+ public function shouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValueDataProvider ()
91
115
{
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
+ ] ;
96
120
}
97
121
98
122
/**
99
- * @expectedException \Magento\Framework\Exception\LocalizedException
100
- * @expectedExceptionMessage Invalid URL key
123
+ * @param $isObjectNew
124
+ * @throws \Magento\Framework\Exception\LocalizedException
125
+ * @dataProvider shouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValueDataProvider
101
126
*/
102
- public function testExecuteWithException ( )
127
+ public function testShouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValue ( $ isObjectNew )
103
128
{
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 ());
115
135
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
136
+ $ this ->assertNull ($ this ->category ->getUrlKey ());
137
+ $ this ->assertNull ($ this ->category ->getUrlPath ());
116
138
}
117
139
118
140
/**
119
- * Test execute method when option use default url key is true.
141
+ * @return array
120
142
*/
121
- public function testExecuteWithUseDefault ()
143
+ public function shouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValueDataProvider ()
122
144
{
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
+ ];
144
149
}
145
150
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 )
147
158
{
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 ());
159
168
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
169
+ $ this ->assertEquals ($ categoryData ['url_key ' ], $ this ->category ->getUrlKey ());
170
+ $ this ->assertEquals ($ categoryData ['url_path ' ], $ this ->category ->getUrlPath ());
160
171
}
161
172
162
- public function testUrlPathAttributeNoUpdatingIfCategoryIsNew ()
173
+ /**
174
+ * @return array
175
+ */
176
+ public function shouldThrowExceptionIfUrlKeyIsEmptyDataProvider ()
163
177
{
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
+ ];
175
183
}
176
184
177
185
public function testUrlPathAttributeUpdating ()
178
186
{
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 );
187
194
$ this ->categoryResource ->expects ($ this ->once ())->method ('saveAttribute ' )->with ($ this ->category , 'url_path ' );
188
-
189
- // break code execution
190
195
$ this ->category ->expects ($ this ->once ())->method ('dataHasChangedFor ' )->with ('url_path ' )->willReturn (false );
191
-
192
196
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
193
197
}
194
198
195
199
public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChanged ()
196
200
{
201
+ $ categoryData = ['url_key ' => 'some_key ' , 'url_path ' => '' ];
202
+ $ this ->category ->setData ($ categoryData );
203
+ $ this ->category ->isObjectNew (false );
204
+
197
205
$ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('url_key ' );
198
206
$ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ('url_path ' );
199
207
200
208
$ this ->categoryResource ->expects ($ this ->once ())->method ('saveAttribute ' )->with ($ this ->category , 'url_path ' );
201
209
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 );
206
210
// break code execution
207
211
$ this ->category ->expects ($ this ->once ())->method ('dataHasChangedFor ' )->with ('url_path ' )->willReturn (false );
208
212
@@ -211,29 +215,31 @@ public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChange
211
215
212
216
public function testChildrenUrlPathAttributeUpdatingForSpecificStore ()
213
217
{
218
+ $ categoryData = ['url_key ' => 'some_key ' , 'url_path ' => '' ];
219
+ $ this ->category ->setData ($ categoryData );
220
+ $ this ->category ->isObjectNew (false );
221
+
214
222
$ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('generated_url_key ' );
215
223
$ 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 );
221
224
$ this ->category ->expects ($ this ->any ())->method ('dataHasChangedFor ' )->willReturn (true );
222
225
// only for specific store
223
226
$ this ->category ->expects ($ this ->atLeastOnce ())->method ('getStoreId ' )->willReturn (1 );
224
227
225
228
$ childCategoryResource = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Category::class)
226
229
->disableOriginalConstructor ()->getMock ();
227
230
$ 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 ();
237
243
$ childCategory ->expects ($ this ->any ())->method ('getResource ' )->willReturn ($ childCategoryResource );
238
244
$ childCategory ->expects ($ this ->once ())->method ('setStoreId ' )->with (1 );
239
245
0 commit comments