diff --git a/app/code/Magento/Customer/Controller/Ajax/Login.php b/app/code/Magento/Customer/Controller/Ajax/Login.php index 81caf7c10e321..bd54f5198bffe 100644 --- a/app/code/Magento/Customer/Controller/Ajax/Login.php +++ b/app/code/Magento/Customer/Controller/Ajax/Login.php @@ -13,6 +13,8 @@ use Magento\Customer\Model\Account\Redirect as AccountRedirect; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory; +use Magento\Framework\Stdlib\CookieManagerInterface; /** * Login controller @@ -58,6 +60,16 @@ class Login extends \Magento\Framework\App\Action\Action */ protected $scopeConfig; + /** + * @var CookieManagerInterface + */ + private $cookieManager; + + /** + * @var CookieMetadataFactory + */ + private $cookieMetadataFactory; + /** * Initialize Login controller * @@ -67,6 +79,8 @@ class Login extends \Magento\Framework\App\Action\Action * @param AccountManagementInterface $customerAccountManagement * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory + * @param CookieManagerInterface $cookieManager + * @param CookieMetadataFactory $cookieMetadataFactory */ public function __construct( \Magento\Framework\App\Action\Context $context, @@ -74,7 +88,9 @@ public function __construct( \Magento\Framework\Json\Helper\Data $helper, AccountManagementInterface $customerAccountManagement, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, - \Magento\Framework\Controller\Result\RawFactory $resultRawFactory + \Magento\Framework\Controller\Result\RawFactory $resultRawFactory, + CookieManagerInterface $cookieManager = null, + CookieMetadataFactory $cookieMetadataFactory = null ) { parent::__construct($context); $this->customerSession = $customerSession; @@ -82,6 +98,10 @@ public function __construct( $this->customerAccountManagement = $customerAccountManagement; $this->resultJsonFactory = $resultJsonFactory; $this->resultRawFactory = $resultRawFactory; + $this->cookieManager = $cookieManager ?: + ObjectManager::getInstance()->get(CookieManagerInterface::class); + $this->cookieMetadataFactory = $cookieMetadataFactory ?: + ObjectManager::getInstance()->get(CookieMetadataFactory::class); } /** @@ -169,6 +189,11 @@ public function execute() $this->customerSession->setCustomerDataAsLoggedIn($customer); $this->customerSession->regenerateId(); $redirectRoute = $this->getAccountRedirect()->getRedirectCookie(); + if ($this->cookieManager->getCookie('mage-cache-sessid')) { + $metadata = $this->cookieMetadataFactory->createCookieMetadata(); + $metadata->setPath('/'); + $this->cookieManager->deleteCookie('mage-cache-sessid', $metadata); + } if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectRoute) { $response['redirectUrl'] = $this->_redirect->success($redirectRoute); $this->getAccountRedirect()->clearRedirectCookie(); diff --git a/app/code/Magento/Customer/Test/Unit/Controller/Ajax/LoginTest.php b/app/code/Magento/Customer/Test/Unit/Controller/Ajax/LoginTest.php index 58d571aa69d80..4e9d6226969c5 100644 --- a/app/code/Magento/Customer/Test/Unit/Controller/Ajax/LoginTest.php +++ b/app/code/Magento/Customer/Test/Unit/Controller/Ajax/LoginTest.php @@ -73,6 +73,21 @@ class LoginTest extends \PHPUnit_Framework_TestCase */ protected $redirectMock; + /** + * @var \Magento\Framework\Stdlib\CookieManagerInterface| \PHPUnit_Framework_MockObject_MockObject + */ + private $cookieManager; + + /** + * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory| \PHPUnit_Framework_MockObject_MockObject + */ + private $cookieMetadataFactory; + + /** + * @var \Magento\Framework\Stdlib\Cookie\CookieMetadata| \PHPUnit_Framework_MockObject_MockObject + */ + private $cookieMetadata; + protected function setUp() { $this->request = $this->getMockBuilder('Magento\Framework\App\Request\Http') @@ -105,14 +120,13 @@ protected function setUp() '', false ); - $this->customerAccountManagementMock = - $this->getMock( - '\Magento\Customer\Model\AccountManagement', - ['authenticate'], - [], - '', - false - ); + $this->customerAccountManagementMock = $this->getMock( + '\Magento\Customer\Model\AccountManagement', + ['authenticate'], + [], + '', + false + ); $this->jsonHelperMock = $this->getMock( '\Magento\Framework\Json\Helper\Data', @@ -130,6 +144,16 @@ protected function setUp() ->setMethods(['create']) ->getMock(); + $this->cookieManager = $this->getMockBuilder(\Magento\Framework\Stdlib\CookieManagerInterface::class) + ->setMethods(['getCookie', 'deleteCookie']) + ->getMockForAbstractClass(); + $this->cookieMetadataFactory = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class) + ->disableOriginalConstructor() + ->getMock(); + $this->cookieMetadata = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class) + ->disableOriginalConstructor() + ->getMock(); + $this->resultRaw = $this->getMockBuilder('Magento\Framework\Controller\Result\Raw') ->disableOriginalConstructor() ->getMock(); @@ -158,6 +182,8 @@ protected function setUp() 'resultJsonFactory' => $this->resultJsonFactory, 'objectManager' => $this->objectManager, 'customerAccountManagement' => $this->customerAccountManagementMock, + 'cookieManager' => $this->cookieManager, + 'cookieMetadataFactory' => $this->cookieMetadataFactory ] ); } @@ -209,6 +235,22 @@ public function testLogin() $this->object->setAccountRedirect($redirectMock); $redirectMock->expects($this->once())->method('getRedirectCookie')->willReturn('some_url1'); + $this->cookieManager->expects($this->once()) + ->method('getCookie') + ->with('mage-cache-sessid') + ->willReturn(true); + $this->cookieMetadataFactory->expects($this->once()) + ->method('createCookieMetadata') + ->willReturn($this->cookieMetadata); + $this->cookieMetadata->expects($this->once()) + ->method('setPath') + ->with('/') + ->willReturnSelf(); + $this->cookieManager->expects($this->once()) + ->method('deleteCookie') + ->with('mage-cache-sessid', $this->cookieMetadata) + ->willReturnSelf(); + $scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); $this->object->setScopeConfig($scopeConfigMock); $scopeConfigMock->expects($this->once())->method('getValue')