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