CheckForgotpasswordObserverTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class CheckForgotpasswordObserverTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Captcha\Observer\CheckForgotpasswordObserver
  14. */
  15. protected $checkForgotpasswordObserver;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $_helper;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $_actionFlag;
  24. /*
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $_messageManager;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $redirect;
  32. /**
  33. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  34. */
  35. protected $_objectManager;
  36. /**
  37. * @var \Magento\Captcha\Observer\CaptchaStringResolver|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $captchaStringResolver;
  40. /**
  41. * @var \PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $_captcha;
  44. protected function setUp()
  45. {
  46. $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  47. $this->_helper = $this->createMock(\Magento\Captcha\Helper\Data::class);
  48. $this->_actionFlag = $this->createMock(\Magento\Framework\App\ActionFlag::class);
  49. $this->_messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  50. $this->redirect = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
  51. $this->captchaStringResolver = $this->createMock(\Magento\Captcha\Observer\CaptchaStringResolver::class);
  52. $this->checkForgotpasswordObserver = $this->_objectManager->getObject(
  53. \Magento\Captcha\Observer\CheckForgotpasswordObserver::class,
  54. [
  55. 'helper' => $this->_helper,
  56. 'actionFlag' => $this->_actionFlag,
  57. 'messageManager' => $this->_messageManager,
  58. 'redirect' => $this->redirect,
  59. 'captchaStringResolver' => $this->captchaStringResolver
  60. ]
  61. );
  62. $this->_captcha = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
  63. }
  64. public function testCheckForgotpasswordRedirects()
  65. {
  66. $formId = 'user_forgotpassword';
  67. $captchaValue = 'some-value';
  68. $warningMessage = 'Incorrect CAPTCHA';
  69. $redirectRoutePath = '*/*/forgotpassword';
  70. $redirectUrl = 'http://magento.com/customer/account/forgotpassword/';
  71. $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  72. $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
  73. $request->expects(
  74. $this->any()
  75. )->method(
  76. 'getPost'
  77. )->with(
  78. \Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE,
  79. null
  80. )->will(
  81. $this->returnValue([$formId => $captchaValue])
  82. );
  83. $this->redirect->expects(
  84. $this->once()
  85. )->method(
  86. 'redirect'
  87. )->with(
  88. $response,
  89. $redirectRoutePath,
  90. []
  91. )->will(
  92. $this->returnValue($redirectUrl)
  93. );
  94. $controller = $this->createMock(\Magento\Framework\App\Action\Action::class);
  95. $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
  96. $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response));
  97. $this->_captcha->expects($this->any())->method('isRequired')->will($this->returnValue(true));
  98. $this->_captcha->expects(
  99. $this->once()
  100. )->method(
  101. 'isCorrect'
  102. )->with(
  103. $captchaValue
  104. )->will(
  105. $this->returnValue(false)
  106. );
  107. $this->captchaStringResolver->expects(
  108. $this->once()
  109. )->method(
  110. 'resolve'
  111. )->with(
  112. $request,
  113. $formId
  114. )->will(
  115. $this->returnValue($captchaValue)
  116. );
  117. $this->_helper->expects(
  118. $this->any()
  119. )->method(
  120. 'getCaptcha'
  121. )->with(
  122. $formId
  123. )->will(
  124. $this->returnValue($this->_captcha)
  125. );
  126. $this->_messageManager->expects($this->once())->method('addError')->with($warningMessage);
  127. $this->_actionFlag->expects(
  128. $this->once()
  129. )->method(
  130. 'set'
  131. )->with(
  132. '',
  133. \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH,
  134. true
  135. );
  136. $this->checkForgotpasswordObserver->execute(
  137. new \Magento\Framework\Event\Observer(['controller_action' => $controller])
  138. );
  139. }
  140. }