Skip to content

Commit 95e38b6

Browse files
MC-21001: Add/move/delete attribute for attribute sets
1 parent 0e95aa8 commit 95e38b6

File tree

2 files changed

+85
-40
lines changed

2 files changed

+85
-40
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Eav\Model;
9+
10+
use Magento\Eav\Api\AttributeGroupRepositoryInterface;
11+
use Magento\Eav\Api\Data\AttributeGroupInterface;
12+
use Magento\Framework\Api\SearchCriteriaBuilder;
13+
14+
/**
15+
* Search and return attribute group by name.
16+
*/
17+
class GetAttributeGroupByName
18+
{
19+
/**
20+
* @var SearchCriteriaBuilder
21+
*/
22+
private $searchCriteriaBuilder;
23+
24+
/**
25+
* @var AttributeGroupRepositoryInterface
26+
*/
27+
private $groupRepository;
28+
29+
/**
30+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
31+
* @param AttributeGroupRepositoryInterface $attributeGroupRepository
32+
*/
33+
public function __construct(
34+
SearchCriteriaBuilder $searchCriteriaBuilder,
35+
AttributeGroupRepositoryInterface $attributeGroupRepository
36+
) {
37+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
38+
$this->groupRepository = $attributeGroupRepository;
39+
}
40+
41+
/**
42+
* Returns attribute group by name.
43+
*
44+
* @param int $setId
45+
* @param string $groupName
46+
* @return AttributeGroupInterface|null
47+
*/
48+
public function execute(int $setId, string $groupName): ?AttributeGroupInterface
49+
{
50+
$searchCriteria = $this->searchCriteriaBuilder->addFilter('attribute_group_name', $groupName)
51+
->addFilter('attribute_set_id', $setId)
52+
->create();
53+
$result = $this->groupRepository->getList($searchCriteria)->getItems();
54+
55+
return array_shift($result);
56+
}
57+
}

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/SetTest.php

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77

88
namespace Magento\Catalog\Model\Product\Attribute;
99

10+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
1011
use Magento\Catalog\Model\Product;
1112
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
12-
use Magento\Eav\Api\AttributeGroupRepositoryInterface;
1313
use Magento\Eav\Api\AttributeSetRepositoryInterface;
1414
use Magento\Eav\Api\Data\AttributeGroupInterface;
1515
use Magento\Eav\Model\Config;
1616
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set as AttributeSetResource;
17-
use Magento\Framework\Api\SearchCriteriaBuilder;
17+
use Magento\Framework\Api\AttributeInterface;
1818
use Magento\Framework\ObjectManagerInterface;
1919
use Magento\TestFramework\Helper\Bootstrap;
20+
use Magento\TestFramework\Eav\Model\GetAttributeGroupByName;
2021

2122
/**
2223
* Provides tests for attribute set model saving.
@@ -36,20 +37,15 @@ class SetTest extends \PHPUnit\Framework\TestCase
3637
private $setRepository;
3738

3839
/**
39-
* @var AttributeGroupRepositoryInterface
40+
* @var ProductAttributeRepositoryInterface
4041
*/
41-
private $groupRepository;
42+
private $attributeRepository;
4243

4344
/**
4445
* @var Config
4546
*/
4647
private $config;
4748

48-
/**
49-
* @var SearchCriteriaBuilder
50-
*/
51-
private $criteriaBuilder;
52-
5349
/**
5450
* @var AttributeSetResource
5551
*/
@@ -65,6 +61,11 @@ class SetTest extends \PHPUnit\Framework\TestCase
6561
*/
6662
private $defaultSetId;
6763

64+
/**
65+
* @var GetAttributeGroupByName
66+
*/
67+
private $attributeGroupByName;
68+
6869
/**
6970
* @inheritdoc
7071
*/
@@ -73,12 +74,12 @@ protected function setUp()
7374
parent::setUp();
7475
$this->objectManager = Bootstrap::getObjectManager();
7576
$this->setRepository = $this->objectManager->get(AttributeSetRepositoryInterface::class);
76-
$this->groupRepository = $this->objectManager->create(AttributeGroupRepositoryInterface::class);
77+
$this->attributeRepository = $this->objectManager->create(ProductAttributeRepositoryInterface::class);
7778
$this->config = $this->objectManager->get(Config::class);
7879
$this->defaultSetId = (int)$this->config->getEntityType(Product::ENTITY)->getDefaultAttributeSetId();
79-
$this->criteriaBuilder = $this->objectManager->create(SearchCriteriaBuilder::class);
8080
$this->attributeSetResource = $this->objectManager->get(AttributeSetResource::class);
8181
$this->attributeCollectionFactory = $this->objectManager->get(CollectionFactory ::class);
82+
$this->attributeGroupByName = $this->objectManager->get(GetAttributeGroupByName::class);
8283
}
8384

