Skip to content

Commit 63d31d3

Browse files
committed
Merge branch '2.4-develop' of github.com:magento/magento2ce into MFTF-3.0.0
2 parents c033356 + 715d9de commit 63d31d3

File tree

56 files changed

+3253
-635
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3253
-635
lines changed
Lines changed: 72 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,107 @@
11
<?php
22
/**
3-
* Refreshes captcha and returns JSON encoded URL to image (AJAX action)
4-
* Example: {'imgSrc': 'http://example.com/media/captcha/67842gh187612ngf8s.png'}
5-
*
63
* Copyright © Magento, Inc. All rights reserved.
74
* See COPYING.txt for license details.
85
*/
6+
declare(strict_types=1);
7+
98
namespace Magento\Captcha\Controller\Refresh;
109

11-
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
12-
use Magento\Framework\App\Action\Context;
10+
use Magento\Captcha\Helper\Data as CaptchaHelper;
11+
use Magento\Framework\App\Action\HttpPostActionInterface;
12+
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\Controller\Result\JsonFactory as JsonResultFactory;
14+
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
15+
use Magento\Framework\View\LayoutInterface;
1316

14-
class Index extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface
17+
/**
18+
* Refreshes captcha and returns JSON encoded URL to image (AJAX action)
19+
* Example: {'imgSrc': 'http://example.com/media/captcha/67842gh187612ngf8s.png'}
20+
*/
21+
class Index implements HttpPostActionInterface
1522
{
1623
/**
17-
* @var \Magento\Captcha\Helper\Data
24+
* @var CaptchaHelper
25+
*/
26+
private $captchaHelper;
27+
28+
/**
29+
* @var JsonSerializer
30+
*/
31+
private $serializer;
32+
33+
/**
34+
* @var RequestInterface
35+
*/
36+
private $request;
37+
38+
/**
39+
* @var LayoutInterface
1840
*/
19-
protected $captchaHelper;
41+
private $layout;
2042

2143
/**
22-
* @var \Magento\Framework\Serialize\Serializer\Json
44+
* @var JsonResultFactory
2345
*/
24-
protected $serializer;
46+
private $jsonResultFactory;
2547

2648
/**
27-
* @param Context $context
28-
* @param \Magento\Captcha\Helper\Data $captchaHelper
29-
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
30-
* @throws \RuntimeException
49+
* @param RequestInterface $request
50+
* @param JsonResultFactory $jsonFactory
51+
* @param CaptchaHelper $captchaHelper
52+
* @param LayoutInterface $layout
53+
* @param JsonSerializer $serializer
3154
*/
3255
public function __construct(
33-
Context $context,
34-
\Magento\Captcha\Helper\Data $captchaHelper,
35-
\Magento\Framework\Serialize\Serializer\Json $serializer = null
56+
RequestInterface $request,
57+
JsonResultFactory $jsonFactory,
58+
CaptchaHelper $captchaHelper,
59+
LayoutInterface $layout,
60+
JsonSerializer $serializer
3661
) {
62+
$this->request = $request;
63+
$this->jsonResultFactory = $jsonFactory;
3764
$this->captchaHelper = $captchaHelper;
38-
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
39-
->get(\Magento\Framework\Serialize\Serializer\Json::class);
40-
parent::__construct($context);
65+
$this->layout = $layout;
66+
$this->serializer = $serializer;
4167
}
4268

4369
/**
44-
* {@inheritdoc}
70+
* @inheritdoc
4571
*/
4672
public function execute()
4773
{
48-
$formId = $this->_request->getPost('formId');
74+
$formId = $this->getRequestFormId();
75+
76+
$captchaModel = $this->captchaHelper->getCaptcha($formId);
77+
$captchaModel->generate();
78+
79+
$block = $this->layout->createBlock($captchaModel->getBlockName());
80+
$block->setFormId($formId)->setIsAjax(true)->toHtml();
81+
82+
$result = $this->jsonResultFactory->create();
83+
84+
return $result->setData(['imgSrc' => $captchaModel->getImgSrc()]);
85+
}
86+
87+
/**
88+
* Returns requested Form ID
89+
*
90+
* @return string|null
91+
*/
92+
private function getRequestFormId(): ?string
93+
{
94+
$formId = $this->request->getPost('formId');
4995
if (null === $formId) {
5096
$params = [];
51-
$content = $this->_request->getContent();
97+
$content = $this->request->getContent();
5298
if ($content) {
5399
$params = $this->serializer->unserialize($content);
54100
}
55-
$formId = isset($params['formId']) ? $params['formId'] : null;
101+
102+
$formId = $params['formId'] ?? null;
56103
}
57-
$captchaModel = $this->captchaHelper->getCaptcha($formId);
58-
$captchaModel->generate();
59104

60-
$block = $this->_view->getLayout()->createBlock($captchaModel->getBlockName());
61-
$block->setFormId($formId)->setIsAjax(true)->toHtml();
62-
$this->_response->representJson($this->serializer->serialize(['imgSrc' => $captchaModel->getImgSrc()]));
63-
$this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true);
105+
return $formId !== null ? (string)$formId : null;
64106
}
65107
}

