Skip to content

Commit

Permalink
MAGETWO-54733: Unable to save product with all unchecked values for m…
Browse files Browse the repository at this point in the history
…ultiple select attribute #7687
  • Loading branch information
VladimirZaets committed Jan 18, 2017
1 parent 146c196 commit 07d6f1c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
2 changes: 0 additions & 2 deletions app/code/Magento/Ui/view/base/web/js/form/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ define([
function beforeSave(data, url, selectorPrefix, messagesClass) {
var save = $.Deferred();

data = utils.filterFormData(data)
data = utils.serialize(utils.filterFormData(data));

data['form_key'] = window.FORM_KEY;

if (!url || url === 'undefined') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@
define([
'underscore',
'uiRegistry',
'Magento_Ui/js/form/client'
], function (_, registry, Constr) {
'Magento_Ui/js/form/client',
'jquery',
'mageUtils'
], function (_, registry, Constr, $, utils) {
'use strict';

describe('Magento_Ui/js/form/client', function () {

var obj = new Constr({
provider: 'provName',
name: '',
index: ''
});

window.FORM_KEY = 'magentoFormKey';

registry.set('provName', {
on: function () {
},
Expand Down Expand Up @@ -50,7 +53,65 @@ define([

expect(type).toEqual('object');
});
it('Check "beforeSave" method. Check call "filterFormData" inside themselves.', function () {
var data = {
key: {
anotherKey: 'value'
},
anotherKey: []
};

obj.urls.beforeSave = 'requestPath';
obj.selectorPrefix = 'selectorPrefix';
obj.messagesClass = 'messagesClass';
utils.filterFormData = jasmine.createSpy().and.returnValue(utils.filterFormData(data));

obj.save(data);
expect(utils.filterFormData).toHaveBeenCalledWith(data);
});
it('Check "beforeSave" method. Check call "serialize" inside themselves.', function () {
var data = {
key: {
anotherKey: 'value'
},
anotherKey: []
};

obj.urls.beforeSave = 'requestPath';
obj.selectorPrefix = 'selectorPrefix';
obj.messagesClass = 'messagesClass';
utils.serialize = jasmine.createSpy().and.returnValue(utils.serialize(data));

obj.save(data);
expect(utils.serialize).toHaveBeenCalledWith(data);
});
it('Check "beforeSave" method. Check call "ajax" inside themselves.', function () {
var data = {
key: {
anotherKey: 'value'
},
'anotherKey-prepared-for-send': []
},
result = {
url: obj.urls.beforeSave,
data: {
'key[anotherKey]': 'value',
'form_key': 'magentoFormKey'
},
success: jasmine.any(Function),
complete: jasmine.any(Function)
};

obj.urls.beforeSave = 'requestPath';
obj.selectorPrefix = 'selectorPrefix';
obj.messagesClass = 'messagesClass';
$.ajax = jasmine.createSpy();

obj.save(data);
expect($.ajax).toHaveBeenCalledWith(result);
});
});

describe('"initialize" method', function () {
it('Check for defined ', function () {
expect(obj.hasOwnProperty('initialize')).toBeDefined();
Expand Down
1 change: 1 addition & 0 deletions dev/tests/js/jasmine/tests/lib/mage/misc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ define([
expect(utils.filterFormData(data, suffix, separator)).toEqual(data);
expect(utils.filterFormData(data, suffix)).toEqual(data);
expect(utils.filterFormData(data)).toEqual(data);
expect(utils.filterFormData()).toEqual({});
});

it('Check convertToMomentFormat function for all Magento supported locales', function () {
Expand Down
19 changes: 15 additions & 4 deletions lib/web/mage/utils/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,25 @@ define([
return formData;
},

filterFormData: function (data, sufix, separator) {
sufix = sufix || 'prepared-for-send';
/**
* Filters data. Find properties with suffix
* and set his value to original properties.
*
* @param {Object} data
* @param {String} suffix
* @param {String} separator
*
* @returns {Object}
*/
filterFormData: function (data, suffix, separator) {
data = data || {};
suffix = suffix || 'prepared-for-send';
separator = separator || '-';

_.each(data, function (value, key) {
if (_.isObject(value) && !value.length) {
this.filterFormData(value, sufix, separator)
} else if (_.isString(key) && ~key.indexOf(sufix)) {
this.filterFormData(value, suffix, separator)
} else if (_.isString(key) && ~key.indexOf(suffix)) {
data[key.split(separator)[0]] = value;
delete data[key];
}
Expand Down

0 comments on commit 07d6f1c

Please sign in to comment.