Skip to content

Commit c6242e7

Browse files
committed
Merge branch 'MC-20689' into 2.3-develop-com-pr2
2 parents b47aa4b + 2ed496a commit c6242e7

File tree

1 file changed

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

1 file changed

+196
-42
lines changed

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

Lines changed: 196 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,31 @@
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\Eav\Model\Config;
19+
use Magento\Framework\Api\DataObjectHelper;
1220
use Magento\Framework\Api\SearchCriteriaBuilder;
13-
use Magento\TestFramework\Helper\Bootstrap;
1421
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;
22+
use Magento\Framework\Logger\Handler\System;
2123
use Magento\Framework\Logger\Monolog;
22-
use Magento\Catalog\Model\Product\Attribute\Repository;
24+
use Magento\Framework\Message\MessageInterface;
25+
use Magento\Framework\Serialize\Serializer\Json;
26+
use Magento\TestFramework\Helper\Bootstrap;
27+
use Magento\TestFramework\TestCase\AbstractBackendController;
2328

2429
/**
25-
* Test save attribute set
30+
* Testing for saving an existing or creating a new attribute set.
2631
*
2732
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2833
*/
29-
class SaveTest extends \Magento\TestFramework\TestCase\AbstractBackendController
34+
class SaveTest extends AbstractBackendController
3035
{
3136
/**
3237
* @var string
@@ -63,6 +68,21 @@ class SaveTest extends \Magento\TestFramework\TestCase\AbstractBackendController
6368
*/
6469
private $attributeRepository;
6570

71+
/**
72+
* @var AttributeSetRepositoryInterface
73+
*/
74+
private $attributeSetRepository;
75+
76+
/**
77+
* @var Config
78+
*/
79+
private $eavConfig;
80+
81+
/**
82+
* @var Json
83+
*/
84+
private $json;
85+
6686
/**
6787
* @inheritDoc
6888
*/
@@ -80,11 +100,13 @@ public function setUp()
80100
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
81101
$this->attributeRepository = $this->_objectManager->get(Repository::class);
82102
$this->dataObjectHelper = $this->_objectManager->get(DataObjectHelper::class);
103+
$this->attributeSetRepository = $this->_objectManager->get(AttributeSetRepositoryInterface::class);
104+
$this->eavConfig = $this->_objectManager->get(Config::class);
105+
$this->json = $this->_objectManager->get(Json::class);
83106
}
84107

85108
/**
86109
* @inheritdoc
87-
* @throws \Magento\Framework\Exception\FileSystemException
88110
*/
89111
public function tearDown()
90112
{
@@ -93,17 +115,76 @@ public function tearDown()
93115
}
94116

