Skip to content

Commit

Permalink
ENGCOM-1402: Rest API attribute set updating not working as expected #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Idolov authored Apr 30, 2018
2 parents 0b54cdc + 24bba3e commit 24a49b9
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 24 deletions.
29 changes: 26 additions & 3 deletions app/code/Magento/Catalog/Model/Product/Attribute/SetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/
namespace Magento\Catalog\Model\Product\Attribute;

use Magento\Framework\Exception\InputException;

class SetRepository implements \Magento\Catalog\Api\AttributeSetRepositoryInterface
{
/**
Expand Down Expand Up @@ -53,7 +51,7 @@ public function __construct(
*/
public function save(\Magento\Eav\Api\Data\AttributeSetInterface $attributeSet)
{
$this->validate($attributeSet);
$this->validateBeforeSave($attributeSet);
return $this->attributeSetRepository->save($attributeSet);
}

Expand Down Expand Up @@ -127,4 +125,29 @@ protected function validate(\Magento\Eav\Api\Data\AttributeSetInterface $attribu
);
}
}

/**
* Validate attribute set entity type id.
*
* @param \Magento\Eav\Api\Data\AttributeSetInterface $attributeSet
* @return void
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function validateBeforeSave(\Magento\Eav\Api\Data\AttributeSetInterface $attributeSet)
{
$productEntityId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId();
$result = $attributeSet->getEntityTypeId() === $productEntityId;
if (!$result && $attributeSet->getAttributeSetId()) {
$existingAttributeSet = $this->attributeSetRepository->get($attributeSet->getAttributeSetId());
$attributeSet->setEntityTypeId($existingAttributeSet->getEntityTypeId());
$result = $existingAttributeSet->getEntityTypeId() === $productEntityId;
}
if (!$result) {
throw new \Magento\Framework\Exception\StateException(
__('Provided Attribute set non product Attribute set.')
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Catalog\Api;

use Magento\Eav\Api\Data\AttributeSetInterface;
use Magento\TestFramework\TestCase\WebapiAbstract;

class AttributeSetRepositoryTest extends WebapiAbstract
Expand Down Expand Up @@ -71,38 +72,39 @@ public function testSave()
{
$attributeSetName = 'empty_attribute_set';
$attributeSet = $this->getAttributeSetByName($attributeSetName);
$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/products/attribute-sets/' . $attributeSet->getId(),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
],
'soap' => [
'service' => 'catalogAttributeSetRepositoryV1',
'serviceVersion' => 'V1',
'operation' => 'catalogAttributeSetRepositoryV1Save',
$updatedSortOrder = $attributeSet->getSortOrder() + 200;
$arguments = [
'attributeSet' => [
'attribute_set_id' => $attributeSet->getId(),
// name is the same, because it is used by fixture rollback script
'attribute_set_name' => $attributeSet->getAttributeSetName(),
'entity_type_id' => $attributeSet->getEntityTypeId(),
'sort_order' => $updatedSortOrder,
],
];
$result = $this->save($attributeSet, $arguments);
$this->assertAttributeSetData($result, $attributeSetName, $updatedSortOrder);
}

/**
* Test update attribute set without specified optional "entity_type_id" param.
*
* @magentoApiDataFixture Magento/Eav/_files/empty_attribute_set.php
*/
public function testUpdateWithoutEntityType()
{
$attributeSetName = 'empty_attribute_set';
$attributeSet = $this->getAttributeSetByName('empty_attribute_set');
$updatedSortOrder = $attributeSet->getSortOrder() + 200;

$arguments = [
'attributeSet' => [
'attribute_set_id' => $attributeSet->getId(),
// name is the same, because it is used by fixture rollback script
'attribute_set_name' => $attributeSet->getAttributeSetName(),
'entity_type_id' => $attributeSet->getEntityTypeId(),
'sort_order' => $updatedSortOrder,
],
];
$result = $this->_webApiCall($serviceInfo, $arguments);
$this->assertNotNull($result);
// Reload attribute set data
$attributeSet = $this->getAttributeSetByName($attributeSetName);
$this->assertEquals($attributeSet->getAttributeSetId(), $result['attribute_set_id']);
$this->assertEquals($attributeSet->getAttributeSetName(), $result['attribute_set_name']);
$this->assertEquals($attributeSet->getEntityTypeId(), $result['entity_type_id']);
$this->assertEquals($updatedSortOrder, $result['sort_order']);
$this->assertEquals($attributeSet->getSortOrder(), $result['sort_order']);
$result = $this->save($attributeSet, $arguments);
$this->assertAttributeSetData($result, $attributeSetName, $updatedSortOrder);
}

/**
Expand Down Expand Up @@ -215,4 +217,50 @@ protected function getAttributeSetByName($attributeSetName)
}
return $attributeSet;
}

/**
* Save given attribute set with specified arguments.
*
* @param AttributeSetInterface $attributeSet
* @param array $arguments
* @return array
*/
private function save(AttributeSetInterface $attributeSet, array $arguments)
{
$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/products/attribute-sets/' . $attributeSet->getAttributeSetId(),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
],
'soap' => [
'service' => 'catalogAttributeSetRepositoryV1',
'serviceVersion' => 'V1',
'operation' => 'catalogAttributeSetRepositoryV1Save',
],
];

return $this->_webApiCall($serviceInfo, $arguments);
}

/**
* Check attribute set data.
*
* @param string $attributeSetName
* @param array $result
* @param int $updatedSortOrder
*/
private function assertAttributeSetData(
array $result,
string $attributeSetName,
int $updatedSortOrder
) {
$this->assertNotNull($result);
// Reload attribute set data
$attributeSet = $this->getAttributeSetByName($attributeSetName);
$this->assertEquals($attributeSet->getAttributeSetId(), $result['attribute_set_id']);
$this->assertEquals($attributeSet->getAttributeSetName(), $result['attribute_set_name']);
$this->assertEquals($attributeSet->getEntityTypeId(), $result['entity_type_id']);
$this->assertEquals($updatedSortOrder, $result['sort_order']);
$this->assertEquals($attributeSet->getSortOrder(), $result['sort_order']);
}
}

0 comments on commit 24a49b9

Please sign in to comment.