Skip to content

Commit f61aa38

Browse files
author
Volodymyr Kublytskyi
committed
Merge magento-partners/magento2ce#74.
2 parents d8f164e + 2149833 commit f61aa38

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

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

Lines changed: 24 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\CookieManagerInterface;
17+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
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,27 @@ 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 ?: ObjectManager::getInstance()->get(CookieManagerInterface::class);
102+
$this->cookieMetadataFactory = $cookieMetadataFactory ?: ObjectManager::getInstance()->get(CookieMetadataFactory::class);
85103
}
86104

87105
/**
@@ -169,6 +187,11 @@ public function execute()
169187
$this->customerSession->setCustomerDataAsLoggedIn($customer);
170188
$this->customerSession->regenerateId();
171189
$redirectRoute = $this->getAccountRedirect()->getRedirectCookie();
190+
if ($this->cookieManager->getCookie('mage-cache-sessid')) {
191+
$metadata = $this->cookieMetadataFactory->createCookieMetadata();
192+
$metadata->setPath('/');
193+
$this->cookieManager->deleteCookie('mage-cache-sessid', $metadata);
194+
}
172195
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectRoute) {
173196
$response['redirectUrl'] = $this->_redirect->success($redirectRoute);
174197
$this->getAccountRedirect()->clearRedirectCookie();

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

Lines changed: 43 additions & 0 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::class)
@@ -100,6 +115,16 @@ protected function setUp()
100115
->setMethods(['create'])
101116
->getMock();
102117

118+
$this->cookieManager = $this->getMockBuilder(\Magento\Framework\Stdlib\CookieManagerInterface::class)
119+
->setMethods(['getCookie', 'deleteCookie'])
120+
->getMockForAbstractClass();
121+
$this->cookieMetadataFactory = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
122+
->disableOriginalConstructor()
123+
->getMock();
124+
$this->cookieMetadata = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
125+
->disableOriginalConstructor()
126+
->getMock();
127+
103128
$this->resultRaw = $this->getMockBuilder(\Magento\Framework\Controller\Result\Raw::class)
104129
->disableOriginalConstructor()
105130
->getMock();
@@ -128,6 +153,8 @@ protected function setUp()
128153
'resultJsonFactory' => $this->resultJsonFactory,
129154
'objectManager' => $this->objectManager,
130155
'customerAccountManagement' => $this->customerAccountManagementMock,
156+
'cookieManager' => $this->cookieManager,
157+
'cookieMetadataFactory' => $this->cookieMetadataFactory
131158
]
132159
);
133160
}
@@ -179,6 +206,22 @@ public function testLogin()
179206
$this->object->setAccountRedirect($redirectMock);
180207
$redirectMock->expects($this->once())->method('getRedirectCookie')->willReturn('some_url1');
181208

209+
$this->cookieManager->expects($this->once())
210+
->method('getCookie')
211+
->with('mage-cache-sessid')
212+
->willReturn(true);
213+
$this->cookieMetadataFactory->expects($this->once())
214+
->method('createCookieMetadata')
215+
->willReturn($this->cookieMetadata);
216+
$this->cookieMetadata->expects($this->once())
217+
->method('setPath')
218+
->with('/')
219+
->willReturnSelf();
220+
$this->cookieManager->expects($this->once())
221+
->method('deleteCookie')
222+
->with('mage-cache-sessid', $this->cookieMetadata)
223+
->willReturnSelf();
224+
182225
$scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
183226
$this->object->setScopeConfig($scopeConfigMock);
184227
$scopeConfigMock->expects($this->once())->method('getValue')

0 commit comments

Comments
 (0)