CheckUserEditObserverTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 CheckUserEditObserverTest 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\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $redirectMock;
  21. /** @var \Magento\Captcha\Observer\CaptchaStringResolver|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $captchaStringResolverMock;
  23. /** @var AuthenticationInterface|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $authenticationMock;
  25. /** @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
  26. protected $customerSessionMock;
  27. /** @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  28. protected $scopeConfigMock;
  29. /** @var \Magento\Captcha\Observer\CheckUserEditObserver */
  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->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
  41. $this->captchaStringResolverMock = $this->createMock(\Magento\Captcha\Observer\CaptchaStringResolver::class);
  42. $this->authenticationMock = $this->getMockBuilder(AuthenticationInterface::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->customerSessionMock = $this->createPartialMock(
  46. \Magento\Customer\Model\Session::class,
  47. ['getCustomerId', 'getCustomer', 'logout', 'start']
  48. );
  49. $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  50. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  51. $this->observer = $objectManager->getObject(
  52. \Magento\Captcha\Observer\CheckUserEditObserver::class,
  53. [
  54. 'helper' => $this->helperMock,
  55. 'actionFlag' => $this->actionFlagMock,
  56. 'messageManager' => $this->messageManagerMock,
  57. 'redirect' => $this->redirectMock,
  58. 'captchaStringResolver' => $this->captchaStringResolverMock,
  59. 'authentication' => $this->authenticationMock,
  60. 'customerSession' => $this->customerSessionMock,
  61. 'scopeConfig' => $this->scopeConfigMock,
  62. ]
  63. );
  64. }
  65. /**
  66. * @return void
  67. */
  68. public function testExecute()
  69. {
  70. $customerId = 7;
  71. $captchaValue = 'some-value';
  72. $email = 'test@example.com';
  73. $redirectUrl = 'http://magento.com/customer/account/edit/';
  74. $captcha = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
  75. $captcha->expects($this->once())
  76. ->method('isRequired')
  77. ->willReturn(true);
  78. $captcha->expects($this->once())
  79. ->method('isCorrect')
  80. ->with($captchaValue)
  81. ->willReturn(false);
  82. $this->helperMock->expects($this->once())
  83. ->method('getCaptcha')
  84. ->with(\Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID)
  85. ->willReturn($captcha);
  86. $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
  87. $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  88. $request->expects($this->any())
  89. ->method('getPost')
  90. ->with(\Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE, null)
  91. ->willReturn([\Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID => $captchaValue]);
  92. $controller = $this->createMock(\Magento\Framework\App\Action\Action::class);
  93. $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
  94. $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response));
  95. $this->captchaStringResolverMock->expects($this->once())
  96. ->method('resolve')
  97. ->with($request, \Magento\Captcha\Observer\CheckUserEditObserver::FORM_ID)
  98. ->willReturn($captchaValue);
  99. $customerDataMock = $this->createMock(\Magento\Customer\Model\Data\Customer::class);
  100. $this->customerSessionMock->expects($this->once())
  101. ->method('getCustomerId')
  102. ->willReturn($customerId);
  103. $this->customerSessionMock->expects($this->atLeastOnce())
  104. ->method('getCustomer')
  105. ->willReturn($customerDataMock);
  106. $this->authenticationMock->expects($this->once())
  107. ->method('processAuthenticationFailure')
  108. ->with($customerId);
  109. $this->authenticationMock->expects($this->once())
  110. ->method('isLocked')
  111. ->with($customerId)
  112. ->willReturn(true);
  113. $this->customerSessionMock->expects($this->once())
  114. ->method('logout');
  115. $this->customerSessionMock->expects($this->once())
  116. ->method('start');
  117. $this->scopeConfigMock->expects($this->once())
  118. ->method('getValue')
  119. ->with('contact/email/recipient_email')
  120. ->willReturn($email);
  121. $message = __('The account is locked. Please wait and try again or contact %1.', $email);
  122. $this->messageManagerMock->expects($this->exactly(2))
  123. ->method('addError')
  124. ->withConsecutive([$message], [__('Incorrect CAPTCHA')]);
  125. $this->actionFlagMock->expects($this->once())
  126. ->method('set')
  127. ->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
  128. $this->redirectMock->expects($this->once())
  129. ->method('redirect')
  130. ->with($response, '*/*/edit')
  131. ->willReturn($redirectUrl);
  132. $this->observer->execute(new \Magento\Framework\Event\Observer(['controller_action' => $controller]));
  133. }
  134. }