Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AuthTest.php
Go to the documentation of this file.
1 <?php
7 
9 
16 {
20  protected $_session;
21 
25  protected $_auth;
26 
27  protected function tearDown()
28  {
29  $this->_session = null;
30  $this->_auth = null;
31  parent::tearDown();
32  }
33 
37  protected function _login()
38  {
40  \Magento\Backend\Model\UrlInterface::class
41  )->turnOffSecretKey();
42 
44  \Magento\Backend\Model\Auth::class
45  );
46  $this->_auth->login(
47  \Magento\TestFramework\Bootstrap::ADMIN_NAME,
48  \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
49  );
50  $this->_session = $this->_auth->getAuthStorage();
51  }
52 
56  protected function _logout()
57  {
58  $this->_auth->logout();
60  \Magento\Backend\Model\UrlInterface::class
61  )->turnOnSecretKey();
62  }
63 
68  public function testNotLoggedLoginAction()
69  {
70  $this->dispatch('backend/admin/auth/login');
73  \Magento\Backend\Model\UrlInterface::class
74  );
75  $backendUrlModel->turnOffSecretKey();
76  $url = $backendUrlModel->getUrl('admin');
77  $this->assertRedirect($this->stringStartsWith($url));
78  }
79 
85  public function testLoggedLoginAction()
86  {
87  $this->_login();
88 
89  $this->dispatch('backend/admin/auth/login');
92  \Magento\Backend\Model\UrlInterface::class
93  );
94  $url = $backendUrlModel->getStartupPageUrl();
95  $expected = $backendUrlModel->getUrl($url);
96  $this->assertRedirect($this->stringStartsWith($expected));
97 
98  $this->_logout();
99  }
100 
104  public function testNotLoggedLoginActionWithRedirect()
105  {
107  $formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
108  $this->getRequest()->setPostValue(
109  [
110  'login' => [
111  'username' => \Magento\TestFramework\Bootstrap::ADMIN_NAME,
112  'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
113  ],
114  'form_key' => $formKey->getFormKey(),
115  ]
116  );
117 
118  $this->dispatch('backend/admin/index/index');
119 
121  ->get(\Magento\Framework\App\ResponseInterface::class);
122  $code = $response->getHttpResponseCode();
123  $this->assertTrue($code >= 300 && $code < 400, 'Incorrect response code');
124 
125  $this->assertTrue(
126  \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
127  \Magento\Backend\Model\Auth::class
128  )->isLoggedIn()
129  );
130  }
131 
136  public function testLogoutAction()
137  {
138  $this->_login();
139  $this->dispatch('backend/admin/auth/logout');
140  $this->assertRedirect(
141  $this->equalTo(
142  \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
143  \Magento\Backend\Helper\Data::class
144  )->getHomePageUrl()
145  )
146  );
147  $this->assertFalse($this->_session->isLoggedIn(), 'User is not logged out.');
148  }
149 
155  public function testDeniedJsonAction()
156  {
157  $this->_login();
158  $this->dispatch('backend/admin/auth/deniedJson');
159  $data = [
160  'ajaxExpired' => 1,
162  \Magento\Backend\Helper\Data::class
163  )->getHomePageUrl(),
164  ];
165  $expected = json_encode($data);
166  $this->assertEquals($expected, $this->getResponse()->getBody());
167  $this->_logout();
168  }
169 
175  public function testDeniedIframeAction()
176  {
177  $this->_login();
178  $this->dispatch('backend/admin/auth/deniedIframe');
180  \Magento\Backend\Helper\Data::class
181  )->getHomePageUrl();
182  $expected = '<script>parent.window.location =';
183  $this->assertStringStartsWith($expected, $this->getResponse()->getBody());
184  $this->assertContains($homeUrl, $this->getResponse()->getBody());
185  $this->_logout();
186  }
187 
195  public function testIncorrectLogin($params)
196  {
198  $formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
199  $params['form_key'] = $formKey->getFormKey();
200  $this->getRequest()->setPostValue($params);
201  $this->dispatch('backend/admin/auth/login');
202  $this->assertSessionMessages(
203  $this->equalTo(
204  [
205  'The account sign-in was incorrect or your account is disabled temporarily. '
206  . 'Please wait and try again later.'
207  ]
208  ),
210  );
212  \Magento\Backend\Model\UrlInterface::class
213  );
214  $backendUrlModel->turnOffSecretKey();
215  $url = $backendUrlModel->getUrl('admin');
216  $this->assertRedirect($this->stringStartsWith($url));
217  }
218 
219  public function incorrectLoginDataProvider()
220  {
221  return [
222  'login dummy user' => [
223  [
224  'login' => [
225  'username' => 'test1',
227  ],
228  ],
229  ],
230  'login without role' => [
231  [
232  'login' => [
233  'username' => 'test2',
235  ],
236  ],
237  ],
238  'login not active user' => [
239  [
240  'login' => [
241  'username' => 'test3',
243  ],
244  ],
245  ]
246  ];
247  }
248 }
$response
Definition: 404.php:11
assertRedirect(\PHPUnit\Framework\Constraint\Constraint $urlConstraint=null)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$code
Definition: info.phtml:12