94 $this->session = $this->getMockBuilder(\
Magento\Customer\Model\Session::class)
95 ->disableOriginalConstructor()
98 'setCustomerDataAsLoggedIn',
104 $this->accountManagement = $this->getMockBuilder(\
Magento\Customer\Api\AccountManagementInterface::class)
105 ->getMockForAbstractClass();
107 $this->url = $this->getMockBuilder(\
Magento\Customer\Model\Url::class)
108 ->disableOriginalConstructor()
111 $this->formkeyValidator = $this->getMockBuilder(\
Magento\Framework\Data\Form\FormKey\Validator::class)
112 ->disableOriginalConstructor()
115 $this->accountRedirect = $this->getMockBuilder(\
Magento\Customer\Model\Account\Redirect::class)
116 ->disableOriginalConstructor()
119 $this->scopeConfig = $this->getMockBuilder(\
Magento\Framework\
App\Config\ScopeConfigInterface::class)
120 ->getMockForAbstractClass();
125 $this->accountManagement,
127 $this->formkeyValidator,
128 $this->accountRedirect
130 $reflection = new \ReflectionClass(get_class($this->controller));
131 $reflectionProperty = $reflection->getProperty(
'scopeConfig');
132 $reflectionProperty->setAccessible(
true);
133 $reflectionProperty->setValue($this->controller, $this->scopeConfig);
145 $this->session->expects($this->once())
146 ->method(
'isLoggedIn')
147 ->willReturn($isLoggedIn);
149 $this->formkeyValidator->expects($this->any())
151 ->with($this->request)
152 ->willReturn($isValidFormKey);
154 $this->resultRedirect->expects($this->once())
159 $this->assertSame($this->resultRedirect, $this->controller->execute());
169 'isLoggedIn' =>
true,
170 'isValidFormKey' =>
false,
173 'isLoggedIn' =>
false,
174 'isValidFormKey' =>
false,
177 'isLoggedIn' =>
true,
178 'isValidFormKey' =>
true,
185 $this->session->expects($this->once())
186 ->method(
'isLoggedIn')
189 $this->formkeyValidator->expects($this->once())
191 ->with($this->request)
194 $this->request->expects($this->once())
198 $this->accountRedirect->expects($this->once())
199 ->method(
'getRedirect')
200 ->willReturn($this->resultRedirect);
202 $this->assertSame($this->resultRedirect, $this->controller->execute());
207 $this->session->expects($this->once())
208 ->method(
'isLoggedIn')
211 $this->formkeyValidator->expects($this->once())
213 ->with($this->request)
216 $this->request->expects($this->once())
219 $this->request->expects($this->once())
224 $this->messageManager->expects($this->once())
226 ->with(
__(
'A login and a password are required.'))
229 $this->accountRedirect->expects($this->once())
230 ->method(
'getRedirect')
231 ->willReturn($this->resultRedirect);
233 $this->assertSame($this->resultRedirect, $this->controller->execute());
241 $this->session->expects($this->once())
242 ->method(
'isLoggedIn')
245 $this->formkeyValidator->expects($this->once())
247 ->with($this->request)
250 $this->request->expects($this->once())
253 $this->request->expects($this->once())
257 'username' => $username,
258 'password' => $password,
261 $customerMock = $this->getMockBuilder(\
Magento\Customer\Api\Data\CustomerInterface::class)
262 ->getMockForAbstractClass();
264 $this->scopeConfig->expects($this->once())
266 ->with(
'customer/startup/redirect_dashboard')
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');
277 $this->redirect->expects($this->once())
280 ->willReturn($returnUrl);
282 $this->resultRedirect->expects($this->once())
286 $this->accountManagement->expects($this->once())
287 ->method(
'authenticate')
288 ->with($username, $password)
289 ->willReturn($customerMock);
291 $this->session->expects($this->once())
292 ->method(
'setCustomerDataAsLoggedIn')
293 ->with($customerMock)
295 $this->session->expects($this->once())
296 ->method(
'regenerateId')
299 $this->accountRedirect->expects($this->never())
300 ->method(
'getRedirect')
301 ->willReturn($this->resultRedirect);
303 $cookieMetadataManager = $this->getMockBuilder(\
Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
304 ->disableOriginalConstructor()
306 $cookieMetadataManager->expects($this->once())
307 ->method(
'getCookie')
308 ->with(
'mage-cache-sessid')
310 $refClass = new \ReflectionClass(LoginPost::class);
311 $refProperty = $refClass->getProperty(
'cookieMetadataManager');
312 $refProperty->setAccessible(
true);
313 $refProperty->setValue($this->controller, $cookieMetadataManager);
315 $this->assertSame($this->resultRedirect, $this->controller->execute());
323 $this->session->expects($this->once())
324 ->method(
'isLoggedIn')
327 $this->formkeyValidator->expects($this->once())
329 ->with($this->request)
332 $this->request->expects($this->once())
335 $this->request->expects($this->once())
339 'username' => $username,
340 'password' => $password,
343 $customerMock = $this->getMockBuilder(\
Magento\Customer\Api\Data\CustomerInterface::class)
344 ->getMockForAbstractClass();
346 $this->scopeConfig->expects($this->once())
348 ->with(
'customer/startup/redirect_dashboard')
351 $this->accountManagement->expects($this->once())
352 ->method(
'authenticate')
353 ->with($username, $password)
354 ->willReturn($customerMock);
356 $this->session->expects($this->once())
357 ->method(
'setCustomerDataAsLoggedIn')
358 ->with($customerMock)
360 $this->session->expects($this->once())
361 ->method(
'regenerateId')
364 $this->accountRedirect->expects($this->once())
365 ->method(
'getRedirect')
366 ->willReturn($this->resultRedirect);
368 $cookieMetadataManager = $this->getMockBuilder(\
Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
369 ->disableOriginalConstructor()
371 $cookieMetadataManager->expects($this->once())
372 ->method(
'getCookie')
373 ->with(
'mage-cache-sessid')
375 $cookieMetadataFactory = $this->getMockBuilder(\
Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
376 ->disableOriginalConstructor()
378 $cookieMetadata = $this->getMockBuilder(\
Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
379 ->disableOriginalConstructor()
381 $cookieMetadataFactory->expects($this->once())
382 ->method(
'createCookieMetadata')
383 ->willReturn($cookieMetadata);
384 $cookieMetadata->expects($this->once())
387 $cookieMetadataManager->expects($this->once())
388 ->method(
'deleteCookie')
389 ->with(
'mage-cache-sessid', $cookieMetadata);
391 $refClass = new \ReflectionClass(LoginPost::class);
392 $cookieMetadataManagerProperty = $refClass->getProperty(
'cookieMetadataManager');
393 $cookieMetadataManagerProperty->setAccessible(
true);
394 $cookieMetadataManagerProperty->setValue($this->controller, $cookieMetadataManager);
396 $cookieMetadataFactoryProperty = $refClass->getProperty(
'cookieMetadataFactory');
397 $cookieMetadataFactoryProperty->setAccessible(
true);
398 $cookieMetadataFactoryProperty->setValue($this->controller, $cookieMetadataFactory);
400 $this->assertSame($this->resultRedirect, $this->controller->execute());
414 $this->session->expects($this->once())
415 ->method(
'isLoggedIn')
418 $this->formkeyValidator->expects($this->once())
420 ->with($this->request)
423 $this->request->expects($this->once())
426 $this->request->expects($this->once())
430 'username' => $username,
431 'password' => $password,
434 $exception =
new $exceptionData[
'exception'](
__($exceptionData[
'message']));
436 $this->accountManagement->expects($this->once())
437 ->method(
'authenticate')
438 ->with($username, $password)
439 ->willThrowException($exception);
443 $this->accountRedirect->expects($this->once())
444 ->method(
'getRedirect')
445 ->willReturn($this->resultRedirect);
447 $this->assertSame($this->resultRedirect, $this->controller->execute());
458 'message' =>
'EmailNotConfirmedException',
459 'exception' => \Magento\Framework\Exception\EmailNotConfirmedException::class,
464 'message' =>
'AuthenticationException',
465 'exception' => \Magento\Framework\Exception\AuthenticationException::class,
470 'message' =>
'Exception',
471 'exception' =>
'\Exception',
476 'message' =>
'UserLockedException',
477 'exception' => \Magento\Framework\Exception\State\UserLockedException::class,
485 $this->context = $this->getMockBuilder(\
Magento\Framework\
App\Action\Context::class)
486 ->disableOriginalConstructor()
489 $this->request = $this->getMockBuilder(\
Magento\Framework\
App\Request\Http::class)
490 ->disableOriginalConstructor()
497 $this->resultRedirect = $this->getMockBuilder(\
Magento\Framework\Controller\Result\Redirect::class)
498 ->disableOriginalConstructor()
501 $this->messageManager = $this->getMockBuilder(\
Magento\Framework\Message\ManagerInterface::class)
502 ->disableOriginalConstructor()
505 $this->redirectFactory = $this->getMockBuilder(\
Magento\Framework\Controller\Result\RedirectFactory::class)
506 ->disableOriginalConstructor()
509 $this->redirect = $this->getMockBuilder(\
Magento\Framework\
App\
Response\RedirectInterface::class)
511 $this->context->expects($this->atLeastOnce())
512 ->method(
'getRedirect')
513 ->willReturn($this->redirect);
515 $this->redirectFactory->expects($this->any())
517 ->willReturn($this->resultRedirect);
519 $this->context->expects($this->any())
520 ->method(
'getRequest')
521 ->willReturn($this->request);
523 $this->context->expects($this->any())
524 ->method(
'getResultRedirectFactory')
525 ->willReturn($this->redirectFactory);
527 $this->context->expects($this->any())
528 ->method(
'getMessageManager')
529 ->willReturn($this->messageManager);
541 switch ($exception) {
542 case \Magento\Framework\Exception\EmailNotConfirmedException::class:
543 $this->url->expects($this->once())
544 ->method(
'getEmailConfirmationUrl')
549 'This account is not confirmed.' .
550 ' <a href="%1">Click here</a> to resend confirmation email.',
553 $this->messageManager->expects($this->once())
558 $this->session->expects($this->once())
559 ->method(
'setUsername')
564 case \Magento\Framework\Exception\AuthenticationException::class:
565 $this->messageManager->expects($this->once())
569 'The account sign-in was incorrect or your account is disabled temporarily. ' 570 .
'Please wait and try again later.' 575 $this->session->expects($this->once())
576 ->method(
'setUsername')
582 $this->messageManager->expects($this->once())
584 ->with(
__(
'An unspecified error occurred. Please contact us for assistance.'))
588 case \Magento\Framework\Exception\State\UserLockedException::class:
590 'The account sign-in was incorrect or your account is disabled temporarily. ' 591 .
'Please wait and try again later.' 593 $this->messageManager->expects($this->once())
597 $this->session->expects($this->once())
598 ->method(
'setUsername')
testExecuteSuccessCustomRedirect()
invalidFormKeyDataProvider()
testExecuteInvalidFormKey( $isLoggedIn, $isValidFormKey)
testExecuteEmptyLoginData()
mockExceptions($exception, $username)
testExecuteWithException( $exceptionData)