Skip to content

Commit 7e4c607

Browse files
author
Oleksandr Karpenko
committed
MAGETWO-52593: Minicart does not clean when customer session is over
1 parent a5fa3af commit 7e4c607

File tree

7 files changed

+305
-38
lines changed

7 files changed

+305
-38
lines changed

app/code/Magento/Customer/Controller/Account/LoginPost.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ class LoginPost extends \Magento\Customer\Controller\AbstractAccount
4242
*/
4343
private $scopeConfig;
4444

45+
/**
46+
* @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
47+
*/
48+
private $cookieMetadataFactory;
49+
50+
/**
51+
* @var \Magento\Framework\Stdlib\Cookie\PhpCookieManager
52+
*/
53+
private $cookieMetadataManager;
54+
4555
/**
4656
* @param Context $context
4757
* @param Session $customerSession
@@ -76,13 +86,45 @@ private function getScopeConfig()
7686
{
7787
if (!($this->scopeConfig instanceof \Magento\Framework\App\Config\ScopeConfigInterface)) {
7888
return \Magento\Framework\App\ObjectManager::getInstance()->get(
79-
'Magento\Framework\App\Config\ScopeConfigInterface'
89+
\Magento\Framework\App\Config\ScopeConfigInterface::class
8090
);
8191
} else {
8292
return $this->scopeConfig;
8393
}
8494
}
8595

96+
/**
97+
* Retrieve cookie manager
98+
*
99+
* @deprecated
100+
* @return \Magento\Framework\Stdlib\Cookie\PhpCookieManager
101+
*/
102+
private function getCookieManager()
103+
{
104+
if (!$this->cookieMetadataManager) {
105+
$this->cookieMetadataManager = \Magento\Framework\App\ObjectManager::getInstance()->get(
106+
\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class
107+
);
108+
}
109+
return $this->cookieMetadataManager;
110+
}
111+
112+
/**
113+
* Retrieve cookie metadata factory
114+
*
115+
* @deprecated
116+
* @return \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
117+
*/
118+
private function getCookieMetadataFactory()
119+
{
120+
if (!$this->cookieMetadataFactory) {
121+
$this->cookieMetadataFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
122+
\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
123+
);
124+
}
125+
return $this->cookieMetadataFactory;
126+
}
127+
86128
/**
87129
* Login post action
88130
*
@@ -105,6 +147,11 @@ public function execute()
105147
$customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
106148
$this->session->setCustomerDataAsLoggedIn($customer);
107149
$this->session->regenerateId();
150+
if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
151+
$metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
152+
$metadata->setPath('/');
153+
$this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
154+
}
108155
$redirectUrl = $this->accountRedirect->getRedirectCookie();
109156
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {
110157
$this->accountRedirect->clearRedirectCookie();

app/code/Magento/Customer/Controller/Account/Logout.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
use Magento\Customer\Model\Session;
1010
use Magento\Framework\App\Action\Context;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
13+
use Magento\Framework\Stdlib\Cookie\PhpCookieManager;
1114

1215
class Logout extends \Magento\Customer\Controller\AbstractAccount
1316
{
@@ -16,6 +19,16 @@ class Logout extends \Magento\Customer\Controller\AbstractAccount
1619
*/
1720
protected $session;
1821

22+
/**
23+
* @var CookieMetadataFactory
24+
*/
25+
private $cookieMetadataFactory;
26+
27+
/**
28+
* @var PhpCookieManager
29+
*/
30+
private $cookieMetadataManager;
31+
1932
/**
2033
* @param Context $context
2134
* @param Session $customerSession
@@ -28,6 +41,34 @@ public function __construct(
2841
parent::__construct($context);
2942
}
3043

44+
/**
45+
* Retrieve cookie manager
46+
*
47+
* @deprecated
48+
* @return PhpCookieManager
49+
*/
50+
private function getCookieManager()
51+
{
52+
if (!$this->cookieMetadataManager) {
53+
$this->cookieMetadataManager = ObjectManager::getInstance()->get(PhpCookieManager::class);
54+
}
55+
return $this->cookieMetadataManager;
56+
}
57+
58+
/**
59+
* Retrieve cookie metadata factory
60+
*
61+
* @deprecated
62+
* @return CookieMetadataFactory
63+
*/
64+
private function getCookieMetadataFactory()
65+
{
66+
if (!$this->cookieMetadataFactory) {
67+
$this->cookieMetadataFactory = ObjectManager::getInstance()->get(CookieMetadataFactory::class);
68+
}
69+
return $this->cookieMetadataFactory;
70+
}
71+
3172
/**
3273
* Customer logout action
3374
*
@@ -38,6 +79,11 @@ public function execute()
3879
$lastCustomerId = $this->session->getId();
3980
$this->session->logout()->setBeforeAuthUrl($this->_redirect->getRefererUrl())
4081
->setLastCustomerId($lastCustomerId);
82+
if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
83+
$metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
84+
$metadata->setPath('/');
85+
$this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
86+
}
4187

4288
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
4389
$resultRedirect = $this->resultRedirectFactory->create();

app/code/Magento/Customer/CustomerData/Plugin/SessionChecker.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
*/
66
namespace Magento\Customer\CustomerData\Plugin;
77

