Skip to content

Commit 02438a7

Browse files
committed
MC-20691: Admin: Update attribute set
1 parent b169e62 commit 02438a7

File tree

7 files changed

+560
-0
lines changed

7 files changed

+560
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\AttributeSetRepositoryInterface;
11+
use Magento\Eav\Api\Data\AttributeSetInterface;
12+
use Magento\Framework\Api\SearchCriteriaBuilder;
13+
14+
/**
15+
* Attribute set additional functions.
16+
*/
17+
class AttributeSet
18+
{
19+
/**
20+
* @var SearchCriteriaBuilder
21+
*/
22+
private $searchCriteriaBuilder;
23+
24+
/**
25+
* @var AttributeSetRepositoryInterface
26+
*/
27+
private $attributeSetRepository;
28+
29+
/**
30+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
31+
* @param AttributeSetRepositoryInterface $attributeSetRepository
32+
*/
33+
public function __construct(
34+
SearchCriteriaBuilder $searchCriteriaBuilder,
35+
AttributeSetRepositoryInterface $attributeSetRepository
36+
) {
37+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
38+
$this->attributeSetRepository = $attributeSetRepository;
39+
}
40+
41+
/**
42+
* Search and return attribute set by name.
43+
*
44+
* @param string $attributeSetName
45+
* @return AttributeSetInterface|null
46+
*/
47+
public function getAttributeSetByName(string $attributeSetName): ?AttributeSetInterface
48+
{
49+
$this->searchCriteriaBuilder->addFilter('attribute_set_name', $attributeSetName);
50+
$searchCriteria = $this->searchCriteriaBuilder->create();
51+
$result = $this->attributeSetRepository->getList($searchCriteria);
52+
$items = $result->getItems();
53+
54+
return array_pop($items);
55+
}
56+
}
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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\Catalog\Controller\Adminhtml\Product\Set;
9+
10+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
11+
use Magento\Eav\Api\AttributeManagementInterface;
12+
use Magento\Eav\Api\AttributeSetRepositoryInterface;
13+
use Magento\Eav\Api\Data\AttributeGroupInterface;
14+
use Magento\Eav\Api\Data\AttributeSetInterface;
15+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection;
16+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
17+
use Magento\Framework\App\Request\Http as HttpRequest;
18+
use Magento\Framework\Serialize\Serializer\Json;
19+
use Magento\TestFramework\Eav\Model\AttributeSet;
20+
use Magento\TestFramework\TestCase\AbstractBackendController;
21+
22+
/**
23+
* Test update attribute set.
24+
*/
25+
class UpdateTest extends AbstractBackendController
26+
{
27+
/**
28+
* @var Json
29+
*/
30+
private $json;
31+
32+
/**
33+
* @var AttributeSetRepositoryInterface
34+
*/
35+
private $attributeSetRepository;
36+
37+
/**
38+
* @var AttributeManagementInterface
39+
*/
40+
private $attributeManagement;
41+
42+
/**
43+
* @var CollectionFactory
44+
*/
45+
private $attributeGroupCollectionFactory;
46+
47+
/**
48+
* @var AttributeSet
49+
*/
50+
private $attributeSet;
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
protected function setUp()
56+
{
57+
parent::setUp();
58+
$this->json = $this->_objectManager->get(Json::class);
59+
$this->attributeSetRepository = $this->_objectManager->get(AttributeSetRepositoryInterface::class);
60+
$this->attributeManagement = $this->_objectManager->get(AttributeManagementInterface::class);
61+
$this->attributeGroupCollectionFactory = $this->_objectManager->get(CollectionFactory::class);
62+
$this->attributeSet = $this->_objectManager->get(AttributeSet::class);
63+
}
64+
65+
/**
66+
* Test that name of attribute set will update/change correctly.
67+
*
68+
* @magentoDataFixture Magento/Catalog/_files/attribute_set_based_on_default.php
69+
*
70+
* @magentoDbIsolation disabled
71+
*
72+
* @return void
73+
*/
74+
public function testUpdateAttributeSetName(): void
75+
{
76+
$attributeSet = $this->attributeSet->getAttributeSetByName('new_attribute_set');
77+
$currentAttrSetName = $attributeSet->getAttributeSetName();
78+
$this->assertNotNull($attributeSet);
79+
$postData = $this->prepareDataToRequest($attributeSet);
80+
$updateName = 'New attribute set name';
81+
$postData['attribute_set_name'] = $updateName;
82+
$this->performRequest((int)$attributeSet->getAttributeSetId(), $postData);
83+
$updatedAttributeSet = $this->attributeSetRepository->get((int)$attributeSet->getAttributeSetId());
84+
$this->assertEquals($updateName, $updatedAttributeSet->getAttributeSetName());
85+
$updatedAttributeSet->setAttributeSetName($currentAttrSetName);
86+
$this->attributeSetRepository->save($updatedAttributeSet);
87+
}
88+
89+
/**
90+
* Test add new group to custom attribute set.
91+
*
92+
* @magentoDataFixture Magento/Catalog/_files/attribute_set_based_on_default.php
93+
*
94+
* @magentoDbIsolation disabled
95+
*
96+
* @return void
97+
*/
98+
public function testUpdateAttributeSetWithNewGroup(): void
99+
{
100+
$currentAttrSet = $this->attributeSet->getAttributeSetByName('new_attribute_set');
101+
$this->assertNotNull($currentAttrSet);
102+
$attrSetId = (int)$currentAttrSet->getAttributeSetId();
103+
$currentAttrGroups = $this->getAttributeSetGroupCollection($attrSetId)->getItems();
104+
$newGroupName = 'Test attribute group name';
105+
$newGroupSortOrder = 11;
106+
$postData = $this->prepareDataToRequest($currentAttrSet);
107+
$postData['groups'][] = [
108+
null,
109+
$newGroupName,
110+
$newGroupSortOrder,
111+
];
112+
$this->performRequest($attrSetId, $postData);
113+
$updatedAttrGroups = $this->getAttributeSetGroupCollection($attrSetId)->getItems();
114+
$diffGroups = array_diff_key($updatedAttrGroups, $currentAttrGroups);
115+
$this->assertCount(1, $diffGroups);
116+
/** @var AttributeGroupInterface $newGroup */
117+
$newGroup = reset($diffGroups);
118+
$this->assertEquals($newGroupName, $newGroup->getAttributeGroupName());
119+
$this->assertEquals($newGroupSortOrder, $newGroup->getSortOrder());
120+
}
121+
122+
/**
123+
* Test delete custom group from custom attribute set.
124+
*
125+
* @magentoDataFixture Magento/Catalog/_files/attribute_set_based_on_default_with_custom_group.php
126+
*
127+
* @magentoDbIsolation disabled
128+
*
129+
* @return void
130+
*/
131+
public function testDeleteCustomGroupFromCustomAttributeSet(): void
132+
{
133+
$testGroupName = 'Test attribute group name';
134+
$currentAttrSet = $this->attributeSet->getAttributeSetByName('new_attribute_set');
135+
$this->assertNotNull($currentAttrSet);
136+
$attrSetId = (int)$currentAttrSet->getAttributeSetId();
137+
$currentAttrGroups = $this->getAttributeSetGroupCollection($attrSetId)->getItems();
138+
$customGroup = null;
139+
/** @var AttributeGroupInterface $attrGroup */
140+
foreach ($currentAttrGroups as $attrGroup) {
141+
if ($attrGroup->getAttributeGroupName() === $testGroupName) {
142+
$customGroup = $attrGroup;
143+
break;
144+
}
145+
}
146+
$this->assertNotNull($customGroup);
147+
$postData = $this->prepareDataToRequest($currentAttrSet);
148+
$postData['removeGroups'] = [
149+
$customGroup->getAttributeGroupId()
150+
];
151+
$this->performRequest($attrSetId, $postData);
152+
$updatedAttrGroups = $this->getAttributeSetGroupCollection($attrSetId)->getItems();
153+
$diffGroups = array_diff_key($currentAttrGroups, $updatedAttrGroups);
154+
$this->assertCount(1, $diffGroups);
155+
/** @var AttributeGroupInterface $deletedGroup */
156+
$deletedGroup = reset($diffGroups);
157+
$this->assertEquals($testGroupName, $deletedGroup->getAttributeGroupName());
158+
}
159+
160+
/**
161+
* Process attribute set save request.
162+
*
163+
* @param int $attributeSetId
164+
* @param array $postData
165+
* @return void
166+
*/
167+
private function performRequest(int $attributeSetId, array $postData = []): void
168+
{
169+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
170+
$this->getRequest()->setPostValue(
171+
'data',
172+
$this->json->serialize($postData)
173+
);
174+
$this->dispatch('backend/catalog/product_set/save/id/' . $attributeSetId);
175+
}
176+
177+
/**
178+
* Prepare default data to request from attribute set.
179+
*
180+
* @param AttributeSetInterface $attributeSet
181+
* @return array
182+
*/
183+
private function prepareDataToRequest(AttributeSetInterface $attributeSet): array
184+
{
185+
$result = [
186+
'attribute_set_name' => $attributeSet->getAttributeSetName(),
187+
'removeGroups' => [],
188+
'not_attributes' => [],
189+
];
190+
$groups = $attributes = [];
191+
/** @var AttributeGroupInterface $group */
192+
foreach ($this->getAttributeSetGroupCollection((int)$attributeSet->getAttributeSetId()) as $group) {
193+
$groups[] = [
194+
$group->getAttributeGroupId(),
195+
$group->getAttributeGroupName(),
196+
$group->getSortOrder(),
197+
];
198+
}
199+
$attributeSetAttributes = $this->attributeManagement->getAttributes(
200+
ProductAttributeInterface::ENTITY_TYPE_CODE,
201+
$attributeSet->getAttributeSetId()
202+
);
203+
foreach ($attributeSetAttributes as $attribute) {
204+
$attributes[] = [
205+
$attribute->getAttributeId(),
206+
$attribute->getAttributeGroupId(),
207+
$attribute->getSortOrder(),
208+
];
209+
}
210+
$result['groups'] = $groups;
211+
$result['attributes'] = $attributes;
212+
213+
return $result;
214+
}
215+
216+
/**
217+
* Build attribute set groups collection by attribute set id.
218+
*
219+
* @param int $attributeSetId
220+
* @return Collection
221+
*/
222+
private function getAttributeSetGroupCollection(int $attributeSetId): Collection
223+
{
224+
$groupCollection = $this->attributeGroupCollectionFactory->create();
225+
$groupCollection->setAttributeSetFilter($attributeSetId);
226+
227+
return $groupCollection;
228+
}
229+
}

0 commit comments

Comments
 (0)