Skip to content

Commit 594db62

Browse files
committed
Merge remote-tracking branch 'origin/AC-15336' into spartans_pr_03092025
2 parents a038b5d + f46f7ec commit 594db62

File tree

3 files changed

+365
-3
lines changed

3 files changed

+365
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"Access denied (403 Forbidden). You may not have permission or your session expired.","Access denied (403 Forbidden). You may not have permission or your session expired."

app/code/Magento/LoginAsCustomerAdminUi/view/adminhtml/web/js/confirmation-popup.js

Lines changed: 11 additions & 3 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
define([
@@ -109,8 +109,16 @@ define([
109109
* @param {Object} jqXHR
110110
*/
111111
error: function (jqXHR) {
112+
let message = jqXHR.responseText;
113+
114+
// If it's HTML (403 page), show only the status text
115+
if (jqXHR.status === 403) {
116+
message = 'Access denied (403 Forbidden). ' +
117+
'You may not have permission or your session expired.';
118+
}
119+
112120
alert({
113-
content: _.escape(jqXHR.responseText)
121+
content: _.escape(message)
114122
});
115123
}
116124
});
Lines changed: 353 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,353 @@
1+
/**
2+
* Copyright 2025 Adobe
3+
* All Rights Reserved.
4+
*/
5+
6+
define([
7+
'jquery',
8+
'underscore',
9+
'squire'
10+
], function ($, _, Squire) {
11+
'use strict';
12+
13+
let injector = new Squire(),
14+
component;
15+
16+
describe('Magento_LoginAsCustomerAdminUi/js/confirmation-popup', function () {
17+
18+
beforeEach(function (done) {
19+
// Clear global function
20+
window.lacConfirmationPopup = undefined;
21+
22+
// Simple mocks - just spy functions
23+
injector.mock({
24+
'Magento_Ui/js/modal/confirm': jasmine.createSpy('confirm'),
25+
'Magento_Ui/js/modal/alert': jasmine.createSpy('alert'),
26+
'mage/translate': function (text) { return text; },
27+
'mage/template': function () { return '<div>Mock Template</div>'; },
28+
'text!Magento_LoginAsCustomerAdminUi/template/confirmation-popup/store-view-ptions.html':
29+
'<div>Mock Template</div>'
30+
});
31+
32+
injector.require([
33+
'Magento_LoginAsCustomerAdminUi/js/confirmation-popup'
34+
], function (Component) {
35+
component = Component;
36+
done();
37+
});
38+
});
39+
40+
afterEach(function () {
41+
window.lacConfirmationPopup = undefined;
42+
try {
43+
injector.clean();
44+
injector.remove();
45+
// eslint-disable-next-line no-unused-vars
46+
} catch (e) {
47+
// Ignore cleanup errors
48+
}
49+
});
50+
51+
describe('Component initialization', function () {
52+
it('Should be defined', function () {
53+
expect(component).toBeDefined();
54+
expect(typeof component).toBe('function');
55+
});
56+
57+
it('Should create lacConfirmationPopup global function after initialization', function () {
58+
const instance = new component({
59+
title: 'Test Title',
60+
content: 'Test Content'
61+
});
62+
63+
instance.initialize();
64+
65+
expect(window.lacConfirmationPopup).toBeDefined();
66+
expect(typeof window.lacConfirmationPopup).toBe('function');
67+
});
68+
69+
it('Should return false when called', function () {
70+
const instance = new component({
71+
title: 'Test Title',
72+
content: 'Test Content'
73+
});
74+
75+
instance.initialize();
76+
77+
// eslint-disable-next-line one-var
78+
const result = window.lacConfirmationPopup('http://test.url');
79+
80+
expect(result).toBe(false);
81+
});
82+
});
83+
84+
describe('Modal configuration', function () {
85+
it('Should call confirm modal with correct configuration', function (done) {
86+
// Create a fresh injector for this test
87+
const testInjector = new Squire(),
88+
confirmSpy = jasmine.createSpy('confirm');
89+
90+
testInjector.mock({
91+
'Magento_Ui/js/modal/confirm': confirmSpy,
92+
'Magento_Ui/js/modal/alert': jasmine.createSpy('alert'),
93+
'mage/translate': function (text) { return text; },
94+
'mage/template': function () { return '<div>Mock Template</div>'; },
95+
'text!Magento_LoginAsCustomerAdminUi/template/confirmation-popup/store-view-ptions.html':
96+
'<div>Mock Template</div>'
97+
});
98+
99+
testInjector.require([
100+
'Magento_LoginAsCustomerAdminUi/js/confirmation-popup'
101+
], function (TestComponent) {
102+
let instance = new TestComponent({
103+
title: 'Login as Customer',
104+
content: 'Are you sure?'
105+
}),
106+
modalConfig;
107+
108+
instance.initialize();
109+
window.lacConfirmationPopup('http://test.url');
110+
111+
expect(confirmSpy).toHaveBeenCalled();
112+
113+
modalConfig = confirmSpy.calls.argsFor(0)[0];
114+
expect(modalConfig.title).toBe('Login as Customer');
115+
expect(modalConfig.modalClass).toBe('confirm lac-confirm');
116+
expect(modalConfig.content).toContain('<div class="message message-warning">Are you sure?</div>');
117+
expect(modalConfig.buttons).toBeDefined();
118+
expect(modalConfig.buttons.length).toBe(2);
119+
120+
testInjector.clean();
121+
testInjector.remove();
122+
done();
123+
});
124+
});
125+
});
126+
127+
describe('AJAX functionality', function () {
128+
it('Should make AJAX request when confirm action is triggered', function (done) {
129+
// Mock AJAX FIRST, before creating the component
130+
const ajaxSpy = jasmine.createSpy('ajax'),
131+
originalAjax = $.ajax,
132+
testInjector = new Squire(), // Create a fresh injector for this test
133+
confirmSpy = jasmine.createSpy('confirm').and.callFake(function (config) {
134+
// Simulate user clicking confirm - trigger the confirm action
135+
if (config.actions && config.actions.confirm) {
136+
config.actions.confirm();
137+
}
138+
}),
139+
mockJQuery = $; // Mock jQuery itself to ensure the component uses our mocked ajax
140+
141+
$.ajax = ajaxSpy;
142+
mockJQuery.ajax = ajaxSpy;
143+
144+
// Mock DOM elements
145+
$('body').append('<input name="form_key" value="test_form_key">');
146+
$('body').append(
147+
'<select id="lac-confirmation-popup-store-id"><option value="2" selected>Store 2</option></select>'
148+
);
149+
150+
testInjector.mock({
151+
'jquery': mockJQuery,
152+
'Magento_Ui/js/modal/confirm': confirmSpy,
153+
'Magento_Ui/js/modal/alert': jasmine.createSpy('alert'),
154+
'mage/translate': function (text) { return text; },
155+
'mage/template': function () { return '<div>Mock Template</div>'; },
156+
'text!Magento_LoginAsCustomerAdminUi/template/confirmation-popup/store-view-ptions.html':
157+
'<div>Mock Template</div>'
158+
});
159+
160+
testInjector.require([
161+
'Magento_LoginAsCustomerAdminUi/js/confirmation-popup'
162+
], function (TestComponent) {
163+
const instance = new TestComponent({
164+
title: 'Test Title',
165+
content: 'Test Content'
166+
});
167+
168+
instance.initialize();
169+
window.lacConfirmationPopup('http://test.url/login');
170+
171+
// Verify confirm was called
172+
expect(confirmSpy).toHaveBeenCalled();
173+
174+
// Verify AJAX was called
175+
expect(ajaxSpy).toHaveBeenCalled();
176+
expect(ajaxSpy.calls.argsFor(0)[0].url).toBe('http://test.url/login');
177+
expect(ajaxSpy.calls.argsFor(0)[0].type).toBe('POST');
178+
expect(ajaxSpy.calls.argsFor(0)[0].dataType).toBe('json');
179+
180+
// Verify form data
181+
// eslint-disable-next-line one-var
182+
const ajaxData = ajaxSpy.calls.argsFor(0)[0].data;
183+
184+
expect(ajaxData.form_key).toBe('test_form_key');
185+
expect(ajaxData.store_id).toBe('2');
186+
187+
// Cleanup
188+
$('input[name="form_key"]').remove();
189+
$('#lac-confirmation-popup-store-id').remove();
190+
$.ajax = originalAjax;
191+
testInjector.clean();
192+
testInjector.remove();
193+
done();
194+
});
195+
});
196+
197+
it('Should handle successful response with redirect URL', function (done) {
198+
const ajaxSpy = jasmine.createSpy('ajax'),
199+
originalAjax = $.ajax;
200+
201+
$.ajax = ajaxSpy;
202+
spyOn(window, 'open');
203+
204+
// eslint-disable-next-line one-var
205+
const testInjector = new Squire(),
206+
confirmSpy = jasmine.createSpy('confirm').and.callFake(function (config) {
207+
if (config.actions && config.actions.confirm) {
208+
config.actions.confirm();
209+
}
210+
}),
211+
mockJQuery = $;
212+
213+
// Mock AJAX to call success callback
214+
ajaxSpy.and.callFake(function (options) {
215+
options.success({
216+
redirectUrl: 'http://customer.frontend.url'
217+
});
218+
});
219+
mockJQuery.ajax = ajaxSpy;
220+
221+
testInjector.mock({
222+
'jquery': mockJQuery,
223+
'Magento_Ui/js/modal/confirm': confirmSpy,
224+
'Magento_Ui/js/modal/alert': jasmine.createSpy('alert'),
225+
'mage/translate': function (text) { return text; },
226+
'mage/template': function () { return '<div>Mock Template</div>'; },
227+
'text!Magento_LoginAsCustomerAdminUi/template/confirmation-popup/store-view-ptions.html':
228+
'<div>Mock Template</div>'
229+
});
230+
231+
testInjector.require([
232+
'Magento_LoginAsCustomerAdminUi/js/confirmation-popup'
233+
], function (TestComponent) {
234+
const instance = new TestComponent({
235+
title: 'Test Title',
236+
content: 'Test Content'
237+
});
238+
239+
instance.initialize();
240+
window.lacConfirmationPopup('http://test.url');
241+
242+
expect(window.open).toHaveBeenCalledWith('http://customer.frontend.url');
243+
244+
// Cleanup
245+
$.ajax = originalAjax;
246+
testInjector.clean();
247+
testInjector.remove();
248+
done();
249+
});
250+
});
251+
252+
it('Should handle error response', function (done) {
253+
const ajaxSpy = jasmine.createSpy('ajax'),
254+
originalAjax = $.ajax,
255+
alertSpy = jasmine.createSpy('alert'),
256+
testInjector = new Squire(),
257+
confirmSpy = jasmine.createSpy('confirm').and.callFake(function (config) {
258+
if (config.actions && config.actions.confirm) {
259+
config.actions.confirm();
260+
}
261+
});
262+
263+
$.ajax = ajaxSpy;
264+
// Mock AJAX to call error callback
265+
ajaxSpy.and.callFake(function (options) {
266+
options.error({
267+
responseText: 'Error message',
268+
status: 500
269+
});
270+
});
271+
272+
// Mock jQuery with our ajax spy
273+
// eslint-disable-next-line one-var
274+
const mockJQuery = $;
275+
276+
mockJQuery.ajax = ajaxSpy;
277+
testInjector.mock({
278+
'jquery': mockJQuery,
279+
'Magento_Ui/js/modal/confirm': confirmSpy,
280+
'Magento_Ui/js/modal/alert': alertSpy,
281+
'mage/translate': function (text) { return text; },
282+
'mage/template': function () { return '<div>Mock Template</div>'; },
283+
'text!Magento_LoginAsCustomerAdminUi/template/confirmation-popup/store-view-ptions.html':
284+
'<div>Mock Template</div>'
285+
});
286+
287+
testInjector.require([
288+
'Magento_LoginAsCustomerAdminUi/js/confirmation-popup'
289+
], function (TestComponent) {
290+
const instance = new TestComponent({
291+
title: 'Test Title',
292+
content: 'Test Content'
293+
});
294+
295+
instance.initialize();
296+
window.lacConfirmationPopup('http://test.url');
297+
298+
expect(alertSpy).toHaveBeenCalled();
299+
expect(alertSpy.calls.argsFor(0)[0].content).toBe('Error message');
300+
301+
// Cleanup
302+
$.ajax = originalAjax;
303+
testInjector.clean();
304+
testInjector.remove();
305+
done();
306+
});
307+
});
308+
});
309+
310+
describe('Button click handlers', function () {
311+
it('Should handle button clicks correctly', function (done) {
312+
const testInjector = new Squire(),
313+
confirmSpy = jasmine.createSpy('confirm');
314+
315+
testInjector.mock({
316+
'Magento_Ui/js/modal/confirm': confirmSpy,
317+
'Magento_Ui/js/modal/alert': jasmine.createSpy('alert'),
318+
'mage/translate': function (text) { return text; },
319+
'mage/template': function () { return '<div>Mock Template</div>'; },
320+
'text!Magento_LoginAsCustomerAdminUi/template/confirmation-popup/store-view-ptions.html':
321+
'<div>Mock Template</div>'
322+
});
323+
324+
testInjector.require([
325+
'Magento_LoginAsCustomerAdminUi/js/confirmation-popup'
326+
], function (TestComponent) {
327+
const instance = new TestComponent({title: 'Test Title', content: 'Test Content'}),
328+
mockModal = { // Test cancel button
329+
closeModal: jasmine.createSpy('closeModal')
330+
};
331+
332+
instance.initialize();
333+
window.lacConfirmationPopup('http://test.url');
334+
335+
// eslint-disable-next-line one-var
336+
const modalConfig = confirmSpy.calls.argsFor(0)[0];
337+
338+
modalConfig.buttons[0].click.call(mockModal, {});
339+
expect(mockModal.closeModal).toHaveBeenCalledWith({});
340+
341+
// Test confirm button
342+
mockModal.closeModal.calls.reset();
343+
modalConfig.buttons[1].click.call(mockModal, {});
344+
expect(mockModal.closeModal).toHaveBeenCalledWith({}, true);
345+
346+
testInjector.clean();
347+
testInjector.remove();
348+
done();
349+
});
350+
});
351+
});
352+
});
353+
});

0 commit comments

Comments
 (0)