7
7
8
8
namespace Magento \CatalogUrlRewrite \Test \Unit \Observer ;
9
9
10
+ use Magento \Catalog \Api \Data \CategoryInterface ;
10
11
use Magento \Catalog \Model \Category ;
11
12
use Magento \Catalog \Model \ResourceModel \Category as CategoryResource ;
12
13
use Magento \CatalogUrlRewrite \Model \Category \ChildrenCategoriesProvider ;
13
14
use Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator ;
14
15
use Magento \CatalogUrlRewrite \Model \ResourceModel \Category \GetDefaultUrlKey ;
15
16
use Magento \CatalogUrlRewrite \Observer \CategoryUrlPathAutogeneratorObserver ;
16
17
use Magento \CatalogUrlRewrite \Service \V1 \StoreViewService ;
18
+ use Magento \Framework \EntityManager \EntityMetadataInterface ;
19
+ use Magento \Framework \EntityManager \MetadataPool ;
17
20
use Magento \Framework \Event \Observer ;
18
21
use Magento \Framework \Exception \LocalizedException ;
19
22
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
24
27
25
28
/**
26
29
* Unit tests for \Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver class.
30
+ *
31
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
32
*/
28
33
class CategoryUrlPathAutogeneratorObserverTest extends TestCase
29
34
{
@@ -72,6 +77,16 @@ class CategoryUrlPathAutogeneratorObserverTest extends TestCase
72
77
*/
73
78
private $ getDefaultUrlKey ;
74
79
80
+ /**
81
+ * @var MetadataPool|MockObject
82
+ */
83
+ private $ metadataPool ;
84
+
85
+ /**
86
+ * @var EntityMetadataInterface|MockObject
87
+ */
88
+ private $ entityMetaDataInterface ;
89
+
75
90
/**
76
91
* @inheritDoc
77
92
*/
@@ -83,17 +98,21 @@ protected function setUp(): void
83
98
->disableOriginalConstructor ()
84
99
->getMock ();
85
100
$ this ->categoryResource = $ this ->createMock (CategoryResource::class);
86
- $ this ->category = $ this ->createPartialMock (
87
- Category::class,
88
- [
89
- 'dataHasChangedFor ' ,
90
- 'getResource ' ,
91
- 'getStoreId ' ,
92
- 'formatUrlKey ' ,
93
- 'getId ' ,
94
- 'hasChildren ' ,
95
- ]
96
- );
101
+ $ this ->category = $ this ->getMockBuilder (Category::class)
102
+ ->onlyMethods (
103
+ [
104
+ 'dataHasChangedFor ' ,
105
+ 'getResource ' ,
106
+ 'getStoreId ' ,
107
+ 'formatUrlKey ' ,
108
+ 'hasChildren ' ,
109
+ 'getData ' ,
110
+ 'getUrlKey '
111
+ ]
112
+ )
113
+ ->addMethods (['getUrlPath ' ])
114
+ ->disableOriginalConstructor ()
115
+ ->getMock ();
97
116
$ this ->category ->expects ($ this ->any ())->method ('getResource ' )->willReturn ($ this ->categoryResource );
98
117
$ this ->observer ->expects ($ this ->any ())->method ('getEvent ' )->willReturnSelf ();
99
118
$ this ->observer ->expects ($ this ->any ())->method ('getCategory ' )->willReturn ($ this ->category );
@@ -112,6 +131,14 @@ protected function setUp(): void
112
131
->onlyMethods (['execute ' ])
113
132
->getMock ();
114
133
134
+ $ this ->metadataPool = $ this ->getMockBuilder (MetadataPool::class)
135
+ ->disableOriginalConstructor ()
136
+ ->onlyMethods (['getMetadata ' ])
137
+ ->getMock ();
138
+
139
+ $ this ->entityMetaDataInterface = $ this ->getMockBuilder (EntityMetadataInterface::class)
140
+ ->getMockForAbstractClass ();
141
+
115
142
$ this ->categoryUrlPathAutogeneratorObserver = (new ObjectManagerHelper ($ this ))->getObject (
116
143
CategoryUrlPathAutogeneratorObserver::class,
117
144
[
@@ -120,6 +147,7 @@ protected function setUp(): void
120
147
'storeViewService ' => $ this ->storeViewService ,
121
148
'compositeUrlValidator ' => $ this ->compositeUrlValidator ,
122
149
'getDefaultUrlKey ' => $ this ->getDefaultUrlKey ,
150
+ 'metadataPool ' => $ this ->metadataPool
123
151
]
124
152
);
125
153
}
@@ -134,13 +162,20 @@ public function testShouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaul
134
162
$ expectedUrlKey = 'formatted_url_key ' ;
135
163
$ expectedUrlPath = 'generated_url_path ' ;
136
164
$ categoryData = ['use_default ' => ['url_key ' => 0 ], 'url_key ' => 'some_key ' , 'url_path ' => '' ];
165
+ $ this ->category ->expects ($ this ->any ())
166
+ ->method ('getUrlKey ' )
167
+ ->willReturnOnConsecutiveCalls ($ categoryData ['url_key ' ], null , $ expectedUrlKey );
168
+ $ this ->category ->expects ($ this ->any ())
169
+ ->method ('getUrlPath ' )
170
+ ->willReturnOnConsecutiveCalls ($ categoryData ['url_path ' ], $ expectedUrlPath );
137
171
$ this ->category ->setData ($ categoryData );
138
172
$ this ->category ->isObjectNew ($ isObjectNew );
139
173
$ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('getUrlKey ' )->willReturn ($ expectedUrlKey );
140
174
$ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('getUrlPath ' )->willReturn ($ expectedUrlPath );
141
175
$ this ->assertEquals ($ categoryData ['url_key ' ], $ this ->category ->getUrlKey ());
142
176
$ this ->assertEquals ($ categoryData ['url_path ' ], $ this ->category ->getUrlPath ());
143
- $ this ->compositeUrlValidator ->expects ($ this ->once ())->method ('validate ' )->with ('formatted_url_key ' )->willReturn ([]);
177
+ $ this ->compositeUrlValidator ->expects ($ this ->once ())->method ('validate ' )
178
+ ->with ('formatted_url_key ' )->willReturn ([]);
144
179
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
145
180
$ this ->assertEquals ($ expectedUrlKey , $ this ->category ->getUrlKey ());
146
181
$ this ->assertEquals ($ expectedUrlPath , $ this ->category ->getUrlPath ());
@@ -179,11 +214,32 @@ public function testShouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValue(bool
179
214
$ this ->category ->expects ($ this ->once ())
180
215
->method ('hasChildren ' )
181
216
->willReturn (false );
217
+ $ this ->metadataPool ->expects ($ this ->any ())
218
+ ->method ('getMetadata ' )
219
+ ->with (CategoryInterface::class)
220
+ ->willReturn ($ this ->entityMetaDataInterface );
221
+ $ this ->entityMetaDataInterface ->expects ($ this ->any ())
222
+ ->method ('getLinkField ' )
223
+ ->willReturn ('row_id ' );
224
+ $ this ->category ->expects ($ this ->any ())
225
+ ->method ('getUrlKey ' )
226
+ ->willReturn ($ categoryData ['url_key ' ]);
227
+ $ this ->category ->expects ($ this ->any ())
228
+ ->method ('getUrlPath ' )
229
+ ->willReturn ($ categoryData ['url_path ' ]);
230
+ $ this ->category ->expects ($ this ->any ())
231
+ ->method ('getData ' )
232
+ ->willReturnMap (
233
+ [
234
+ ['use_default ' , null , ['url_key ' => 1 ]],
235
+ ['row_id ' , null , null ],
236
+ ]
237
+ );
182
238
$ this ->assertEquals ($ categoryData ['url_key ' ], $ this ->category ->getUrlKey ());
183
239
$ this ->assertEquals ($ categoryData ['url_path ' ], $ this ->category ->getUrlPath ());
184
240
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
185
- $ this ->assertNull ($ this ->category ->getUrlKey ());
186
- $ this ->assertNull ($ this ->category ->getUrlPath ());
241
+ $ this ->assertNotEmpty ($ this ->category ->getUrlKey ());
242
+ $ this ->assertNotEmpty ($ this ->category ->getUrlPath ());
187
243
}
188
244
189
245
/**
@@ -201,15 +257,18 @@ public function shouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValueDataProvid
201
257
202
258
/**
203
259
* @return void
260
+ * @throws LocalizedException
204
261
*/
205
262
public function testShouldUpdateUrlPathForChildrenIfUrlKeyIsUsingDefaultValueForSpecificStore (): void
206
263
{
207
264
$ storeId = 1 ;
208
265
$ categoryId = 1 ;
266
+ $ rowId = 1 ;
209
267
$ categoryData = [
210
268
'use_default ' => ['url_key ' => 1 ],
211
269
'url_key ' => null ,
212
270
'url_path ' => 'some_path ' ,
271
+ 'row_id ' => 1
213
272
];
214
273
215
274
$ this ->category ->setData ($ categoryData );
@@ -220,9 +279,24 @@ public function testShouldUpdateUrlPathForChildrenIfUrlKeyIsUsingDefaultValueFor
220
279
$ this ->category ->expects ($ this ->once ())
221
280
->method ('hasChildren ' )
222
281
->willReturn (true );
223
- $ this ->category ->expects ($ this ->exactly (2 ))
224
- ->method ('getId ' )
225
- ->willReturn ($ categoryId );
282
+ $ this ->metadataPool ->expects ($ this ->any ())
283
+ ->method ('getMetadata ' )
284
+ ->with (CategoryInterface::class)
285
+ ->willReturn ($ this ->entityMetaDataInterface );
286
+ $ this ->entityMetaDataInterface ->expects ($ this ->any ())
287
+ ->method ('getLinkField ' )
288
+ ->willReturn ('row_id ' );
289
+ $ this ->category ->expects ($ this ->any ())
290
+ ->method ('getUrlKey ' )
291
+ ->willReturn (false );
292
+ $ this ->category ->expects ($ this ->any ())
293
+ ->method ('getData ' )
294
+ ->willReturnMap (
295
+ [
296
+ ['use_default ' , null , ['url_key ' => 1 ]],
297
+ ['row_id ' , null , $ rowId ],
298
+ ]
299
+ );
226
300
$ this ->getDefaultUrlKey ->expects ($ this ->once ())
227
301
->method ('execute ' )
228
302
->with ($ categoryId )
@@ -262,7 +336,7 @@ public function testShouldUpdateUrlPathForChildrenIfUrlKeyIsUsingDefaultValueFor
262
336
->willReturn ([$ childCategory ]);
263
337
264
338
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
265
- $ this ->assertNull ($ this ->category ->getUrlKey ());
339
+ $ this ->assertFalse ($ this ->category ->getUrlKey ());
266
340
$ this ->assertNull ($ this ->category ->getUrlPath ());
267
341
}
268
342
@@ -312,7 +386,8 @@ public function testUrlPathAttributeUpdating()
312
386
$ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ($ expectedUrlPath );
313
387
$ this ->categoryResource ->expects ($ this ->once ())->method ('saveAttribute ' )->with ($ this ->category , 'url_path ' );
314
388
$ this ->category ->expects ($ this ->once ())->method ('dataHasChangedFor ' )->with ('url_path ' )->willReturn (false );
315
- $ this ->compositeUrlValidator ->expects ($ this ->once ())->method ('validate ' )->with ('formatted_url_key ' )->willReturn ([]);
389
+ $ this ->compositeUrlValidator ->expects ($ this ->once ())->method ('validate ' )
390
+ ->with ('formatted_url_key ' )->willReturn ([]);
316
391
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
317
392
}
318
393
@@ -368,7 +443,8 @@ public function testChildrenUrlPathAttributeUpdatingForSpecificStore()
368
443
$ this ->childrenCategoriesProvider ->expects ($ this ->once ())->method ('getChildren ' )->willReturn ([$ childCategory ]);
369
444
$ childCategory ->expects ($ this ->once ())->method ('setUrlPath ' )->with ('generated_url_path ' )->willReturnSelf ();
370
445
$ childCategoryResource ->expects ($ this ->once ())->method ('saveAttribute ' )->with ($ childCategory , 'url_path ' );
371
- $ this ->compositeUrlValidator ->expects ($ this ->once ())->method ('validate ' )->with ('generated_url_key ' )->willReturn ([]);
446
+ $ this ->compositeUrlValidator ->expects ($ this ->once ())->method ('validate ' )
447
+ ->with ('generated_url_key ' )->willReturn ([]);
372
448
373
449
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
374
450
}
0 commit comments