Skip to content

Commit 9310b48

Browse files
committed
MC-20689: Admin: Create new attribute set
1 parent 99f206f commit 9310b48

File tree

1 file changed

+160
-37
lines changed
  • dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/Set

1 file changed

+160
-37
lines changed

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

Lines changed: 160 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,29 @@
77

88
namespace Magento\Catalog\Controller\Adminhtml\Product\Set;
99

10+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
use Magento\Catalog\Model\Product\Attribute\Repository;
14+
use Magento\Developer\Model\Logger\Handler\Syslog;
15+
use Magento\Eav\Api\AttributeManagementInterface;
1016
use Magento\Eav\Api\AttributeSetRepositoryInterface;
1117
use Magento\Eav\Api\Data\AttributeSetInterface;
18+
use Magento\Framework\Api\DataObjectHelper;
1219
use Magento\Framework\Api\SearchCriteriaBuilder;
13-
use Magento\TestFramework\Helper\Bootstrap;
1420
use Magento\Framework\App\Request\Http as HttpRequest;
15-
use Magento\Eav\Api\AttributeManagementInterface;
16-
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
17-
use Magento\Framework\Api\DataObjectHelper;
18-
use Magento\Catalog\Api\ProductRepositoryInterface;
19-
use Magento\Catalog\Api\Data\ProductInterface;
20-
use Magento\Developer\Model\Logger\Handler\Syslog;
21+
use Magento\Framework\Logger\Handler\System;
2122
use Magento\Framework\Logger\Monolog;
22-
use Magento\Catalog\Model\Product\Attribute\Repository;
23+
use Magento\Framework\Message\MessageInterface;
24+
use Magento\TestFramework\Helper\Bootstrap;
25+
use Magento\TestFramework\TestCase\AbstractBackendController;
2326

2427
/**
25-
* Test save attribute set
28+
* Testing for saving an existing or creating a new attribute set.
2629
*
2730
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2831
*/
29-
class SaveTest extends \Magento\TestFramework\TestCase\AbstractBackendController
32+
class SaveTest extends AbstractBackendController
3033
{
3134
/**
3235
* @var string
@@ -63,6 +66,11 @@ class SaveTest extends \Magento\TestFramework\TestCase\AbstractBackendController
6366
*/
6467
private $attributeRepository;
6568

69+
/**
70+
* @var AttributeSetRepositoryInterface
71+
*/
72+
private $attributeSetRepository;
73+
6674
/**
6775
* @inheritDoc
6876
*/
@@ -80,11 +88,11 @@ public function setUp()
8088
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
8189
$this->attributeRepository = $this->_objectManager->get(Repository::class);
8290
$this->dataObjectHelper = $this->_objectManager->get(DataObjectHelper::class);
91+
$this->attributeSetRepository = $this->_objectManager->get(AttributeSetRepositoryInterface::class);
8392
}
8493

8594
/**
8695
* @inheritdoc
87-
* @throws \Magento\Framework\Exception\FileSystemException
8896
*/
8997
public function tearDown()
9098
{
@@ -93,9 +101,65 @@ public function tearDown()
93101
}
94102

95103
/**
104+
* Test that new attribute set based on default attribute set will be successfully created.
105+
*
106+
* @magentoDbIsolation enabled
107+
*
108+
* @return void
109+
*/
110+
public function testCreateNewAttributeSetBasedOnDefaultAttributeSet(): void
111+
{
112+
$this->createAttributeSetBySkeletonAndAssert('Attribute set name for test', 4);
113+
}
114+
115+
/**
116+
* Test that new attribute set based on custom attribute set will be successfully created.
117+
*
118+
* @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
119+
*
120+
* @magentoDbIsolation enabled
121+
*
122+
* @return void
123+
*/
124+
public function testCreateNewAttributeSetBasedOnCustomAttributeSet(): void
125+
{
126+
$existCustomAttributeSet = $this->getAttributeSetByName('attribute_set_test');
127+
$this->createAttributeSetBySkeletonAndAssert(
128+
'Attribute set name for test',
129+
(int)$existCustomAttributeSet->getAttributeSetId()
130+
);
131+
}
132+
133+
/**
134+
* Test that new attribute set based on custom attribute set will be successfully created.
135+
*
136+
* @magentoDbIsolation enabled
137+
*
138+
* @return void
139+
*/
140+
public function testGotErrorDuringCreateAttributeSetWithoutName(): void
141+
{
142+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
143+
$this->getRequest()->setPostValue(
144+
[
145+
'gotoEdit' => '1',
146+
'skeleton_set' => 4,
147+
]
148+
);
149+
$this->dispatch('backend/catalog/product_set/save/');
150+
$this->assertSessionMessages(
151+
$this->contains('The attribute set name is empty. Enter the name and try again.'),
152+
MessageInterface::TYPE_ERROR
153+
);
154+
}
155+
156+
/**
157+
* Test that exception throws during save attribute set name process if name of attribute set already exists.
158+
*
96159
* @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
160+
* @return void
97161
*/
98-
public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated()
162+
public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated(): void
99163
{
100164
$attributeSet = $this->getAttributeSetByName('attribute_set_test');
101165
$this->assertNotEmpty($attributeSet, 'Attribute set with name "attribute_set_test" is missed');
@@ -128,35 +192,16 @@ public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated()
128192
);
129193
}
130194

