Skip to content

Commit e0991ff

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-4149' into PR_2025_10_01_flowers
2 parents 729ca02 + c711c90 commit e0991ff

File tree

5 files changed

+118
-14
lines changed

5 files changed

+118
-14
lines changed

app/code/Magento/Customer/Test/Unit/ViewModel/Customer/AuthTest.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\Customer\Test\Unit\ViewModel\Customer;
99

1010
use Magento\Customer\ViewModel\Customer\Auth;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
1112
use Magento\Framework\App\Http\Context;
1213
use PHPUnit\Framework\MockObject\MockObject;
1314
use PHPUnit\Framework\TestCase;
@@ -19,6 +20,11 @@ class AuthTest extends TestCase
1920
*/
2021
private mixed $contextMock;
2122

23+
/**
24+
* @var ScopeConfigInterface|MockObject
25+
*/
26+
private mixed $scopeConfigMock;
27+
2228
/**
2329
* @var Auth
2430
*/
@@ -33,8 +39,13 @@ protected function setUp(): void
3339
->disableOriginalConstructor()
3440
->getMock();
3541

42+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
43+
->disableOriginalConstructor()
44+
->getMock();
45+
3646
$this->model = new Auth(
37-
$this->contextMock
47+
$this->contextMock,
48+
$this->scopeConfigMock
3849
);
3950
parent::setUp();
4051
}
@@ -55,4 +66,33 @@ public function testIsLoggedIn(): void
5566
$this->model->isLoggedIn()
5667
);
5768
}
69+
70+
/**
71+
* @dataProvider getCustomerShareScopeDataProvider
72+
*/
73+
public function testGetCustomerShareScope($configValue, int $expected): void
74+
{
75+
$this->scopeConfigMock->expects($this->once())
76+
->method('getValue')
77+
->with(
78+
'customer/account_share/scope',
79+
\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE
80+
)
81+
->willReturn($configValue);
82+
83+
$this->assertSame($expected, $this->model->getCustomerShareScope());
84+
}
85+
86+
/**
87+
* @return array
88+
*/
89+
public function getCustomerShareScopeDataProvider(): array
90+
{
91+
return [
92+
'global scope as string 0' => ['0', 0],
93+
'website scope as string 1' => ['1', 1],
94+
'null value defaults to 0' => [null, 0],
95+
'empty string defaults to 0' => ['', 0],
96+
];
97+
}
5898
}

app/code/Magento/Customer/ViewModel/Customer/Auth.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\Customer\ViewModel\Customer;
99

1010
use Magento\Customer\Model\Context;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
1112
use Magento\Framework\App\Http\Context as HttpContext;
1213
use Magento\Framework\View\Element\Block\ArgumentInterface;
14+
use Magento\Store\Model\ScopeInterface;
1315

1416
/**
1517
* Customer's auth view model
@@ -18,9 +20,11 @@ class Auth implements ArgumentInterface
1820
{
1921
/**
2022
* @param HttpContext $httpContext
23+
* @param ScopeConfigInterface $scopeConfig
2124
*/
2225
public function __construct(
23-
private HttpContext $httpContext
26+
private HttpContext $httpContext,
27+
private ScopeConfigInterface $scopeConfig
2428
) {
2529
}
2630

