Skip to content

Commit 6cde8c5

Browse files
MC-18821: Increase test coverage for Catalog functional area
- Integration test for MC-11151
1 parent cc23756 commit 6cde8c5

File tree

3 files changed

+220
-11
lines changed

3 files changed

+220
-11
lines changed

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

Lines changed: 177 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,95 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Controller\Adminhtml\Product\Set;
79

810
use Magento\Eav\Api\AttributeSetRepositoryInterface;
911
use Magento\Eav\Api\Data\AttributeSetInterface;
1012
use Magento\Framework\Api\SearchCriteriaBuilder;
1113
use Magento\TestFramework\Helper\Bootstrap;
1214
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\Monolog;
22+
use Magento\Catalog\Model\Product\Attribute\Repository;
1323

24+
/**
25+
* Test save attribute set
26+
*
27+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28+
*/
1429
class SaveTest extends \Magento\TestFramework\TestCase\AbstractBackendController
1530
{
31+
/**
32+
* @var string
33+
*/
34+
private $systemLogPath = '';
35+
36+
/**
37+
* @var Monolog
38+
*/
39+
private $logger;
40+
41+
/**
42+
* @var Syslog
43+
*/
44+
private $syslogHandler;
45+
46+
/**
47+
* @var AttributeManagementInterface
48+
*/
49+
private $attributeManagement;
50+
51+
/**
52+
* @var DataObjectHelper
53+
*/
54+
private $dataObjectHelper;
55+
56+
/**
57+
* @var ProductRepositoryInterface
58+
*/
59+
private $productRepository;
60+
61+
/**
62+
* @var Repository
63+
*/
64+
private $attributeRepository;
65+
66+
/**
67+
* @inheritDoc
68+
*/
69+
public function setUp()
70+
{
71+
parent::setUp();
72+
$this->logger = $this->_objectManager->get(Monolog::class);
73+
$this->syslogHandler = $this->_objectManager->create(
74+
Syslog::class,
75+
[
76+
'filePath' => Bootstrap::getInstance()->getAppTempDir(),
77+
]
78+
);
79+
$this->attributeManagement = $this->_objectManager->get(AttributeManagementInterface::class);
80+
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
81+
$this->attributeRepository = $this->_objectManager->get(Repository::class);
82+
$this->dataObjectHelper = $this->_objectManager->get(DataObjectHelper::class);
83+
}
84+
85+
/**
86+
* @inheritdoc
87+
* @throws \Magento\Framework\Exception\FileSystemException
88+
*/
89+
public function tearDown()
90+
{
91+
$this->attributeRepository->get('country_of_manufacture')->setIsUserDefined(false);
92+
parent::tearDown();
93+
}
94+
1695
/**
1796
* @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
1897
*/
@@ -22,17 +101,22 @@ public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated()
22101
$this->assertNotEmpty($attributeSet, 'Attribute set with name "attribute_set_test" is missed');
23102

24103
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
25-
$this->getRequest()->setPostValue('data', json_encode([
26-
'attribute_set_name' => 'attribute_set_test',
27-
'groups' => [
28-
['ynode-418', 'attribute-group-name', 1],
29-
],
30-
'attributes' => [
31-
['9999', 'ynode-418', 1, null]
32-
],
33-
'not_attributes' => [],
34-
'removeGroups' => [],
35-
]));
104+
$this->getRequest()->setPostValue(
105+
'data',
106+
json_encode(
107+
[
108+
'attribute_set_name' => 'attribute_set_test',
109+
'groups' => [
110+
['ynode-418', 'attribute-group-name', 1],
111+
],
112+
'attributes' => [
113+
['9999', 'ynode-418', 1, null]
114+
],
115+
'not_attributes' => [],
116+
'removeGroups' => [],
117+
]
118+
)
119+
);
36120
$this->dispatch('backend/catalog/product_set/save/id/' . $attributeSet->getAttributeSetId());
37121

38122
$jsonResponse = json_decode($this->getResponse()->getBody());
@@ -63,4 +147,86 @@ protected function getAttributeSetByName($attributeSetName)
63147
$items = $result->getItems();
64148
return $result->getTotalCount() ? array_pop($items) : null;
65149
}
150+
151+
/**
152+
* Test behavior when attribute set was changed to a new set
153+
* with deleted attribute from the previous set
154+
*
155+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
156+
* @magentoDataFixture Magento/Catalog/_files/attribute_set_based_on_default.php
157+
* @magentoDbIsolation disabled
158+
*/
159+
public function testRemoveAttributeFromAttributeSet()
160+
{
161+
$message = 'Attempt to load value of nonexistent EAV attribute';
162+
$this->removeSyslog();
163+
$attributeSet = $this->getAttributeSetByName('new_attribute_set');
164+
$product = $this->productRepository->get('simple');
165+
$this->attributeRepository->get('country_of_manufacture')->setIsUserDefined(true);
166+
$this->attributeManagement->unassign($attributeSet->getId(), 'country_of_manufacture');
167+
$productData = [
168+
'country_of_manufacture' => 'Angola'
169+
];
170+
$this->dataObjectHelper->populateWithArray($product, $productData, ProductInterface::class);
171+
$this->productRepository->save($product);
172+
$product->setAttributeSetId($attributeSet->getId());
173+
$product = $this->productRepository->save($product);
174+
$this->dispatch('backend/catalog/product/edit/id/' . $product->getEntityId());
175+
$this->assertNotContains($message, $this->getSyslogContent());
176+
}
177+
178+
/**
179+
* Retrieve system.log file path
180+
*
181+
* @return string
182+
*/
183+
private function getSyslogPath()
184+
{
185+
if (!$this->systemLogPath) {
186+
foreach ($this->logger->getHandlers() as $handler) {
187+
if ($handler instanceof \Magento\Framework\Logger\Handler\System) {
188+
$this->systemLogPath = $handler->getUrl();
189+
}
190+
}
191+
}
192+
193+
return $this->systemLogPath;
194+
}
195+
196+
/**
197+
* Remove system.log file
198+
*
199+
* @return void
200+
*/
201+
private function removeSyslog()
202+
{
203+
$this->detachLogger();
204+
if (file_exists($this->getSyslogPath())) {
205+
unlink($this->getSyslogPath());
206+
}
207+
}
208+
209+
/**
210+
* Detach system log handler.
211+
*
212+
* @return void
213+
*/
214+
private function detachLogger()
215+
{
216+
$this->syslogHandler->close();
217+
}
218+
219+
/**
220+
* Retrieve content of system.log file
221+
*
222+
* @return bool|string
223+
*/
224+
private function getSyslogContent()
225+
{
226+
if (!file_exists($this->getSyslogPath())) {
227+
return '';
228+
}
229+
230+
return file_get_contents($this->getSyslogPath());
231+
}
66232
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
9+
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
10+
$entityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)->loadByCode('catalog_product');
11+
$defaultSetId = $objectManager->create(\Magento\Catalog\Model\Product::class)->getDefaultAttributeSetid();
12+
$data = [
13+
'attribute_set_name' => 'new_attribute_set',
14+
'entity_type_id' => $entityType->getId(),
15+
'sort_order' => 300,
16+
];
17+
18+
$attributeSet->setData($data);
19+
$attributeSet->validate();
20+
$attributeSet->save();
21+
$attributeSet->initFromSkeleton($defaultSetId);
22+
$attributeSet->save();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
9+
$entityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)->loadByCode('catalog_product');
10+
11+
$attributeSetCollection = $objectManager->create(
12+
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection::class
13+
);
14+
$attributeSetCollection->addFilter('attribute_set_name', 'new_attribute_set');
15+
$attributeSetCollection->addFilter('entity_type_id', $entityType->getId());
16+
$attributeSetCollection->setOrder('attribute_set_id');
17+
$attributeSetCollection->setPageSize(1);
18+
$attributeSetCollection->load();
19+
20+
$attributeSet = $attributeSetCollection->fetchItem();
21+
$attributeSet->delete();

0 commit comments

Comments
 (0)