Skip to content

Commit 32acf60

Browse files
MAGETWO-85579: 10814: Attribute repository resets sourceModel for new attributes. #1012
2 parents 506d500 + 208b470 commit 32acf60

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

app/code/Magento/Catalog/Model/Product/Attribute/Repository.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
118118
$attribute->setAttributeId($existingModel->getAttributeId());
119119
$attribute->setIsUserDefined($existingModel->getIsUserDefined());
120120
$attribute->setFrontendInput($existingModel->getFrontendInput());
121+
if ($attribute->getIsUserDefined()) {
122+
$this->processAttributeData($attribute);
123+
}
121124

122125
if (is_array($attribute->getFrontendLabels())) {
123126
$defaultFrontendLabel = $attribute->getDefaultFrontendLabel();
@@ -156,15 +159,7 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
156159
$this->validateCode($attribute->getAttributeCode());
157160
$this->validateFrontendInput($attribute->getFrontendInput());
158161

159-
$attribute->setBackendType(
160-
$attribute->getBackendTypeByInput($attribute->getFrontendInput())
161-
);
162-
$attribute->setSourceModel(
163-
$this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput())
164-
);
165-
$attribute->setBackendModel(
166-
$this->productHelper->getAttributeBackendModelByInputType($attribute->getFrontendInput())
167-
);
162+
$this->processAttributeData($attribute);
168163
$attribute->setEntityTypeId(
169164
$this->eavConfig
170165
->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)
@@ -275,4 +270,23 @@ protected function validateFrontendInput($frontendInput)
275270
throw InputException::invalidFieldValue('frontend_input', $frontendInput);
276271
}
277272
}
273+
274+
/**
275+
* Process attribute data based on attribute frontend input type.
276+
*
277+
* @param \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute
278+
* @return void
279+
*/
280+
private function processAttributeData(\Magento\Catalog\Api\Data\ProductAttributeInterface $attribute)
281+
{
282+
$attribute->setBackendType(
283+
$attribute->getBackendTypeByInput($attribute->getFrontendInput())
284+
);
285+
$attribute->setSourceModel(
286+
$this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput())
287+
);
288+
$attribute->setBackendModel(
289+
$this->productHelper->getAttributeBackendModelByInputType($attribute->getFrontendInput())
290+
);
291+
}
278292
}

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeRepositoryTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
namespace Magento\Catalog\Api;
88

99
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
10-
use Magento\TestFramework\Helper\Bootstrap;
1110

1211
class ProductAttributeRepositoryTest extends \Magento\TestFramework\TestCase\WebapiAbstract
1312
{
@@ -194,6 +193,36 @@ public function testUpdate()
194193
$this->assertEquals("Default Blue Updated", $result['options'][1]['label']);
195194
}
196195

196+
/**
197+
* Test source model and backend type can not be changed to custom, as they depends on attribute frontend type.
198+
*
199+
* @magentoApiDataFixture Magento/Catalog/Model/Product/Attribute/_files/create_attribute_service.php
200+
* @return void
201+
*/
202+
public function testUpdateAttributeSourceAndType()
203+
{
204+
$attributeCode = uniqid('label_attr_code');
205+
$attribute = $this->createAttribute($attributeCode);
206+
$attributeData = [
207+
'attribute' => [
208+
'attribute_id' => $attribute['attribute_id'],
209+
'attribute_code' => $attributeCode,
210+
'entity_type_id' => 4,
211+
'is_required' => false,
212+
'frontend_input' => 'select',
213+
'source_model' => "Some/Custom/Source/Model",
214+
'backend_type' => 'varchar',
215+
'frontend_labels' => [
216+
['store_id' => 1, 'label' => 'front_lbl_new'],
217+
],
218+
],
219+
];
220+
221+
$result = $this->updateAttribute($attributeCode, $attributeData);
222+
$this->assertEquals(\Magento\Eav\Model\Entity\Attribute\Source\Table::class, $result['source_model']);
223+
$this->assertEquals('int', $result['backend_type']);
224+
}
225+
197226
/**
198227
* @magentoApiDataFixture Magento/Catalog/Model/Product/Attribute/_files/create_attribute_service.php
199228
*/

0 commit comments

Comments
 (0)