Skip to content

Commit 3e53601

Browse files
author
Alex Akimov
committed
MAGETWO-51943: Required varchar and text EAV Attributes Can be Saved as Empty String
1 parent 8cf5dc6 commit 3e53601

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

app/code/Magento/Eav/Model/Entity/Attribute/AbstractAttribute.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -593,15 +593,13 @@ protected function _getDefaultSourceModel()
593593
*/
594594
public function isValueEmpty($value)
595595
{
596-
$attrType = $this->getBackend()->getType();
597-
$isEmpty = (is_array($value) && count($value) == 0) ||
598-
$value === null ||
599-
$value === false && $attrType != 'int' ||
600-
$value === '' && ($attrType == 'int' ||
601-
$attrType == 'decimal' ||
602-
$attrType == 'datetime');
603-
604-
return $isEmpty;
596+
/** @var array $emptyStringTypes list of attribute types that treat empty string as a possible value */
597+
$emptyStringTypes = ['int', 'decimal', 'datetime', 'varchar', 'text'];
598+
$attributeType = $this->getBackend()->getType();
599+
return (is_array($value) && count($value) == 0)
600+
|| $value === null
601+
|| ($value === false && $attributeType != 'int')
602+
|| ($value === '' && in_array($attributeType, $emptyStringTypes));
605603
}
606604

607605
/**

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,59 @@ public function testGetValidationRulesWhenRuleIsEmpty()
175175

176176
$this->assertEquals([], $model->getValidationRules());
177177
}
178+
179+
/**
180+
* @param bool $isEmpty
181+
* @param mixed $value
182+
* @param string $attributeType
183+
* @dataProvider attributeValueDataProvider
184+
*/
185+
public function testIsValueEmpty($isEmpty, $value, $attributeType)
186+
{
187+
/** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $model */
188+
$model = $this->getMockForAbstractClass(
189+
'\Magento\Eav\Model\Entity\Attribute\AbstractAttribute',
190+
[],
191+
'',
192+
false,
193+
false,
194+
true,
195+
[
196+
'getBackend'
197+
]
198+
);
199+
$backendModelMock = $this->getMockForAbstractClass(
200+
'\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend',
201+
[],
202+
'',
203+
false,
204+
false,
205+
true,
206+
[
207+
'getType'
208+
]
209+
);
210+
$backendModelMock->expects($this->any())->method('getType')->willReturn($attributeType);
211+
$model->expects($this->once())->method('getBackend')->willReturn($backendModelMock);
212+
$this->assertEquals($isEmpty, $model->isValueEmpty($value));
213+
}
214+
215+
/**
216+
* @return array
217+
*/
218+
public function attributeValueDataProvider()
219+
{
220+
return [
221+
[true, '', 'int'],
222+
[true, '', 'decimal'],
223+
[true, '', 'datetime'],
224+
[true, '', 'varchar'],
225+
[true, '', 'text'],
226+
[true, null, 'varchar'],
227+
[true, [], 'varchar'],
228+
[true, false, 'varchar'],
229+
[false, 'not empty value', 'varchar'],
230+
[false, false, 'int'],
231+
];
232+
}
178233
}

0 commit comments

Comments
 (0)