Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckUserCreateObserverTest.php
Go to the documentation of this file.
1 <?php
7 
11 class CheckUserCreateObserverTest 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 $_session;
37 
41  protected $_urlManager;
42 
46  protected $_objectManager;
47 
52 
56  protected $_captcha;
57 
61  protected $redirect;
62 
63  protected function setUp()
64  {
65  $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
66  $this->_helper = $this->createMock(\Magento\Captcha\Helper\Data::class);
67  $this->_actionFlag = $this->createMock(\Magento\Framework\App\ActionFlag::class);
68  $this->_messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
69  $this->_session = $this->createMock(\Magento\Framework\Session\SessionManager::class);
70  $this->_urlManager = $this->createMock(\Magento\Framework\Url::class);
71  $this->captchaStringResolver = $this->createMock(\Magento\Captcha\Observer\CaptchaStringResolver::class);
72  $this->redirect = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
73  $this->checkUserCreateObserver = $this->_objectManager->getObject(
74  \Magento\Captcha\Observer\CheckUserCreateObserver::class,
75  [
76  'helper' => $this->_helper,
77  'actionFlag' => $this->_actionFlag,
78  'messageManager' => $this->_messageManager,
79  'session' => $this->_session,
80  'urlManager' => $this->_urlManager,
81  'redirect' => $this->redirect,
82  'captchaStringResolver' => $this->captchaStringResolver
83  ]
84  );
85  $this->_captcha = $this->createMock(\Magento\Captcha\Model\DefaultModel::class);
86  }
87 
89  {
90  $formId = 'user_create';
91  $captchaValue = 'some-value';
92  $warningMessage = 'Incorrect CAPTCHA';
93  $redirectRoutePath = '*/*/create';
94  $redirectUrl = 'http://magento.com/customer/account/create/';
95 
96  $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
97 
98  $this->redirect->expects(
99  $this->once()
100  )->method(
101  'error'
102  )->with(
103  $redirectUrl
104  )->will(
105  $this->returnValue($redirectUrl)
106  );
107 
108  $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
109  $response->expects($this->once())->method('setRedirect')->with($redirectUrl);
110 
111  $this->_urlManager->expects(
112  $this->once()
113  )->method(
114  'getUrl'
115  )->with(
116  $redirectRoutePath,
117  ['_nosecret' => true]
118  )->will(
119  $this->returnValue($redirectUrl)
120  );
121 
122  $controller = $this->createMock(\Magento\Framework\App\Action\Action::class);
123  $controller->expects($this->any())->method('getRequest')->will($this->returnValue($request));
124  $controller->expects($this->any())->method('getResponse')->will($this->returnValue($response));
125  $this->_captcha->expects($this->any())->method('isRequired')->will($this->returnValue(true));
126  $this->_captcha->expects(
127  $this->once()
128  )->method(
129  'isCorrect'
130  )->with(
131  $captchaValue
132  )->will(
133  $this->returnValue(false)
134  );
135  $this->captchaStringResolver->expects(
136  $this->once()
137  )->method(
138  'resolve'
139  )->with(
140  $request,
141  $formId
142  )->will(
143  $this->returnValue($captchaValue)
144  );
145  $this->_helper->expects(
146  $this->any()
147  )->method(
148  'getCaptcha'
149  )->with(
150  $formId
151  )->will(
152  $this->returnValue($this->_captcha)
153  );
154  $this->_messageManager->expects($this->once())->method('addError')->with($warningMessage);
155  $this->_actionFlag->expects(
156  $this->once()
157  )->method(
158  'set'
159  )->with(
160  '',
161  \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH,
162  true
163  );
164 
165  $this->checkUserCreateObserver->execute(
166  new \Magento\Framework\Event\Observer(['controller_action' => $controller])
167  );
168  }
169 }
$response
Definition: 404.php:11
$controller
Definition: info.phtml:14