Skip to content

Commit b64e78a

Browse files
committed
MAGETWO-34346: Remove hard coded IDs from catalog API code
1 parent f4121a4 commit b64e78a

File tree

11 files changed

+182
-131
lines changed

11 files changed

+182
-131
lines changed

app/code/Magento/Catalog/Api/Data/CategoryAttributeInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@
99
interface CategoryAttributeInterface extends \Magento\Catalog\Api\Data\EavAttributeInterface
1010
{
1111
const ENTITY_TYPE_CODE = 'catalog_category';
12-
13-
const DEFAULT_ATTRIBUTE_SET_ID = 3;
1412
}

app/code/Magento/Catalog/Api/Data/ProductAttributeInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@
99
interface ProductAttributeInterface extends \Magento\Catalog\Api\Data\EavAttributeInterface
1010
{
1111
const ENTITY_TYPE_CODE = 'catalog_product';
12-
13-
const DEFAULT_ATTRIBUTE_SET_ID = 4;
1412
}

app/code/Magento/Catalog/Model/Category/AttributeRepository.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,30 @@ class AttributeRepository implements CategoryAttributeRepositoryInterface
2424
*/
2525
protected $eavAttributeRepository;
2626

27+
/**
28+
* @var \Magento\Eav\Model\Config
29+
*/
30+
protected $eavConfig;
31+
2732
/**
2833
* @param \Magento\Framework\Api\Config\MetadataConfig $metadataConfig
2934
* @param \Magento\Framework\Api\SearchCriteriaDataBuilder $searchCriteriaBuilder
3035
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
3136
* @param \Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepository
37+
* @param \Magento\Eav\Model\Config $eavConfig
3238
*/
3339
public function __construct(
3440
\Magento\Framework\Api\Config\MetadataConfig $metadataConfig,
3541
\Magento\Framework\Api\SearchCriteriaDataBuilder $searchCriteriaBuilder,
3642
\Magento\Framework\Api\FilterBuilder $filterBuilder,
37-
\Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepository
43+
\Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepository,
44+
\Magento\Eav\Model\Config $eavConfig
3845
) {
3946
$this->metadataConfig = $metadataConfig;
4047
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
4148
$this->filterBuilder = $filterBuilder;
4249
$this->eavAttributeRepository = $eavAttributeRepository;
50+
$this->eavConfig = $eavConfig;
4351
}
4452