8485
/**
@@ -91,10 +92,9 @@ protected function setUp()
9192
public function testSaveWithGroupsAndAttributes(string $groupName, string $attributeCode): void
9293
{
9394
$set = $this->setRepository->get($this->defaultSetId);
94-
$groupId = $this->getAttributeGroup($groupName)
95-
? $this->getAttributeGroup($groupName)->getAttributeGroupId()
96-
: 'ynode-1';
97-
$attributeId = (int)$this->config->getAttribute(Product::ENTITY, $attributeCode)->getAttributeId();
95+
$attributeGroup = $this->getAttributeGroup($groupName);
96+
$groupId = $attributeGroup ? $attributeGroup->getAttributeGroupId() : 'ynode-1';
97+
$attributeId = (int)$this->attributeRepository->get($attributeCode)->getAttributeId();
9898
$additional = [
9999
'attributes' => [
100100
[$attributeId, $groupId, 1],
@@ -177,11 +177,11 @@ public function testSaveWithRemovedGroup(): void
177177
$this->getAttributeGroup('Design'),
178178
'Group Design wan\'t deleted.'
179179
);
180-
$unusedSetAttributes = $this->getUnusedSetAttributes((int)$set->getAttributeSetId());
180+
$unusedSetAttributes = $this->getSetExcludedAttributes((int)$set->getAttributeSetId());
181181
$designAttributeCodes = ['page_layout', 'options_container', 'custom_layout_update'];
182182
$this->assertNotEmpty(
183183
array_intersect($designAttributeCodes, $unusedSetAttributes),
184-
'Attributes from Design group still assigned to attribute set.'
184+
'Attributes from "Design" group still assigned to attribute set.'
185185
);
186186
}
187187

@@ -191,8 +191,7 @@ public function testSaveWithRemovedGroup(): void
191191
public function testSaveWithRemovedAttribute(): void
192192
{
193193
$set = $this->setRepository->get($this->defaultSetId);
194-
$attributeId = (int)$this->config->getAttribute(Product::ENTITY, 'meta_description')
195-
->getAttributeId();
194+
$attributeId = (int)$this->attributeRepository->get('meta_description')->getAttributeId();
196195
$additional = [
197196
'not_attributes' => [$this->getEntityAttributeId($this->defaultSetId, $attributeId)],
198197
];
@@ -201,7 +200,7 @@ public function testSaveWithRemovedAttribute(): void
201200
$this->config->clear();
202201
$setInfo = $this->attributeSetResource->getSetInfo([$attributeId], $this->defaultSetId);
203202
$this->assertEmpty($setInfo[$attributeId]);
204-
$unusedSetAttributes = $this->getUnusedSetAttributes((int)$set->getAttributeSetId());
203+
$unusedSetAttributes = $this->getSetExcludedAttributes((int)$set->getAttributeSetId());
205204
$this->assertNotEmpty(
206205
array_intersect(['meta_description'], $unusedSetAttributes),
207206
'Attribute still assigned to attribute set.'
@@ -235,12 +234,7 @@ private function getAttributeSetData(array $additional): array
235234
*/
236235
private function getAttributeGroup(string $groupName): ?AttributeGroupInterface
237236
{
238-
$searchCriteria = $this->criteriaBuilder->addFilter('attribute_group_name', $groupName)
239-
->addFilter('attribute_set_id', $this->defaultSetId)
240-
->create();
241-
$result = $this->groupRepository->getList($searchCriteria)->getItems();
242-
243-
return !empty($result) ? reset($result) : null;
237+
return $this->attributeGroupByName->execute($this->defaultSetId, $groupName);
244238
}
245239

246240
/**
@@ -249,32 +243,26 @@ private function getAttributeGroup(string $groupName): ?AttributeGroupInterface
249243
* @param int $setId
250244
* @return array
251245
*/
252-
private function getUnusedSetAttributes(int $setId): array
246+
private function getSetExcludedAttributes(int $setId): array
253247
{
254-
$result = [];
255-
$attributesIds = $this->attributeCollectionFactory->create()
256-
->setAttributeSetFilter($setId)
257-
->getAllIds();
258248
$collection = $this->attributeCollectionFactory->create()
259-
->setAttributesExcludeFilter($attributesIds)
260-
->addVisibleFilter();
261-
/** @var AbstractAttribute $attribute */
262-
foreach ($collection as $attribute) {
263-
$result[] = $attribute->getAttributeCode();
264-
}
249+
->setExcludeSetFilter($setId);
250+
$result = $collection->getColumnValues(AttributeInterface::ATTRIBUTE_CODE);
265251

266252
return $result;
267253
}
268254

269255
/**
270-
* @param int|null $setId
256+
* Returns entity attribute id.
257+
*
258+
* @param int $setId
271259
* @param int $attributeId
272260
* @return int
273261
*/
274-
private function getEntityAttributeId(?int $setId, int $attributeId): int
262+
private function getEntityAttributeId(int $setId, int $attributeId): int
275263
{
276264
$select = $this->attributeSetResource->getConnection()->select()
277-
->from('eav_entity_attribute', ['entity_attribute_id'])
265+
->from($this->attributeSetResource->getTable('eav_entity_attribute'), ['entity_attribute_id'])
278266
->where('attribute_set_id = ?', $setId)
279267
->where('attribute_id = ?', $attributeId);
280268

0 commit comments

Comments
 (0)