Skip to content

Commit 9eb4b1f

Browse files
author
Oleksandr Iegorov
committed
Merge branch 'MAGETWO-93425' of github.com:magento-tango/magento2ce into PR-2311
2 parents 40b423d + 52fabb9 commit 9eb4b1f

File tree

4 files changed

+210
-0
lines changed

4 files changed

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

app/code/Magento/CatalogUrlRewrite/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"magento/module-ui": "*",
1717
"magento/module-url-rewrite": "*"
1818
},
19+
"suggest": {
20+
"magento/module-webapi": "*"
21+
},
1922
"type": "magento2-module",
2023
"license": [
2124
"OSL-3.0",

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)