8-
use Magento\Customer\Model\Session;
9-
use Magento\Framework\App\Response\Http;
8+
use \Magento\Framework\Session\SessionManager;
109
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
1110
use Magento\Framework\Stdlib\Cookie\PhpCookieManager;
1211

@@ -22,36 +21,28 @@ class SessionChecker
2221
*/
2322
private $cookieMetadataFactory;
2423

25-
/**
26-
* @var Session
27-
*/
28-
private $session;
29-
3024
/**
3125
* @param PhpCookieManager $cookieManager
3226
* @param CookieMetadataFactory $cookieMetadataFactory
33-
* @param Session $session
3427
*/
3528
public function __construct(
3629
PhpCookieManager $cookieManager,
37-
CookieMetadataFactory $cookieMetadataFactory,
38-
Session $session
30+
CookieMetadataFactory $cookieMetadataFactory
3931
) {
4032
$this->cookieManager = $cookieManager;
4133
$this->cookieMetadataFactory = $cookieMetadataFactory;
42-
$this->session = $session;
4334
}
4435

4536
/**
4637
* Delete frontend session cookie if customer session is expired
4738
*
48-
* @param Http $response
49-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
50-
* @return void
39+
* @param SessionManager $sessionManager
5140
*/
52-
public function beforeSendVary(Http $response)
41+
public function beforeStart(SessionManager $sessionManager)
5342
{
54-
if (!$this->session->isLoggedIn()) {
43+
if (!$this->cookieManager->getCookie($sessionManager->getName())
44+
&& $this->cookieManager->getCookie('mage-cache-sessid')
45+
) {
5546
$metadata = $this->cookieMetadataFactory->createCookieMetadata();
5647
$metadata->setPath('/');
5748
$this->cookieManager->deleteCookie('mage-cache-sessid', $metadata);

app/code/Magento/Customer/Test/Unit/Controller/Account/LoginPostTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,18 @@ public function testExecuteSuccessCustomRedirect()
299299
->method('getRedirect')
300300
->willReturn($this->resultRedirect);
301301

302+
$cookieMetadataManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
303+
->disableOriginalConstructor()
304+
->getMock();
305+
$cookieMetadataManager->expects($this->once())
306+
->method('getCookie')
307+
->with('mage-cache-sessid')
308+
->willReturn(false);
309+
$refClass = new \ReflectionClass(LoginPost::class);
310+
$refProperty = $refClass->getProperty('cookieMetadataManager');
311+
$refProperty->setAccessible(true);
312+
$refProperty->setValue($this->controller, $cookieMetadataManager);
313+
302314
$this->assertSame($this->resultRedirect, $this->controller->execute());
303315
}
304316

@@ -352,6 +364,38 @@ public function testExecuteSuccess()
352364
->method('getRedirect')
353365
->willReturn($this->resultRedirect);
354366

367+
$cookieMetadataManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
368+
->disableOriginalConstructor()
369+
->getMock();
370+
$cookieMetadataManager->expects($this->once())
371+
->method('getCookie')
372+
->with('mage-cache-sessid')
373+
->willReturn(true);
374+
$cookieMetadataFactory = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
375+
->disableOriginalConstructor()
376+
->getMock();
377+
$cookieMetadata = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
378+
->disableOriginalConstructor()
379+
->getMock();
380+
$cookieMetadataFactory->expects($this->once())
381+
->method('createCookieMetadata')
382+
->willReturn($cookieMetadata);
383+
$cookieMetadata->expects($this->once())
384+
->method('setPath')
385+
->with('/');
386+
$cookieMetadataManager->expects($this->once())
387+
->method('deleteCookie')
388+
->with('mage-cache-sessid', $cookieMetadata);
389+
390+
$refClass = new \ReflectionClass(LoginPost::class);
391+
$cookieMetadataManagerProperty = $refClass->getProperty('cookieMetadataManager');
392+
$cookieMetadataManagerProperty->setAccessible(true);
393+
$cookieMetadataManagerProperty->setValue($this->controller, $cookieMetadataManager);
394+
395+
$cookieMetadataFactoryProperty = $refClass->getProperty('cookieMetadataFactory');
396+
$cookieMetadataFactoryProperty->setAccessible(true);
397+
$cookieMetadataFactoryProperty->setValue($this->controller, $cookieMetadataFactory);
398+
355399
$this->assertSame($this->resultRedirect, $this->controller->execute());
356400
}
357401

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Controller\Account;
7+
8+
use Magento\Customer\Controller\Account\Logout;
9+
use Magento\Framework\App\Response\RedirectInterface;
10+
use Magento\Framework\Controller\Result\Redirect;
11+
use Magento\Framework\Controller\Result\RedirectFactory;
12+
use Magento\Framework\Stdlib\Cookie\CookieMetadata;
13+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
14+
use Magento\Framework\Stdlib\Cookie\PhpCookieManager;
15+
16+
class LogoutTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/** @var Logout */
19+
protected $controller;
20+
21+
/** @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */
22+
protected $contextMock;
23+
24+
/** @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
25+
protected $sessionMock;
26+
27+
/** @var CookieMetadataFactory|\PHPUnit_Framework_MockObject_MockObject */
28+
protected $cookieMetadataFactory;
29+
30+
/** @var PhpCookieManager|\PHPUnit_Framework_MockObject_MockObject */
31+
protected $cookieManager;
32+
33+
/** @var CookieMetadata|\PHPUnit_Framework_MockObject_MockObject */
34+
protected $cookieMetadata;
35+
36+
/** @var Redirect|\PHPUnit_Framework_MockObject_MockObject */
37+
protected $resultRedirect;
38+
39+
/** @var RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */
40+
protected $redirectFactory;
41+
42+
/** @var RedirectInterface|\PHPUnit_Framework_MockObject_MockObject */
43+
protected $redirect;
44+
45+
protected function setUp()
46+
{
47+
$this->contextMock = $this->getMockBuilder('Magento\Framework\App\Action\Context')
48+
->disableOriginalConstructor()
49+
->getMock();
50+
$this->sessionMock = $this->getMockBuilder('Magento\Customer\Model\Session')
51+
->disableOriginalConstructor()
52+
->setMethods(['getId', 'logout', 'setBeforeAuthUrl', 'setLastCustomerId'])
53+
->getMock();
54+
55+
$this->cookieMetadataFactory = $this->getMockBuilder(CookieMetadataFactory::class)
56+
->disableOriginalConstructor()
57+
->getMock();
58+
$this->cookieManager = $this->getMockBuilder(PhpCookieManager::class)
59+
->disableOriginalConstructor()
60+
->getMock();
61+
$this->cookieMetadata = $this->getMockBuilder(CookieMetadata::class)
62+
->disableOriginalConstructor()
63+
->getMock();
64+
$this->redirectFactory = $this->getMockBuilder(RedirectFactory::class)
65+
->disableOriginalConstructor()
66+
->getMock();
67+
$this->resultRedirect = $this->getMockBuilder(Redirect::class)
68+
->disableOriginalConstructor()
69+
->getMock();
70+
$this->contextMock->expects($this->once())
71+
->method('getResultRedirectFactory')
72+
->willReturn($this->redirectFactory);
73+
74+
$this->redirect = $this->getMockBuilder(RedirectInterface::class)
75+
->getMockForAbstractClass();
76+
$this->contextMock->expects($this->once())
77+
->method('getRedirect')
78+
->willReturn($this->redirect);
79+
80+
$this->controller = new Logout($this->contextMock, $this->sessionMock);
81+
82+
$refClass = new \ReflectionClass(Logout::class);
83+
$cookieMetadataManagerProperty = $refClass->getProperty('cookieMetadataManager');
84+
$cookieMetadataManagerProperty->setAccessible(true);
85+
$cookieMetadataManagerProperty->setValue($this->controller, $this->cookieManager);
86+
87+
$cookieMetadataFactoryProperty = $refClass->getProperty('cookieMetadataFactory');
88+
$cookieMetadataFactoryProperty->setAccessible(true);
89+
$cookieMetadataFactoryProperty->setValue($this->controller, $this->cookieMetadataFactory);
90+
91+
92+
}
93+
94+
public function testExecute()
95+
{
96+
$customerId = 1;
97+
$refererUrl = 'http://referer.url';
98+
99+
$this->sessionMock->expects($this->once())
100+
->method('getId')
101+
->willReturn($customerId);
102+
$this->sessionMock->expects($this->once())
103+
->method('logout')
104+
->willReturnSelf();
105+
$this->redirect->expects($this->once())
106+
->method('getRefererUrl')
107+
->willReturn($refererUrl);
108+
$this->sessionMock->expects($this->once())
109+
->method('setBeforeAuthUrl')
110+
->with($refererUrl)
111+
->willReturnSelf();
112+
$this->sessionMock->expects($this->once())
113+
->method('setLastCustomerId')
114+
->with($customerId);
115+
116+
$this->cookieManager->expects($this->once())
117+
->method('getCookie')
118+
->with('mage-cache-sessid')
119+
->willReturn(true);
120+
$this->cookieMetadataFactory->expects($this->once())
121+
->method('createCookieMetadata')
122+
->willReturn($this->cookieMetadata);
123+
$this->cookieMetadata->expects($this->once())
124+
->method('setPath')
125+
->with('/');
126+
$this->cookieManager->expects($this->once())
127+
->method('deleteCookie')
128+
->with('mage-cache-sessid', $this->cookieMetadata);
129+
$this->redirectFactory->expects($this->once())
130+
->method('create')
131+
->willReturn($this->resultRedirect);
132+
$this->resultRedirect->expects($this->once())
133+
->method('setPath')
134+
->with('*/*/logoutSuccess');
135+
$this->assertSame($this->resultRedirect, $this->controller->execute());
136+
}
137+
}

0 commit comments

Comments
 (0)