app/code/Magento/Captcha/Test/Unit/Controller/Refresh/IndexTest.php

Lines changed: 113 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -5,133 +5,140 @@
55
*/
66
namespace Magento\Captcha\Test\Unit\Controller\Refresh;
77

8-
class IndexTest extends \PHPUnit\Framework\TestCase
8+
use Magento\Captcha\Controller\Refresh\Index;
9+
use Magento\Captcha\Helper\Data as CaptchaHelper;
10+
use Magento\Captcha\Model\CaptchaInterface;
11+
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\Controller\Result\Json as ResultJson;
13+
use Magento\Framework\Controller\Result\JsonFactory as ResultJsonFactory;
14+
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
15+
use Magento\Framework\View\Element\BlockInterface;
16+
use Magento\Framework\View\LayoutInterface;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class IndexTest extends TestCase
921
{
10-
/**
11-
* @var \PHPUnit_Framework_MockObject_MockObject
12-
*/
13-
protected $captchaHelperMock;
22+
private const STUB_FORM_ID = 'StubFormId';
23+
private const STUB_CAPTCHA_SOURCE = '/stub-captcha-source.jpg';
1424

15-
/**
16-
* @var \PHPUnit_Framework_MockObject_MockObject
17-
*/
18-
protected $captchaMock;
19-
20-
/**
21-
* @var \PHPUnit_Framework_MockObject_MockObject
22-
*/
23-
protected $requestMock;
25+
/** @var MockObject|RequestInterface */
26+
private $requestMock;
2427

25-
/**
26-
* @var \PHPUnit_Framework_MockObject_MockObject
27-
*/
28-
protected $responseMock;
28+
/** @var MockObject|ResultJsonFactory */
29+
private $jsonResultFactoryMock;
2930

30-
/**
31-
* @var \PHPUnit_Framework_MockObject_MockObject
32-
*/
33-
protected $contextMock;
31+
/** @var MockObject|ResultJson */
32+
private $jsonResultMock;
3433

35-
/**
36-
* @var \PHPUnit_Framework_MockObject_MockObject
37-
*/
38-
protected $viewMock;
34+
/** @var MockObject|CaptchaHelper */
35+
private $captchaHelperMock;
3936

40-
/**
41-
* @var \PHPUnit_Framework_MockObject_MockObject
42-
*/
43-
protected $layoutMock;
37+
/** @var MockObject|LayoutInterface */
38+
private $layoutMock;
4439

45-
/**
46-
* @var \PHPUnit_Framework_MockObject_MockObject
47-
*/
48-
protected $flagMock;
40+
/** @var MockObject|BlockInterface */
41+
private $blockMock;
4942

50-
/**
51-
* @var \PHPUnit_Framework_MockObject_MockObject
52-
*/
53-
protected $serializerMock;
43+
/** @var MockObject|JsonSerializer */
44+
private $jsonSerializerMock;
5445

55-
/**
56-
* @var \Magento\Captcha\Controller\Refresh\Index
57-
*/
58-
protected $model;
46+
/** @var Index */
47+
private $refreshAction;
5948

6049
protected function setUp()
6150
{
62-
$this->captchaHelperMock = $this->createMock(\Magento\Captcha\Helper\Data::class);
63-
$this->captchaMock = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
64-
$this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
65-
$this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
66-
$this->contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
67-
$this->viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
68-
$this->layoutMock = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
69-
$this->flagMock = $this->createMock(\Magento\Framework\App\ActionFlag::class);
70-
$this->serializerMock = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
71-
72-
$this->contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock));
73-
$this->contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewMock));
74-
$this->contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
75-
$this->contextMock->expects($this->any())->method('getActionFlag')->will($this->returnValue($this->flagMock));
76-
$this->viewMock->expects($this->any())->method('getLayout')->will($this->returnValue($this->layoutMock));
77-
78-
$this->model = new \Magento\Captcha\Controller\Refresh\Index(
79-
$this->contextMock,
51+
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
52+
->setMethods(['getPost', 'getContent'])
53+
->getMockForAbstractClass();
54+
$this->layoutMock = $this->getMockBuilder(LayoutInterface::class)
55+
->setMethods(['createBlock'])
56+
->getMockForAbstractClass();
57+
$this->blockMock = $this->getMockBuilder(BlockInterface::class)
58+
->setMethods(['setFormId', 'setIsAjax', 'toHtml'])
59+
->getMockForAbstractClass();
60+
$this->jsonResultFactoryMock = $this->createMock(ResultJsonFactory::class);
61+
$this->jsonResultMock = $this->createMock(ResultJson::class);
62+
$this->jsonResultFactoryMock->method('create')
63+
->willReturn($this->jsonResultMock);
64+
$this->jsonSerializerMock = $this->createMock(JsonSerializer::class);
65+
$this->captchaHelperMock = $this->createMock(CaptchaHelper::class);
66+
67+
$this->blockMock->method('setIsAjax')
68+
->willReturnSelf();
69+
70+
$this->layoutMock->method('createBlock')
71+
->willReturn($this->blockMock);
72+
73+
$this->refreshAction = new Index(
74+
$this->requestMock,
75+
$this->jsonResultFactoryMock,
8076
$this->captchaHelperMock,
81-
$this->serializerMock
77+
$this->layoutMock,
78+
$this->jsonSerializerMock
8279
);
8380
}
8481

