| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Controller\Account;
- use Magento\Customer\Api\AccountManagementInterface;
- use Magento\Customer\Controller\Account\LoginPost;
- use Magento\Customer\Model\Account\Redirect as AccountRedirect;
- use Magento\Customer\Model\Session;
- use Magento\Customer\Model\Url;
- use Magento\Framework\App\Action\Context;
- use Magento\Framework\App\Request\Http;
- use Magento\Framework\Controller\Result\Redirect;
- /**
- * Test customer account controller
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class LoginPostTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var LoginPost
- */
- protected $controller;
- /**
- * @var Context | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $context;
- /**
- * @var Session | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $session;
- /**
- * @var AccountManagementInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $accountManagement;
- /**
- * @var Url | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $url;
- /**
- * @var \Magento\Framework\Data\Form\FormKey\Validator | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $formkeyValidator;
- /**
- * @var AccountRedirect | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $accountRedirect;
- /**
- * @var Http | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $request;
- /**
- * @var Redirect | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultRedirect;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $redirectFactory;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $redirect;
- /**
- * @var \Magento\Framework\Message\ManagerInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $messageManager;
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeConfig;
- protected function setUp()
- {
- $this->prepareContext();
- $this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'isLoggedIn',
- 'setCustomerDataAsLoggedIn',
- 'regenerateId',
- 'setUsername',
- ])
- ->getMock();
- $this->accountManagement = $this->getMockBuilder(\Magento\Customer\Api\AccountManagementInterface::class)
- ->getMockForAbstractClass();
- $this->url = $this->getMockBuilder(\Magento\Customer\Model\Url::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->formkeyValidator = $this->getMockBuilder(\Magento\Framework\Data\Form\FormKey\Validator::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->accountRedirect = $this->getMockBuilder(\Magento\Customer\Model\Account\Redirect::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
- ->getMockForAbstractClass();
- $this->controller = new LoginPost(
- $this->context,
- $this->session,
- $this->accountManagement,
- $this->url,
- $this->formkeyValidator,
- $this->accountRedirect
- );
- $reflection = new \ReflectionClass(get_class($this->controller));
- $reflectionProperty = $reflection->getProperty('scopeConfig');
- $reflectionProperty->setAccessible(true);
- $reflectionProperty->setValue($this->controller, $this->scopeConfig);
- }
- /**
- * @param boolean $isLoggedIn
- * @param boolean $isValidFormKey
- * @dataProvider invalidFormKeyDataProvider
- */
- public function testExecuteInvalidFormKey(
- $isLoggedIn,
- $isValidFormKey
- ) {
- $this->session->expects($this->once())
- ->method('isLoggedIn')
- ->willReturn($isLoggedIn);
- $this->formkeyValidator->expects($this->any())
- ->method('validate')
- ->with($this->request)
- ->willReturn($isValidFormKey);
- $this->resultRedirect->expects($this->once())
- ->method('setPath')
- ->with('*/*/')
- ->willReturnSelf();
- $this->assertSame($this->resultRedirect, $this->controller->execute());
- }
- /**
- * @return array
- */
- public function invalidFormKeyDataProvider()
- {
- return [
- [
- 'isLoggedIn' => true,
- 'isValidFormKey' => false,
- ],
- [
- 'isLoggedIn' => false,
- 'isValidFormKey' => false,
- ],
- [
- 'isLoggedIn' => true,
- 'isValidFormKey' => true,
- ],
- ];
- }
- public function testExecuteNoPostData()
- {
- $this->session->expects($this->once())
- ->method('isLoggedIn')
- ->willReturn(false);
- $this->formkeyValidator->expects($this->once())
- ->method('validate')
- ->with($this->request)
- ->willReturn(true);
- $this->request->expects($this->once())
- ->method('isPost')
- ->willReturn(null);
- $this->accountRedirect->expects($this->once())
- ->method('getRedirect')
- ->willReturn($this->resultRedirect);
- $this->assertSame($this->resultRedirect, $this->controller->execute());
- }
- public function testExecuteEmptyLoginData()
- {
- $this->session->expects($this->once())
- ->method('isLoggedIn')
- ->willReturn(false);
- $this->formkeyValidator->expects($this->once())
- ->method('validate')
- ->with($this->request)
- ->willReturn(true);
- $this->request->expects($this->once())
- ->method('isPost')
- ->willReturn(true);
- $this->request->expects($this->once())
- ->method('getPost')
- ->with('login')
- ->willReturn([]);
- $this->messageManager->expects($this->once())
- ->method('addError')
- ->with(__('A login and a password are required.'))
- ->willReturnSelf();
- $this->accountRedirect->expects($this->once())
- ->method('getRedirect')
- ->willReturn($this->resultRedirect);
- $this->assertSame($this->resultRedirect, $this->controller->execute());
- }
- public function testExecuteSuccessCustomRedirect()
- {
- $username = 'user1';
- $password = 'pass1';
- $this->session->expects($this->once())
- ->method('isLoggedIn')
- ->willReturn(false);
- $this->formkeyValidator->expects($this->once())
- ->method('validate')
- ->with($this->request)
- ->willReturn(true);
- $this->request->expects($this->once())
- ->method('isPost')
- ->willReturn(true);
- $this->request->expects($this->once())
- ->method('getPost')
- ->with('login')
- ->willReturn([
- 'username' => $username,
- 'password' => $password,
- ]);
- $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
- ->getMockForAbstractClass();
- $this->scopeConfig->expects($this->once())
- ->method('getValue')
- ->with('customer/startup/redirect_dashboard')
- ->willReturn(0);
- $cookieUrl = 'someUrl1';
- $returnUrl = 'someUrl2';
- $this->accountRedirect->expects($this->once())
- ->method('getRedirectCookie')
- ->willReturn($cookieUrl);
- $this->accountRedirect->expects($this->once())
- ->method('clearRedirectCookie');
- $this->redirect->expects($this->once())
- ->method('success')
- ->with($cookieUrl)
- ->willReturn($returnUrl);
- $this->resultRedirect->expects($this->once())
- ->method('setUrl')
- ->with($returnUrl);
- $this->accountManagement->expects($this->once())
- ->method('authenticate')
- ->with($username, $password)
- ->willReturn($customerMock);
- $this->session->expects($this->once())
- ->method('setCustomerDataAsLoggedIn')
- ->with($customerMock)
- ->willReturnSelf();
- $this->session->expects($this->once())
- ->method('regenerateId')
- ->willReturnSelf();
- $this->accountRedirect->expects($this->never())
- ->method('getRedirect')
- ->willReturn($this->resultRedirect);
- $cookieMetadataManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $cookieMetadataManager->expects($this->once())
- ->method('getCookie')
- ->with('mage-cache-sessid')
- ->willReturn(false);
- $refClass = new \ReflectionClass(LoginPost::class);
- $refProperty = $refClass->getProperty('cookieMetadataManager');
- $refProperty->setAccessible(true);
- $refProperty->setValue($this->controller, $cookieMetadataManager);
- $this->assertSame($this->resultRedirect, $this->controller->execute());
- }
- public function testExecuteSuccess()
- {
- $username = 'user1';
- $password = 'pass1';
- $this->session->expects($this->once())
- ->method('isLoggedIn')
- ->willReturn(false);
- $this->formkeyValidator->expects($this->once())
- ->method('validate')
- ->with($this->request)
- ->willReturn(true);
- $this->request->expects($this->once())
- ->method('isPost')
- ->willReturn(true);
- $this->request->expects($this->once())
- ->method('getPost')
- ->with('login')
- ->willReturn([
- 'username' => $username,
- 'password' => $password,
- ]);
- $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
- ->getMockForAbstractClass();
- $this->scopeConfig->expects($this->once())
- ->method('getValue')
- ->with('customer/startup/redirect_dashboard')
- ->willReturn(1);
- $this->accountManagement->expects($this->once())
- ->method('authenticate')
- ->with($username, $password)
- ->willReturn($customerMock);
- $this->session->expects($this->once())
- ->method('setCustomerDataAsLoggedIn')
- ->with($customerMock)
- ->willReturnSelf();
- $this->session->expects($this->once())
- ->method('regenerateId')
- ->willReturnSelf();
- $this->accountRedirect->expects($this->once())
- ->method('getRedirect')
- ->willReturn($this->resultRedirect);
- $cookieMetadataManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $cookieMetadataManager->expects($this->once())
- ->method('getCookie')
- ->with('mage-cache-sessid')
- ->willReturn(true);
- $cookieMetadataFactory = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $cookieMetadata = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
- ->disableOriginalConstructor()
- ->getMock();
- $cookieMetadataFactory->expects($this->once())
- ->method('createCookieMetadata')
- ->willReturn($cookieMetadata);
- $cookieMetadata->expects($this->once())
- ->method('setPath')
- ->with('/');
- $cookieMetadataManager->expects($this->once())
- ->method('deleteCookie')
- ->with('mage-cache-sessid', $cookieMetadata);
- $refClass = new \ReflectionClass(LoginPost::class);
- $cookieMetadataManagerProperty = $refClass->getProperty('cookieMetadataManager');
- $cookieMetadataManagerProperty->setAccessible(true);
- $cookieMetadataManagerProperty->setValue($this->controller, $cookieMetadataManager);
- $cookieMetadataFactoryProperty = $refClass->getProperty('cookieMetadataFactory');
- $cookieMetadataFactoryProperty->setAccessible(true);
- $cookieMetadataFactoryProperty->setValue($this->controller, $cookieMetadataFactory);
- $this->assertSame($this->resultRedirect, $this->controller->execute());
- }
- /**
- * @param array $exceptionData
- *
- * @dataProvider exceptionDataProvider
- */
- public function testExecuteWithException(
- $exceptionData
- ) {
- $username = 'user1';
- $password = 'pass1';
- $this->session->expects($this->once())
- ->method('isLoggedIn')
- ->willReturn(false);
- $this->formkeyValidator->expects($this->once())
- ->method('validate')
- ->with($this->request)
- ->willReturn(true);
- $this->request->expects($this->once())
- ->method('isPost')
- ->willReturn(true);
- $this->request->expects($this->once())
- ->method('getPost')
- ->with('login')
- ->willReturn([
- 'username' => $username,
- 'password' => $password,
- ]);
- $exception = new $exceptionData['exception'](__($exceptionData['message']));
- $this->accountManagement->expects($this->once())
- ->method('authenticate')
- ->with($username, $password)
- ->willThrowException($exception);
- $this->mockExceptions($exceptionData['exception'], $username);
- $this->accountRedirect->expects($this->once())
- ->method('getRedirect')
- ->willReturn($this->resultRedirect);
- $this->assertSame($this->resultRedirect, $this->controller->execute());
- }
- /**
- * @return array
- */
- public function exceptionDataProvider()
- {
- return [
- [
- [
- 'message' => 'EmailNotConfirmedException',
- 'exception' => \Magento\Framework\Exception\EmailNotConfirmedException::class,
- ],
- ],
- [
- [
- 'message' => 'AuthenticationException',
- 'exception' => \Magento\Framework\Exception\AuthenticationException::class,
- ],
- ],
- [
- [
- 'message' => 'Exception',
- 'exception' => '\Exception',
- ],
- ],
- [
- [
- 'message' => 'UserLockedException',
- 'exception' => \Magento\Framework\Exception\State\UserLockedException::class,
- ],
- ],
- ];
- }
- protected function prepareContext()
- {
- $this->context = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'isPost',
- 'getPost',
- ])
- ->getMock();
- $this->resultRedirect = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->redirectFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\RedirectFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->redirect = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)
- ->getMock();
- $this->context->expects($this->atLeastOnce())
- ->method('getRedirect')
- ->willReturn($this->redirect);
- $this->redirectFactory->expects($this->any())
- ->method('create')
- ->willReturn($this->resultRedirect);
- $this->context->expects($this->any())
- ->method('getRequest')
- ->willReturn($this->request);
- $this->context->expects($this->any())
- ->method('getResultRedirectFactory')
- ->willReturn($this->redirectFactory);
- $this->context->expects($this->any())
- ->method('getMessageManager')
- ->willReturn($this->messageManager);
- }
- /**
- * @param string $exception
- * @param string $username
- * @return void
- */
- protected function mockExceptions($exception, $username)
- {
- $url = 'url1';
- switch ($exception) {
- case \Magento\Framework\Exception\EmailNotConfirmedException::class:
- $this->url->expects($this->once())
- ->method('getEmailConfirmationUrl')
- ->with($username)
- ->willReturn($url);
- $message = __(
- 'This account is not confirmed.' .
- ' <a href="%1">Click here</a> to resend confirmation email.',
- $url
- );
- $this->messageManager->expects($this->once())
- ->method('addError')
- ->with($message)
- ->willReturnSelf();
- $this->session->expects($this->once())
- ->method('setUsername')
- ->with($username)
- ->willReturnSelf();
- break;
- case \Magento\Framework\Exception\AuthenticationException::class:
- $this->messageManager->expects($this->once())
- ->method('addError')
- ->with(
- __(
- 'The account sign-in was incorrect or your account is disabled temporarily. '
- . 'Please wait and try again later.'
- )
- )
- ->willReturnSelf();
- $this->session->expects($this->once())
- ->method('setUsername')
- ->with($username)
- ->willReturnSelf();
- break;
- case '\Exception':
- $this->messageManager->expects($this->once())
- ->method('addError')
- ->with(__('An unspecified error occurred. Please contact us for assistance.'))
- ->willReturnSelf();
- break;
- case \Magento\Framework\Exception\State\UserLockedException::class:
- $message = __(
- 'The account sign-in was incorrect or your account is disabled temporarily. '
- . 'Please wait and try again later.'
- );
- $this->messageManager->expects($this->once())
- ->method('addError')
- ->with($message)
- ->willReturnSelf();
- $this->session->expects($this->once())
- ->method('setUsername')
- ->with($username)
- ->willReturnSelf();
- break;
- }
- }
- }
|