Skip to content

Commit e7f5565

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-1445' into FEB132023_PR_arul
2 parents 66fbb33 + b232c2d commit e7f5565

File tree

7 files changed

+201
-6
lines changed

7 files changed

+201
-6
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\App\FrontController;
9+
10+
use Magento\Framework\App\Response\Http as ResponseHttp;
11+
use Magento\Customer\Model\Session;
12+
13+
/**
14+
* Plugin for delete the cookie when the customer is not exist.
15+
*
16+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
17+
*/
18+
class DeleteCookieWhenCustomerNotExistPlugin
19+
{
20+
/**
21+
* @var ResponseHttp
22+
*/
23+
private $responseHttp;
24+
25+
/**
26+
* @var Session
27+
*/
28+
private $session;
29+
30+
/**
31+
* Constructor
32+
*
33+
* @param ResponseHttp $responseHttp
34+
* @param Session $session
35+
*/
36+
public function __construct(
37+
ResponseHttp $responseHttp,
38+
Session $session
39+
) {
40+
$this->responseHttp = $responseHttp;
41+
$this->session = $session;
42+
}
43+
44+
/**
45+
* Delete the cookie when the customer is not exist before dispatch the front controller.
46+
*
47+
* @return void
48+
*/
49+
public function beforeDispatch(): void
50+
{
51+
if (!$this->session->getCustomerId()) {
52+
$this->responseHttp->sendVary();
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Test\Unit\Model\App\FrontController;
9+
10+
use Magento\Customer\Model\App\FrontController\DeleteCookieWhenCustomerNotExistPlugin;
11+
use Magento\Framework\App\Response\Http as ResponseHttp;
12+
use Magento\Customer\Model\Session;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Tests \Magento\Customer\Model\App\FrontController\DeleteCookieWhenCustomerNotExistPluginTest.
18+
*/
19+
class DeleteCookieWhenCustomerNotExistPluginTest extends TestCase
20+
{
21+
/**
22+
* @var DeleteCookieWhenCustomerNotExistPlugin
23+
*/
24+
protected DeleteCookieWhenCustomerNotExistPlugin $plugin;
25+
26+
/**
27+
* @var ResponseHttp|MockObject
28+
*/
29+
protected ResponseHttp|MockObject $responseHttpMock;
30+
31+
/**
32+
* @var Session|MockObject
33+
*/
34+
protected MockObject|Session $customerSessionMock;
35+
36+
/**
37+
* Set up
38+
*/
39+
protected function setUp(): void
40+
{
41+
$this->customerSessionMock = $this->createMock(Session::class);
42+
$this->responseHttpMock = $this->createMock(ResponseHttp::class);
43+
$this->plugin = new DeleteCookieWhenCustomerNotExistPlugin(
44+
$this->responseHttpMock,
45+
$this->customerSessionMock
46+
);
47+
}
48+
49+
public function testBeforeDispatch()
50+
{
51+
$this->customerSessionMock->expects($this->once())
52+
->method('getCustomerId')
53+
->willReturn(0);
54+
$this->plugin->beforeDispatch();
55+
}
56+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\ViewModel\Customer;
9+
10+
use Magento\Customer\Model\Context;
11+
use Magento\Framework\App\Http\Context as HttpContext;
12+
use Magento\Framework\Serialize\Serializer\Json as Json;
13+
use Magento\Framework\View\Element\Block\ArgumentInterface;
14+
15+
/**
16+
* Customer's data view model
17+
*/
18+
class Data implements ArgumentInterface
19+
{
20+
/**
21+
* @var Json
22+
*/
23+
private $jsonEncoder;
24+
25+
/**
26+
*
27+
* @var HttpContext
28+
*/
29+
private $httpContext;
30+
31+
/**
32+
* @param HttpContext $httpContext
33+
* @param Json $jsonEncoder
34+
*/
35+
public function __construct(
36+
HttpContext $httpContext,
37+
Json $jsonEncoder
38+
) {
39+
$this->httpContext = $httpContext;
40+
$this->jsonEncoder = $jsonEncoder;
41+
}
42+
43+
/**
44+
* Check is user login
45+
*
46+
* @return bool
47+
*/
48+
public function isLoggedIn()
49+
{
50+
return $this->httpContext->getValue(Context::CONTEXT_AUTH);
51+
}
52+
53+
/**
54+
* Encode the mixed $valueToEncode into the JSON format
55+
*
56+
* @param mixed $valueToEncode
57+
* @return string
58+
*/
59+
public function jsonEncode($valueToEncode)
60+
{
61+
return $this->jsonEncoder->serialize($valueToEncode);
62+
}
63+
}

app/code/Magento/Customer/etc/frontend/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,7 @@
127127
</argument>
128128
</arguments>
129129
</type>
130+
<type name="Magento\Framework\App\FrontControllerInterface">
131+
<plugin name="delete-cookie-when-customer-not-exist" type="Magento\Customer\Model\App\FrontController\DeleteCookieWhenCustomerNotExistPlugin"/>
132+
</type>
130133
</config>

app/code/Magento/Customer/view/frontend/layout/default.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@
4848
</arguments>
4949
</block>
5050
<block name="customer.customer.data" class="Magento\Customer\Block\CustomerData"
51-
template="Magento_Customer::js/customer-data.phtml"/>
51+
template="Magento_Customer::js/customer-data.phtml">
52+
<arguments>
53+
<argument name="view_model" xsi:type="object">Magento\Customer\ViewModel\Customer\Data</argument>
54+
</arguments>
55+
</block>
5256
<block name="customer.data.invalidation.rules" class="Magento\Customer\Block\CustomerScopeData"
5357
template="Magento_Customer::js/customer-data/invalidation-rules.phtml"/>
5458
</referenceContainer>

app/code/Magento/Customer/view/frontend/templates/js/customer-data.phtml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
use Magento\Customer\ViewModel\Customer\Data;
7+
use Magento\Framework\App\ObjectManager;
68

79
/** @var \Magento\Customer\Block\CustomerData $block */
810

911
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundHelper
12+
/** @var Data $viewModel */
13+
$viewModel = $block->getViewModel() ?? ObjectManager::getInstance()->get(Data::class);
14+
$customerDataUrl = $block->getCustomerDataUrl('customer/account/updateSession');
15+
$expirableSectionNames = $block->getExpirableSectionNames();
1016
?>
1117
<script type="text/x-magento-init">
1218
{
1319
"*": {
1420
"Magento_Customer/js/customer-data": {
1521
"sectionLoadUrl": "<?= $block->escapeJs($block->getCustomerDataUrl('customer/section/load')) ?>",
1622
"expirableSectionLifetime": <?= (int)$block->getExpirableSectionLifetime() ?>,
17-
"expirableSectionNames": <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)
18-
->jsonEncode($block->getExpirableSectionNames()) ?>,
23+
"expirableSectionNames": <?= /* @noEscape */ $viewModel->jsonEncode($expirableSectionNames) ?>,
1924
"cookieLifeTime": "<?= $block->escapeJs($block->getCookieLifeTime()) ?>",
20-
"updateSessionUrl": "<?= $block->escapeJs(
21-
$block->getCustomerDataUrl('customer/account/updateSession')
22-
) ?>"
25+
"updateSessionUrl": "<?= $block->escapeJs($customerDataUrl) ?>",
26+
"isLoggedIn": "<?= /* @noEscape */ $viewModel->isLoggedIn() ?>"
2327
}
2428
}
2529
}

app/code/Magento/Customer/view/frontend/web/js/customer-data.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,20 @@ define([
4747
* Invalidate Cache By Close Cookie Session
4848
*/
4949
invalidateCacheByCloseCookieSession = function () {
50+
var isLoggedIn = parseInt(options.isLoggedIn, 10) || 0;
51+
5052
if (!$.cookieStorage.isSet('mage-cache-sessid')) {
5153
storage.removeAll();
5254
}
5355

56+
if (!$.localStorage.isSet('mage-customer-login')) {
57+
$.localStorage.set('mage-customer-login', isLoggedIn);
58+
}
59+
if ($.localStorage.get('mage-customer-login') !== isLoggedIn) {
60+
$.localStorage.set('mage-customer-login', isLoggedIn);
61+
storage.removeAll();
62+
}
63+
5464
$.cookieStorage.set('mage-cache-sessid', true);
5565
};
5666

0 commit comments

Comments
 (0)