85-
/**
86-
* @dataProvider executeDataProvider
87-
* @param int $formId
88-
* @param int $callsNumber
89-
*/
90-
public function testExecute($formId, $callsNumber)
82+
public function testCaptchaGeneratedWhenPostDataContainsFormId()
9183
{
92-
$content = ['formId' => $formId];
93-
$imgSource = ['imgSrc' => 'source'];
94-
95-
$blockMethods = ['setFormId', 'setIsAjax', 'toHtml'];
96-
$blockMock = $this->createPartialMock(\Magento\Captcha\Block\Captcha::class, $blockMethods);
97-
98-
$this->requestMock->expects($this->any())->method('getPost')->with('formId')->will($this->returnValue($formId));
99-
$this->requestMock->expects($this->exactly($callsNumber))->method('getContent')
100-
->will($this->returnValue(json_encode($content)));
101-
$this->captchaHelperMock->expects($this->any())->method('getCaptcha')->with($formId)
102-
->will($this->returnValue($this->captchaMock));
103-
$this->captchaMock->expects($this->once())->method('generate');
104-
$this->captchaMock->expects($this->once())->method('getBlockName')->will($this->returnValue('block'));
105-
$this->captchaMock->expects($this->once())->method('getImgSrc')->will($this->returnValue('source'));
106-
$this->layoutMock->expects($this->once())->method('createBlock')->with('block')
107-
->will($this->returnValue($blockMock));
108-
$blockMock->expects($this->any())->method('setFormId')->with($formId)->will($this->returnValue($blockMock));
109-
$blockMock->expects($this->any())->method('setIsAjax')->with(true)->will($this->returnValue($blockMock));
110-
$blockMock->expects($this->once())->method('toHtml');
111-
$this->responseMock->expects($this->once())->method('representJson')->with(json_encode($imgSource));
112-
$this->flagMock->expects($this->once())->method('set')->with('', 'no-postDispatch', true);
113-
$this->serializerMock->expects($this->exactly($callsNumber))
114-
->method('unserialize')->will($this->returnValue($content));
115-
$this->serializerMock->expects($this->once())
116-
->method('serialize')->will($this->returnValue(json_encode($imgSource)));
117-
118-
$this->model->execute();
84+
// Given
85+
$this->requestMock->method('getPost')
86+
->with('formId')
87+
->willReturn(self::STUB_FORM_ID);
88+
$this->blockMock->method('setFormId')
89+
->willReturnSelf();
90+
91+
// Expect
92+
$this->requestMock->expects($this->never())
93+
->method('getContent');
94+
$this->captchaHelperMock->expects($this->once())
95+
->method('getCaptcha')
96+
->with(self::STUB_FORM_ID)
97+
->willReturn(
98+
$this->getCaptchaModelMock(self::STUB_CAPTCHA_SOURCE)
99+
);
100+
101+
// When
102+
$this->refreshAction->execute();
103+
}
104+
105+
public function testCaptchaFallsBackToRequestContentIfPostMissing()
106+
{
107+
// Given
108+
$this->requestMock->method('getPost')
109+
->with('formId')
110+
->willReturn(null);
111+
$this->blockMock->method('setFormId')
112+
->willReturnSelf();
113+
114+
// Expect
115+
$this->requestMock->expects(self::once())
116+
->method('getContent')
117+
->willReturn(null);
118+
$this->captchaHelperMock->expects($this->once())
119+
->method('getCaptcha')
120+
->with(null)
121+
->willReturn(
122+
$this->getCaptchaModelMock(self::STUB_CAPTCHA_SOURCE)
123+
);
124+
125+
// When
126+
$this->refreshAction->execute();
119127
}
120128

