Skip to content

#29174 Add save_rewrites_history to categories via API #29205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogUrlRewrite\Plugin\Model;

use Magento\Catalog\Model\Category;
use Magento\Framework\Webapi\Rest\Request as RestRequest;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;

class CategorySetSaveRewriteHistory
{
private const SAVE_REWRITES_HISTORY = 'save_rewrites_history';

/**
* @var RestRequest
*/
private $request;

/**
* @param RestRequest $request
*/
public function __construct(RestRequest $request)
{
$this->request = $request;
}

/**
* Add 'save_rewrites_history' param to the category for list
*
* @param CategoryUrlRewriteGenerator $subject
* @param Category $category
* @param bool $overrideStoreUrls
* @param int|null $rootCategoryId
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeGenerate(
CategoryUrlRewriteGenerator $subject,
Category $category,
bool $overrideStoreUrls = false,
?int $rootCategoryId = null
) {
$requestBodyParams = $this->request->getBodyParams();

if ($this->isCustomAttributesExists($requestBodyParams, CategoryUrlRewriteGenerator::ENTITY_TYPE)) {
foreach ($requestBodyParams[CategoryUrlRewriteGenerator::ENTITY_TYPE]['custom_attributes'] as $attribute) {
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
$category->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
}
}
}

return [$category, $overrideStoreUrls, $rootCategoryId];
}

/**
* Check is any custom options exists in data
*
* @param array $requestBodyParams
* @param string $entityCode
* @return bool
*/
private function isCustomAttributesExists(array $requestBodyParams, string $entityCode): bool
{
return !empty($requestBodyParams[$entityCode]['custom_attributes']);
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/CatalogUrlRewrite/etc/webapi_rest/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
<type name="Magento\Webapi\Controller\Rest\InputParamsResolver">
<plugin name="product_save_rewrites_history_rest_plugin" type="Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\InputParamsResolver" sortOrder="1" disabled="false" />
</type>
<type name="Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator">
<plugin name="category_set_save_rewrites_history_rest_plugin" type="Magento\CatalogUrlRewrite\Plugin\Model\CategorySetSaveRewriteHistory" disabled="false" />
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,82 @@ public function testUpdateWithDefaultSortByAttribute()
$this->createdCategories = [$categoryId];
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/category.php
*/
public function testUpdateUrlKey()
{
$this->_markTestAsRestOnly('Functionality available in REST mode only.');

$categoryId = 333;
$categoryData = [
'name' => 'Update Category Test Old Name',
'custom_attributes' => [
[
'attribute_code' => 'url_key',
'value' => "Update Category Test Old Name",
],
],
];
$result = $this->updateCategory($categoryId, $categoryData);
$this->assertEquals($categoryId, $result['id']);

$categoryData = [
'name' => 'Update Category Test New Name',
'custom_attributes' => [
[
'attribute_code' => 'url_key',
'value' => "Update Category Test New Name",
],
[
'attribute_code' => 'save_rewrites_history',
'value' => 1,
],
],
];
$result = $this->updateCategory($categoryId, $categoryData);
$this->assertEquals($categoryId, $result['id']);
/** @var \Magento\Catalog\Model\Category $model */
$model = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\Category::class);
$category = $model->load($categoryId);
$this->assertEquals("Update Category Test New Name", $category->getName());

// check for the url rewrite for the new name
$storage = Bootstrap::getObjectManager()->get(\Magento\UrlRewrite\Model\Storage\DbStorage::class);
$data = [
UrlRewrite::ENTITY_ID => $categoryId,
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::REDIRECT_TYPE => 0,
];

$urlRewrite = $storage->findOneByData($data);

// Assert that a url rewrite is auto-generated for the category created from the data fixture
$this->assertNotNull($urlRewrite);
$this->assertEquals(1, $urlRewrite->getIsAutogenerated());
$this->assertEquals($categoryId, $urlRewrite->getEntityId());
$this->assertEquals(CategoryUrlRewriteGenerator::ENTITY_TYPE, $urlRewrite->getEntityType());
$this->assertEquals('update-category-test-new-name.html', $urlRewrite->getRequestPath());

// check for the forward from the old name to the new name
$storage = Bootstrap::getObjectManager()->get(\Magento\UrlRewrite\Model\Storage\DbStorage::class);
$data = [
UrlRewrite::ENTITY_ID => $categoryId,
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::REDIRECT_TYPE => 301,
];

$urlRewrite = $storage->findOneByData($data);

$this->assertNotNull($urlRewrite);
$this->assertEquals(0, $urlRewrite->getIsAutogenerated());
$this->assertEquals($categoryId, $urlRewrite->getEntityId());
$this->assertEquals(CategoryUrlRewriteGenerator::ENTITY_TYPE, $urlRewrite->getEntityType());
$this->assertEquals('update-category-test-old-name.html', $urlRewrite->getRequestPath());

$this->deleteCategory($categoryId);
}

protected function getSimpleCategoryData($categoryData = [])
{
return [
Expand Down