@@ -249,6 +249,82 @@ public function testUpdateWithDefaultSortByAttribute()
249
249
$ this ->createdCategories = [$ categoryId ];
250
250
}
251
251
252
+ /**
253
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
254
+ */
255
+ public function testUpdateUrlKey ()
256
+ {
257
+ $ this ->_markTestAsRestOnly ('Functionality available in REST mode only. ' );
258
+
259
+ $ categoryId = 333 ;
260
+ $ categoryData = [
261
+ 'name ' => 'Update Category Test Old Name ' ,
262
+ 'custom_attributes ' => [
263
+ [
264
+ 'attribute_code ' => 'url_key ' ,
265
+ 'value ' => "Update Category Test Old Name " ,
266
+ ],
267
+ ],
268
+ ];
269
+ $ result = $ this ->updateCategory ($ categoryId , $ categoryData );
270
+ $ this ->assertEquals ($ categoryId , $ result ['id ' ]);
271
+
272
+ $ categoryData = [
273
+ 'name ' => 'Update Category Test New Name ' ,
274
+ 'custom_attributes ' => [
275
+ [
276
+ 'attribute_code ' => 'url_key ' ,
277
+ 'value ' => "Update Category Test New Name " ,
278
+ ],
279
+ [
280
+ 'attribute_code ' => 'save_rewrites_history ' ,
281
+ 'value ' => 1 ,
282
+ ],
283
+ ],
284
+ ];
285
+ $ result = $ this ->updateCategory ($ categoryId , $ categoryData );
286
+ $ this ->assertEquals ($ categoryId , $ result ['id ' ]);
287
+ /** @var \Magento\Catalog\Model\Category $model */
288
+ $ model = Bootstrap::getObjectManager ()->get (\Magento \Catalog \Model \Category::class);
289
+ $ category = $ model ->load ($ categoryId );
290
+ $ this ->assertEquals ("Update Category Test New Name " , $ category ->getName ());
291
+
292
+ // check for the url rewrite for the new name
293
+ $ storage = Bootstrap::getObjectManager ()->get (\Magento \UrlRewrite \Model \Storage \DbStorage::class);
294
+ $ data = [
295
+ UrlRewrite::ENTITY_ID => $ categoryId ,
296
+ UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE ,
297
+ UrlRewrite::REDIRECT_TYPE => 0 ,
298
+ ];
299
+
300
+ $ urlRewrite = $ storage ->findOneByData ($ data );
301
+
302
+ // Assert that a url rewrite is auto-generated for the category created from the data fixture
303
+ $ this ->assertNotNull ($ urlRewrite );
304
+ $ this ->assertEquals (1 , $ urlRewrite ->getIsAutogenerated ());
305
+ $ this ->assertEquals ($ categoryId , $ urlRewrite ->getEntityId ());
306
+ $ this ->assertEquals (CategoryUrlRewriteGenerator::ENTITY_TYPE , $ urlRewrite ->getEntityType ());
307
+ $ this ->assertEquals ('update-category-test-new-name.html ' , $ urlRewrite ->getRequestPath ());
308
+
309
+ // check for the forward from the old name to the new name
310
+ $ storage = Bootstrap::getObjectManager ()->get (\Magento \UrlRewrite \Model \Storage \DbStorage::class);
311
+ $ data = [
312
+ UrlRewrite::ENTITY_ID => $ categoryId ,
313
+ UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE ,
314
+ UrlRewrite::REDIRECT_TYPE => 301 ,
315
+ ];
316
+
317
+ $ urlRewrite = $ storage ->findOneByData ($ data );
318
+
319
+ $ this ->assertNotNull ($ urlRewrite );
320
+ $ this ->assertEquals (0 , $ urlRewrite ->getIsAutogenerated ());
321
+ $ this ->assertEquals ($ categoryId , $ urlRewrite ->getEntityId ());
322
+ $ this ->assertEquals (CategoryUrlRewriteGenerator::ENTITY_TYPE , $ urlRewrite ->getEntityType ());
323
+ $ this ->assertEquals ('update-category-test-old-name.html ' , $ urlRewrite ->getRequestPath ());
324
+
325
+ $ this ->deleteCategory ($ categoryId );
326
+ }
327
+
252
328
protected function getSimpleCategoryData ($ categoryData = [])
253
329
{
254
330
return [
0 commit comments