Skip to content

Commit 8f5dd5f

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-57607' into 2.1.8-develop-pr17
2 parents dc2c986 + 97b10fa commit 8f5dd5f

File tree

6 files changed

+86
-9
lines changed

6 files changed

+86
-9
lines changed

app/code/Magento/Eav/Model/Entity/Attribute/Backend/ArrayBackend.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public function validate($object)
4242
$data = $object->getData($attributeCode);
4343
if (is_array($data)) {
4444
$object->setData($attributeCode, implode(',', array_filter($data)));
45+
} elseif (empty($data)) {
46+
$object->setData($attributeCode, null);
4547
}
4648
return parent::validate($object);
4749
}

app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Backend/ArrayTest.php renamed to app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Backend/ArrayBackendTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\Eav\Test\Unit\Model\Entity\Attribute\Backend;
77

8-
class ArrayTest extends \PHPUnit_Framework_TestCase
8+
class ArrayBackendTest extends \PHPUnit_Framework_TestCase
99
{
1010
/**
1111
* @var \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend
@@ -20,13 +20,13 @@ class ArrayTest extends \PHPUnit_Framework_TestCase
2020
protected function setUp()
2121
{
2222
$this->_attribute = $this->getMock(
23-
'Magento\Eav\Model\Entity\Attribute',
23+
\Magento\Eav\Model\Entity\Attribute::class,
2424
['getAttributeCode', '__wakeup'],
2525
[],
2626
'',
2727
false
2828
);
29-
$logger = $this->getMock('Psr\Log\LoggerInterface');
29+
$logger = $this->getMock(\Psr\Log\LoggerInterface::class);
3030
$this->_model = new \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend($logger);
3131
$this->_model->setAttribute($this->_attribute);
3232
}
@@ -37,9 +37,10 @@ protected function setUp()
3737
public function testValidate($data)
3838
{
3939
$this->_attribute->expects($this->atLeastOnce())->method('getAttributeCode')->will($this->returnValue('code'));
40-
$product = new \Magento\Framework\DataObject(['code' => $data]);
40+
$product = new \Magento\Framework\DataObject(['code' => $data, 'empty' => '']);
4141
$this->_model->validate($product);
4242
$this->assertEquals('1,2,3', $product->getCode());
43+
$this->assertEquals(null, $product->getEmpty());
4344
}
4445

4546
public static function attributeValueDataProvider()

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

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

25-
data = utils.serialize(data);
25+
data = utils.serialize(utils.filterFormData(data));
2626

2727
data['form_key'] = window.FORM_KEY;
2828

app/code/Magento/Ui/view/base/web/js/form/element/multiselect.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ define([
1313
return Select.extend({
1414
defaults: {
1515
size: 5,
16-
elementTmpl: 'ui/form/element/multiselect'
16+
elementTmpl: 'ui/form/element/multiselect',
17+
listens: {
18+
value: 'setDifferedFromDefault setPrepareToSendData'
19+
}
1720
},
1821

1922
/**
@@ -38,6 +41,21 @@ define([
3841
return _.isString(value) ? value.split(',') : value;
3942
},
4043

44+
/**
45+
* Sets the prepared data to dataSource
46+
* by path, where key is component link to dataSource with
47+
* suffix "-prepared-for-send".
48+
*
49+
* @param {Array} data - current component value
50+
*/
51+
setPrepareToSendData: function (data) {
52+
if (!data.length) {
53+
data = '';
54+
}
55+
56+
this.source.set(this.dataScope + '-prepared-for-send', data);
57+
},
58+
4159
/**
4260
* @inheritdoc
4361
*/

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/ProductTest.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Catalog\Controller\Adminhtml;
77

8+
use Magento\Catalog\Model\ProductRepository;
9+
810
/**
911
* @magentoAppArea adminhtml
1012
*/
@@ -27,7 +29,7 @@ public function testSaveActionWithDangerRequest()
2729
public function testSaveActionAndNew()
2830
{
2931
$this->getRequest()->setPostValue(['back' => 'new']);
30-
$repository = $this->_objectManager->create('Magento\Catalog\Model\ProductRepository');
32+
$repository = $this->_objectManager->create(ProductRepository::class);
3133
$product = $repository->get('simple');
3234
$this->dispatch('backend/catalog/product/save/id/' . $product->getEntityId());
3335
$this->assertRedirect($this->stringStartsWith('http://localhost/index.php/backend/catalog/product/new/'));
@@ -43,7 +45,7 @@ public function testSaveActionAndNew()
4345
public function testSaveActionAndDuplicate()
4446
{
4547
$this->getRequest()->setPostValue(['back' => 'duplicate']);
46-
$repository = $this->_objectManager->create('Magento\Catalog\Model\ProductRepository');
48+
$repository = $this->_objectManager->create(ProductRepository::class);
4749
$product = $repository->get('simple');
4850
$this->dispatch('backend/catalog/product/save/id/' . $product->getEntityId());
4951
$this->assertRedirect($this->stringStartsWith('http://localhost/index.php/backend/catalog/product/edit/'));
@@ -100,7 +102,7 @@ public function testIndexAction()
100102
*/
101103
public function testEditAction()
102104
{
103-
$repository = $this->_objectManager->create('Magento\Catalog\Model\ProductRepository');
105+
$repository = $this->_objectManager->create(ProductRepository::class);
104106
$product = $repository->get('simple');
105107
$this->dispatch('backend/catalog/product/edit/id/' . $product->getEntityId());
106108
$body = $this->getResponse()->getBody();
@@ -119,4 +121,31 @@ public function testEditAction()
119121
'"Save & Duplicate" button isn\'t present on Edit Product page'
120122
);
121123
}
124+
125+
/**
126+
* Tests Validate product action.
127+
*
128+
* @magentoDataFixture Magento/Catalog/_files/products_with_multiselect_attribute.php
129+
*
130+
* @return void
131+
*/
132+
public function testValidateAction()
133+
{
134+
$expectedResult = json_encode(['error' => false]);
135+
136+
$repository = $this->_objectManager->create(ProductRepository::class);
137+
$product = $repository->get('simple_ms_2');
138+
$data = $product->getData();
139+
unset($data['multiselect_attribute']);
140+
141+
$this->getRequest()->setPostValue(['product' => $data]);
142+
$this->dispatch('backend/catalog/product/validate');
143+
$response = $this->getResponse()->getBody();
144+
145+
$this->assertJsonStringEqualsJsonString(
146+
$expectedResult,
147+
$response,
148+
'Validate action returned incorrect result.'
149+
);
150+
}
122151
}

lib/web/mage/utils/misc.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,33 @@ define([
205205
}
206206

207207
return formData;
208+
},
209+
210+
/**
211+
* Filters data object. Finds properties with suffix
212+
* and sets their values to properties with the same name without suffix.
213+
*
214+
* @param {Object} data - The data object that should be filtered
215+
* @param {String} suffix - The string by which data object should be filtered
216+
* @param {String} separator - The string that is separator between property and suffix
217+
*
218+
* @returns {Object} Filtered data object
219+
*/
220+
filterFormData: function (data, suffix, separator) {
221+
data = data || {};
222+
suffix = suffix || 'prepared-for-send';
223+
separator = separator || '-';
224+
225+
_.each(data, function (value, key) {
226+
if (_.isObject(value) && !value.length) {
227+
this.filterFormData(value, suffix, separator);
228+
} else if (_.isString(key) && ~key.indexOf(suffix)) {
229+
data[key.split(separator)[0]] = value;
230+
delete data[key];
231+
}
232+
}, this);
233+
234+
return data;
208235
}
209236
};
210237
});

0 commit comments

Comments
 (0)