131-
/**
132-
* @param string $attributeSetName
133-
* @return AttributeSetInterface|null
134-
*/
135-
protected function getAttributeSetByName($attributeSetName)
136-
{
137-
$objectManager = Bootstrap::getObjectManager();
138-
139-
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
140-
$searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
141-
$searchCriteriaBuilder->addFilter('attribute_set_name', $attributeSetName);
142-
143-
/** @var AttributeSetRepositoryInterface $attributeSetRepository */
144-
$attributeSetRepository = $objectManager->get(AttributeSetRepositoryInterface::class);
145-
$result = $attributeSetRepository->getList($searchCriteriaBuilder->create());
146-
147-
$items = $result->getItems();
148-
return $result->getTotalCount() ? array_pop($items) : null;
149-
}
150-
151195
/**
152196
* Test behavior when attribute set was changed to a new set
153-
* with deleted attribute from the previous set
197+
* with deleted attribute from the previous set.
154198
*
155199
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
156200
* @magentoDataFixture Magento/Catalog/_files/attribute_set_based_on_default.php
157201
* @magentoDbIsolation disabled
202+
* @return void
158203
*/
159-
public function testRemoveAttributeFromAttributeSet()
204+
public function testRemoveAttributeFromAttributeSet(): void
160205
{
161206
$message = 'Attempt to load value of nonexistent EAV attribute';
162207
$this->removeSyslog();
@@ -178,15 +223,15 @@ public function testRemoveAttributeFromAttributeSet()
178223
}
179224

180225
/**
181-
* Retrieve system.log file path
226+
* Retrieve system.log file path.
182227
*
183228
* @return string
184229
*/
185230
private function getSyslogPath(): string
186231
{
187232
if (!$this->systemLogPath) {
188233
foreach ($this->logger->getHandlers() as $handler) {
189-
if ($handler instanceof \Magento\Framework\Logger\Handler\System) {
234+
if ($handler instanceof System) {
190235
$this->systemLogPath = $handler->getUrl();
191236
}
192237
}
@@ -200,11 +245,89 @@ private function getSyslogPath(): string
200245
*
201246
* @return void
202247
*/
203-
private function removeSyslog()
248+
private function removeSyslog(): void
204249
{
205250
$this->syslogHandler->close();
206251
if (file_exists($this->getSyslogPath())) {
207252
unlink($this->getSyslogPath());
208253
}
209254
}
255+
256+
/**
257+
* Search and return attribute set by name.
258+
*
259+
* @param string $attributeSetName
260+
* @return AttributeSetInterface|null
261+
*/
262+
private function getAttributeSetByName(string $attributeSetName): ?AttributeSetInterface
263+
{
264+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
265+
$searchCriteriaBuilder = $this->_objectManager->get(SearchCriteriaBuilder::class);
266+
$searchCriteriaBuilder->addFilter('attribute_set_name', $attributeSetName);
267+
$result = $this->attributeSetRepository->getList($searchCriteriaBuilder->create());
268+
$items = $result->getItems();
269+
270+
return $result->getTotalCount() ? array_pop($items) : null;
271+
}
272+
273+
/**
274+
* Create attribute set by skeleton attribute set id and assert that attribute set
275+
* created successfully and attributes from skeleton attribute set and created attribute set are equals.
276+
*
277+
* @param string $attributeSetName
278+
* @param int $skeletonAttributeSetId
279+
*/
280+
private function createAttributeSetBySkeletonAndAssert(
281+
string $attributeSetName,
282+
int $skeletonAttributeSetId
283+
): void {
284+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
285+
$this->getRequest()->setPostValue(
286+
[
287+
'attribute_set_name' => $attributeSetName,
288+
'gotoEdit' => '1',
289+
'skeleton_set' => $skeletonAttributeSetId,
290+
]
291+
);
292+
$this->dispatch('backend/catalog/product_set/save/');
293+
$this->assertSessionMessages(
294+
$this->contains('You saved the attribute set.'),
295+
MessageInterface::TYPE_SUCCESS
296+
);
297+
$createdAttributeSet = $this->getAttributeSetByName($attributeSetName);
298+
$existAttributeSet = $this->attributeSetRepository->get($skeletonAttributeSetId);
299+
300+
$this->assertNotNull($createdAttributeSet);
301+
$this->assertEquals($attributeSetName, $createdAttributeSet->getAttributeSetName());
302+
303+
$this->assertAttributeSetsAttributesAreEquals($createdAttributeSet, $existAttributeSet);
304+
}
305+
306+
/**
307+
* Assert that both attribute sets contains identical attributes by attribute ids.
308+
*
309+
* @param AttributeSetInterface $createdAttributeSet
310+
* @param AttributeSetInterface $existAttributeSet
311+
*/
312+
private function assertAttributeSetsAttributesAreEquals(
313+
AttributeSetInterface $createdAttributeSet,
314+
AttributeSetInterface $existAttributeSet
315+
): void {
316+
$expectedAttributeIds = array_keys(
317+
$this->attributeManagement->getAttributes(
318+
ProductAttributeInterface::ENTITY_TYPE_CODE,
319+
$existAttributeSet->getAttributeSetId()
320+
)
321+
);
322+
$actualAttributeIds = array_keys(
323+
$this->attributeManagement->getAttributes(
324+
ProductAttributeInterface::ENTITY_TYPE_CODE,
325+
$createdAttributeSet->getAttributeSetId()
326+
)
327+
);
328+
$this->assertEquals(count($expectedAttributeIds), count($actualAttributeIds));
329+
foreach ($actualAttributeIds as $attributeId) {
330+
$this->assertTrue(in_array($attributeId, $expectedAttributeIds, true));
331+
}
332+
}
210333
}

0 commit comments

Comments
 (0)