diff --git a/app/code/Magento/Captcha/Controller/Adminhtml/Refresh/Refresh.php b/app/code/Magento/Captcha/Controller/Adminhtml/Refresh/Refresh.php index 9c7ccc0983db7..47e0103dd1907 100644 --- a/app/code/Magento/Captcha/Controller/Adminhtml/Refresh/Refresh.php +++ b/app/code/Magento/Captcha/Controller/Adminhtml/Refresh/Refresh.php @@ -10,13 +10,39 @@ class Refresh extends \Magento\Backend\App\Action { + /** + * @var \Magento\Framework\Serialize\Serializer\Json + */ + protected $serializer; + + /** + * @var \Magento\Captcha\Helper\Data + */ + protected $captchaHelper; + + /** + * Refresh constructor. + * @param \Magento\Backend\App\Action\Context $context + * @param \Magento\Captcha\Helper\Data $captchaHelper + * @param \Magento\Framework\Serialize\Serializer\Json $serializer + */ + public function __construct( + \Magento\Backend\App\Action\Context $context, + \Magento\Captcha\Helper\Data $captchaHelper, + \Magento\Framework\Serialize\Serializer\Json $serializer + ) { + parent::__construct($context); + $this->serializer = $serializer; + $this->captchaHelper = $captchaHelper; + } + /** * {@inheritdoc} */ public function execute() { $formId = $this->getRequest()->getPost('formId'); - $captchaModel = $this->_objectManager->get(\Magento\Captcha\Helper\Data::class)->getCaptcha($formId); + $captchaModel = $this->captchaHelper->getCaptcha($formId); $this->_view->getLayout()->createBlock( $captchaModel->getBlockName() )->setFormId( @@ -24,7 +50,7 @@ public function execute() )->setIsAjax( true )->toHtml(); - $this->getResponse()->representJson(json_encode(['imgSrc' => $captchaModel->getImgSrc()])); + $this->getResponse()->representJson($this->serializer->serialize(['imgSrc' => $captchaModel->getImgSrc()])); $this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true); } diff --git a/app/code/Magento/Captcha/Controller/Refresh/Index.php b/app/code/Magento/Captcha/Controller/Refresh/Index.php index 0124adc348bb7..ef757d1b2fc35 100644 --- a/app/code/Magento/Captcha/Controller/Refresh/Index.php +++ b/app/code/Magento/Captcha/Controller/Refresh/Index.php @@ -17,13 +17,25 @@ class Index extends \Magento\Framework\App\Action\Action */ protected $captchaHelper; + /** + * @var \Magento\Framework\Serialize\Serializer\Json + */ + protected $serializer; + /** * @param Context $context * @param \Magento\Captcha\Helper\Data $captchaHelper + * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer + * @throws \RuntimeException */ - public function __construct(Context $context, \Magento\Captcha\Helper\Data $captchaHelper) - { + public function __construct( + Context $context, + \Magento\Captcha\Helper\Data $captchaHelper, + \Magento\Framework\Serialize\Serializer\Json $serializer = null + ) { $this->captchaHelper = $captchaHelper; + $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Serialize\Serializer\Json::class); parent::__construct($context); } @@ -34,23 +46,19 @@ public function execute() { $formId = $this->_request->getPost('formId'); if (null === $formId) { - try { - $params = []; - $content = $this->_request->getContent(); - if ($content) { - $params = \Zend_Json::decode($content); - } - $formId = isset($params['formId']) ? $params['formId'] : null; - } catch (\Zend_Json_Exception $exception) { - $formId = null; + $params = []; + $content = $this->_request->getContent(); + if ($content) { + $params = $this->serializer->unserialize($content); } + $formId = isset($params['formId']) ? $params['formId'] : null; } $captchaModel = $this->captchaHelper->getCaptcha($formId); $captchaModel->generate(); $block = $this->_view->getLayout()->createBlock($captchaModel->getBlockName()); $block->setFormId($formId)->setIsAjax(true)->toHtml(); - $this->_response->representJson(json_encode(['imgSrc' => $captchaModel->getImgSrc()])); + $this->_response->representJson($this->serializer->serialize(['imgSrc' => $captchaModel->getImgSrc()])); $this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true); } } diff --git a/app/code/Magento/Captcha/Model/Customer/Plugin/AjaxLogin.php b/app/code/Magento/Captcha/Model/Customer/Plugin/AjaxLogin.php index d2f3cbda170b4..a21c007056e73 100644 --- a/app/code/Magento/Captcha/Model/Customer/Plugin/AjaxLogin.php +++ b/app/code/Magento/Captcha/Model/Customer/Plugin/AjaxLogin.php @@ -26,6 +26,11 @@ class AjaxLogin */ protected $resultJsonFactory; + /** + * @var \Magento\Framework\Serialize\Serializer\Json + */ + protected $serializer; + /** * @var array */ @@ -36,16 +41,21 @@ class AjaxLogin * @param SessionManagerInterface $sessionManager * @param JsonFactory $resultJsonFactory * @param array $formIds + * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer + * @throws \RuntimeException */ public function __construct( CaptchaHelper $helper, SessionManagerInterface $sessionManager, JsonFactory $resultJsonFactory, - array $formIds + array $formIds, + \Magento\Framework\Serialize\Serializer\Json $serializer = null ) { $this->helper = $helper; $this->sessionManager = $sessionManager; $this->resultJsonFactory = $resultJsonFactory; + $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Serialize\Serializer\Json::class); $this->formIds = $formIds; } @@ -53,7 +63,6 @@ public function __construct( * @param \Magento\Customer\Controller\Ajax\Login $subject * @param \Closure $proceed * @return $this - * @throws \Zend_Json_Exception * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ @@ -70,7 +79,7 @@ public function aroundExecute( $loginParams = []; $content = $request->getContent(); if ($content) { - $loginParams = \Zend_Json::decode($content); + $loginParams = $this->serializer->unserialize($content); } $username = isset($loginParams['username']) ? $loginParams['username'] : null; $captchaString = isset($loginParams[$captchaInputName]) ? $loginParams[$captchaInputName] : null; diff --git a/app/code/Magento/Captcha/Test/Unit/Controller/Refresh/IndexTest.php b/app/code/Magento/Captcha/Test/Unit/Controller/Refresh/IndexTest.php index a99ec53ab2400..5fcb4c4fe8828 100644 --- a/app/code/Magento/Captcha/Test/Unit/Controller/Refresh/IndexTest.php +++ b/app/code/Magento/Captcha/Test/Unit/Controller/Refresh/IndexTest.php @@ -47,6 +47,11 @@ class IndexTest extends \PHPUnit_Framework_TestCase */ protected $flagMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $serializerMock; + /** * @var \Magento\Captcha\Controller\Refresh\Index */ @@ -62,6 +67,13 @@ protected function setUp() $this->viewMock = $this->getMock(\Magento\Framework\App\ViewInterface::class); $this->layoutMock = $this->getMock(\Magento\Framework\View\LayoutInterface::class); $this->flagMock = $this->getMock(\Magento\Framework\App\ActionFlag::class, [], [], '', false); + $this->serializerMock = $this->getMock( + \Magento\Framework\Serialize\Serializer\Json::class, + [], + [], + '', + false + ); $this->contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock)); $this->contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewMock)); @@ -69,17 +81,23 @@ protected function setUp() $this->contextMock->expects($this->any())->method('getActionFlag')->will($this->returnValue($this->flagMock)); $this->viewMock->expects($this->any())->method('getLayout')->will($this->returnValue($this->layoutMock)); - $this->model = new \Magento\Captcha\Controller\Refresh\Index($this->contextMock, $this->captchaHelperMock); + $this->model = new \Magento\Captcha\Controller\Refresh\Index( + $this->contextMock, + $this->captchaHelperMock, + $this->serializerMock + ); } /** * @dataProvider executeDataProvider * @param int $formId * @param int $callsNumber + * @throws \PHPUnit_Framework_Exception */ public function testExecute($formId, $callsNumber) { $content = ['formId' => $formId]; + $imgSource = ['imgSrc' => 'source']; $blockMethods = ['setFormId', 'setIsAjax', 'toHtml']; $blockMock = $this->getMock(\Magento\Captcha\Block\Captcha::class, $blockMethods, [], '', false); @@ -97,8 +115,12 @@ public function testExecute($formId, $callsNumber) $blockMock->expects($this->any())->method('setFormId')->with($formId)->will($this->returnValue($blockMock)); $blockMock->expects($this->any())->method('setIsAjax')->with(true)->will($this->returnValue($blockMock)); $blockMock->expects($this->once())->method('toHtml'); - $this->responseMock->expects($this->once())->method('representJson')->with(json_encode(['imgSrc' => 'source'])); + $this->responseMock->expects($this->once())->method('representJson')->with(json_encode($imgSource)); $this->flagMock->expects($this->once())->method('set')->with('', 'no-postDispatch', true); + $this->serializerMock->expects($this->exactly($callsNumber)) + ->method('unserialize')->will($this->returnValue($content)); + $this->serializerMock->expects($this->once()) + ->method('serialize')->will($this->returnValue(json_encode($imgSource))); $this->model->execute(); } diff --git a/app/code/Magento/Captcha/Test/Unit/Model/Customer/Plugin/AjaxLoginTest.php b/app/code/Magento/Captcha/Test/Unit/Model/Customer/Plugin/AjaxLoginTest.php index 8047138e34528..858b4bc8d9ebc 100644 --- a/app/code/Magento/Captcha/Test/Unit/Model/Customer/Plugin/AjaxLoginTest.php +++ b/app/code/Magento/Captcha/Test/Unit/Model/Customer/Plugin/AjaxLoginTest.php @@ -42,6 +42,11 @@ class AjaxLoginTest extends \PHPUnit_Framework_TestCase */ protected $loginControllerMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $serializerMock; + /** * @var array */ @@ -79,12 +84,20 @@ protected function setUp() $this->captchaHelperMock->expects($this->once())->method('getCaptcha') ->with('user_login')->will($this->returnValue($this->captchaMock)); $this->formIds = ['user_login']; + $this->serializerMock = $this->getMock( + \Magento\Framework\Serialize\Serializer\Json::class, + [], + [], + '', + false + ); $this->model = new \Magento\Captcha\Model\Customer\Plugin\AjaxLogin( $this->captchaHelperMock, $this->sessionManagerMock, $this->jsonFactoryMock, - $this->formIds + $this->formIds, + $this->serializerMock ); } @@ -92,11 +105,12 @@ public function testAroundExecute() { $username = 'name'; $captchaString = 'string'; - $requestContent = json_encode([ + $requestData = [ 'username' => $username, 'captcha_string' => $captchaString, 'captcha_form_id' => $this->formIds[0] - ]); + ]; + $requestContent = json_encode($requestData); $this->requestMock->expects($this->once())->method('getContent')->will($this->returnValue($requestContent)); $this->captchaMock->expects($this->once())->method('isRequired')->with($username) @@ -104,6 +118,7 @@ public function testAroundExecute() $this->captchaMock->expects($this->once())->method('logAttempt')->with($username); $this->captchaMock->expects($this->once())->method('isCorrect')->with($captchaString) ->will($this->returnValue(true)); + $this->serializerMock->expects(($this->once()))->method('unserialize')->will($this->returnValue($requestData)); $closure = function () { return 'result'; @@ -115,11 +130,12 @@ public function testAroundExecuteIncorrectCaptcha() { $username = 'name'; $captchaString = 'string'; - $requestContent = json_encode([ + $requestData = [ 'username' => $username, 'captcha_string' => $captchaString, 'captcha_form_id' => $this->formIds[0] - ]); + ]; + $requestContent = json_encode($requestData); $this->requestMock->expects($this->once())->method('getContent')->will($this->returnValue($requestContent)); $this->captchaMock->expects($this->once())->method('isRequired')->with($username) @@ -127,6 +143,7 @@ public function testAroundExecuteIncorrectCaptcha() $this->captchaMock->expects($this->once())->method('logAttempt')->with($username); $this->captchaMock->expects($this->once())->method('isCorrect') ->with($captchaString)->will($this->returnValue(false)); + $this->serializerMock->expects(($this->once()))->method('unserialize')->will($this->returnValue($requestData)); $this->sessionManagerMock->expects($this->once())->method('setUsername')->with($username); $this->jsonFactoryMock->expects($this->once())->method('create') @@ -147,7 +164,10 @@ public function testAroundExecuteIncorrectCaptcha() */ public function testAroundExecuteCaptchaIsNotRequired($username, $requestContent) { - $this->requestMock->expects($this->once())->method('getContent')->will($this->returnValue($requestContent)); + $this->requestMock->expects($this->once())->method('getContent') + ->will($this->returnValue(json_encode($requestContent))); + $this->serializerMock->expects(($this->once()))->method('unserialize') + ->will($this->returnValue($requestContent)); $this->captchaMock->expects($this->once())->method('isRequired')->with($username) ->will($this->returnValue(false)); @@ -168,11 +188,11 @@ public function aroundExecuteCaptchaIsNotRequired() return [ [ 'username' => 'name', - 'requestContent' => json_encode(['username' => 'name', 'captcha_string' => 'string']), + 'requestData' => ['username' => 'name', 'captcha_string' => 'string'], ], [ 'username' => null, - 'requestContent' => json_encode(['captcha_string' => 'string']), + 'requestData' => ['captcha_string' => 'string'], ], ]; }