121129
/**
122-
* @return array
130+
* @param string $imageSource
131+
* @return MockObject|CaptchaInterface
123132
*/
124-
public function executeDataProvider()
133+
private function getCaptchaModelMock(string $imageSource): CaptchaInterface
125134
{
126-
return [
127-
[
128-
'formId' => null,
129-
'callsNumber' => 1,
130-
],
131-
[
132-
'formId' => 1,
133-
'callsNumber' => 0,
134-
]
135-
];
135+
$modelMock = $this->getMockBuilder(CaptchaInterface::class)
136+
->setMethods(['generate', 'getBlockName', 'getImgSrc'])
137+
->getMockForAbstractClass();
138+
139+
$modelMock->method('getImgSrc')
140+
->willReturn($imageSource);
141+
142+
return $modelMock;
136143
}
137144
}

app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ private function formatValues($values)
4545
{
4646
if (isset($values[$this->getOption()->getId()])) {
4747
$value = $values[$this->getOption()->getId()];
48+
if (isset($value['date']) || isset($value['day'], $value['month'], $value['year'])) {
49+
return $values;
50+
}
4851
$dateTime = \DateTime::createFromFormat(DateTime::DATETIME_PHP_FORMAT, $value);
4952

5053
if ($dateTime === false) {

0 commit comments

Comments
 (0)