Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LoginPostTest.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Customer\Model\Account\Redirect as AccountRedirect;
17 
23 class LoginPostTest extends \PHPUnit\Framework\TestCase
24 {
28  protected $controller;
29 
33  protected $context;
34 
38  protected $session;
39 
43  protected $accountManagement;
44 
48  protected $url;
49 
53  protected $formkeyValidator;
54 
58  protected $accountRedirect;
59 
63  protected $request;
64 
68  protected $resultRedirect;
69 
73  protected $redirectFactory;
74 
78  protected $redirect;
79 
83  protected $messageManager;
84 
88  protected $scopeConfig;
89 
90  protected function setUp()
91  {
92  $this->prepareContext();
93 
94  $this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
95  ->disableOriginalConstructor()
96  ->setMethods([
97  'isLoggedIn',
98  'setCustomerDataAsLoggedIn',
99  'regenerateId',
100  'setUsername',
101  ])
102  ->getMock();
103 
104  $this->accountManagement = $this->getMockBuilder(\Magento\Customer\Api\AccountManagementInterface::class)
105  ->getMockForAbstractClass();
106 
107  $this->url = $this->getMockBuilder(\Magento\Customer\Model\Url::class)
108  ->disableOriginalConstructor()
109  ->getMock();
110 
111  $this->formkeyValidator = $this->getMockBuilder(\Magento\Framework\Data\Form\FormKey\Validator::class)
112  ->disableOriginalConstructor()
113  ->getMock();
114 
115  $this->accountRedirect = $this->getMockBuilder(\Magento\Customer\Model\Account\Redirect::class)
116  ->disableOriginalConstructor()
117  ->getMock();
118 
119  $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
120  ->getMockForAbstractClass();
121 
122  $this->controller = new LoginPost(
123  $this->context,
124  $this->session,
125  $this->accountManagement,
126  $this->url,
127  $this->formkeyValidator,
128  $this->accountRedirect
129  );
130  $reflection = new \ReflectionClass(get_class($this->controller));
131  $reflectionProperty = $reflection->getProperty('scopeConfig');
132  $reflectionProperty->setAccessible(true);
133  $reflectionProperty->setValue($this->controller, $this->scopeConfig);
134  }
135 
141  public function testExecuteInvalidFormKey(
142  $isLoggedIn,
143  $isValidFormKey
144  ) {
145  $this->session->expects($this->once())
146  ->method('isLoggedIn')
147  ->willReturn($isLoggedIn);
148 
149  $this->formkeyValidator->expects($this->any())
150  ->method('validate')
151  ->with($this->request)
152  ->willReturn($isValidFormKey);
153 
154  $this->resultRedirect->expects($this->once())
155  ->method('setPath')
156  ->with('*/*/')
157  ->willReturnSelf();
158 
159  $this->assertSame($this->resultRedirect, $this->controller->execute());
160  }
161 
165  public function invalidFormKeyDataProvider()
166  {
167  return [
168  [
169  'isLoggedIn' => true,
170  'isValidFormKey' => false,
171  ],
172  [
173  'isLoggedIn' => false,
174  'isValidFormKey' => false,
175  ],
176  [
177  'isLoggedIn' => true,
178  'isValidFormKey' => true,
179  ],
180  ];
181  }
182 
183  public function testExecuteNoPostData()
184  {
185  $this->session->expects($this->once())
186  ->method('isLoggedIn')
187  ->willReturn(false);
188 
189  $this->formkeyValidator->expects($this->once())
190  ->method('validate')
191  ->with($this->request)
192  ->willReturn(true);
193 
194  $this->request->expects($this->once())
195  ->method('isPost')
196  ->willReturn(null);
197 
198  $this->accountRedirect->expects($this->once())
199  ->method('getRedirect')
200  ->willReturn($this->resultRedirect);
201 
202  $this->assertSame($this->resultRedirect, $this->controller->execute());
203  }
204 
205  public function testExecuteEmptyLoginData()
206  {
207  $this->session->expects($this->once())
208  ->method('isLoggedIn')
209  ->willReturn(false);
210 
211  $this->formkeyValidator->expects($this->once())
212  ->method('validate')
213  ->with($this->request)
214  ->willReturn(true);
215 
216  $this->request->expects($this->once())
217  ->method('isPost')
218  ->willReturn(true);
219  $this->request->expects($this->once())
220  ->method('getPost')
221  ->with('login')
222  ->willReturn([]);
223 
224  $this->messageManager->expects($this->once())
225  ->method('addError')
226  ->with(__('A login and a password are required.'))
227  ->willReturnSelf();
228 
229  $this->accountRedirect->expects($this->once())
230  ->method('getRedirect')
231  ->willReturn($this->resultRedirect);
232 
233  $this->assertSame($this->resultRedirect, $this->controller->execute());
234  }
235 
237  {
238  $username = 'user1';
239  $password = 'pass1';
240 
241  $this->session->expects($this->once())
242  ->method('isLoggedIn')
243  ->willReturn(false);
244 
245  $this->formkeyValidator->expects($this->once())
246  ->method('validate')
247  ->with($this->request)
248  ->willReturn(true);
249 
250  $this->request->expects($this->once())
251  ->method('isPost')
252  ->willReturn(true);
253  $this->request->expects($this->once())
254  ->method('getPost')
255  ->with('login')
256  ->willReturn([
257  'username' => $username,
258  'password' => $password,
259  ]);
260 
261  $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
262  ->getMockForAbstractClass();
263 
264  $this->scopeConfig->expects($this->once())
265  ->method('getValue')
266  ->with('customer/startup/redirect_dashboard')
267  ->willReturn(0);
268 
269  $cookieUrl = 'someUrl1';
270  $returnUrl = 'someUrl2';
271  $this->accountRedirect->expects($this->once())
272  ->method('getRedirectCookie')
273  ->willReturn($cookieUrl);
274  $this->accountRedirect->expects($this->once())
275  ->method('clearRedirectCookie');
276 
277  $this->redirect->expects($this->once())
278  ->method('success')
279  ->with($cookieUrl)
280  ->willReturn($returnUrl);
281 
282  $this->resultRedirect->expects($this->once())
283  ->method('setUrl')
284  ->with($returnUrl);
285 
286  $this->accountManagement->expects($this->once())
287  ->method('authenticate')
288  ->with($username, $password)
289  ->willReturn($customerMock);
290 
291  $this->session->expects($this->once())
292  ->method('setCustomerDataAsLoggedIn')
293  ->with($customerMock)
294  ->willReturnSelf();
295  $this->session->expects($this->once())
296  ->method('regenerateId')
297  ->willReturnSelf();
298 
299  $this->accountRedirect->expects($this->never())
300  ->method('getRedirect')
301  ->willReturn($this->resultRedirect);
302 
303  $cookieMetadataManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
304  ->disableOriginalConstructor()
305  ->getMock();
306  $cookieMetadataManager->expects($this->once())
307  ->method('getCookie')
308  ->with('mage-cache-sessid')
309  ->willReturn(false);
310  $refClass = new \ReflectionClass(LoginPost::class);
311  $refProperty = $refClass->getProperty('cookieMetadataManager');
312  $refProperty->setAccessible(true);
313  $refProperty->setValue($this->controller, $cookieMetadataManager);
314 
315  $this->assertSame($this->resultRedirect, $this->controller->execute());
316  }
317 
318  public function testExecuteSuccess()
319  {
320  $username = 'user1';
321  $password = 'pass1';
322 
323  $this->session->expects($this->once())
324  ->method('isLoggedIn')
325  ->willReturn(false);
326 
327  $this->formkeyValidator->expects($this->once())
328  ->method('validate')
329  ->with($this->request)
330  ->willReturn(true);
331 
332  $this->request->expects($this->once())
333  ->method('isPost')
334  ->willReturn(true);
335  $this->request->expects($this->once())
336  ->method('getPost')
337  ->with('login')
338  ->willReturn([
339  'username' => $username,
340  'password' => $password,
341  ]);
342 
343  $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
344  ->getMockForAbstractClass();
345 
346  $this->scopeConfig->expects($this->once())
347  ->method('getValue')
348  ->with('customer/startup/redirect_dashboard')
349  ->willReturn(1);
350 
351  $this->accountManagement->expects($this->once())
352  ->method('authenticate')
353  ->with($username, $password)
354  ->willReturn($customerMock);
355 
356  $this->session->expects($this->once())
357  ->method('setCustomerDataAsLoggedIn')
358  ->with($customerMock)
359  ->willReturnSelf();
360  $this->session->expects($this->once())
361  ->method('regenerateId')
362  ->willReturnSelf();
363 
364  $this->accountRedirect->expects($this->once())
365  ->method('getRedirect')
366  ->willReturn($this->resultRedirect);
367 
368  $cookieMetadataManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
369  ->disableOriginalConstructor()
370  ->getMock();
371  $cookieMetadataManager->expects($this->once())
372  ->method('getCookie')
373  ->with('mage-cache-sessid')
374  ->willReturn(true);
375  $cookieMetadataFactory = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
376  ->disableOriginalConstructor()
377  ->getMock();
378  $cookieMetadata = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
379  ->disableOriginalConstructor()
380  ->getMock();
381  $cookieMetadataFactory->expects($this->once())
382  ->method('createCookieMetadata')
383  ->willReturn($cookieMetadata);
384  $cookieMetadata->expects($this->once())
385  ->method('setPath')
386  ->with('/');
387  $cookieMetadataManager->expects($this->once())
388  ->method('deleteCookie')
389  ->with('mage-cache-sessid', $cookieMetadata);
390 
391  $refClass = new \ReflectionClass(LoginPost::class);
392  $cookieMetadataManagerProperty = $refClass->getProperty('cookieMetadataManager');
393  $cookieMetadataManagerProperty->setAccessible(true);
394  $cookieMetadataManagerProperty->setValue($this->controller, $cookieMetadataManager);
395 
396  $cookieMetadataFactoryProperty = $refClass->getProperty('cookieMetadataFactory');
397  $cookieMetadataFactoryProperty->setAccessible(true);
398  $cookieMetadataFactoryProperty->setValue($this->controller, $cookieMetadataFactory);
399 
400  $this->assertSame($this->resultRedirect, $this->controller->execute());
401  }
402 
408  public function testExecuteWithException(
409  $exceptionData
410  ) {
411  $username = 'user1';
412  $password = 'pass1';
413 
414  $this->session->expects($this->once())
415  ->method('isLoggedIn')
416  ->willReturn(false);
417 
418  $this->formkeyValidator->expects($this->once())
419  ->method('validate')
420  ->with($this->request)
421  ->willReturn(true);
422 
423  $this->request->expects($this->once())
424  ->method('isPost')
425  ->willReturn(true);
426  $this->request->expects($this->once())
427  ->method('getPost')
428  ->with('login')
429  ->willReturn([
430  'username' => $username,
431  'password' => $password,
432  ]);
433 
434  $exception = new $exceptionData['exception'](__($exceptionData['message']));
435 
436  $this->accountManagement->expects($this->once())
437  ->method('authenticate')
438  ->with($username, $password)
439  ->willThrowException($exception);
440 
441  $this->mockExceptions($exceptionData['exception'], $username);
442 
443  $this->accountRedirect->expects($this->once())
444  ->method('getRedirect')
445  ->willReturn($this->resultRedirect);
446 
447  $this->assertSame($this->resultRedirect, $this->controller->execute());
448  }
449 
453  public function exceptionDataProvider()
454  {
455  return [
456  [
457  [
458  'message' => 'EmailNotConfirmedException',
459  'exception' => \Magento\Framework\Exception\EmailNotConfirmedException::class,
460  ],
461  ],
462  [
463  [
464  'message' => 'AuthenticationException',
465  'exception' => \Magento\Framework\Exception\AuthenticationException::class,
466  ],
467  ],
468  [
469  [
470  'message' => 'Exception',
471  'exception' => '\Exception',
472  ],
473  ],
474  [
475  [
476  'message' => 'UserLockedException',
477  'exception' => \Magento\Framework\Exception\State\UserLockedException::class,
478  ],
479  ],
480  ];
481  }
482 
483  protected function prepareContext()
484  {
485  $this->context = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
486  ->disableOriginalConstructor()
487  ->getMock();
488 
489  $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
490  ->disableOriginalConstructor()
491  ->setMethods([
492  'isPost',
493  'getPost',
494  ])
495  ->getMock();
496 
497  $this->resultRedirect = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
498  ->disableOriginalConstructor()
499  ->getMock();
500 
501  $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
502  ->disableOriginalConstructor()
503  ->getMock();
504 
505  $this->redirectFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\RedirectFactory::class)
506  ->disableOriginalConstructor()
507  ->getMock();
508 
509  $this->redirect = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)
510  ->getMock();
511  $this->context->expects($this->atLeastOnce())
512  ->method('getRedirect')
513  ->willReturn($this->redirect);
514 
515  $this->redirectFactory->expects($this->any())
516  ->method('create')
517  ->willReturn($this->resultRedirect);
518 
519  $this->context->expects($this->any())
520  ->method('getRequest')
521  ->willReturn($this->request);
522 
523  $this->context->expects($this->any())
524  ->method('getResultRedirectFactory')
525  ->willReturn($this->redirectFactory);
526 
527  $this->context->expects($this->any())
528  ->method('getMessageManager')
529  ->willReturn($this->messageManager);
530  }
531 
537  protected function mockExceptions($exception, $username)
538  {
539  $url = 'url1';
540 
541  switch ($exception) {
542  case \Magento\Framework\Exception\EmailNotConfirmedException::class:
543  $this->url->expects($this->once())
544  ->method('getEmailConfirmationUrl')
545  ->with($username)
546  ->willReturn($url);
547 
548  $message = __(
549  'This account is not confirmed.' .
550  ' <a href="%1">Click here</a> to resend confirmation email.',
551  $url
552  );
553  $this->messageManager->expects($this->once())
554  ->method('addError')
555  ->with($message)
556  ->willReturnSelf();
557 
558  $this->session->expects($this->once())
559  ->method('setUsername')
560  ->with($username)
561  ->willReturnSelf();
562  break;
563 
564  case \Magento\Framework\Exception\AuthenticationException::class:
565  $this->messageManager->expects($this->once())
566  ->method('addError')
567  ->with(
568  __(
569  'The account sign-in was incorrect or your account is disabled temporarily. '
570  . 'Please wait and try again later.'
571  )
572  )
573  ->willReturnSelf();
574 
575  $this->session->expects($this->once())
576  ->method('setUsername')
577  ->with($username)
578  ->willReturnSelf();
579  break;
580 
581  case '\Exception':
582  $this->messageManager->expects($this->once())
583  ->method('addError')
584  ->with(__('An unspecified error occurred. Please contact us for assistance.'))
585  ->willReturnSelf();
586  break;
587 
588  case \Magento\Framework\Exception\State\UserLockedException::class:
589  $message = __(
590  'The account sign-in was incorrect or your account is disabled temporarily. '
591  . 'Please wait and try again later.'
592  );
593  $this->messageManager->expects($this->once())
594  ->method('addError')
595  ->with($message)
596  ->willReturnSelf();
597  $this->session->expects($this->once())
598  ->method('setUsername')
599  ->with($username)
600  ->willReturnSelf();
601  break;
602  }
603  }
604 }
setUp()
$request
testExecuteSuccessCustomRedirect()
$formkeyValidator
testExecuteSuccess()
$url
invalidFormKeyDataProvider()
__()
Definition: __.php:13
$context
Definition: LoginPost.php:31
$message
testExecuteInvalidFormKey( $isLoggedIn, $isValidFormKey)
$session
testExecuteEmptyLoginData()
$resultRedirect
exceptionDataProvider()
mockExceptions($exception, $username)
$accountManagement
$scopeConfig
$redirectFactory
testExecuteNoPostData()
$accountRedirect
$redirect
$controller
prepareContext()
testExecuteWithException( $exceptionData)
$messageManager