@@ -218,6 +218,83 @@ public function testUpdate()
218
218
$ this ->deleteCategory ($ categoryId );
219
219
}
220
220
221
+ /**
222
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
223
+ */
224
+ public function testUpdateUrlKey ()
225
+ {
226
+ $ categoryId = 333 ;
227
+ $ categoryData = [
228
+ 'name ' => 'Update Category Test Old Name ' ,
229
+ 'custom_attributes ' => [
230
+ [
231
+ 'attribute_code ' => 'url_key ' ,
232
+ 'value ' => "Update Category Test Old Name " ,
233
+ ],
234
+ ],
235
+ ];
236
+ $ result = $ this ->updateCategory ($ categoryId , $ categoryData );
237
+ $ this ->assertEquals ($ categoryId , $ result ['id ' ]);
238
+
239
+
240
+ $ categoryData = [
241
+ 'name ' => 'Update Category Test New Name ' ,
242
+ 'custom_attributes ' => [
243
+ [
244
+ 'attribute_code ' => 'url_key ' ,
245
+ 'value ' => "Update Category Test New Name " ,
246
+ ],
247
+ [
248
+ 'attribute_code ' => 'save_rewrites_history ' ,
249
+ 'value ' => 1 ,
250
+ ],
251
+ ],
252
+ ];
253
+ $ result = $ this ->updateCategory ($ categoryId , $ categoryData );
254
+ $ this ->assertEquals ($ categoryId , $ result ['id ' ]);
255
+ /** @var \Magento\Catalog\Model\Category $model */
256
+ $ model = Bootstrap::getObjectManager ()->get (\Magento \Catalog \Model \Category::class);
257
+ $ category = $ model ->load ($ categoryId );
258
+ $ this ->assertEquals ("Update Category Test New Name " , $ category ->getName ());
259
+ // delete category to clean up auto-generated url rewrites
260
+
261
+
262
+ $ storage = Bootstrap::getObjectManager ()->get (\Magento \UrlRewrite \Model \Storage \DbStorage::class);
263
+ $ data = [
264
+ UrlRewrite::ENTITY_ID => $ categoryId ,
265
+ UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE ,
266
+ UrlRewrite::REDIRECT_TYPE => 0 ,
267
+ ];
268
+
269
+ $ urlRewrite = $ storage ->findOneByData ($ data );
270
+
271
+ // Assert that a url rewrite is auto-generated for the category created from the data fixture
272
+ $ this ->assertNotNull ($ urlRewrite );
273
+ $ this ->assertEquals (1 , $ urlRewrite ->getIsAutogenerated ());
274
+ $ this ->assertEquals ($ categoryId , $ urlRewrite ->getEntityId ());
275
+ $ this ->assertEquals (CategoryUrlRewriteGenerator::ENTITY_TYPE , $ urlRewrite ->getEntityType ());
276
+ $ this ->assertEquals ('update-category-test-new-name.html ' , $ urlRewrite ->getRequestPath ());
277
+
278
+
279
+ // check for the forward
280
+ $ storage = Bootstrap::getObjectManager ()->get (\Magento \UrlRewrite \Model \Storage \DbStorage::class);
281
+ $ data = [
282
+ UrlRewrite::ENTITY_ID => $ categoryId ,
283
+ UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE ,
284
+ UrlRewrite::REDIRECT_TYPE => 301 ,
285
+ ];
286
+
287
+ $ urlRewrite = $ storage ->findOneByData ($ data );
288
+
289
+ $ this ->assertNotNull ($ urlRewrite );
290
+ $ this ->assertEquals (0 , $ urlRewrite ->getIsAutogenerated ());
291
+ $ this ->assertEquals ($ categoryId , $ urlRewrite ->getEntityId ());
292
+ $ this ->assertEquals (CategoryUrlRewriteGenerator::ENTITY_TYPE , $ urlRewrite ->getEntityType ());
293
+ $ this ->assertEquals ('update-category-test-old-name.html ' , $ urlRewrite ->getRequestPath ());
294
+
295
+ $ this ->deleteCategory ($ categoryId );
296
+ }
297
+
221
298
protected function getSimpleCategoryData ($ categoryData = [])
222
299
{
223
300
return [
0 commit comments