Skip to content

Commit 2a8c5c7

Browse files
committed
Merge remote-tracking branch 'tango/MC-35934' into PR-10-14
2 parents 3cea4b1 + 6124598 commit 2a8c5c7

File tree

2 files changed

+88
-16
lines changed

2 files changed

+88
-16
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ public function beforeSave($object)
4242
public function validate($object)
4343
{
4444
$attributeCode = $this->getAttribute()->getAttributeCode();
45-
$data = $object->getData($attributeCode);
46-
if (is_array($data)) {
47-
$object->setData($attributeCode, implode(',', array_filter($data)));
48-
} elseif (empty($data)) {
49-
$object->setData($attributeCode, null);
45+
if ($object->hasData($attributeCode)) {
46+
$data = $object->getData($attributeCode);
47+
if (is_array($data)) {
48+
$object->setData($attributeCode, implode(',', array_filter($data)));
49+
} elseif (empty($data)) {
50+
$object->setData($attributeCode, null);
51+
}
5052
}
53+
5154
return parent::validate($object);
5255
}
5356
}

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

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,109 @@ class ArrayBackendTest extends TestCase
1717
/**
1818
* @var ArrayBackend
1919
*/
20-
protected $_model;
20+
private $_model;
2121

2222
/**
2323
* @var Attribute
2424
*/
25-
protected $_attribute;
25+
private $_attribute;
2626

2727
protected function setUp(): void
2828
{
2929
$this->_attribute = $this->createPartialMock(
3030
Attribute::class,
31-
['getAttributeCode', '__wakeup']
31+
['getAttributeCode', 'getDefaultValue', '__wakeup']
3232
);
3333
$this->_model = new ArrayBackend();
3434
$this->_model->setAttribute($this->_attribute);
3535
}
3636

3737
/**
38-
* @dataProvider attributeValueDataProvider
38+
* @dataProvider validateDataProvider
39+
* @param array $productData
40+
* @param bool $hasData
41+
* @param string|int|float|null $expectedValue
3942
*/
40-
public function testValidate($data)
43+
public function testValidate(array $productData, bool $hasData, $expectedValue)
4144
{
42-
$this->_attribute->expects($this->atLeastOnce())->method('getAttributeCode')->willReturn('code');
43-
$product = new DataObject(['code' => $data, 'empty' => null]);
45+
$this->_attribute->expects($this->atLeastOnce())
46+
->method('getAttributeCode')
47+
->willReturn('attr');
48+
49+
$product = new DataObject($productData);
4450
$this->_model->validate($product);
45-
$this->assertEquals('1,2,3', $product->getCode());
46-
$this->assertNull($product->getEmpty());
51+
$this->assertEquals($hasData, $product->hasData('attr'));
52+
$this->assertEquals($expectedValue, $product->getAttr());
53+
}
54+
55+
/**
56+
* @return array
57+
*/
58+
public static function validateDataProvider(): array
59+
{
60+
return [
61+
[
62+
['sku' => 'test1', 'attr' => [1, 2, 3]],
63+
true,
64+
'1,2,3',
65+
],
66+
[
67+
['sku' => 'test1', 'attr' => '1,2,3'],
68+
true,
69+
'1,2,3',
70+
],
71+
[
72+
['sku' => 'test1', 'attr' => null],
73+
true,
74+
null,
75+
],
76+
[
77+
['sku' => 'test1'],
78+
false,
79+
null,
80+
],
81+
];
82+
}
83+
84+
/**
85+
* @dataProvider beforeSaveDataProvider
86+
* @param array $productData
87+
* @param string $defaultValue
88+
* @param string $expectedValue
89+
*/
90+
public function testBeforeSave(
91+
array $productData,
92+
string $defaultValue,
93+
string $expectedValue
94+
) {
95+
$this->_attribute->expects($this->atLeastOnce())
96+
->method('getAttributeCode')
97+
->willReturn('attr');
98+
$this->_attribute->expects($this->any())
99+
->method('getDefaultValue')
100+
->willReturn($defaultValue);
101+
102+
$product = new DataObject($productData);
103+
$this->_model->beforeSave($product);
104+
$this->assertEquals($expectedValue, $product->getAttr());
47105
}
48106

49107
/**
50108
* @return array
51109
*/
52-
public static function attributeValueDataProvider()
110+
public function beforeSaveDataProvider(): array
53111
{
54-
return [[[1, 2, 3]], ['1,2,3']];
112+
return [
113+
[
114+
['sku' => 'test1', 'attr' => 'Value 2'],
115+
'Default value 1',
116+
'Value 2',
117+
],
118+
[
119+
['sku' => 'test1'],
120+
'Default value 1',
121+
'Default value 1',
122+
],
123+
];
55124
}
56125
}

0 commit comments

Comments
 (0)