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
- ]);
64
+ 'formatUrlKey '
65
+ ]
66
+ );
54
67
$ this ->category ->expects ($ this ->any ())->method ('getResource ' )->willReturn ($ this ->categoryResource );
55
68
$ this ->observer ->expects ($ this ->any ())->method ('getEvent ' )->willReturnSelf ();
56
69
$ this ->observer ->expects ($ this ->any ())->method ('getCategory ' )->willReturn ($ this ->category );
@@ -73,106 +86,125 @@ protected function setUp()
73
86
);
74
87
}
75
88
76
- public function testSetCategoryUrlAndCategoryPath ()
89
+ /**
90
+ * @param $isObjectNew
91
+ * @throws \Magento\Framework\Exception\LocalizedException
92
+ * @dataProvider shouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValueDataProvider
93
+ */
94
+ public function testShouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValue ($ isObjectNew )
77
95
{
78
- $ this ->category ->expects ($ this ->once ())->method ('getUrlKey ' )->willReturn ('category ' );
79
- $ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('getUrlKey ' )->willReturn ('urk_key ' );
80
- $ this ->category ->expects ($ this ->once ())->method ('setUrlKey ' )->with ('urk_key ' )->willReturnSelf ();
81
- $ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('getUrlPath ' )->willReturn ('url_path ' );
82
- $ this ->category ->expects ($ this ->once ())->method ('setUrlPath ' )->with ('url_path ' )->willReturnSelf ();
83
- $ this ->category ->expects ($ this ->exactly (2 ))->method ('isObjectNew ' )->willReturn (true );
84
-
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 ());
85
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 ' );
86
109
}
87
110
88
- public function testExecuteWithoutUrlKeyAndUrlPathUpdating ()
111
+ /**
112
+ * @return array
113
+ */
114
+ public function shouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValueDataProvider ()
89
115
{
90
- $ this -> category -> expects ( $ this -> once ())-> method ( ' getUrlKey ' )-> willReturn ( false );
91
- $ this -> category -> expects ( $ this -> never ())-> method ( ' setUrlKey ' );
92
- $ this -> category -> expects ( $ this -> never ())-> method ( ' setUrlPath ' );
93
- $ this -> categoryUrlPathAutogeneratorObserver -> execute ( $ this -> observer ) ;
116
+ return [
117
+ [ true ],
118
+ [ false ],
119
+ ] ;
94
120
}
95
121
96
122
/**
97
- * @expectedException \Magento\Framework\Exception\LocalizedException
98
- * @expectedExceptionMessage Invalid URL key
123
+ * @param $isObjectNew
124
+ * @throws \Magento\Framework\Exception\LocalizedException
125
+ * @dataProvider shouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValueDataProvider
99
126
*/
100
- public function testExecuteWithException ( )
127
+ public function testShouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValue ( $ isObjectNew )
101
128
{
102
- $ categoryName = 'test ' ;
103
- $ categoryData = ['url_key ' => 0 ];
104
- $ this ->category ->expects ($ this ->once ())->method ('getUrlKey ' )->willReturn ($ categoryName );
105
- $ this ->category ->expects ($ this ->once ())
106
- ->method ('getData ' )
107
- ->with ('use_default ' )
108
- ->willReturn ($ categoryData );
109
- $ this ->categoryUrlPathGenerator ->expects ($ this ->once ())
110
- ->method ('getUrlKey ' )
111
- ->with ($ this ->category )
112
- ->willReturn (null );
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 ());
113
135
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
136
+ $ this ->assertNull ($ this ->category ->getUrlKey ());
137
+ $ this ->assertNull ($ this ->category ->getUrlPath ());
114
138
}
115
139
116
- public function testUrlKeyAndUrlPathUpdating ()
140
+ /**
141
+ * @return array
142
+ */
143
+ public function shouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValueDataProvider ()
117
144
{
118
- $ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('getUrlKey ' )->with ($ this ->category )
119
- ->willReturn ('url_key ' );
120
- $ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('getUrlPath ' )->with ($ this ->category )
121
- ->willReturn ('url_path ' );
122
-
123
- $ this ->category ->expects ($ this ->once ())->method ('getUrlKey ' )->willReturn ('not_formatted_url_key ' );
124
- $ this ->category ->expects ($ this ->once ())->method ('setUrlKey ' )->with ('url_key ' )->willReturnSelf ();
125
- $ this ->category ->expects ($ this ->once ())->method ('setUrlPath ' )->with ('url_path ' )->willReturnSelf ();
126
- // break code execution
127
- $ this ->category ->expects ($ this ->exactly (2 ))->method ('isObjectNew ' )->willReturn (true );
145
+ return [
146
+ [true ],
147
+ [false ],
148
+ ];
149
+ }
128
150
151
+ /**
152
+ * @param $useDefaultUrlKey
153
+ * @param $isObjectNew
154
+ * @throws \Magento\Framework\Exception\LocalizedException
155
+ * @dataProvider shouldThrowExceptionIfUrlKeyIsEmptyDataProvider
156
+ */
157
+ public function testShouldThrowExceptionIfUrlKeyIsEmpty ($ useDefaultUrlKey , $ isObjectNew )
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 ->assertEquals ($ isObjectNew , $ this ->category ->isObjectNew ());
164
+ $ this ->assertEquals ($ categoryData ['url_key ' ], $ this ->category ->getUrlKey ());
165
+ $ this ->assertEquals ($ categoryData ['url_path ' ], $ this ->category ->getUrlPath ());
129
166
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
167
+ $ this ->assertEquals ($ categoryData ['url_key ' ], $ this ->category ->getUrlKey ());
168
+ $ this ->assertEquals ($ categoryData ['url_path ' ], $ this ->category ->getUrlPath ());
130
169
}
131
170
132
- public function testUrlPathAttributeNoUpdatingIfCategoryIsNew ()
171
+ /**
172
+ * @return array
173
+ */
174
+ public function shouldThrowExceptionIfUrlKeyIsEmptyDataProvider ()
133
175
{
134
- $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('url_key ' );
135
- $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ('url_path ' );
136
-
137
- $ this ->category ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('not_formatted_url_key ' );
138
- $ this ->category ->expects ($ this ->any ())->method ('setUrlKey ' )->willReturnSelf ();
139
- $ this ->category ->expects ($ this ->any ())->method ('setUrlPath ' )->willReturnSelf ();
140
-
141
- $ this ->category ->expects ($ this ->exactly (2 ))->method ('isObjectNew ' )->willReturn (true );
142
- $ this ->categoryResource ->expects ($ this ->never ())->method ('saveAttribute ' );
143
-
144
- $ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
176
+ return [
177
+ [0 , false ],
178
+ [0 , true ],
179
+ [1 , false ],
180
+ ];
145
181
}
146
182
147
183
public function testUrlPathAttributeUpdating ()
148
184
{
149
- $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('url_key ' );
150
- $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ('url_path ' );
151
-
152
- $ this ->category ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('not_formatted_url_key ' );
153
- $ this ->category ->expects ($ this ->any ())->method ('setUrlKey ' )->willReturnSelf ();
154
- $ this ->category ->expects ($ this ->any ())->method ('setUrlPath ' )->willReturnSelf ();
155
- $ this ->category ->expects ($ this ->exactly (2 ))->method ('isObjectNew ' )->willReturn (false );
156
-
185
+ $ categoryData = ['url_key ' => 'some_key ' , 'url_path ' => '' ];
186
+ $ this ->category ->setData ($ categoryData );
187
+ $ this ->category ->isObjectNew (false );
188
+ $ expectedUrlKey = 'formatted_url_key ' ;
189
+ $ expectedUrlPath = 'generated_url_path ' ;
190
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ($ expectedUrlKey );
191
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ($ expectedUrlPath );
157
192
$ this ->categoryResource ->expects ($ this ->once ())->method ('saveAttribute ' )->with ($ this ->category , 'url_path ' );
158
-
159
- // break code execution
160
193
$ this ->category ->expects ($ this ->once ())->method ('dataHasChangedFor ' )->with ('url_path ' )->willReturn (false );
161
-
162
194
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
163
195
}
164
196
165
197
public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChanged ()
166
198
{
199
+ $ categoryData = ['url_key ' => 'some_key ' , 'url_path ' => '' ];
200
+ $ this ->category ->setData ($ categoryData );
201
+ $ this ->category ->isObjectNew (false );
202
+
167
203
$ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('url_key ' );
168
204
$ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ('url_path ' );
169
205
170
206
$ this ->categoryResource ->expects ($ this ->once ())->method ('saveAttribute ' )->with ($ this ->category , 'url_path ' );
171
207
172
- $ this ->category ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('not_formatted_url_key ' );
173
- $ this ->category ->expects ($ this ->any ())->method ('setUrlKey ' )->willReturnSelf ();
174
- $ this ->category ->expects ($ this ->any ())->method ('setUrlPath ' )->willReturnSelf ();
175
- $ this ->category ->expects ($ this ->exactly (2 ))->method ('isObjectNew ' )->willReturn (false );
176
208
// break code execution
177
209
$ this ->category ->expects ($ this ->once ())->method ('dataHasChangedFor ' )->with ('url_path ' )->willReturn (false );
178
210
@@ -181,29 +213,31 @@ public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChange
181
213
182
214
public function testChildrenUrlPathAttributeUpdatingForSpecificStore ()
183
215
{
216
+ $ categoryData = ['url_key ' => 'some_key ' , 'url_path ' => '' ];
217
+ $ this ->category ->setData ($ categoryData );
218
+ $ this ->category ->isObjectNew (false );
219
+
184
220
$ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('generated_url_key ' );
185
221
$ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ('generated_url_path ' );
186
-
187
- $ this ->category ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('not_formatted_url_key ' );
188
- $ this ->category ->expects ($ this ->any ())->method ('setUrlKey ' )->willReturnSelf ();
189
- $ this ->category ->expects ($ this ->any ())->method ('setUrlPath ' )->willReturnSelf ();
190
- $ this ->category ->expects ($ this ->exactly (2 ))->method ('isObjectNew ' )->willReturn (false );
191
222
$ this ->category ->expects ($ this ->any ())->method ('dataHasChangedFor ' )->willReturn (true );
192
223
// only for specific store
193
224
$ this ->category ->expects ($ this ->atLeastOnce ())->method ('getStoreId ' )->willReturn (1 );
194
225
195
226
$ childCategoryResource = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Category::class)
196
227
->disableOriginalConstructor ()->getMock ();
197
228
$ childCategory = $ this ->getMockBuilder (\Magento \Catalog \Model \Category::class)
198
- ->setMethods ([
199
- 'getUrlPath ' ,
200
- 'setUrlPath ' ,
201
- 'getResource ' ,
202
- 'getStore ' ,
203
- 'getStoreId ' ,
204
- 'setStoreId '
205
- ])
206
- ->disableOriginalConstructor ()->getMock ();
229
+ ->setMethods (
230
+ [
231
+ 'getUrlPath ' ,
232
+ 'setUrlPath ' ,
233
+ 'getResource ' ,
234
+ 'getStore ' ,
235
+ 'getStoreId ' ,
236
+ 'setStoreId '
237
+ ]
238
+ )
239
+ ->disableOriginalConstructor ()
240
+ ->getMock ();
207
241
$ childCategory ->expects ($ this ->any ())->method ('getResource ' )->willReturn ($ childCategoryResource );
208
242
$ childCategory ->expects ($ this ->once ())->method ('setStoreId ' )->with (1 );
209
243
0 commit comments