Skip to content

Commit 7c482c7

Browse files
committed
Fix failing Unit Test and refactor main class
1 parent 06f78cf commit 7c482c7

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

app/code/Magento/Captcha/Controller/Refresh/Index.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Captcha\Controller\Refresh;
79

810
use Magento\Captcha\Helper\Data as CaptchaHelper;
@@ -68,6 +70,26 @@ public function __construct(
6870
* @inheritdoc
6971
*/
7072
public function execute()
73+
{
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
7193
{
7294
$formId = $this->request->getPost('formId');
7395
if (null === $formId) {
@@ -76,15 +98,10 @@ public function execute()
7698
if ($content) {
7799
$params = $this->serializer->unserialize($content);
78100
}
79-
$formId = isset($params['formId']) ? $params['formId'] : null;
80-
}
81-
$captchaModel = $this->captchaHelper->getCaptcha($formId);
82-
$captchaModel->generate();
83101

84-
$block = $this->layout->createBlock($captchaModel->getBlockName());
85-
$block->setFormId($formId)->setIsAjax(true)->toHtml();
102+
$formId = $params['formId'] ?? null;
103+
}
86104

87-
$result = $this->jsonResultFactory->create();
88-
return $result->setData(['imgSrc' => $captchaModel->getImgSrc()]);
105+
return $formId !== null ? (string)$formId : null;
89106
}
90107
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Captcha\Helper\Data as CaptchaHelper;
1010
use Magento\Captcha\Model\CaptchaInterface;
1111
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\Controller\Result\Json as ResultJson;
1213
use Magento\Framework\Controller\Result\JsonFactory as ResultJsonFactory;
1314
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
1415
use Magento\Framework\View\Element\BlockInterface;
@@ -27,6 +28,9 @@ class IndexTest extends TestCase
2728
/** @var MockObject|ResultJsonFactory */
2829
private $jsonResultFactoryMock;
2930

31+
/** @var MockObject|ResultJson */
32+
private $jsonResultMock;
33+
3034
/** @var MockObject|CaptchaHelper */
3135
private $captchaHelperMock;
3236

@@ -54,6 +58,9 @@ protected function setUp()
5458
->setMethods(['setFormId', 'setIsAjax', 'toHtml'])
5559
->getMockForAbstractClass();
5660
$this->jsonResultFactoryMock = $this->createMock(ResultJsonFactory::class);
61+
$this->jsonResultMock = $this->createMock(ResultJson::class);
62+
$this->jsonResultFactoryMock->method('create')
63+
->willReturn($this->jsonResultMock);
5764
$this->jsonSerializerMock = $this->createMock(JsonSerializer::class);
5865
$this->captchaHelperMock = $this->createMock(CaptchaHelper::class);
5966

0 commit comments

Comments
 (0)