Skip to content

Commit 07d6f1c

Browse files
committed
MAGETWO-54733: Unable to save product with all unchecked values for multiple select attribute #7687
1 parent 146c196 commit 07d6f1c

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed

app/code/Magento/Ui/view/base/web/js/form/client.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ define([
2222
function beforeSave(data, url, selectorPrefix, messagesClass) {
2323
var save = $.Deferred();
2424

25-
data = utils.filterFormData(data)
2625
data = utils.serialize(utils.filterFormData(data));
27-
2826
data['form_key'] = window.FORM_KEY;
2927

3028
if (!url || url === 'undefined') {

dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/client.test.js

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@
1010
define([
1111
'underscore',
1212
'uiRegistry',
13-
'Magento_Ui/js/form/client'
14-
], function (_, registry, Constr) {
13+
'Magento_Ui/js/form/client',
14+
'jquery',
15+
'mageUtils'
16+
], function (_, registry, Constr, $, utils) {
1517
'use strict';
1618

1719
describe('Magento_Ui/js/form/client', function () {
18-
1920
var obj = new Constr({
2021
provider: 'provName',
2122
name: '',
2223
index: ''
2324
});
2425

26+
window.FORM_KEY = 'magentoFormKey';
27+
2528
registry.set('provName', {
2629
on: function () {
2730
},
@@ -50,7 +53,65 @@ define([
5053

5154
expect(type).toEqual('object');
5255
});
56+
it('Check "beforeSave" method. Check call "filterFormData" inside themselves.', function () {
57+
var data = {
58+
key: {
59+
anotherKey: 'value'
60+
},
61+
anotherKey: []
62+
};
63+
64+
obj.urls.beforeSave = 'requestPath';
65+
obj.selectorPrefix = 'selectorPrefix';
66+
obj.messagesClass = 'messagesClass';
67+
utils.filterFormData = jasmine.createSpy().and.returnValue(utils.filterFormData(data));
68+
69+
obj.save(data);
70+
expect(utils.filterFormData).toHaveBeenCalledWith(data);
71+
});
72+
it('Check "beforeSave" method. Check call "serialize" inside themselves.', function () {
73+
var data = {
74+
key: {
75+
anotherKey: 'value'
76+
},
77+
anotherKey: []
78+
};
79+
80+
obj.urls.beforeSave = 'requestPath';
81+
obj.selectorPrefix = 'selectorPrefix';
82+
obj.messagesClass = 'messagesClass';
83+
utils.serialize = jasmine.createSpy().and.returnValue(utils.serialize(data));
84+
85+
obj.save(data);
86+
expect(utils.serialize).toHaveBeenCalledWith(data);
87+
});
88+
it('Check "beforeSave" method. Check call "ajax" inside themselves.', function () {
89+
var data = {
90+
key: {
91+
anotherKey: 'value'
92+
},
93+
'anotherKey-prepared-for-send': []
94+
},
95+
result = {
96+
url: obj.urls.beforeSave,
97+
data: {
98+
'key[anotherKey]': 'value',
99+
'form_key': 'magentoFormKey'
100+
},
101+
success: jasmine.any(Function),
102+
complete: jasmine.any(Function)
103+
};
104+
105+
obj.urls.beforeSave = 'requestPath';
106+
obj.selectorPrefix = 'selectorPrefix';
107+
obj.messagesClass = 'messagesClass';
108+
$.ajax = jasmine.createSpy();
109+
110+
obj.save(data);
111+
expect($.ajax).toHaveBeenCalledWith(result);
112+
});
53113
});
114+
54115
describe('"initialize" method', function () {
55116
it('Check for defined ', function () {
56117
expect(obj.hasOwnProperty('initialize')).toBeDefined();

dev/tests/js/jasmine/tests/lib/mage/misc.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ define([
2828
expect(utils.filterFormData(data, suffix, separator)).toEqual(data);
2929
expect(utils.filterFormData(data, suffix)).toEqual(data);
3030
expect(utils.filterFormData(data)).toEqual(data);
31+
expect(utils.filterFormData()).toEqual({});
3132
});
3233

3334
it('Check convertToMomentFormat function for all Magento supported locales', function () {

lib/web/mage/utils/misc.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,25 @@ define([
209209
return formData;
210210
},
211211

212-
filterFormData: function (data, sufix, separator) {
213-
sufix = sufix || 'prepared-for-send';
212+
/**
213+
* Filters data. Find properties with suffix
214+
* and set his value to original properties.
215+
*
216+
* @param {Object} data
217+
* @param {String} suffix
218+
* @param {String} separator
219+
*
220+
* @returns {Object}
221+
*/
222+
filterFormData: function (data, suffix, separator) {
223+
data = data || {};
224+
suffix = suffix || 'prepared-for-send';
214225
separator = separator || '-';
215226

216227
_.each(data, function (value, key) {
217228
if (_.isObject(value) && !value.length) {
218-
this.filterFormData(value, sufix, separator)
219-
} else if (_.isString(key) && ~key.indexOf(sufix)) {
229+
this.filterFormData(value, suffix, separator)
230+
} else if (_.isString(key) && ~key.indexOf(suffix)) {
220231
data[key.split(separator)[0]] = value;
221232
delete data[key];
222233
}

0 commit comments

Comments
 (0)