4553
/**
@@ -69,11 +77,14 @@ public function get($attributeCode)
6977
*/
7078
public function getCustomAttributesMetadata($dataObjectClassName = null)
7179
{
80+
$defaultAttributeSetId = $this->eavConfig
81+
->getEntityType(\Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE)
82+
->getDefaultAttributeSetId();
7283
$searchCriteria = $this->searchCriteriaBuilder->addFilter(
7384
[
7485
$this->filterBuilder
7586
->setField('attribute_set_id')
76-
->setValue(\Magento\Catalog\Api\Data\CategoryAttributeInterface::DEFAULT_ATTRIBUTE_SET_ID)
87+
->setValue($defaultAttributeSetId)
7788
->create(),
7889
]
7990
);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,14 @@ public function deleteById($attributeCode)
227227
*/
228228
public function getCustomAttributesMetadata($dataObjectClassName = null)
229229
{
230+
$defaultAttributeSetId = $this->eavConfig
231+
->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)
232+
->getDefaultAttributeSetId();
230233
$searchCriteria = $this->searchCriteriaBuilder->addFilter(
231234
[
232235
$this->filterBuilder
233236
->setField('attribute_set_id')
234-
->setValue(\Magento\Catalog\Api\Data\ProductAttributeInterface::DEFAULT_ATTRIBUTE_SET_ID)
237+
->setValue($defaultAttributeSetId)
235238
->create(),
236239
]
237240
);

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
7171
*/
7272
protected $metadataService;
7373

74+
/**
75+
* @var \Magento\Eav\Model\Config
76+
*/
77+
protected $eavConfig;
78+
7479
/**
7580
* @param ProductFactory $productFactory
7681
* @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $initializationHelper
@@ -81,6 +86,8 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
8186
* @param Resource\Product $resourceModel
8287
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
8388
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $metadataServiceInterface
89+
* @param \Magento\Eav\Model\Config $eavConfig
90+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8491
*/
8592
public function __construct(
8693
ProductFactory $productFactory,
@@ -91,7 +98,8 @@ public function __construct(
9198
\Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository,
9299
\Magento\Catalog\Model\Resource\Product $resourceModel,
93100
\Magento\Framework\Api\FilterBuilder $filterBuilder,
94-
\Magento\Catalog\Api\ProductAttributeRepositoryInterface $metadataServiceInterface
101+
\Magento\Catalog\Api\ProductAttributeRepositoryInterface $metadataServiceInterface,
102+
\Magento\Eav\Model\Config $eavConfig
95103
) {
96104
$this->productFactory = $productFactory;
97105
$this->collectionFactory = $collectionFactory;
@@ -102,6 +110,7 @@ public function __construct(
102110
$this->attributeRepository = $attributeRepository;
103111
$this->filterBuilder = $filterBuilder;
104112
$this->metadataService = $metadataServiceInterface;
113+
$this->eavConfig = $eavConfig;
105114
}
106115

107116
/**
@@ -261,12 +270,14 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
261270
{
262271
/** @var \Magento\Catalog\Model\Resource\Product\Collection $collection */
263272
$collection = $this->collectionFactory->create();
264-
273+
$defaultAttributeSetId = $this->eavConfig
274+
->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)
275+
->getDefaultAttributeSetId();
265276
$extendedSearchCriteria = $this->searchCriteriaBuilder->addFilter(
266277
[
267278
$this->filterBuilder
268279
->setField('attribute_set_id')
269-
->setValue(\Magento\Catalog\Api\Data\ProductAttributeInterface::DEFAULT_ATTRIBUTE_SET_ID)
280+
->setValue($defaultAttributeSetId)
270281
->create(),
271282
]
272283
);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ProductAttributeManagementTest extends \Magento\TestFramework\TestCase\Web
2121

2222
public function testGetAttributes()
2323
{
24-
$attributeSetId = \Magento\Catalog\Api\Data\ProductAttributeInterface::DEFAULT_ATTRIBUTE_SET_ID;
24+
$attributeSetId = 4;
2525

2626
$serviceInfo = [
2727
'rest' => [
@@ -156,7 +156,7 @@ public function testUnassignAttribute()
156156
$serviceInfo,
157157
[
158158
'attributeSetId' => $payload['attributeSetId'],
159-
'attributeCode' => $payload['attributeCode']
159+
'attributeCode' => $payload['attributeCode'],
160160
]
161161
)
162162
);
@@ -165,10 +165,10 @@ public function testUnassignAttribute()
165165
protected function getAttributeData()
166166
{
167167
return [
168-
'attributeSetId' => \Magento\Catalog\Api\Data\ProductAttributeInterface::DEFAULT_ATTRIBUTE_SET_ID,
168+
'attributeSetId' => 4,
169169
'attributeGroupId' => 8,
170170
'attributeCode' => 'cost',
171-
'sortOrder' => 3
171+
'sortOrder' => 3,
172172
];
173173
}
174174

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/NewActionTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ class NewActionTest extends \Magento\Backend\Utility\Controller
2222
*/
2323
public function testCustomerGroupArePresentInGroupPriceTemplate()
2424
{
25-
$this->dispatch('backend/catalog/product/new/set/'
26-
. \Magento\Catalog\Api\Data\ProductAttributeInterface::DEFAULT_ATTRIBUTE_SET_ID
27-
. '/type/' . \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
28-
);
25+
$this->dispatch('backend/catalog/product/new/set/4/type/' . \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
2926
$lines = explode(PHP_EOL, $this->getResponse()->getBody());
3027
foreach ($lines as $index => $line) {
3128
if ($line && strpos($line, 'name="product[group_price][<%= data.index %>][cust_group]"') !== false) {

0 commit comments

Comments
 (0)