forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EngCom] Public Pull Requests - 2.2-develop
- merged latest code from mainline branch
- Loading branch information
Showing
10 changed files
with
369 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/code/Magento/CatalogUrlRewrite/Plugin/Webapi/Controller/Rest/InputParamsResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest; | ||
|
||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Framework\Webapi\Rest\Request as RestRequest; | ||
use Magento\Webapi\Controller\Rest\InputParamsResolver as InputParamsResolverController; | ||
|
||
/** | ||
* Plugin for InputParamsResolver | ||
* | ||
* Used to modify product data with save_rewrites_history flag | ||
*/ | ||
class InputParamsResolver | ||
{ | ||
/** | ||
* @var RestRequest | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @param RestRequest $request | ||
*/ | ||
public function __construct(RestRequest $request) | ||
{ | ||
$this->request = $request; | ||
} | ||
|
||
/** | ||
* Add 'save_rewrites_history' param to the product data | ||
* | ||
* @see \Magento\CatalogUrlRewrite\Plugin\Catalog\Controller\Adminhtml\Product\Initialization\Helper | ||
* @param InputParamsResolverController $subject | ||
* @param array $result | ||
* @return array | ||
*/ | ||
public function afterResolve(InputParamsResolverController $subject, array $result): array | ||
{ | ||
$route = $subject->getRoute(); | ||
$serviceMethodName = $route->getServiceMethod(); | ||
$serviceClassName = $route->getServiceClass(); | ||
$requestBodyParams = $this->request->getBodyParams(); | ||
|
||
if ($this->isProductSaveCalled($serviceClassName, $serviceMethodName) | ||
&& $this->isCustomAttributesExists($requestBodyParams)) { | ||
foreach ($requestBodyParams['product']['custom_attributes'] as $attribute) { | ||
if ($attribute['attribute_code'] === 'save_rewrites_history') { | ||
foreach ($result as $resultItem) { | ||
if ($resultItem instanceof Product) { | ||
$resultItem->setData('save_rewrites_history', (bool)$attribute['value']); | ||
break 2; | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* Check that product save method called | ||
* | ||
* @param string $serviceClassName | ||
* @param string $serviceMethodName | ||
* @return bool | ||
*/ | ||
private function isProductSaveCalled(string $serviceClassName, string $serviceMethodName): bool | ||
{ | ||
return $serviceClassName === ProductRepositoryInterface::class && $serviceMethodName === 'save'; | ||
} | ||
|
||
/** | ||
* Check is any custom options exists in product data | ||
* | ||
* @param array $requestBodyParams | ||
* @return bool | ||
*/ | ||
private function isCustomAttributesExists(array $requestBodyParams): bool | ||
{ | ||
return !empty($requestBodyParams['product']['custom_attributes']); | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
...nto/CatalogUrlRewrite/Test/Unit/Plugin/Webapi/Controller/Rest/InputParamsResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogUrlRewrite\Test\Unit\Plugin\Webapi\Controller\Rest; | ||
|
||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\Webapi\Controller\Rest\InputParamsResolver; | ||
use Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\InputParamsResolver as InputParamsResolverPlugin; | ||
use Magento\Framework\Webapi\Rest\Request as RestRequest; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Webapi\Controller\Rest\Router\Route; | ||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use PHPUnit_Framework_MockObject_MockObject as MockObject; | ||
|
||
/** | ||
* Unit test for InputParamsResolver plugin | ||
*/ | ||
class InputParamsResolverTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $saveRewritesHistory; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $requestBodyParams; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $result; | ||
|
||
/** | ||
* @var ObjectManager | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @var InputParamsResolver|MockObject | ||
*/ | ||
private $subject; | ||
|
||
/** | ||
* @var RestRequest|MockObject | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @var Product|MockObject | ||
*/ | ||
private $product; | ||
|
||
/** | ||
* @var Route|MockObject | ||
*/ | ||
private $route; | ||
|
||
/** | ||
* @var InputParamsResolverPlugin | ||
*/ | ||
private $plugin; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->saveRewritesHistory = 'save_rewrites_history'; | ||
$this->requestBodyParams = [ | ||
'product' => [ | ||
'sku' => 'test', | ||
'custom_attributes' => [ | ||
[ | ||
'attribute_code' => $this->saveRewritesHistory, | ||
'value' => 1 | ||
] | ||
] | ||
] | ||
]; | ||
|
||
$this->route = $this->createPartialMock(Route::class, ['getServiceMethod', 'getServiceClass']); | ||
$this->request = $this->createPartialMock(RestRequest::class, ['getBodyParams']); | ||
$this->request->method('getBodyParams') | ||
->willReturn($this->requestBodyParams); | ||
$this->subject = $this->createPartialMock(InputParamsResolver::class, ['getRoute']); | ||
$this->subject->method('getRoute') | ||
->willReturn($this->route); | ||
$this->product = $this->createPartialMock(Product::class, ['setData']); | ||
|
||
$this->result = [false, $this->product, 'test']; | ||
|
||
$this->objectManager = new ObjectManager($this); | ||
$this->plugin = $this->objectManager->getObject( | ||
InputParamsResolverPlugin::class, | ||
[ | ||
'request' => $this->request | ||
] | ||
); | ||
} | ||
|
||
public function testAfterResolve() | ||
{ | ||
$this->route->method('getServiceClass') | ||
->willReturn(ProductRepositoryInterface::class); | ||
$this->route->method('getServiceMethod') | ||
->willReturn('save'); | ||
$this->product->expects($this->once()) | ||
->method('setData') | ||
->with($this->saveRewritesHistory, true); | ||
|
||
$this->plugin->afterResolve($this->subject, $this->result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.