Skip to content

Commit 5307bad

Browse files
committed
#29174 Add save_rewrites_history to categories via API
1 parent 25772e6 commit 5307bad

File tree

2 files changed

+86
-9
lines changed

2 files changed

+86
-9
lines changed

app/code/Magento/CatalogUrlRewrite/Plugin/Webapi/Controller/Rest/InputParamsResolver.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest;
1010

11+
use Magento\Catalog\Api\CategoryRepositoryInterface;
1112
use Magento\Catalog\Api\ProductRepositoryInterface;
1213
use Magento\Framework\Webapi\Rest\Request as RestRequest;
1314

@@ -18,6 +19,8 @@
1819
*/
1920
class InputParamsResolver
2021
{
22+
const SAVE_REWRITES_HISTORY = 'save_rewrites_history';
23+
2124
/**
2225
* @var RestRequest
2326
*/
@@ -32,7 +35,7 @@ public function __construct(RestRequest $request)
3235
}
3336

3437
/**
35-
* Add 'save_rewrites_history' param to the product data
38+
* Add 'save_rewrites_history' param to the product and category data
3639
*
3740
* @see \Magento\CatalogUrlRewrite\Plugin\Catalog\Controller\Adminhtml\Product\Initialization\Helper
3841
* @param \Magento\Webapi\Controller\Rest\InputParamsResolver $subject
@@ -47,12 +50,27 @@ public function afterResolve(\Magento\Webapi\Controller\Rest\InputParamsResolver
4750
$requestBodyParams = $this->request->getBodyParams();
4851

4952
if ($this->isProductSaveCalled($serviceClassName, $serviceMethodName)
50-
&& $this->isCustomAttributesExists($requestBodyParams)) {
53+
&& $this->isCustomAttributesExists($requestBodyParams, 'product')) {
5154
foreach ($requestBodyParams['product']['custom_attributes'] as $attribute) {
52-
if ($attribute['attribute_code'] === 'save_rewrites_history') {
55+
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
5356
foreach ($result as $resultItem) {
5457
if ($resultItem instanceof \Magento\Catalog\Model\Product) {
55-
$resultItem->setData('save_rewrites_history', (bool)$attribute['value']);
58+
$resultItem->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
59+
break 2;
60+
}
61+
}
62+
break;
63+
}
64+
}
65+
}
66+
67+
if ($this->isCategorySaveCalled($serviceClassName, $serviceMethodName)
68+
&& $this->isCustomAttributesExists($requestBodyParams, 'category')) {
69+
foreach ($requestBodyParams['category']['custom_attributes'] as $attribute) {
70+
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
71+
foreach ($result as $resultItem) {
72+
if ($resultItem instanceof \Magento\Catalog\Model\Category) {
73+
$resultItem->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
5674
break 2;
5775
}
5876
}
@@ -75,14 +93,27 @@ private function isProductSaveCalled(string $serviceClassName, string $serviceMe
7593
return $serviceClassName === ProductRepositoryInterface::class && $serviceMethodName === 'save';
7694
}
7795

96+
/**
97+
* Check that category save method called
98+
*
99+
* @param string $serviceClassName
100+
* @param string $serviceMethodName
101+
* @return bool
102+
*/
103+
private function isCategorySaveCalled(string $serviceClassName, string $serviceMethodName): bool
104+
{
105+
return $serviceClassName === CategoryRepositoryInterface::class && $serviceMethodName === 'save';
106+
}
107+
78108
/**
79109
* Check is any custom options exists in product data
80110
*
81111
* @param array $requestBodyParams
112+
* @param string $entityCode
82113
* @return bool
83114
*/
84-
private function isCustomAttributesExists(array $requestBodyParams): bool
115+
private function isCustomAttributesExists(array $requestBodyParams, string $entityCode): bool
85116
{
86-
return !empty($requestBodyParams['product']['custom_attributes']);
117+
return !empty($requestBodyParams[$entityCode]['custom_attributes']);
87118
}
88119
}

app/code/Magento/CatalogUrlRewrite/Test/Unit/Plugin/Webapi/Controller/Rest/InputParamsResolverTest.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
namespace Magento\CatalogUrlRewrite\Test\Unit\Plugin\Webapi\Controller\Rest;
1010

11+
use Magento\Catalog\Api\CategoryRepositoryInterface;
1112
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Catalog\Model\Category;
1214
use Magento\Catalog\Model\Product;
1315
use Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\InputParamsResolver as InputParamsResolverPlugin;
1416
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -74,6 +76,10 @@ class InputParamsResolverTest extends TestCase
7476
protected function setUp(): void
7577
{
7678
$this->saveRewritesHistory = 'save_rewrites_history';
79+
}
80+
81+
public function testAfterResolveWithProduct()
82+
{
7783
$this->requestBodyParams = [
7884
'product' => [
7985
'sku' => 'test',
@@ -99,10 +105,7 @@ protected function setUp(): void
99105
'request' => $this->request
100106
]
101107
);
102-
}
103108

104-
public function testAfterResolve()
105-
{
106109
$this->route->expects($this->once())
107110
->method('getServiceClass')
108111
->willReturn(ProductRepositoryInterface::class);
@@ -115,4 +118,47 @@ public function testAfterResolve()
115118

116119
$this->plugin->afterResolve($this->subject, $this->result);
117120
}
121+
122+
123+
public function testAfterResolveWithCategory()
124+
{
125+
$this->requestBodyParams = [
126+
'category' => [
127+
'name' => 'new name',
128+
'custom_attributes' => [
129+
['attribute_code' => $this->saveRewritesHistory, 'value' => 1],
130+
['attribute_code' => 'url_key', 'value' => 'new name']
131+
]
132+
]
133+
];
134+
135+
$this->route = $this->createPartialMock(Route::class, ['getServiceMethod', 'getServiceClass']);
136+
$this->request = $this->createPartialMock(RestRequest::class, ['getBodyParams']);
137+
$this->request->expects($this->any())->method('getBodyParams')->willReturn($this->requestBodyParams);
138+
$this->subject = $this->createPartialMock(InputParamsResolver::class, ['getRoute']);
139+
$this->subject->expects($this->any())->method('getRoute')->willReturn($this->route);
140+
$this->category = $this->createPartialMock(Category::class, ['setData']);
141+
142+
$this->result = [false, $this->category, 'test'];
143+
144+
$this->objectManager = new ObjectManager($this);
145+
$this->plugin = $this->objectManager->getObject(
146+
InputParamsResolverPlugin::class,
147+
[
148+
'request' => $this->request
149+
]
150+
);
151+
152+
$this->route->expects($this->once())
153+
->method('getServiceClass')
154+
->willReturn(CategoryRepositoryInterface::class);
155+
$this->route->expects($this->once())
156+
->method('getServiceMethod')
157+
->willReturn('save');
158+
$this->category->expects($this->once())
159+
->method('setData')
160+
->with($this->saveRewritesHistory, true);
161+
162+
$this->plugin->afterResolve($this->subject, $this->result);
163+
}
118164
}

0 commit comments

Comments
 (0)