Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckForgotpasswordObserverTest.php
Go to the documentation of this file.
1 <?php
7 
11 class CheckForgotpasswordObserverTest extends \PHPUnit\Framework\TestCase
12 {
17 
21  protected $_helper;
22 
26  protected $_actionFlag;
27 
28  /*
29  * @var \PHPUnit_Framework_MockObject_MockObject
30  */
31  protected $_messageManager;
32 
36  protected $redirect;
37 
41  protected $_objectManager;
42 
47 
51  protected $_captcha;
52 
53  protected function setUp()
54  {
55  $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
56  $this->_helper = $this->createMock(\Magento\Captcha\Helper\Data::class);
57  $this->_actionFlag = $this->createMock(\Magento\Framework\App\ActionFlag::class);
58  $this->_messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
59  $this->redirect = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
60  $this->captchaStringResolver = $this->createMock(\Magento\Captcha\Observer\CaptchaStringResolver::class);
61  $this->checkForgotpasswordObserver = $this->_objectManager->getObject(
62  \Magento\Captcha\Observer\CheckForgotpasswordObserver::class,
63  [
64  'helper' => $this->_helper,
65  'actionFlag' => $this->_actionFlag,
66  'messageManager' => $this->_messageManager,
67  'redirect' => $this->redirect,
68  'captchaStringResolver' => $this->captchaStringResolver
69  ]
70  );
71  $this->_captcha = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
72  }
73 
75  {
76  $formId = 'user_forgotpassword';
77  $captchaValue = 'some-value';
78  $warningMessage = 'Incorrect CAPTCHA';
79  $redirectRoutePath = '*/*/forgotpassword';
80  $redirectUrl = 'http://magento.com/customer/account/forgotpassword/';
81 
82  $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
83  $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
84  $request->expects(
85  $this->any()
86  )->method(
87  'getPost'
88  )->with(
89  \Magento\Captcha\Helper\Data::INPUT_NAME_FIELD_VALUE,
90  null
91  )->will(
92  $this->returnValue([$formId => $captchaValue])
93  );
94 
95  $this->redirect->expects(
96  $this->once()
97  )->method(
98  'redirect'
99  )->with(
100  $response,
101  $redirectRoutePath,
102  []
103  )->will(
104  $this->returnValue($redirectUrl)
105  );
106 
107  $controller = $this->createMock(\Magento\Framework\App\Action\Action::class);
108  $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
109  $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response));
110  $this->_captcha->expects($this->any())->method('isRequired')->will($this->returnValue(true));
111  $this->_captcha->expects(
112  $this->once()
113  )->method(
114  'isCorrect'
115  )->with(
116  $captchaValue
117  )->will(
118  $this->returnValue(false)
119  );
120 
121  $this->captchaStringResolver->expects(
122  $this->once()
123  )->method(
124  'resolve'
125  )->with(
126  $request,
127  $formId
128  )->will(
129  $this->returnValue($captchaValue)
130  );
131 
132  $this->_helper->expects(
133  $this->any()
134  )->method(
135  'getCaptcha'
136  )->with(
137  $formId
138  )->will(
139  $this->returnValue($this->_captcha)
140  );
141  $this->_messageManager->expects($this->once())->method('addError')->with($warningMessage);
142  $this->_actionFlag->expects(
143  $this->once()
144  )->method(
145  'set'
146  )->with(
147  '',
148  \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH,
149  true
150  );
151 
152  $this->checkForgotpasswordObserver->execute(
153  new \Magento\Framework\Event\Observer(['controller_action' => $controller])
154  );
155  }
156 }
$response
Definition: 404.php:11
$controller
Definition: info.phtml:14