Skip to content

Commit bfd3bf6

Browse files
Merge pull request #1783 from magento-engcom/2.1-develop-prs
[EngCom] Public Pull Requests - 2.1-develop - MAGETWO-84733: Clear mage-cache-sessid cookie on Ajax Login #12246
2 parents bf06306 + 764e6c5 commit bfd3bf6

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

app/code/Magento/Customer/Controller/Ajax/Login.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Magento\Customer\Model\Account\Redirect as AccountRedirect;
1414
use Magento\Framework\App\Config\ScopeConfigInterface;
1515
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
17+
use Magento\Framework\Stdlib\CookieManagerInterface;
1618

1719
/**
1820
* Login controller
@@ -58,6 +60,16 @@ class Login extends \Magento\Framework\App\Action\Action
5860
*/
5961
protected $scopeConfig;
6062

63+
/**
64+
* @var CookieManagerInterface
65+
*/
66+
private $cookieManager;
67+
68+
/**
69+
* @var CookieMetadataFactory
70+
*/
71+
private $cookieMetadataFactory;
72+
6173
/**
6274
* Initialize Login controller
6375
*
@@ -67,21 +79,29 @@ class Login extends \Magento\Framework\App\Action\Action
6779
* @param AccountManagementInterface $customerAccountManagement
6880
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
6981
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
82+
* @param CookieManagerInterface $cookieManager
83+
* @param CookieMetadataFactory $cookieMetadataFactory
7084
*/
7185
public function __construct(
7286
\Magento\Framework\App\Action\Context $context,
7387
\Magento\Customer\Model\Session $customerSession,
7488
\Magento\Framework\Json\Helper\Data $helper,
7589
AccountManagementInterface $customerAccountManagement,
7690
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
77-
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory
91+
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
92+
CookieManagerInterface $cookieManager = null,
93+
CookieMetadataFactory $cookieMetadataFactory = null
7894
) {
7995
parent::__construct($context);
8096
$this->customerSession = $customerSession;
8197
$this->helper = $helper;
8298
$this->customerAccountManagement = $customerAccountManagement;
8399
$this->resultJsonFactory = $resultJsonFactory;
84100
$this->resultRawFactory = $resultRawFactory;
101+
$this->cookieManager = $cookieManager ?:
102+
ObjectManager::getInstance()->get(CookieManagerInterface::class);
103+
$this->cookieMetadataFactory = $cookieMetadataFactory ?:
104+
ObjectManager::getInstance()->get(CookieMetadataFactory::class);
85105
}
86106

87107
/**
@@ -169,6 +189,11 @@ public function execute()
169189
$this->customerSession->setCustomerDataAsLoggedIn($customer);
170190
$this->customerSession->regenerateId();
171191
$redirectRoute = $this->getAccountRedirect()->getRedirectCookie();
192+
if ($this->cookieManager->getCookie('mage-cache-sessid')) {
193+
$metadata = $this->cookieMetadataFactory->createCookieMetadata();
194+
$metadata->setPath('/');
195+
$this->cookieManager->deleteCookie('mage-cache-sessid', $metadata);
196+
}
172197
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectRoute) {
173198
$response['redirectUrl'] = $this->_redirect->success($redirectRoute);
174199
$this->getAccountRedirect()->clearRedirectCookie();

app/code/Magento/Customer/Test/Unit/Controller/Ajax/LoginTest.php

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ class LoginTest extends \PHPUnit_Framework_TestCase
7373
*/
7474
protected $redirectMock;
7575

76+
/**
77+
* @var \Magento\Framework\Stdlib\CookieManagerInterface| \PHPUnit_Framework_MockObject_MockObject
78+
*/
79+
private $cookieManager;
80+
81+
/**
82+
* @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory| \PHPUnit_Framework_MockObject_MockObject
83+
*/
84+
private $cookieMetadataFactory;
85+
86+
/**
87+
* @var \Magento\Framework\Stdlib\Cookie\CookieMetadata| \PHPUnit_Framework_MockObject_MockObject
88+
*/
89+
private $cookieMetadata;
90+
7691
protected function setUp()
7792
{
7893
$this->request = $this->getMockBuilder('Magento\Framework\App\Request\Http')
@@ -105,14 +120,13 @@ protected function setUp()
105120
'',
106121
false
107122
);
108-
$this->customerAccountManagementMock =
109-
$this->getMock(
110-
'\Magento\Customer\Model\AccountManagement',
111-
['authenticate'],
112-
[],
113-
'',
114-
false
115-
);
123+
$this->customerAccountManagementMock = $this->getMock(
124+
'\Magento\Customer\Model\AccountManagement',
125+
['authenticate'],
126+
[],
127+
'',
128+
false
129+
);
116130

117131
$this->jsonHelperMock = $this->getMock(
118132
'\Magento\Framework\Json\Helper\Data',
@@ -130,6 +144,16 @@ protected function setUp()
130144
->setMethods(['create'])
131145
->getMock();
132146

147+
$this->cookieManager = $this->getMockBuilder(\Magento\Framework\Stdlib\CookieManagerInterface::class)
148+
->setMethods(['getCookie', 'deleteCookie'])
149+
->getMockForAbstractClass();
150+
$this->cookieMetadataFactory = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
151+
->disableOriginalConstructor()
152+
->getMock();
153+
$this->cookieMetadata = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
154+
->disableOriginalConstructor()
155+
->getMock();
156+
133157
$this->resultRaw = $this->getMockBuilder('Magento\Framework\Controller\Result\Raw')
134158
->disableOriginalConstructor()
135159
->getMock();
@@ -158,6 +182,8 @@ protected function setUp()
158182
'resultJsonFactory' => $this->resultJsonFactory,
159183
'objectManager' => $this->objectManager,
160184
'customerAccountManagement' => $this->customerAccountManagementMock,
185+
'cookieManager' => $this->cookieManager,
186+
'cookieMetadataFactory' => $this->cookieMetadataFactory
161187
]
162188
);
163189
}
@@ -209,6 +235,22 @@ public function testLogin()
209235
$this->object->setAccountRedirect($redirectMock);
210236
$redirectMock->expects($this->once())->method('getRedirectCookie')->willReturn('some_url1');
211237

238+
$this->cookieManager->expects($this->once())
239+
->method('getCookie')
240+
->with('mage-cache-sessid')
241+
->willReturn(true);
242+
$this->cookieMetadataFactory->expects($this->once())
243+
->method('createCookieMetadata')
244+
->willReturn($this->cookieMetadata);
245+
$this->cookieMetadata->expects($this->once())
246+
->method('setPath')
247+
->with('/')
248+
->willReturnSelf();
249+
$this->cookieManager->expects($this->once())
250+
->method('deleteCookie')
251+
->with('mage-cache-sessid', $this->cookieMetadata)
252+
->willReturnSelf();
253+
212254
$scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
213255
$this->object->setScopeConfig($scopeConfigMock);
214256
$scopeConfigMock->expects($this->once())->method('getValue')

0 commit comments

Comments
 (0)