Skip to content

Commit

Permalink
Merge remote-tracking branch 'mainline/2.4-develop' into 2.4.1-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
slavvka committed May 28, 2020
2 parents 7f2272d + c8794cb commit f824b9e
Show file tree
Hide file tree
Showing 24 changed files with 321 additions and 36 deletions.
7 changes: 6 additions & 1 deletion app/code/Magento/Captcha/Controller/Refresh/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace Magento\Captcha\Controller\Refresh;

use Magento\Captcha\Helper\Data as CaptchaHelper;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\JsonFactory as JsonResultFactory;
Expand All @@ -18,7 +20,7 @@
* Refreshes captcha and returns JSON encoded URL to image (AJAX action)
* Example: {'imgSrc': 'http://example.com/media/captcha/67842gh187612ngf8s.png'}
*/
class Index implements HttpPostActionInterface
class Index extends Action implements HttpPostActionInterface
{
/**
* @var CaptchaHelper
Expand Down Expand Up @@ -46,19 +48,22 @@ class Index implements HttpPostActionInterface
private $jsonResultFactory;

/**
* @param Context $context
* @param RequestInterface $request
* @param JsonResultFactory $jsonFactory
* @param CaptchaHelper $captchaHelper
* @param LayoutInterface $layout
* @param JsonSerializer $serializer
*/
public function __construct(
Context $context,
RequestInterface $request,
JsonResultFactory $jsonFactory,
CaptchaHelper $captchaHelper,
LayoutInterface $layout,
JsonSerializer $serializer
) {
parent::__construct($context);
$this->request = $request;
$this->jsonResultFactory = $jsonFactory;
$this->captchaHelper = $captchaHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Captcha\Controller\Refresh\Index;
use Magento\Captcha\Helper\Data as CaptchaHelper;
use Magento\Captcha\Model\CaptchaInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\Json as ResultJson;
use Magento\Framework\Controller\Result\JsonFactory as ResultJsonFactory;
Expand Down Expand Up @@ -45,6 +46,9 @@ class IndexTest extends TestCase
/** @var MockObject|JsonSerializer */
private $jsonSerializerMock;

/** @var MockObject|Context */
private $contextMock;

/** @var Index */
private $refreshAction;

Expand All @@ -66,13 +70,16 @@ protected function setUp(): void
$this->jsonSerializerMock = $this->createMock(JsonSerializer::class);
$this->captchaHelperMock = $this->createMock(CaptchaHelper::class);

$this->contextMock = $this->createMock(Context::class);

$this->blockMock->method('setIsAjax')
->willReturnSelf();

$this->layoutMock->method('createBlock')
->willReturn($this->blockMock);

$this->refreshAction = new Index(
$this->contextMock,
$this->requestMock,
$this->jsonResultFactoryMock,
$this->captchaHelperMock,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Product\Webapi\Rest;

use Magento\Framework\Webapi\Rest\Request\DeserializerInterface;
use Magento\Framework\Webapi\Rest\Request\DeserializerFactory;
use Magento\Framework\Webapi\Rest\Request;

/**
* Class RequestTypeBasedDeserializer
*
* Used for deserialization rest request body.
* Runs appropriate deserialization class object based on request body content type.
*/
class RequestTypeBasedDeserializer implements DeserializerInterface
{
/**
* @var Request
*/
private $request;

/**
* @var DeserializerFactory
*/
private $deserializeFactory;

/**
* RequestTypeBasedDeserializer constructor.
*
* @param DeserializerFactory $deserializeFactory
* @param Request $request
*/
public function __construct(
DeserializerFactory $deserializeFactory,
Request $request
) {
$this->deserializeFactory = $deserializeFactory;
$this->request = $request;
}

/**
* @inheritdoc
*
* Parse request body into array of params with identifying request body content type
* to use appropriate instance of deserializer class
*
* @param string $body Posted content from request
* @return array|null Return NULL if content is invalid
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Webapi\Exception
*/
public function deserialize($body)
{
$deserializer = $this->deserializeFactory->get($this->request->getContentType());
return $deserializer->deserialize($body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Test\Unit\Model\Product\Webapi\Rest;

use Magento\Catalog\Model\Product\Webapi\Rest\RequestTypeBasedDeserializer;
use Magento\Framework\Webapi\Rest\Request\DeserializerFactory;
use Magento\Framework\Webapi\Rest\Request;
use PHPUnit\Framework\MockObject\MockObject;
use Magento\Framework\Webapi\Rest\Request\DeserializerInterface;
use Magento\Framework\Webapi\Rest\Request\Deserializer\Json as DeserializerJson;
use Magento\Framework\Webapi\Rest\Request\Deserializer\Xml as DeserializerXml;
use Magento\Framework\App\State;
use Magento\Framework\Json\Decoder;
use Magento\Framework\Serialize\Serializer\Json as SerializerJson;
use Magento\Framework\Xml\Parser as ParserXml;

class RequestTypeBasedDeserializerTest extends \PHPUnit\Framework\TestCase
{
/** @var RequestTypeBasedDeserializer */
private $requestTypeBasedDeserializer;
/**
* @var DeserializerFactory|MockObject
*/
private $deserializeFactoryMock;

/**
* @var Request|MockObject
*/
private $requestMock;

public function setUp(): void
{
/** @var DeserializerFactory|MockObject $deserializeFactoryMock */
$this->deserializeFactoryMock = $this->createMock(DeserializerFactory::class);
/** @var Request|MockObject $requestMock */
$this->requestMock = $this->createMock(Request::class);
/** @var requestTypeBasedDeserializer */
$this->requestTypeBasedDeserializer = new RequestTypeBasedDeserializer(
$this->deserializeFactoryMock,
$this->requestMock
);
}

/**
* Test RequestTypeBasedDeserializer::deserializeMethod()
*
* @dataProvider getDeserializerDataProvider
* @param string $body
* @param string $contentType
* @param DeserializerInterface $deserializer
* @param array $expectedResult
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Webapi\Exception
*/
public function testDeserialize(
string $body,
string $contentType,
DeserializerInterface $deserializer,
array $expectedResult
): void {
$this->requestMock->method('getContentType')
->willReturn($contentType);
$this->deserializeFactoryMock->expects($this->any())
->method('get')
->with($contentType)
->willReturn($deserializer);
$this->assertEquals($expectedResult, $this->requestTypeBasedDeserializer->deserialize($body));
}

public function getDeserializerDataProvider(): array
{
return [
'request body with xml data' => [
'body' => '<products>
<product>
<sku>testSku1</sku>
<name>testName1</name>
<weight>10</weight>
<attribute_set_id>4</attribute_set_id>
<status>1</status>
</product>
</products>',
'content-type' => 'application/xml',
'deserializer' => $this->prepareXmlDeserializer(),
'expectedResult' => [
'product' => [
'sku' => 'testSku1',
'name' => 'testName1',
'weight' => '10',
'attribute_set_id' => '4',
'status' => '1'
]
]
],
'request body with json data' => [
'body' => '{
"product": {
"sku": "testSku2",
"name": "testName2",
"weight": 5,
"attribute_set_id": 4,
"status": 0
}
}',
'content-type' => 'application/json',
'deserializer' => $this->prepareJsonDeserializer(),
'expectedResult' => [
'product' => [
'sku' => 'testSku2',
'name' => 'testName2',
'weight' => 5,
'attribute_set_id' => 4,
'status' => 0
]
]
]
];
}

/**
* Creates Json Deserializer instance with some mocked parameters
*
* @return DeserializerJson
*/
private function prepareJsonDeserializer(): DeserializerJson
{
/** @var Decoder|MockObject $decoder */
$decoder = $this->createMock(Decoder::class);
/** @var State|MockObject $appStateMock */
$appStateMock = $this->createMock(State::class);
$serializer = new SerializerJson();
return new DeserializerJson($decoder, $appStateMock, $serializer);
}

/**
* Creates XML Deserializer instance with some mocked parameters
*
* @return DeserializerXml
*/
private function prepareXmlDeserializer(): DeserializerXml
{
$parserXml = new ParserXml();
/** @var State|MockObject $appStateMock */
$appStateMock = $this->createMock(State::class);
return new DeserializerXml($parserXml, $appStateMock);
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/etc/webapi_rest/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</type>
<type name="Magento\Catalog\Model\Product\Webapi\ProductOutputProcessor">
<arguments>
<argument name="deserializer" xsi:type="object">Magento\Framework\Webapi\Rest\Request\Deserializer\Json</argument>
<argument name="deserializer" xsi:type="object">Magento\Catalog\Model\Product\Webapi\Rest\RequestTypeBasedDeserializer</argument>
</arguments>
</type>
</config>
9 changes: 7 additions & 2 deletions app/code/Magento/Checkout/Controller/Sidebar/RemoveItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use Exception;
use Magento\Checkout\Model\Sidebar;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\JsonFactory as ResultJsonFactory;
Expand All @@ -17,7 +19,7 @@
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;

class RemoveItem implements HttpPostActionInterface
class RemoveItem extends Action implements HttpPostActionInterface
{
/**
* @var RequestInterface
Expand All @@ -32,7 +34,7 @@ class RemoveItem implements HttpPostActionInterface
/**
* @var ResultRedirectFactory
*/
private $resultRedirectFactory;
protected $resultRedirectFactory;

/**
* @var Sidebar
Expand All @@ -50,6 +52,7 @@ class RemoveItem implements HttpPostActionInterface
protected $logger;

/**
* @param Context $context
* @param RequestInterface $request
* @param ResultJsonFactory $resultJsonFactory
* @param ResultRedirectFactory $resultRedirectFactory
Expand All @@ -58,13 +61,15 @@ class RemoveItem implements HttpPostActionInterface
* @param LoggerInterface $logger
*/
public function __construct(
Context $context,
RequestInterface $request,
ResultJsonFactory $resultJsonFactory,
ResultRedirectFactory $resultRedirectFactory,
Sidebar $sidebar,
Validator $formKeyValidator,
LoggerInterface $logger
) {
parent::__construct($context);
$this->request = $request;
$this->resultJsonFactory = $resultJsonFactory;
$this->resultRedirectFactory = $resultRedirectFactory;
Expand Down
7 changes: 6 additions & 1 deletion app/code/Magento/Cms/Controller/Page/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace Magento\Cms\Controller\Page;

use Magento\Cms\Helper\Page as PageHelper;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\RequestInterface;
Expand All @@ -17,7 +19,7 @@
/**
* Custom page for storefront. Needs to be accessible by POST because of the store switching.
*/
class View implements HttpGetActionInterface, HttpPostActionInterface
class View extends Action implements HttpGetActionInterface, HttpPostActionInterface
{
/**
* @var ForwardFactory
Expand All @@ -35,15 +37,18 @@ class View implements HttpGetActionInterface, HttpPostActionInterface
private $pageHelper;

/**
* @param Context $context
* @param RequestInterface $request
* @param PageHelper $pageHelper
* @param ForwardFactory $resultForwardFactory
*/
public function __construct(
Context $context,
RequestInterface $request,
PageHelper $pageHelper,
ForwardFactory $resultForwardFactory
) {
parent::__construct($context);
$this->request = $request;
$this->pageHelper = $pageHelper;
$this->resultForwardFactory = $resultForwardFactory;
Expand Down
4 changes: 0 additions & 4 deletions app/code/Magento/Eav/etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,6 @@
referenceColumn="attribute_id" onDelete="CASCADE"/>
<constraint xsi:type="foreign" referenceId="EAV_ATTRIBUTE_LABEL_STORE_ID_STORE_STORE_ID" table="eav_attribute_label"
column="store_id" referenceTable="store" referenceColumn="store_id" onDelete="CASCADE"/>
<constraint xsi:type="unique" referenceId="EAV_ATTRIBUTE_LABEL_ATTRIBUTE_ID_STORE_ID_UNIQUE">
<column name="store_id"/>
<column name="attribute_id"/>
</constraint>
<index referenceId="EAV_ATTRIBUTE_LABEL_STORE_ID" indexType="btree">
<column name="store_id"/>
</index>
Expand Down
Loading

0 comments on commit f824b9e

Please sign in to comment.