Skip to content

Commit f7a2105

Browse files
author
Roman Lytvynenko
committed
Merge branch 'MC-36258' of https://github.com/magento-mpi/magento2ce into TANGO-PR-09-01-2020_24
2 parents 0678748 + 02934d4 commit f7a2105

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ define([
261261
}
262262
});
263263

264+
//remove expired section names of previously installed/enable modules
265+
expiredSectionNames = _.intersection(expiredSectionNames, sectionConfig.getSectionNames());
266+
264267
return _.uniq(expiredSectionNames);
265268
},
266269

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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+
9+
define([
10+
'jquery',
11+
'underscore',
12+
'Magento_Customer/js/section-config',
13+
'Magento_Customer/js/customer-data'
14+
], function (
15+
$,
16+
_,
17+
sectionConfig,
18+
customerData
19+
) {
20+
'use strict';
21+
22+
var sectionConfigSettings = {
23+
baseUrls: [
24+
'http://localhost/'
25+
],
26+
sections: {
27+
'customer/account/loginpost': ['*'],
28+
'checkout/cart/add': ['cart'],
29+
'rest/*/v1/guest-carts/*/selected-payment-method': ['cart','checkout-data'],
30+
'*': ['messages']
31+
},
32+
clientSideSections: [
33+
'checkout-data',
34+
'cart-data'
35+
],
36+
sectionNames: [
37+
'customer',
38+
'product_data_storage',
39+
'cart',
40+
'messages'
41+
]
42+
},
43+
cookieLifeTime = 3600,
44+
jQueryGetJSON;
45+
46+
function init(config) {
47+
var defaultConfig = {
48+
sectionLoadUrl: 'http://localhost/customer/section/load/',
49+
expirableSectionLifetime: 60, // minutes
50+
expirableSectionNames: ['cart'],
51+
cookieLifeTime: cookieLifeTime,
52+
updateSessionUrl: 'http://localhost/customer/account/updateSession/'
53+
};
54+
55+
customerData['Magento_Customer/js/customer-data']($.extend({}, defaultConfig, config || {}));
56+
}
57+
58+
function setupLocalStorage(sections) {
59+
var mageCacheStorage = {},
60+
sectionDataIds = {};
61+
62+
_.each(sections, function (sectionData, sectionName) {
63+
sectionDataIds[sectionName] = sectionData['data_id'];
64+
65+
if (typeof sectionData.content !== 'undefined') {
66+
mageCacheStorage[sectionName] = sectionData;
67+
}
68+
});
69+
70+
$.localStorage.set(
71+
'mage-cache-storage',
72+
mageCacheStorage
73+
);
74+
$.cookieStorage.set(
75+
'section_data_ids',
76+
sectionDataIds
77+
);
78+
79+
$.localStorage.set(
80+
'mage-cache-timeout',
81+
new Date(Date.now() + cookieLifeTime * 1000)
82+
);
83+
$.cookieStorage.set(
84+
'mage-cache-sessid',
85+
true
86+
);
87+
}
88+
89+
function clearLocalStorage() {
90+
$.cookieStorage.set('section_data_ids', {});
91+
92+
if (window.localStorage) {
93+
window.localStorage.clear();
94+
}
95+
}
96+
97+
describe('Magento_Customer/js/customer-data', function () {
98+
beforeAll(function () {
99+
clearLocalStorage();
100+
});
101+
102+
beforeEach(function () {
103+
jQueryGetJSON = $.getJSON;
104+
sectionConfig['Magento_Customer/js/section-config'](sectionConfigSettings);
105+
});
106+
107+
afterEach(function () {
108+
$.getJSON = jQueryGetJSON;
109+
clearLocalStorage();
110+
});
111+
112+
describe('getExpiredSectionNames()', function () {
113+
it('check that result contains expired section names', function () {
114+
setupLocalStorage({
115+
'cart': {
116+
'data_id': Math.floor(Date.now() / 1000) - 61 * 60, // 61 minutes ago
117+
'content': {}
118+
}
119+
});
120+
init();
121+
expect(customerData.getExpiredSectionNames()).toEqual(['cart']);
122+
});
123+
124+
it('check that result doest not contain unexpired section names', function () {
125+
setupLocalStorage({
126+
'cart': {
127+
'data_id': Math.floor(Date.now() / 1000) + 60, // in 1 minute
128+
'content': {}
129+
}
130+
});
131+
init();
132+
expect(customerData.getExpiredSectionNames()).toEqual([]);
133+
});
134+
135+
it('check that result contains invalidated section names', function () {
136+
setupLocalStorage({
137+
'cart': { // without storage content
138+
'data_id': Math.floor(Date.now() / 1000) + 60 // in 1 minute
139+
}
140+
});
141+
142+
init();
143+
expect(customerData.getExpiredSectionNames()).toEqual(['cart']);
144+
});
145+
146+
it('check that result does not contain unsupported section names', function () {
147+
setupLocalStorage({
148+
'catalog': { // without storage content
149+
'data_id': Math.floor(Date.now() / 1000) + 60 // in 1 minute
150+
}
151+
});
152+
153+
init();
154+
expect(customerData.getExpiredSectionNames()).toEqual([]);
155+
});
156+
});
157+
158+
describe('init()', function () {
159+
it('check that sections are not requested from server, if there are no expired sections', function () {
160+
setupLocalStorage({
161+
'catalog': { // without storage content
162+
'data_id': Math.floor(Date.now() / 1000) + 60 // in 1 minute
163+
}
164+
});
165+
166+
$.getJSON = jasmine.createSpy('$.getJSON').and.callFake(function () {
167+
var deferred = $.Deferred();
168+
169+
return deferred.promise();
170+
});
171+
172+
init();
173+
expect($.getJSON).not.toHaveBeenCalled();
174+
});
175+
it('check that sections are requested from server, if there are expired sections', function () {
176+
setupLocalStorage({
177+
'customer': {
178+
'data_id': Math.floor(Date.now() / 1000) + 60 // invalidated,
179+
},
180+
'cart': {
181+
'data_id': Math.floor(Date.now() / 1000) - 61 * 60, // 61 minutes ago
182+
'content': {}
183+
},
184+
'product_data_storage': {
185+
'data_id': Math.floor(Date.now() / 1000) + 60, // in 1 minute
186+
'content': {}
187+
},
188+
'catalog': {
189+
'data_id': Math.floor(Date.now() / 1000) + 60 // invalid section,
190+
},
191+
'checkout': {
192+
'data_id': Math.floor(Date.now() / 1000) - 61 * 60, // invalid section,
193+
'content': {}
194+
}
195+
});
196+
197+
$.getJSON = jasmine.createSpy('$.getJSON').and.callFake(function () {
198+
var deferred = $.Deferred();
199+
200+
return deferred.promise();
201+
});
202+
203+
init();
204+
expect($.getJSON).toHaveBeenCalledWith(
205+
'http://localhost/customer/section/load/',
206+
jasmine.objectContaining({
207+
sections: 'cart,customer'
208+
})
209+
);
210+
});
211+
});
212+
});
213+
});

0 commit comments

Comments
 (0)