Skip to content

Commit

Permalink
MAGETWO-85579: 10814: Attribute repository resets sourceModel for new…
Browse files Browse the repository at this point in the history
… attributes. #1012
  • Loading branch information
ishakhsuvarov authored Dec 13, 2017
2 parents 506d500 + 208b470 commit 32acf60
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
32 changes: 23 additions & 9 deletions app/code/Magento/Catalog/Model/Product/Attribute/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
$attribute->setAttributeId($existingModel->getAttributeId());
$attribute->setIsUserDefined($existingModel->getIsUserDefined());
$attribute->setFrontendInput($existingModel->getFrontendInput());
if ($attribute->getIsUserDefined()) {
$this->processAttributeData($attribute);
}

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

$attribute->setBackendType(
$attribute->getBackendTypeByInput($attribute->getFrontendInput())
);
$attribute->setSourceModel(
$this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput())
);
$attribute->setBackendModel(
$this->productHelper->getAttributeBackendModelByInputType($attribute->getFrontendInput())
);
$this->processAttributeData($attribute);
$attribute->setEntityTypeId(
$this->eavConfig
->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)
Expand Down Expand Up @@ -275,4 +270,23 @@ protected function validateFrontendInput($frontendInput)
throw InputException::invalidFieldValue('frontend_input', $frontendInput);
}
}

/**
* Process attribute data based on attribute frontend input type.
*
* @param \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute
* @return void
*/
private function processAttributeData(\Magento\Catalog\Api\Data\ProductAttributeInterface $attribute)
{
$attribute->setBackendType(
$attribute->getBackendTypeByInput($attribute->getFrontendInput())
);
$attribute->setSourceModel(
$this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput())
);
$attribute->setBackendModel(
$this->productHelper->getAttributeBackendModelByInputType($attribute->getFrontendInput())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
namespace Magento\Catalog\Api;

use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
use Magento\TestFramework\Helper\Bootstrap;

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

/**
* Test source model and backend type can not be changed to custom, as they depends on attribute frontend type.
*
* @magentoApiDataFixture Magento/Catalog/Model/Product/Attribute/_files/create_attribute_service.php
* @return void
*/
public function testUpdateAttributeSourceAndType()
{
$attributeCode = uniqid('label_attr_code');
$attribute = $this->createAttribute($attributeCode);
$attributeData = [
'attribute' => [
'attribute_id' => $attribute['attribute_id'],
'attribute_code' => $attributeCode,
'entity_type_id' => 4,
'is_required' => false,
'frontend_input' => 'select',
'source_model' => "Some/Custom/Source/Model",
'backend_type' => 'varchar',
'frontend_labels' => [
['store_id' => 1, 'label' => 'front_lbl_new'],
],
],
];

$result = $this->updateAttribute($attributeCode, $attributeData);
$this->assertEquals(\Magento\Eav\Model\Entity\Attribute\Source\Table::class, $result['source_model']);
$this->assertEquals('int', $result['backend_type']);
}

/**
* @magentoApiDataFixture Magento/Catalog/Model/Product/Attribute/_files/create_attribute_service.php
*/
Expand Down

0 comments on commit 32acf60

Please sign in to comment.