Skip to content

Commit 12c7f14

Browse files
committed
ACP2E-3688: Attribute Default Value for Options Not Working
1 parent 9608ca2 commit 12c7f14

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,11 @@ protected function _saveOption(AbstractModel $object)
400400
}
401401

402402
if ($object->getDefaultValue()) {
403-
$defaultValue = array_unique(array_merge($defaultValue, explode(",", $object->getDefaultValue())));
403+
$frontendInput = $object->getFrontendInput();
404+
if ($frontendInput === 'multiselect') {
405+
$defaultValue =
406+
array_unique(array_merge($defaultValue, explode(",", $object->getDefaultValue())));
407+
}
404408
}
405409

406410
$this->_saveDefaultValue($object, $defaultValue);

app/code/Magento/Eav/Test/Unit/Model/ResourceModel/Entity/AttributeTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,78 @@ public function testSaveOptionSystemAttribute()
123123
$resourceModel->save($model);
124124
}
125125

126+
/**
127+
* Verify select option with multiple choice save one default value
128+
*
129+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
130+
*/
131+
public function testSaveSelectDefaultOptionAttribute()
132+
{
133+
list($connectionMock, $resourceModel) = $this->_prepareResourceModel();
134+
135+
$originalData = [
136+
'entity_type_id' => 4,
137+
'attribute_code' => 'custom_layout',
138+
'backend_model' => null,
139+
'backend_type' => 'varchar',
140+
'frontend_input' => 'select',
141+
'frontend_label' => 'Test Dropdown',
142+
'frontend_class' => null,
143+
'source_model' => Layout::class,
144+
'is_required' => 0,
145+
'is_user_defined' => 1,
146+
'is_unique' => 0,
147+
'default_value' => "option_3",
148+
];
149+
150+
$attributeData = [
151+
'id' => '999',
152+
'attribute_id' => '123',
153+
'entity_type_id' => 4,
154+
'attribute_code' => 'custom_layout',
155+
'backend_model' => null,
156+
'backend_type' => 'varchar',
157+
'frontend_input' => 'select',
158+
'frontend_label' => 'Test Dropdown',
159+
'frontend_class' => null,
160+
'source_model' => Layout::class,
161+
'is_required' => 0,
162+
'is_user_defined' => 1,
163+
'is_unique' => 0,
164+
'default_value' => "option_3",
165+
'default' => ['option_1'],
166+
'sort_order' => 1,
167+
];
168+
169+
$objectManagerHelper = new ObjectManager($this);
170+
/** @var AbstractModel $model */
171+
$arguments = $objectManagerHelper->getConstructArguments(AbstractModel::class);
172+
$arguments['data'] = $attributeData;
173+
$arguments['context'] = $this->contextMock;
174+
$model = $this->getMockBuilder(AbstractModel::class)
175+
->onlyMethods(['save'])
176+
->setConstructorArgs($arguments)
177+
->getMock();
178+
// $model->expects($this->any())->method('hasDataChanges')->willReturn(true);
179+
$model->setOption(
180+
[
181+
'delete' =>
182+
[
183+
'option_1' => ['choice_1', 'Frontend choice_1'],
184+
'option_2' => ['choice_2', 'Frontend choice_2'],
185+
'option_3' => ['choice_3', 'Frontend choice_3']
186+
]
187+
]
188+
);
189+
190+
$connectionMock->expects($this->any())
191+
->method('update')
192+
->with('eav_attribute', $this->logicalOr($originalData, ['default_value' => "option_1"]));
193+
// $connectionMock->expects($this->any())->method('getTransactionLevel')->willReturn(1);
194+
195+
$resourceModel->save($model);
196+
}
197+
126198
/**
127199
* @covers \Magento\Eav\Model\ResourceModel\Entity\Attribute::_saveOption
128200
*/

0 commit comments

Comments
 (0)