Skip to content

Commit e36b246

Browse files
ENGCOM-8286: #29174 Add save_rewrites_history to categories via API #29205
2 parents 51b53be + d21807c commit e36b246

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogUrlRewrite\Plugin\Model;
9+
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Framework\Webapi\Rest\Request as RestRequest;
12+
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
13+
14+
class CategorySetSaveRewriteHistory
15+
{
16+
private const SAVE_REWRITES_HISTORY = 'save_rewrites_history';
17+
18+
/**
19+
* @var RestRequest
20+
*/
21+
private $request;
22+
23+
/**
24+
* @param RestRequest $request
25+
*/
26+
public function __construct(RestRequest $request)
27+
{
28+
$this->request = $request;
29+
}
30+
31+
/**
32+
* Add 'save_rewrites_history' param to the category for list
33+
*
34+
* @param CategoryUrlRewriteGenerator $subject
35+
* @param Category $category
36+
* @param bool $overrideStoreUrls
37+
* @param int|null $rootCategoryId
38+
* @return array
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
40+
*/
41+
public function beforeGenerate(
42+
CategoryUrlRewriteGenerator $subject,
43+
Category $category,
44+
bool $overrideStoreUrls = false,
45+
?int $rootCategoryId = null
46+
) {
47+
$requestBodyParams = $this->request->getBodyParams();
48+
49+
if ($this->isCustomAttributesExists($requestBodyParams, CategoryUrlRewriteGenerator::ENTITY_TYPE)) {
50+
foreach ($requestBodyParams[CategoryUrlRewriteGenerator::ENTITY_TYPE]['custom_attributes'] as $attribute) {
51+
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
52+
$category->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
53+
}
54+
}
55+
}
56+
57+
return [$category, $overrideStoreUrls, $rootCategoryId];
58+
}
59+
60+
/**
61+
* Check is any custom options exists in data
62+
*
63+
* @param array $requestBodyParams
64+
* @param string $entityCode
65+
* @return bool
66+
*/
67+
private function isCustomAttributesExists(array $requestBodyParams, string $entityCode): bool
68+
{
69+
return !empty($requestBodyParams[$entityCode]['custom_attributes']);
70+
}
71+
}

app/code/Magento/CatalogUrlRewrite/etc/webapi_rest/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99
<type name="Magento\Webapi\Controller\Rest\InputParamsResolver">
1010
<plugin name="product_save_rewrites_history_rest_plugin" type="Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\InputParamsResolver" sortOrder="1" disabled="false" />
1111
</type>
12+
<type name="Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator">
13+
<plugin name="category_set_save_rewrites_history_rest_plugin" type="Magento\CatalogUrlRewrite\Plugin\Model\CategorySetSaveRewriteHistory" disabled="false" />
14+
</type>
1215
</config>

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,82 @@ public function testUpdateWithDefaultSortByAttribute()
249249
$this->createdCategories = [$categoryId];
250250
}
251251

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+
252328
protected function getSimpleCategoryData($categoryData = [])
253329
{
254330
return [

0 commit comments

Comments
 (0)