CheckUserLoginObserverTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Captcha\Test\Unit\Observer;
  7. use Magento\Customer\Model\AuthenticationInterface;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class CheckUserLoginObserverTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \Magento\Captcha\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
  14. protected $helperMock;
  15. /** @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $actionFlagMock;
  17. /* @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $messageManagerMock;
  19. /** @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $customerSessionMock;
  21. /** @var \Magento\Captcha\Observer\CaptchaStringResolver|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $captchaStringResolverMock;
  23. /** @var \Magento\Customer\Model\Url|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $customerUrlMock;
  25. /** @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
  26. protected $customerRepositoryMock;
  27. /** @var AuthenticationInterface|\PHPUnit_Framework_MockObject_MockObject */
  28. protected $authenticationMock;
  29. /** @var \Magento\Captcha\Observer\CheckUserLoginObserver */
  30. protected $observer;
  31. /**
  32. * Init mocks for tests
  33. * @return void
  34. */
  35. protected function setUp()
  36. {
  37. $this->helperMock = $this->createMock(\Magento\Captcha\Helper\Data::class);
  38. $this->actionFlagMock = $this->createMock(\Magento\Framework\App\ActionFlag::class);
  39. $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  40. $this->customerSessionMock = $this->createPartialMock(
  41. \Magento\Customer\Model\Session::class,
  42. ['setUsername', 'getBeforeAuthUrl']
  43. );
  44. $this->captchaStringResolverMock = $this->createMock(\Magento\Captcha\Observer\CaptchaStringResolver::class);
  45. $this->customerUrlMock = $this->createMock(\Magento\Customer\Model\Url::class);
  46. $this->customerRepositoryMock = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  47. $this->authenticationMock = $this->createMock(AuthenticationInterface::class);
  48. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  49. $this->observer = $objectManager->getObject(
  50. \Magento\Captcha\Observer\CheckUserLoginObserver::class,
  51. [
  52. 'helper' => $this->helperMock,
  53. 'actionFlag' => $this->actionFlagMock,
  54. 'messageManager' => $this->messageManagerMock,
  55. 'customerSession' => $this->customerSessionMock,
  56. 'captchaStringResolver' => $this->captchaStringResolverMock,
  57. 'customerUrl' => $this->customerUrlMock,
  58. ]
  59. );
  60. $reflection = new \ReflectionClass(get_class($this->observer));
  61. $reflectionProperty = $reflection->getProperty('authentication');
  62. $reflectionProperty->setAccessible(true);
  63. $reflectionProperty->setValue($this->observer, $this->authenticationMock);
  64. $reflectionProperty2 = $reflection->getProperty('customerRepository');
  65. $reflectionProperty2->setAccessible(true);
  66. $reflectionProperty2->setValue($this->observer, $this->customerRepositoryMock);
  67. }
  68. /**
  69. * @return void
  70. */
  71. public function testExecute()
  72. {
  73. $formId = 'user_login';
  74. $login = 'login';
  75. $loginParams = ['username' => $login];
  76. $customerId = 7;
  77. $redirectUrl = 'http://magento.com/customer/account/login/';
  78. $captchaValue = 'some-value';
  79. $captcha = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
  80. $captcha->expects($this->once())
  81. ->method('isRequired')
  82. ->with($login)
  83. ->willReturn(true);
  84. $captcha->expects($this->once())
  85. ->method('isCorrect')
  86. ->with($captchaValue)
  87. ->willReturn(false);
  88. $captcha->expects($this->once())
  89. ->method('logAttempt')
  90. ->with($login);
  91. $this->helperMock->expects($this->once())
  92. ->method('getCaptcha')
  93. ->with($formId)
  94. ->willReturn($captcha);
  95. $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
  96. $response->expects($this->once())
  97. ->method('setRedirect')
  98. ->with($redirectUrl);
  99. $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  100. $request->expects($this->any())
  101. ->method('getPost')
  102. ->with('login')
  103. ->willReturn($loginParams);
  104. $controller = $this->createMock(\Magento\Framework\App\Action\Action::class);
  105. $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
  106. $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response));
  107. $this->captchaStringResolverMock->expects($this->once())
  108. ->method('resolve')
  109. ->with($request, $formId)
  110. ->willReturn($captchaValue);
  111. $customerDataMock = $this->createPartialMock(\Magento\Customer\Model\Data\Customer::class, ['getId']);
  112. $customerDataMock->expects($this->once())
  113. ->method('getId')
  114. ->willReturn($customerId);
  115. $this->customerRepositoryMock->expects($this->once())
  116. ->method('get')
  117. ->with($login)
  118. ->willReturn($customerDataMock);
  119. $this->authenticationMock->expects($this->once())
  120. ->method('processAuthenticationFailure')
  121. ->with($customerId);
  122. $this->messageManagerMock->expects($this->once())
  123. ->method('addError')
  124. ->with(__('Incorrect CAPTCHA'));
  125. $this->actionFlagMock->expects($this->once())
  126. ->method('set')
  127. ->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
  128. $this->customerSessionMock->expects($this->once())
  129. ->method('setUsername')
  130. ->with($login);
  131. $this->customerSessionMock->expects($this->once())
  132. ->method('getBeforeAuthUrl')
  133. ->willReturn(false);
  134. $this->customerUrlMock->expects($this->once())
  135. ->method('getLoginUrl')
  136. ->willReturn($redirectUrl);
  137. $this->observer->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller]));
  138. }
  139. }