AjaxLoginTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Captcha\Test\Unit\Model\Customer\Plugin;
  7. class AjaxLoginTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Checkout\Model\Session
  11. */
  12. protected $sessionManagerMock;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Captcha\Helper\Data
  15. */
  16. protected $captchaHelperMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Controller\Result\JsonFactory
  19. */
  20. protected $jsonFactoryMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $captchaMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $resultJsonMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $requestMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Controller\Ajax\Login
  35. */
  36. protected $loginControllerMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Serialize\Serializer\Json
  39. */
  40. protected $serializerMock;
  41. /**
  42. * @var array
  43. */
  44. protected $formIds;
  45. /**
  46. * @var \Magento\Captcha\Model\Customer\Plugin\AjaxLogin
  47. */
  48. protected $model;
  49. /**
  50. * @inheritdoc
  51. */
  52. protected function setUp()
  53. {
  54. $this->sessionManagerMock = $this->createPartialMock(\Magento\Checkout\Model\Session::class, ['setUsername']);
  55. $this->captchaHelperMock = $this->createMock(\Magento\Captcha\Helper\Data::class);
  56. $this->captchaMock = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
  57. $this->jsonFactoryMock = $this->createPartialMock(
  58. \Magento\Framework\Controller\Result\JsonFactory::class,
  59. ['create']
  60. );
  61. $this->resultJsonMock = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
  62. $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  63. $this->loginControllerMock = $this->createMock(\Magento\Customer\Controller\Ajax\Login::class);
  64. $this->loginControllerMock->expects($this->any())->method('getRequest')
  65. ->will($this->returnValue($this->requestMock));
  66. $this->captchaHelperMock
  67. ->expects($this->exactly(1))
  68. ->method('getCaptcha')
  69. ->will($this->returnValue($this->captchaMock));
  70. $this->formIds = ['user_login'];
  71. $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
  72. $this->model = new \Magento\Captcha\Model\Customer\Plugin\AjaxLogin(
  73. $this->captchaHelperMock,
  74. $this->sessionManagerMock,
  75. $this->jsonFactoryMock,
  76. $this->formIds,
  77. $this->serializerMock
  78. );
  79. }
  80. /**
  81. * Test aroundExecute.
  82. */
  83. public function testAroundExecute()
  84. {
  85. $username = 'name';
  86. $captchaString = 'string';
  87. $requestData = [
  88. 'username' => $username,
  89. 'captcha_string' => $captchaString,
  90. 'captcha_form_id' => $this->formIds[0]
  91. ];
  92. $requestContent = json_encode($requestData);
  93. $this->requestMock->expects($this->once())->method('getContent')->will($this->returnValue($requestContent));
  94. $this->captchaMock->expects($this->once())->method('isRequired')->with($username)
  95. ->will($this->returnValue(true));
  96. $this->captchaMock->expects($this->once())->method('logAttempt')->with($username);
  97. $this->captchaMock->expects($this->once())->method('isCorrect')->with($captchaString)
  98. ->will($this->returnValue(true));
  99. $this->serializerMock->expects($this->once())->method('unserialize')->will($this->returnValue($requestData));
  100. $closure = function () {
  101. return 'result';
  102. };
  103. $this->captchaHelperMock
  104. ->expects($this->exactly(1))
  105. ->method('getCaptcha')
  106. ->with('user_login')
  107. ->will($this->returnValue($this->captchaMock));
  108. $this->assertEquals('result', $this->model->aroundExecute($this->loginControllerMock, $closure));
  109. }
  110. /**
  111. * Test aroundExecuteIncorrectCaptcha.
  112. */
  113. public function testAroundExecuteIncorrectCaptcha()
  114. {
  115. $username = 'name';
  116. $captchaString = 'string';
  117. $requestData = [
  118. 'username' => $username,
  119. 'captcha_string' => $captchaString,
  120. 'captcha_form_id' => $this->formIds[0]
  121. ];
  122. $requestContent = json_encode($requestData);
  123. $this->requestMock->expects($this->once())->method('getContent')->will($this->returnValue($requestContent));
  124. $this->captchaMock->expects($this->once())->method('isRequired')->with($username)
  125. ->will($this->returnValue(true));
  126. $this->captchaMock->expects($this->once())->method('logAttempt')->with($username);
  127. $this->captchaMock->expects($this->once())->method('isCorrect')
  128. ->with($captchaString)->will($this->returnValue(false));
  129. $this->serializerMock->expects($this->once())->method('unserialize')->will($this->returnValue($requestData));
  130. $this->sessionManagerMock->expects($this->once())->method('setUsername')->with($username);
  131. $this->jsonFactoryMock->expects($this->once())->method('create')
  132. ->will($this->returnValue($this->resultJsonMock));
  133. $this->resultJsonMock
  134. ->expects($this->once())
  135. ->method('setData')
  136. ->with(['errors' => true, 'message' => __('Incorrect CAPTCHA')])
  137. ->will($this->returnSelf());
  138. $closure = function () {
  139. };
  140. $this->assertEquals($this->resultJsonMock, $this->model->aroundExecute($this->loginControllerMock, $closure));
  141. }
  142. /**
  143. * @dataProvider aroundExecuteCaptchaIsNotRequired
  144. * @param string $username
  145. * @param array $requestContent
  146. */
  147. public function testAroundExecuteCaptchaIsNotRequired($username, $requestContent)
  148. {
  149. $this->requestMock->expects($this->once())->method('getContent')
  150. ->will($this->returnValue(json_encode($requestContent)));
  151. $this->serializerMock->expects($this->once())->method('unserialize')
  152. ->will($this->returnValue($requestContent));
  153. $this->captchaMock->expects($this->once())->method('isRequired')->with($username)
  154. ->will($this->returnValue(false));
  155. $this->captchaMock->expects($this->never())->method('logAttempt')->with($username);
  156. $this->captchaMock->expects($this->never())->method('isCorrect');
  157. $closure = function () {
  158. return 'result';
  159. };
  160. $this->assertEquals('result', $this->model->aroundExecute($this->loginControllerMock, $closure));
  161. }
  162. /**
  163. * @return array
  164. */
  165. public function aroundExecuteCaptchaIsNotRequired(): array
  166. {
  167. return [
  168. [
  169. 'username' => 'name',
  170. 'requestData' => ['username' => 'name', 'captcha_string' => 'string'],
  171. ],
  172. [
  173. 'username' => 'name',
  174. 'requestData' =>
  175. [
  176. 'username' => 'name',
  177. 'captcha_string' => 'string',
  178. 'captcha_form_id' => $this->formIds[0]
  179. ],
  180. ],
  181. [
  182. 'username' => null,
  183. 'requestData' =>
  184. [
  185. 'username' => null,
  186. 'captcha_string' => 'string',
  187. 'captcha_form_id' => $this->formIds[0]
  188. ],
  189. ],
  190. [
  191. 'username' => 'name',
  192. 'requestData' =>
  193. [
  194. 'username' => 'name',
  195. 'captcha_string' => 'string',
  196. 'captcha_form_id' => null
  197. ],
  198. ],
  199. ];
  200. }
  201. }