Skip to content

Commit f0ba72a

Browse files
committed
#29174 Add API functional test for redirect generation for category updates
1 parent 4c99f0d commit f0ba72a

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

dev/tests/api-functional/testsuite/Magento/Catalog/Api/CategoryRepositoryTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,83 @@ public function testUpdate()
218218
$this->deleteCategory($categoryId);
219219
}
220220

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+
221298
protected function getSimpleCategoryData($categoryData = [])
222299
{
223300
return [

0 commit comments

Comments
 (0)