Skip to content

Commit e530d78

Browse files
author
Oleksandr Dubovyk
committed
MAGETWO-53583: "Use Default" checkbox is checked again after saving empty fields (description, name, etc)
- Refactor - Fixed test
1 parent b840466 commit e530d78

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\Api\AttributeValueFactory;
1010
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Serialize\SerializerInterface;
1112

1213
/**
1314
* Entity/Attribute/Model - attribute abstract
@@ -116,6 +117,13 @@ abstract class AbstractAttribute extends \Magento\Framework\Model\AbstractExtens
116117
*/
117118
protected $dataObjectHelper;
118119

120+
/**
121+
* Serializer Instance.
122+
*
123+
* @var SerializerInterface
124+
*/
125+
private $serializer;
126+
119127
/**
120128
* Array of attribute types that have empty string as a possible value.
121129
*
@@ -185,6 +193,22 @@ public function __construct(
185193
$this->dataObjectHelper = $dataObjectHelper;
186194
}
187195

196+
/**
197+
* Get Serializer instance.
198+
* @deprecated
199+
*
200+
* @return SerializerInterface
201+
*/
202+
private function getSerializer()
203+
{
204+
if ($this->serializer === null) {
205+
$this->serializer = \Magento\Framework\App\ObjectManager::getInstance()->create(SerializerInterface::class);
206+
}
207+
208+
return $this->serializer;
209+
}
210+
211+
188212
/**
189213
* Initialize resource model
190214
*
@@ -1252,7 +1276,7 @@ public function getValidationRules()
12521276
if (is_array($rules)) {
12531277
return $rules;
12541278
} elseif (!empty($rules)) {
1255-
return unserialize($rules);
1279+
return $this->getSerializer()->unserialize($rules);
12561280
}
12571281

12581282
return [];

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,33 @@ public function testGetValidationRulesWhenRuleIsArray()
144144

145145
public function testGetValidationRulesWhenRuleIsSerialized()
146146
{
147-
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
148-
$rule = 'some value';
149-
$model = $objectManagerHelper->getObject(
150-
\Magento\Catalog\Model\Entity\Attribute::class,
151-
[
152-
'data' => [
153-
\Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES => serialize($rule)
154-
]
147+
$rule = serialize('some value');
148+
$expected = 'some test result';
155149

156-
]
157-
);
150+
$modelClassName = \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class;
151+
$model = $this->getMockForAbstractClass($modelClassName, [], '', false);
158152

159-
$this->assertEquals($rule, $model->getValidationRules());
153+
$serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class);
154+
155+
$reflection = new \ReflectionClass($modelClassName);
156+
$reflectionProperty = $reflection->getProperty('serializer');
157+
$reflectionProperty->setAccessible(true);
158+
$reflectionProperty->setValue($model, $serializerMock);
159+
160+
$model->setData(\Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES, $rule);
161+
162+
$serializerMock->method('unserialize')
163+
->with($rule)
164+
->willReturn($expected);
165+
166+
$this->assertEquals($expected, $model->getValidationRules());
167+
168+
$data = ['test array'];
169+
$model->setData(\Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES, $data);
170+
$this->assertEquals($data, $model->getValidationRules());
171+
172+
$model->setData(\Magento\Eav\Api\Data\AttributeInterface::VALIDATE_RULES, null);
173+
$this->assertEquals([], $model->getValidationRules());
160174
}
161175

162176
public function testGetValidationRulesWhenRuleIsEmpty()

0 commit comments

Comments
 (0)