Skip to content

Commit bc1cd5a

Browse files
Merge branch 'MAGETWO-93424' into 2.2-develop-pr11
2 parents da57658 + fc510d3 commit bc1cd5a

File tree

4 files changed

+216
-0
lines changed

4 files changed

+216
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest;
10+
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\Product;
13+
use Magento\Framework\Webapi\Rest\Request as RestRequest;
14+
use Magento\Webapi\Controller\Rest\InputParamsResolver as InputParamsResolverController;
15+
16+
/**
17+
* Plugin for InputParamsResolver
18+
*
19+
* Used to modify product data with save_rewrites_history flag
20+
*/
21+
class InputParamsResolver
22+
{
23+
/**
24+
* @var RestRequest
25+
*/
26+
private $request;
27+
28+
/**
29+
* @param RestRequest $request
30+
*/
31+
public function __construct(RestRequest $request)
32+
{
33+
$this->request = $request;
34+
}
35+
36+
/**
37+
* Add 'save_rewrites_history' param to the product data
38+
*
39+
* @see \Magento\CatalogUrlRewrite\Plugin\Catalog\Controller\Adminhtml\Product\Initialization\Helper
40+
* @param InputParamsResolverController $subject
41+
* @param array $result
42+
* @return array
43+
*/
44+
public function afterResolve(InputParamsResolverController $subject, array $result): array
45+
{
46+
$route = $subject->getRoute();
47+
$serviceMethodName = $route->getServiceMethod();
48+
$serviceClassName = $route->getServiceClass();
49+
$requestBodyParams = $this->request->getBodyParams();
50+
51+
if ($this->isProductSaveCalled($serviceClassName, $serviceMethodName)
52+
&& $this->isCustomAttributesExists($requestBodyParams)) {
53+
foreach ($requestBodyParams['product']['custom_attributes'] as $attribute) {
54+
if ($attribute['attribute_code'] === 'save_rewrites_history') {
55+
foreach ($result as $resultItem) {
56+
if ($resultItem instanceof Product) {
57+
$resultItem->setData('save_rewrites_history', (bool)$attribute['value']);
58+
break 2;
59+
}
60+
}
61+
break;
62+
}
63+
}
64+
}
65+
return $result;
66+
}
67+
68+
/**
69+
* Check that product save method called
70+
*
71+
* @param string $serviceClassName
72+
* @param string $serviceMethodName
73+
* @return bool
74+
*/
75+
private function isProductSaveCalled(string $serviceClassName, string $serviceMethodName): bool
76+
{
77+
return $serviceClassName === ProductRepositoryInterface::class && $serviceMethodName === 'save';
78+
}
79+
80+
/**
81+
* Check is any custom options exists in product data
82+
*
83+
* @param array $requestBodyParams
84+
* @return bool
85+
*/
86+
private function isCustomAttributesExists(array $requestBodyParams): bool
87+
{
88+
return !empty($requestBodyParams['product']['custom_attributes']);
89+
}
90+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CatalogUrlRewrite\Test\Unit\Plugin\Webapi\Controller\Rest;
10+
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\Webapi\Controller\Rest\InputParamsResolver;
13+
use Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\InputParamsResolver as InputParamsResolverPlugin;
14+
use Magento\Framework\Webapi\Rest\Request as RestRequest;
15+
use Magento\Catalog\Model\Product;
16+
use Magento\Webapi\Controller\Rest\Router\Route;
17+
use Magento\Catalog\Api\ProductRepositoryInterface;
18+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
19+
20+
/**
21+
* Unit test for InputParamsResolver plugin
22+
*/
23+
class InputParamsResolverTest extends \PHPUnit\Framework\TestCase
24+
{
25+
/**
26+
* @var string
27+
*/
28+
private $saveRewritesHistory;
29+
30+
/**
31+
* @var array
32+
*/
33+
private $requestBodyParams;
34+
35+
/**
36+
* @var array
37+
*/
38+
private $result;
39+
40+
/**
41+
* @var ObjectManager
42+
*/
43+
private $objectManager;
44+
45+
/**
46+
* @var InputParamsResolver|MockObject
47+
*/
48+
private $subject;
49+
50+
/**
51+
* @var RestRequest|MockObject
52+
*/
53+
private $request;
54+
55+
/**
56+
* @var Product|MockObject
57+
*/
58+
private $product;
59+
60+
/**
61+
* @var Route|MockObject
62+
*/
63+
private $route;
64+
65+
/**
66+
* @var InputParamsResolverPlugin
67+
*/
68+
private $plugin;
69+
70+
/**
71+
* @inheritdoc
72+
*/
73+
protected function setUp()
74+
{
75+
$this->saveRewritesHistory = 'save_rewrites_history';
76+
$this->requestBodyParams = [
77+
'product' => [
78+
'sku' => 'test',
79+
'custom_attributes' => [
80+
[
81+
'attribute_code' => $this->saveRewritesHistory,
82+
'value' => 1
83+
]
84+
]
85+
]
86+
];
87+
88+
$this->route = $this->createPartialMock(Route::class, ['getServiceMethod', 'getServiceClass']);
89+
$this->request = $this->createPartialMock(RestRequest::class, ['getBodyParams']);
90+
$this->request->method('getBodyParams')
91+
->willReturn($this->requestBodyParams);
92+
$this->subject = $this->createPartialMock(InputParamsResolver::class, ['getRoute']);
93+
$this->subject->method('getRoute')
94+
->willReturn($this->route);
95+
$this->product = $this->createPartialMock(Product::class, ['setData']);
96+
97+
$this->result = [false, $this->product, 'test'];
98+
99+
$this->objectManager = new ObjectManager($this);
100+
$this->plugin = $this->objectManager->getObject(
101+
InputParamsResolverPlugin::class,
102+
[
103+
'request' => $this->request
104+
]
105+
);
106+
}
107+
108+
public function testAfterResolve()
109+
{
110+
$this->route->method('getServiceClass')
111+
->willReturn(ProductRepositoryInterface::class);
112+
$this->route->method('getServiceMethod')
113+
->willReturn('save');
114+
$this->product->expects($this->once())
115+
->method('setData')
116+
->with($this->saveRewritesHistory, true);
117+
118+
$this->plugin->afterResolve($this->subject, $this->result);
119+
}
120+
}

app/code/Magento/CatalogUrlRewrite/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"magento/framework": "101.0.*",
1414
"magento/module-ui": "101.0.*"
1515
},
16+
"suggest": {
17+
"magento/module-webapi": "*"
18+
},
1619
"type": "magento2-module",
1720
"version": "100.2.5",
1821
"license": [

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator" type="Magento\CatalogUrlRewrite\Model\WebapiProductUrlPathGenerator"/>
10+
<type name="Magento\Webapi\Controller\Rest\InputParamsResolver">
11+
<plugin name="product_save_rewrites_history_rest_plugin" type="Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\InputParamsResolver" sortOrder="1" disabled="false" />
12+
</type>
1013
</config>

0 commit comments

Comments
 (0)