Skip to content

Commit 94277ef

Browse files
committed
MC-20694: Admin: Delete attribute set
1 parent 5a82cc9 commit 94277ef

File tree

6 files changed

+325
-17
lines changed

6 files changed

+325
-17
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+
}

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

Lines changed: 121 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,146 @@
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

8-
use Magento\Framework\Message\MessageInterface;
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\Product;
13+
use Magento\Eav\Api\AttributeSetRepositoryInterface;
914
use Magento\Framework\App\Request\Http as HttpRequest;
15+
use Magento\Framework\Escaper;
16+
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Framework\Message\MessageInterface;
18+
use Magento\TestFramework\Eav\Model\GetAttributeSetByName;
19+
use Magento\TestFramework\TestCase\AbstractBackendController;
1020

11-
class DeleteTest extends \Magento\TestFramework\TestCase\AbstractBackendController
21+
/**
22+
* Test for attribute set deleting.
23+
*
24+
* @magentoAppArea adminhtml
25+
* @magentoDbIsolation enabled
26+
*/
27+
class DeleteTest extends AbstractBackendController
1228
{
1329
/**
30+
* @var GetAttributeSetByName
31+
*/
32+
private $getAttributeSetByName;
33+
34+
/**
35+
* @var ProductInterface|Product
36+
*/
37+
private $product;
38+
39+
/**
40+
* @var AttributeSetRepositoryInterface
41+
*/
42+
private $attributeSetRepository;
43+
44+
/**
45+
* @var Escaper
46+
*/
47+
private $escaper;
48+
49+
/**
50+
* @var ProductRepositoryInterface
51+
*/
52+
private $productRepository;
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
protected function setUp()
58+
{
59+
parent::setUp();
60+
$this->getAttributeSetByName = $this->_objectManager->get(GetAttributeSetByName::class);
61+
$this->product = $this->_objectManager->get(ProductInterface::class);
62+
$this->attributeSetRepository = $this->_objectManager->get(AttributeSetRepositoryInterface::class);
63+
$this->escaper = $this->_objectManager->get(Escaper::class);
64+
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
65+
}
66+
67+
/**
68+
* Assert that default attribute set is not deleted.
69+
*
70+
* @return void
71+
*/
72+
public function testDefaultAttributeSetIsNotDeleted(): void
73+
{
74+
$productDefaultAttrSetId = (int)$this->product->getDefaultAttributeSetId();
75+
$this->performDeleteAttributeSetRequest($productDefaultAttrSetId);
76+
$expectedSessionMessage = $this->escaper->escapeHtml((string)__('We can\'t delete this set right now.'));
77+
$this->assertSessionMessages(
78+
$this->equalTo([$expectedSessionMessage]),
79+
MessageInterface::TYPE_ERROR
80+
);
81+
try {
82+
$this->attributeSetRepository->get($productDefaultAttrSetId);
83+
} catch (NoSuchEntityException $e) {
84+
$this->fail(sprintf('Default attribute set was deleted. Message: %s', $e->getMessage()));
85+
}
86+
}
87+
88+
/**
89+
* Assert that custom attribute set deleting properly.
90+
*
1491
* @magentoDataFixture Magento/Eav/_files/empty_attribute_set.php
92+
*
93+
* @return void
1594
*/
16-
public function testDeleteById()
95+
public function testDeleteCustomAttributeSetById(): void
1796
{
18-
$attributeSet = $this->getAttributeSetByName('empty_attribute_set');
19-
$this->getRequest()->setParam('id', $attributeSet->getId())->setMethod(HttpRequest::METHOD_POST);
97+
$this->deleteAttributeSetByNameAndAssert('empty_attribute_set');
98+
}
2099

21-
$this->dispatch('backend/catalog/product_set/delete/');
100+
/**
101+
* Assert that product will be deleted if delete attribute set which the product is attached.
102+
*
103+
* @magentoDataFixture Magento/Catalog/_files/product_with_test_attribute_set.php
104+
*
105+
* @return void
106+
*/
107+
public function testProductIsDeletedAfterDeleteItsAttributeSet(): void
108+
{
109+
$this->deleteAttributeSetByNameAndAssert('new_attribute_set');
110+
$this->expectExceptionObject(
111+
new NoSuchEntityException(
112+
__('The product that was requested doesn\'t exist. Verify the product and try again.')
113+
)
114+
);
115+
$this->productRepository->get('simple');
116+
}
22117

23-
$this->assertNull($this->getAttributeSetByName('empty_attribute_set'));
118+
/**
119+
* Perform request to delete attribute set and assert that attribute set is deleted.
120+
*
121+
* @param string $attributeSetName
122+
* @return void
123+
*/
124+
private function deleteAttributeSetByNameAndAssert(string $attributeSetName): void
125+
{
126+
$attributeSet = $this->getAttributeSetByName->execute($attributeSetName);
127+
$this->performDeleteAttributeSetRequest((int)$attributeSet->getAttributeSetId());
24128
$this->assertSessionMessages(
25-
$this->equalTo(['The attribute set has been removed.']),
129+
$this->equalTo([(string)__('The attribute set has been removed.')]),
26130
MessageInterface::TYPE_SUCCESS
27131
);
28-
$this->assertRedirect($this->stringContains('catalog/product_set/index/'));
132+
$this->assertNull($this->getAttributeSetByName->execute($attributeSetName));
29133
}
30134

31135
/**
32-
* Retrieve attribute set based on given name.
136+
* Perform "catalog/product_set/delete" controller dispatch.
33137
*
34-
* @param string $attributeSetName
35-
* @return \Magento\Eav\Model\Entity\Attribute\Set|null
138+
* @param int $attributeSetId
139+
* @return void
36140
*/
37-
protected function getAttributeSetByName($attributeSetName)
141+
private function performDeleteAttributeSetRequest(int $attributeSetId): void
38142
{
39-
$attributeSet = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
40-
\Magento\Eav\Model\Entity\Attribute\Set::class
41-
)->load($attributeSetName, 'attribute_set_name');
42-
return $attributeSet->getId() === null ? null : $attributeSet;
143+
$this->getRequest()
144+
->setParam('id', $attributeSetId)
145+
->setMethod(HttpRequest::METHOD_POST);
146+
$this->dispatch('backend/catalog/product_set/delete/');
43147
}
44148
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\Eav\Api\AttributeSetRepositoryInterface;
11+
use Magento\Eav\Api\Data\AttributeGroupInterface;
12+
use Magento\Eav\Api\Data\AttributeSetInterfaceFactory;
13+
use Magento\Eav\Model\Entity\Attribute\GroupFactory;
14+
use Magento\Eav\Model\Entity\Type;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
17+
$objectManager = Bootstrap::getObjectManager();
18+
/** @var AttributeSetRepositoryInterface $attributeSetRepository */
19+
$attributeSetRepository = $objectManager->get(AttributeSetRepositoryInterface::class);
20+
/** @var AttributeSetInterfaceFactory $attributeSetFactory */
21+
$attributeSetFactory = $objectManager->get(AttributeSetInterfaceFactory::class);
22+
/** @var Type $entityType */
23+
$entityType = $objectManager->create(Type::class)->loadByCode(ProductAttributeInterface::ENTITY_TYPE_CODE);
24+
/** @var ProductInterface $product */
25+
$product = $objectManager->create(ProductInterface::class);
26+
$attributeSet = $attributeSetFactory->create(
27+
[
28+
'data' => [
29+
'id' => null,
30+
'attribute_set_name' => 'new_attribute_set',
31+
'entity_type_id' => $entityType->getId(),
32+
'sort_order' => 300,
33+
],
34+
]
35+
);
36+
$attributeSet->isObjectNew(true);
37+
$attributeSet->setHasDataChanges(true);
38+
$attributeSet->validate();
39+
$attributeSetRepository->save($attributeSet);
40+
$attributeSet->initFromSkeleton($product->getDefaultAttributeSetid());
41+
/** @var AttributeGroupInterface $newGroup */
42+
$newGroup = $objectManager->get(GroupFactory::class)->create();
43+
$newGroup->setId(null)
44+
->setAttributeGroupName('Test attribute group name')
45+
->setAttributeSetId($attributeSet->getAttributeSetId())
46+
->setSortOrder(11)
47+
->setAttributes([]);
48+
/** @var AttributeGroupInterface[] $groups */
49+
$groups = $attributeSet->getGroups();
50+
array_push($groups, $newGroup);
51+
$attributeSet->setGroups($groups);
52+
$attributeSetRepository->save($attributeSet);
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+
declare(strict_types=1);
7+
8+
use Magento\Eav\Api\AttributeSetRepositoryInterface;
9+
use Magento\TestFramework\Eav\Model\GetAttributeSetByName;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
/** @var GetAttributeSetByName $getAttributeSetByName */
14+
$getAttributeSetByName = $objectManager->get(GetAttributeSetByName::class);
15+
/** @var AttributeSetRepositoryInterface $attributeSetRepository */
16+
$attributeSetRepository = $objectManager->get(AttributeSetRepositoryInterface::class);
17+
$attributeSet = $getAttributeSetByName->execute('new_attribute_set');
18+
19+
if ($attributeSet) {
20+
$attributeSetRepository->delete($attributeSet);
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
require __DIR__ . '/attribute_set_based_on_default_with_custom_group.php';
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
12+
use Magento\Catalog\Model\Product\Visibility;
13+
use Magento\Catalog\Model\ProductFactory;
14+
use Magento\Store\Model\Store;
15+
use Magento\TestFramework\Eav\Model\GetAttributeSetByName;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
18+
$objectManager = Bootstrap::getObjectManager();
19+
/** @var ProductRepositoryInterface $productRepository */
20+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
21+
/** @var ProductFactory $productFactory */
22+
$productFactory = $objectManager->get(ProductFactory::class);
23+
/** @var GetAttributeSetByName $attributeSet */
24+
$attributeSet = $objectManager->get(GetAttributeSetByName::class);
25+
$customAttributeSet = $attributeSet->execute('new_attribute_set');
26+
$product = $productFactory->create();
27+
$product
28+
->setTypeId('simple')
29+
->setAttributeSetId($customAttributeSet->getAttributeSetId())
30+
->setWebsiteIds([1])
31+
->setStoreId(Store::DEFAULT_STORE_ID)
32+
->setName('Simple Product')
33+
->setSku('simple')
34+
->setPrice(10)
35+
->setMetaTitle('meta title')
36+
->setMetaKeyword('meta keyword')
37+
->setMetaDescription('meta description')
38+
->setVisibility(Visibility::VISIBILITY_BOTH)
39+
->setStatus(Status::STATUS_ENABLED)
40+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 22, 'is_in_stock' => 1])
41+
->setQty(22);
42+
$productRepository->save($product);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\CatalogInventory\Model\StockRegistryStorage;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\Registry;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
14+
$objectManager = Bootstrap::getObjectManager();
15+
/** @var Registry $registry */
16+
$registry = $objectManager->get(Registry::class);
17+
$registry->unregister('isSecureArea');
18+
$registry->register('isSecureArea', true);
19+
/** @var ProductRepositoryInterface $productRepository */
20+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
21+
/** @var StockRegistryStorage $stockRegistryStorage */
22+
$stockRegistryStorage = $objectManager->get(StockRegistryStorage::class);
23+
try {
24+
$product = $productRepository->get('simple');
25+
$productRepository->delete($product);
26+
} catch (NoSuchEntityException $e) {
27+
//Product already deleted.
28+
}
29+
$stockRegistryStorage->clean();
30+
$registry->unregister('isSecureArea');
31+
$registry->register('isSecureArea', false);
32+
33+
require __DIR__ . '/attribute_set_based_on_default_with_custom_group_rollback.php';

0 commit comments

Comments
 (0)