@@ -33,4 +37,17 @@ public function isLoggedIn(): bool
3337
{
3438
return $this->httpContext->getValue(Context::CONTEXT_AUTH) ?? false;
3539
}
40+
41+
/**
42+
* Get customer account share scope
43+
*
44+
* @return int
45+
*/
46+
public function getCustomerShareScope(): int
47+
{
48+
return (int) $this->scopeConfig->getValue(
49+
'customer/account_share/scope',
50+
ScopeInterface::SCOPE_WEBSITE
51+
);
52+
}
3653
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ $cookieSettings = $block->getCookieSettings();
3333
"cookieLifeTime": "<?= $escaper->escapeJs($block->getCookieLifeTime()) ?>",
3434
"cookieDomain": "<?= $escaper->escapeJs($cookieSettings->getCookieDomain()) ?>",
3535
"updateSessionUrl": "<?= $escaper->escapeJs($customerDataUrl) ?>",
36-
"isLoggedIn": "<?= /* @noEscape */ $auth->isLoggedIn() ?>"
36+
"isLoggedIn": "<?= /* @noEscape */ $auth->isLoggedIn() ?>",
37+
"customerShare": "<?= $escaper->escapeJs($auth->getCustomerShareScope()) ?>"
3738
}
3839
}
3940
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ define([
1717
], function ($, _, ko, sectionConfig, url) {
1818
'use strict';
1919

20+
const GLOBAL_SHARE = '0';
21+
2022
var options = {},
2123
storage,
2224
storageInvalidation,
@@ -47,17 +49,22 @@ define([
4749
* Invalidate Cache By Close Cookie Session
4850
*/
4951
invalidateCacheByCloseCookieSession = function () {
50-
var isLoggedIn = parseInt(options.isLoggedIn, 10) || 0;
52+
var isLoggedIn = parseInt(options.isLoggedIn, 10) || 0,
53+
loginStorage = $.localStorage;
5154

5255
if (!$.cookieStorage.isSet('mage-cache-sessid')) {
5356
storage.removeAll();
5457
}
5558

56-
if (!$.localStorage.isSet('mage-customer-login')) {
57-
$.localStorage.set('mage-customer-login', isLoggedIn);
59+
if (options.customerShare === GLOBAL_SHARE) {
60+
loginStorage = $.cookieStorage;
61+
}
62+
63+
if (!loginStorage.isSet('mage-customer-login')) {
64+
loginStorage.set('mage-customer-login', isLoggedIn);
5865
}
59-
if ($.localStorage.get('mage-customer-login') !== isLoggedIn) {
60-
$.localStorage.set('mage-customer-login', isLoggedIn);
66+
if (loginStorage.get('mage-customer-login') !== isLoggedIn) {
67+
loginStorage.set('mage-customer-login', isLoggedIn);
6168
storage.removeAll();
6269
}
6370

dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* Copyright © Magento, Inc. All rights reserved.
3-
* See COPYING.txt for license details.
2+
* Copyright 2020 Adobe
3+
* All Rights Reserved.
44
*/
55

66
/* eslint max-nested-callbacks: 0 */
@@ -122,6 +122,7 @@ define([
122122
clearLocalStorage();
123123
injector.clean();
124124
injector.remove();
125+
// eslint-disable-next-line no-unused-vars
125126
} catch (e) {
126127
}
127128
});
@@ -525,5 +526,43 @@ define([
525526
expect(obj.hasOwnProperty('Magento_Customer/js/customer-data')).toBeDefined();
526527
});
527528
});
529+
530+
describe('Customer share scope handling', function () {
531+
var originalCookieStorage,
532+
originalLocalStorage;
533+
534+
beforeEach(function () {
535+
originalCookieStorage = $.cookieStorage;
536+
originalLocalStorage = $.localStorage;
537+
538+
$.cookieStorage = jasmine.createSpyObj('cookieStorage', ['isSet', 'get', 'set', 'setConf']);
539+
$.localStorage = jasmine.createSpyObj('localStorage', ['isSet', 'get', 'set']);
540+
});
541+
542+
afterEach(function () {
543+
$.cookieStorage = originalCookieStorage;
544+
$.localStorage = originalLocalStorage;
545+
});
546+
547+
it('Should use cookieStorage for login state when customerShare is global (0)', function () {
548+
init({
549+
customerShare: '0',
550+
isLoggedIn: '1'
551+
});
552+
553+
expect($.cookieStorage.isSet).toHaveBeenCalledWith('mage-customer-login');
554+
expect($.cookieStorage.get).toHaveBeenCalledWith('mage-customer-login');
555+
});
556+
557+
it('Should use localStorage for login state when customerShare is not global (1)', function () {
558+
init({
559+
customerShare: '1',
560+
isLoggedIn: '1'
561+
});
562+
563+
expect($.localStorage.isSet).toHaveBeenCalledWith('mage-customer-login');
564+
expect($.localStorage.get).toHaveBeenCalledWith('mage-customer-login');
565+
});
566+
});
528567
});
529568
});

0 commit comments

Comments
 (0)