Skip to content

Commit 7fa9534

Browse files
author
Anna Bukatar
committed
Merge branch 'ACP2E-98' of https://github.com/magento-l3/magento2ce into PR-2022-03-11
2 parents 0f0d7e0 + 331ad05 commit 7fa9534

File tree

6 files changed

+189
-1
lines changed

6 files changed

+189
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Customer\ViewModel\Customer;
10+
11+
use Magento\Customer\Model\Config\Share as ConfigShare;
12+
use Magento\Framework\View\Element\Block\ArgumentInterface;
13+
14+
class StoreConfig implements ArgumentInterface
15+
{
16+
17+
/**
18+
* @var ConfigShare
19+
*/
20+
private ConfigShare $configShare;
21+
22+
/**
23+
* Constructor
24+
*
25+
* @param ConfigShare $configShare
26+
*/
27+
public function __construct(
28+
ConfigShare $configShare
29+
) {
30+
$this->configShare = $configShare;
31+
}
32+
33+
/**
34+
* Get global account sharing is enabled or not
35+
*
36+
* @return bool
37+
*/
38+
public function isGlobalScopeEnabled(): bool
39+
{
40+
return $this->configShare->isGlobalScope();
41+
}
42+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
</item>
3939
</item>
4040
</argument>
41+
<argument name="view_model" xsi:type="object">Magento\Customer\ViewModel\Customer\StoreConfig</argument>
4142
</arguments>
4243
</block>
4344
<block name="customer.section.config" class="Magento\Customer\Block\SectionConfig"

app/code/Magento/Customer/view/frontend/requirejs-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ var config = {
1414
addressValidation: 'Magento_Customer/js/addressValidation',
1515
showPassword: 'Magento_Customer/js/show-password',
1616
'Magento_Customer/address': 'Magento_Customer/js/address',
17-
'Magento_Customer/change-email-password': 'Magento_Customer/js/change-email-password'
17+
'Magento_Customer/change-email-password': 'Magento_Customer/js/change-email-password',
18+
globalSessionLoader: 'Magento_Customer/js/customer-global-session-loader.js'
1819
}
1920
}
2021
};

app/code/Magento/Customer/view/frontend/templates/account/authentication-popup.phtml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
/** @var \Magento\Customer\Block\Account\AuthenticationPopup $block */
88
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
9+
/** @var Magento\Customer\ViewModel\Customer\StoreConfig $viewModel */
10+
$viewModel = $block->getViewModel();
911
?>
1012
<div id="authenticationPopup" data-bind="scope:'authenticationPopup', style: {display: 'none'}">
1113
<?php $scriptString = 'window.authenticationPopup = ' . /* @noEscape */ $block->getSerializedConfig(); ?>
1214
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false); ?>
1315
<!-- ko template: getTemplate() --><!-- /ko -->
16+
<?php // phpcs:ignore Magento2.Legacy.PhtmlTemplate ?>
1417
<script type="text/x-magento-init">
1518
{
1619
"#authenticationPopup": {
@@ -20,6 +23,10 @@
2023
"Magento_Ui/js/block-loader": "<?= $block->escapeJs($block->escapeUrl($block->getViewFileUrl(
2124
'images/loader-1.gif'
2225
))) ?>"
26+
<?php if ($viewModel->isGlobalScopeEnabled()): ?>
27+
,
28+
"Magento_Customer/js/customer-global-session-loader": {}
29+
<?php endif; ?>
2330
}
2431
}
2532
</script>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'jquery',
8+
'Magento_Customer/js/customer-data'
9+
], function ($, customerData) {
10+
'use strict';
11+
12+
return function () {
13+
var customer;
14+
15+
// When the session is available, this customer menu will be available
16+
if ($('.customer-menu').length > 0) {
17+
customer = customerData.get('customer');
18+
19+
customerData.getInitCustomerData().done(function () {
20+
// Check if the customer data is set in local storage, if not reload data from server
21+
if (!customer().firstname) {
22+
customerData.reload([], false);
23+
}
24+
});
25+
}
26+
};
27+
});
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/* eslint-disable max-nested-callbacks */
7+
/*jscs:disable jsDoc*/
8+
define([
9+
'squire', 'jquery', 'ko'
10+
], function (Squire, $, ko) {
11+
'use strict';
12+
13+
describe('Magento_Customer/js/customer-global-session-loader', function () {
14+
var injector,
15+
customer = ko.observable({}),
16+
mocks = {
17+
'Magento_Customer/js/customer-data': {
18+
get: jasmine.createSpy('get', function () {
19+
return customer;
20+
}).and.callThrough(),
21+
reload: jasmine.createSpy('reload'),
22+
getInitCustomerData: function () {}
23+
}
24+
},
25+
deferred,
26+
customerSessionLoader;
27+
28+
beforeAll(function () {
29+
$('body').append('<div id="customerMenu" class="customer-menu">Customer Menu</div>');
30+
});
31+
32+
beforeEach(function (done) {
33+
injector = new Squire();
34+
injector.mock(mocks);
35+
injector.require(['Magento_Customer/js/customer-global-session-loader'], function (instance) {
36+
customerSessionLoader = instance;
37+
done();
38+
});
39+
});
40+
41+
afterEach(function () {
42+
try {
43+
injector.clean();
44+
injector.remove();
45+
injector = null;
46+
} catch (e) {
47+
console.log(e);
48+
}
49+
50+
customer({});
51+
});
52+
53+
afterAll(function () {
54+
$('#customerMenu').remove();
55+
});
56+
57+
describe('Check customer data preparation process', function () {
58+
it('Tests that customer data does not reloads if it has first name defined', function () {
59+
spyOn(mocks['Magento_Customer/js/customer-data'], 'getInitCustomerData').and.callFake(function () {
60+
deferred = $.Deferred();
61+
62+
deferred.resolve();
63+
64+
return deferred.promise();
65+
});
66+
customer({
67+
firstname: 'First Name'
68+
});
69+
customerSessionLoader();
70+
71+
expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('customer');
72+
expect(mocks['Magento_Customer/js/customer-data'].reload).not.toHaveBeenCalled();
73+
});
74+
75+
it('Tests that customer data reloads after init, if customer first name is not there', function () {
76+
spyOn(mocks['Magento_Customer/js/customer-data'], 'getInitCustomerData').and.callFake(function () {
77+
deferred = $.Deferred();
78+
79+
return deferred.promise();
80+
});
81+
customer({
82+
_data: null
83+
});
84+
customerSessionLoader();
85+
86+
expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('customer');
87+
expect(mocks['Magento_Customer/js/customer-data'].reload).not.toHaveBeenCalled();
88+
89+
deferred.resolve();
90+
91+
expect(mocks['Magento_Customer/js/customer-data'].reload).toHaveBeenCalledWith([], false);
92+
});
93+
94+
it('Tests that customer data reloads if customer data is not there', function () {
95+
spyOn(mocks['Magento_Customer/js/customer-data'], 'getInitCustomerData').and.callFake(function () {
96+
deferred = $.Deferred();
97+
98+
deferred.resolve();
99+
100+
return deferred.promise();
101+
});
102+
expect(customerSessionLoader()).toBe(undefined);
103+
104+
expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('customer');
105+
expect(mocks['Magento_Customer/js/customer-data'].getInitCustomerData).toHaveBeenCalled();
106+
expect(mocks['Magento_Customer/js/customer-data'].reload).toHaveBeenCalledWith([], false);
107+
});
108+
});
109+
});
110+
});

0 commit comments

Comments
 (0)