95117
/**
118+
* Test that new attribute set based on default attribute set will be successfully created.
119+
*
120+
* @magentoDbIsolation enabled
121+
*
122+
* @return void
123+
*/
124+
public function testCreateNewAttributeSetBasedOnDefaultAttributeSet(): void
125+
{
126+
$this->createAttributeSetBySkeletonAndAssert(
127+
'Attribute set name for test',
128+
$this->getCatalogProductDefaultAttributeSetId()
129+
);
130+
}
131+
132+
/**
133+
* Test that new attribute set based on custom attribute set will be successfully created.
134+
*
96135
* @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
136+
*
137+
* @magentoDbIsolation enabled
138+
*
139+
* @return void
140+
*/
141+
public function testCreateNewAttributeSetBasedOnCustomAttributeSet(): void
142+
{
143+
$existCustomAttributeSet = $this->getAttributeSetByName('attribute_set_test');
144+
$this->createAttributeSetBySkeletonAndAssert(
145+
'Attribute set name for test',
146+
(int)$existCustomAttributeSet->getAttributeSetId()
147+
);
148+
}
149+
150+
/**
151+
* Test that new attribute set based on custom attribute set will be successfully created.
152+
*
153+
* @magentoDbIsolation enabled
154+
*
155+
* @return void
156+
*/
157+
public function testGotErrorDuringCreateAttributeSetWithoutName(): void
158+
{
159+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
160+
$this->getRequest()->setPostValue(
161+
[
162+
'gotoEdit' => '1',
163+
'skeleton_set' => $this->getCatalogProductDefaultAttributeSetId(),
164+
]
165+
);
166+
$this->dispatch('backend/catalog/product_set/save/');
167+
$this->assertSessionMessages(
168+
$this->equalTo([(string)__('The attribute set name is empty. Enter the name and try again.')]),
169+
MessageInterface::TYPE_ERROR
170+
);
171+
}
172+
173+
/**
174+
* Test that exception throws during save attribute set name process if name of attribute set already exists.
175+
*
176+
* @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
177+
* @return void
97178
*/
98-
public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated()
179+
public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated(): void
99180
{
100181
$attributeSet = $this->getAttributeSetByName('attribute_set_test');
101182
$this->assertNotEmpty($attributeSet, 'Attribute set with name "attribute_set_test" is missed');
102183

103184
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
104185
$this->getRequest()->setPostValue(
105186
'data',
106-
json_encode(
187+
$this->json->serialize(
107188
[
108189
'attribute_set_name' => 'attribute_set_test',
109190
'groups' => [
@@ -119,44 +200,25 @@ public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated()
119200
);
120201
$this->dispatch('backend/catalog/product_set/save/id/' . $attributeSet->getAttributeSetId());
121202

122-
$jsonResponse = json_decode($this->getResponse()->getBody());
203+
$jsonResponse = $this->json->unserialize($this->getResponse()->getBody());
123204
$this->assertNotNull($jsonResponse);
124-
$this->assertEquals(1, $jsonResponse->error);
205+
$this->assertEquals(1, $jsonResponse['error']);
125206
$this->assertContains(
126-
'Attribute group with same code already exist. Please rename "attribute-group-name" group',
127-
$jsonResponse->message
207+
(string)__('Attribute group with same code already exist. Please rename "attribute-group-name" group'),
208+
$jsonResponse['message']
128209
);
129210
}
130211

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-
151212
/**
152213
* Test behavior when attribute set was changed to a new set
153-
* with deleted attribute from the previous set
214+
* with deleted attribute from the previous set.
154215
*
155216
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
156217
* @magentoDataFixture Magento/Catalog/_files/attribute_set_based_on_default.php
157218
* @magentoDbIsolation disabled
219+
* @return void
158220
*/
159-
public function testRemoveAttributeFromAttributeSet()
221+
public function testRemoveAttributeFromAttributeSet(): void
160222
{
161223
$message = 'Attempt to load value of nonexistent EAV attribute';
162224
$this->removeSyslog();
@@ -178,15 +240,15 @@ public function testRemoveAttributeFromAttributeSet()
178240
}
179241

180242
/**
181-
* Retrieve system.log file path
243+
* Retrieve system.log file path.
182244
*
183245
* @return string
184246
*/
185247
private function getSyslogPath(): string
186248
{
187249
if (!$this->systemLogPath) {
188250
foreach ($this->logger->getHandlers() as $handler) {
189-
if ($handler instanceof \Magento\Framework\Logger\Handler\System) {
251+
if ($handler instanceof System) {
190252
$this->systemLogPath = $handler->getUrl();
191253
}
192254
}
@@ -200,11 +262,103 @@ private function getSyslogPath(): string
200262
*
201263
* @return void
202264
*/
203-
private function removeSyslog()
265+
private function removeSyslog(): void
204266
{
205267
$this->syslogHandler->close();
206268
if (file_exists($this->getSyslogPath())) {
207269
unlink($this->getSyslogPath());
208270
}
209271
}
272+
273+
/**
274+
* Search and return attribute set by name.
275+
*
276+
* @param string $attributeSetName
277+
* @return AttributeSetInterface|null
278+
*/
279+
private function getAttributeSetByName(string $attributeSetName): ?AttributeSetInterface
280+
{
281+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
282+
$searchCriteriaBuilder = $this->_objectManager->get(SearchCriteriaBuilder::class);
283+
$searchCriteriaBuilder->addFilter('attribute_set_name', $attributeSetName);
284+
$result = $this->attributeSetRepository->getList($searchCriteriaBuilder->create());
285+
286+
$items = $result->getItems();
287+
288+
return array_pop($items);
289+
}
290+
291+
/**
292+
* Create attribute set by skeleton attribute set id and assert that attribute set
293+
* created successfully and attributes from skeleton attribute set and created attribute set are equals.
294+
*
295+
* @param string $attributeSetName
296+
* @param int $skeletonAttributeSetId
297+
* @return void
298+
*/
299+
private function createAttributeSetBySkeletonAndAssert(
300+
string $attributeSetName,
301+
int $skeletonAttributeSetId
302+
): void {
303+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
304+
$this->getRequest()->setPostValue(
305+
[
306+
'attribute_set_name' => $attributeSetName,
307+
'gotoEdit' => '1',
308+
'skeleton_set' => $skeletonAttributeSetId,
309+
]
310+
);
311+
$this->dispatch('backend/catalog/product_set/save/');
312+
$this->assertSessionMessages(
313+
$this->equalTo([(string)__('You saved the attribute set.')]),
314+
MessageInterface::TYPE_SUCCESS
315+
);
316+
$createdAttributeSet = $this->getAttributeSetByName($attributeSetName);
317+
$existAttributeSet = $this->attributeSetRepository->get($skeletonAttributeSetId);
318+
319+
$this->assertNotNull($createdAttributeSet);
320+
$this->assertEquals($attributeSetName, $createdAttributeSet->getAttributeSetName());
321+
322+
$this->assertAttributeSetsAttributesAreEquals($createdAttributeSet, $existAttributeSet);
323+
}
324+
325+
/**
326+
* Assert that both attribute sets contains identical attributes by attribute ids.
327+
*
328+
* @param AttributeSetInterface $createdAttributeSet
329+
* @param AttributeSetInterface $existAttributeSet
330+
* @return void
331+
*/
332+
private function assertAttributeSetsAttributesAreEquals(
333+
AttributeSetInterface $createdAttributeSet,
334+
AttributeSetInterface $existAttributeSet
335+
): void {
336+
$expectedAttributeIds = array_keys(
337+
$this->attributeManagement->getAttributes(
338+
ProductAttributeInterface::ENTITY_TYPE_CODE,
339+
$existAttributeSet->getAttributeSetId()
340+
)
341+
);
342+
sort($expectedAttributeIds);
343+
$actualAttributeIds = array_keys(
344+
$this->attributeManagement->getAttributes(
345+
ProductAttributeInterface::ENTITY_TYPE_CODE,
346+
$createdAttributeSet->getAttributeSetId()
347+
)
348+
);
349+
sort($actualAttributeIds);
350+
$this->assertSame($expectedAttributeIds, $actualAttributeIds);
351+
}
352+
353+
/**
354+
* Retrieve default catalog product attribute set ID.
355+
*
356+
* @return int
357+
*/
358+
private function getCatalogProductDefaultAttributeSetId(): int
359+
{
360+
return (int)$this->eavConfig
361+
->getEntityType(ProductAttributeInterface::ENTITY_TYPE_CODE)
362+
->getDefaultAttributeSetId();
363+
}
210364
}

0 commit comments

Comments
 (0)