Skip to content

Commit ff1eb2a

Browse files
committed
Merge remote-tracking branch 'origin/MC-20691' into 2.3-develop-com-pr3
2 parents 9dbdffd + c1bd70e commit ff1eb2a

File tree

7 files changed

+562
-0
lines changed

7 files changed

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

0 commit comments

Comments
 (0)