Skip to content

Remove Zend1 Json from Magento Captcha module #8331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions app/code/Magento/Captcha/Controller/Adminhtml/Refresh/Refresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,47 @@

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(
$formId
)->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);
}

Expand Down
32 changes: 20 additions & 12 deletions app/code/Magento/Captcha/Controller/Refresh/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
}
15 changes: 12 additions & 3 deletions app/code/Magento/Captcha/Model/Customer/Plugin/AjaxLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class AjaxLogin
*/
protected $resultJsonFactory;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
protected $serializer;

/**
* @var array
*/
Expand All @@ -36,24 +41,28 @@ 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Magento\Framework\Serialize\Serializer\Json instead of SerializerInterface, since we can not guarantee that preference will remain Json in the future.

->get(\Magento\Framework\Serialize\Serializer\Json::class);
$this->formIds = $formIds;
}

/**
* @param \Magento\Customer\Controller\Ajax\Login $subject
* @param \Closure $proceed
* @return $this
* @throws \Zend_Json_Exception
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -62,24 +67,37 @@ 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));
$this->contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
$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);
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class AjaxLoginTest extends \PHPUnit_Framework_TestCase
*/
protected $loginControllerMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $serializerMock;

/**
* @var array
*/
Expand Down Expand Up @@ -79,31 +84,41 @@ 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
);
}

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)
->will($this->returnValue(true));
$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';
Expand All @@ -115,18 +130,20 @@ 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)
->will($this->returnValue(true));
$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')
Expand All @@ -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));
Expand All @@ -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'],
],
];
}
Expand Down