Skip to content

Commit d1132c1

Browse files
committed
Merge remote-tracking branch 'origin/MC-24931' into 2.4-develop-com-pr2
2 parents d6bd516 + bc6ec62 commit d1132c1

File tree

7 files changed

+467
-0
lines changed

7 files changed

+467
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\Attribute\Delete;
9+
10+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
11+
use Magento\Framework\App\Request\Http;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Framework\Message\MessageInterface;
14+
use Magento\TestFramework\TestCase\AbstractBackendController;
15+
16+
/**
17+
* Abstract delete attribute test using catalog/product_attribute/delete controller action.
18+
*/
19+
abstract class AbstractDeleteAttributeControllerTest extends AbstractBackendController
20+
{
21+
/**
22+
* @var string
23+
*/
24+
protected $uri = 'backend/catalog/product_attribute/delete/attribute_id/%s';
25+
26+
/**
27+
* @var ProductAttributeRepositoryInterface
28+
*/
29+
private $productAttributeRepository;
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
protected function setUp()
35+
{
36+
parent::setUp();
37+
$this->productAttributeRepository = $this->_objectManager->get(ProductAttributeRepositoryInterface::class);
38+
}
39+
40+
/**
41+
* Delete attribute via controller action.
42+
*
43+
* @param string $attributeCode
44+
* @return void
45+
*/
46+
protected function dispatchDeleteAttribute(string $attributeCode): void
47+
{
48+
$attribute = $this->productAttributeRepository->get($attributeCode);
49+
$this->getRequest()->setMethod(Http::METHOD_POST);
50+
$this->dispatch(sprintf($this->uri, $attribute->getAttributeId()));
51+
$this->assertSessionMessages(
52+
$this->equalTo([(string)__('You deleted the product attribute.')]),
53+
MessageInterface::TYPE_SUCCESS
54+
);
55+
}
56+
57+
/**
58+
* Assert that attribute is deleted from DB.
59+
*
60+
* @param string $attributeCode
61+
* @return void
62+
*/
63+
protected function assertAttributeIsDeleted(string $attributeCode): void
64+
{
65+
$this->expectExceptionObject(
66+
new NoSuchEntityException(
67+
__(
68+
'The attribute with a "%1" attributeCode doesn\'t exist. Verify the attribute and try again.',
69+
$attributeCode
70+
)
71+
)
72+
);
73+
$this->productAttributeRepository->get($attributeCode);
74+
}
75+
76+
/**
77+
* @inheritdoc
78+
*/
79+
public function testAclHasAccess()
80+
{
81+
$this->markTestIncomplete('AclHasAccess test is not complete');
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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\Attribute\Delete;
9+
10+
/**
11+
* Delete catalog product attributes with input types like "media_image", "price",
12+
* "date", "select", "multiselect", "textarea", "texteditor", "text" and "boolean".
13+
* Attributes from Magento_Catalog and Magento_Eav modules.
14+
*
15+
* @magentoAppArea adminhtml
16+
* @magentoDbIsolation enabled
17+
*/
18+
class CatalogAttributesControllerTest extends AbstractDeleteAttributeControllerTest
19+
{
20+
/**
21+
* Assert that attribute with input type "media_image" will be deleted
22+
* after dispatch delete product attribute action.
23+
*
24+
* @magentoDataFixture Magento/Catalog/_files/product_image_attribute.php
25+
*
26+
* @return void
27+
*/
28+
public function testDeleteMediaImageAttribute(): void
29+
{
30+
$this->dispatchDeleteAttribute('image_attribute');
31+
$this->assertAttributeIsDeleted('image_attribute');
32+
}
33+
34+
/**
35+
* Assert that attribute with input type "price" will be deleted
36+
* after dispatch delete product attribute action.
37+
*
38+
* @magentoDataFixture Magento/Catalog/_files/product_decimal_attribute.php
39+
*
40+
* @return void
41+
*/
42+
public function testDeletePriceAttribute(): void
43+
{
44+
$this->dispatchDeleteAttribute('decimal_attribute');
45+
$this->assertAttributeIsDeleted('decimal_attribute');
46+
}
47+
48+
/**
49+
* Assert that attribute with input type "date" will be deleted
50+
* after dispatch delete product attribute action.
51+
*
52+
* @magentoDataFixture Magento/Catalog/_files/product_date_attribute.php
53+
*
54+
* @return void
55+
*/
56+
public function testDeleteDateAttribute(): void
57+
{
58+
$this->dispatchDeleteAttribute('date_attribute');
59+
$this->assertAttributeIsDeleted('date_attribute');
60+
}
61+
62+
/**
63+
* Assert that attribute with input type "select" will be deleted
64+
* after dispatch delete product attribute action.
65+
*
66+
* @magentoDataFixture Magento/Catalog/_files/product_dropdown_attribute.php
67+
*
68+
* @return void
69+
*/
70+
public function testDeleteSelectAttribute(): void
71+
{
72+
$this->dispatchDeleteAttribute('dropdown_attribute');
73+
$this->assertAttributeIsDeleted('dropdown_attribute');
74+
}
75+
76+
/**
77+
* Assert that attribute with input type "multiselect" will be deleted
78+
* after dispatch delete product attribute action.
79+
*
80+
* @magentoDataFixture Magento/Catalog/_files/multiselect_attribute.php
81+
*
82+
* @return void
83+
*/
84+
public function testDeleteMultiselectAttribute(): void
85+
{
86+
$this->dispatchDeleteAttribute('multiselect_attribute');
87+
$this->assertAttributeIsDeleted('multiselect_attribute');
88+
}
89+
90+
/**
91+
* Assert that attribute with input type "textarea" will be deleted
92+
* after dispatch delete product attribute action.
93+
*
94+
* @magentoDataFixture Magento/Catalog/_files/product_text_attribute.php
95+
*
96+
* @return void
97+
*/
98+
public function testDeleteTextareaAttribute(): void
99+
{
100+
$this->dispatchDeleteAttribute('text_attribute');
101+
$this->assertAttributeIsDeleted('text_attribute');
102+
}
103+
104+
/**
105+
* Assert that attribute with input type "texteditor" will be deleted
106+
* after dispatch delete product attribute action.
107+
*
108+
* @magentoDataFixture Magento/Eav/_files/product_texteditor_attribute.php
109+
*
110+
* @return void
111+
*/
112+
public function testDeleteTextEditorAttribute(): void
113+
{
114+
$this->dispatchDeleteAttribute('text_editor_attribute');
115+
$this->assertAttributeIsDeleted('text_editor_attribute');
116+
}
117+
118+
/**
119+
* Assert that attribute with input type "text" will be deleted
120+
* after dispatch delete product attribute action.
121+
*
122+
* @magentoDataFixture Magento/Catalog/_files/product_varchar_attribute.php
123+
*
124+
* @return void
125+
*/
126+
public function testDeleteTextAttribute(): void
127+
{
128+
$this->dispatchDeleteAttribute('varchar_attribute');
129+
$this->assertAttributeIsDeleted('varchar_attribute');
130+
}
131+
132+
/**
133+
* Assert that attribute with input type "boolean" will be deleted
134+
* after dispatch delete product attribute action.
135+
*
136+
* @magentoDataFixture Magento/Catalog/_files/product_boolean_attribute.php
137+
*
138+
* @return void
139+
*/
140+
public function testDeleteBooleanAttribute(): void
141+
{
142+
$this->dispatchDeleteAttribute('boolean_attribute');
143+
$this->assertAttributeIsDeleted('boolean_attribute');
144+
}
145+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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\Attribute\Delete;
9+
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Eav\Api\AttributeRepositoryInterface;
12+
use Magento\Framework\App\Request\Http;
13+
use Magento\Framework\Escaper;
14+
use Magento\Framework\Message\MessageInterface;
15+
16+
/**
17+
* Error during delete attribute using catalog/product_attribute/delete controller action.
18+
*
19+
* @magentoAppArea adminhtml
20+
* @magentoDbIsolation enabled
21+
*/
22+
class DeleteAttributeControllerErrorTest extends AbstractDeleteAttributeControllerTest
23+
{
24+
/**
25+
* @var Escaper
26+
*/
27+
private $escaper;
28+
29+
/**
30+
* @var AttributeRepositoryInterface
31+
*/
32+
private $attributeRepository;
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
protected function setUp()
38+
{
39+
parent::setUp();
40+
$this->escaper = $this->_objectManager->get(Escaper::class);
41+
$this->attributeRepository = $this->_objectManager->get(AttributeRepositoryInterface::class);
42+
}
43+
44+
/**
45+
* Try to delete attribute via controller action without attribute ID.
46+
*
47+
* @return void
48+
*/
49+
public function testDispatchWithoutAttributeId(): void
50+
{
51+
$this->getRequest()->setMethod(Http::METHOD_POST);
52+
$this->dispatch(sprintf($this->uri, ''));
53+
$this->assertSessionMessages(
54+
$this->equalTo([$this->escaper->escapeHtml((string)__('We can\'t find an attribute to delete.'))]),
55+
MessageInterface::TYPE_ERROR
56+
);
57+
}
58+
59+
/**
60+
* Try to delete category attribute via controller action.
61+
*
62+
* @magentoDataFixture Magento/Catalog/_files/category_attribute.php
63+
*
64+
* @return void
65+
*/
66+
public function testDispatchWithNonProductAttribute(): void
67+
{
68+
$categoryAttribute = $this->attributeRepository->get(
69+
Category::ENTITY,
70+
'test_attribute_code_666'
71+
);
72+
$this->getRequest()->setMethod(Http::METHOD_POST);
73+
$this->dispatch(sprintf($this->uri, $categoryAttribute->getAttributeId()));
74+
$this->assertSessionMessages(
75+
$this->equalTo([$this->escaper->escapeHtml((string)__('We can\'t delete the attribute.'))]),
76+
MessageInterface::TYPE_ERROR
77+
);
78+
}
79+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\ProductAttributeRepositoryInterface;
9+
use Magento\Catalog\Model\Product;
10+
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
11+
use Magento\Eav\Setup\EavSetup;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\Catalog\Model\Product\Attribute\Frontend\Inputtype\Presentation;
14+
15+
$objectManager = Bootstrap::getObjectManager();
16+
/** @var AttributeFactory $attributeFactory */
17+
$attributeFactory = $objectManager->get(AttributeFactory::class);
18+
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
19+
$productAttributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class);
20+
$attribute = $attributeFactory->create()->loadByCode(Product::ENTITY, 'text_editor_attribute');
21+
if (!$attribute->getId()) {
22+
/** @var Presentation $presentation */
23+
$presentation = $objectManager->get(Presentation::class);
24+
/** @var EavSetup $installer */
25+
$installer = $objectManager->create(EavSetup::class);
26+
$attributeData = [
27+
'attribute_code' => 'text_editor_attribute',
28+
'is_global' => 1,
29+
'is_user_defined' => 1,
30+
'frontend_input' => 'texteditor',
31+
'is_unique' => 0,
32+
'is_required' => 0,
33+
'is_searchable' => 0,
34+
'is_visible_in_advanced_search' => 0,
35+
'is_comparable' => 0,
36+
'is_filterable' => 0,
37+
'is_filterable_in_search' => 0,
38+
'is_used_for_promo_rules' => 0,
39+
'is_html_allowed_on_front' => 1,
40+
'is_visible_on_front' => 0,
41+
'used_in_product_listing' => 0,
42+
'used_for_sort_by' => 0,
43+
'frontend_label' => ['Text editor attribute'],
44+
'backend_type' => 'text',
45+
];
46+
$attribute->setData($presentation->convertPresentationDataToInputType($attributeData));
47+
$productAttributeRepository->save($attribute);
48+
$attribute = $productAttributeRepository->get('text_editor_attribute');
49+
/* Assign attribute to attribute set */
50+
$installer->addAttributeToGroup(Product::ENTITY, 'Default', 'Attributes', $attribute->getId());
51+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\ProductAttributeRepositoryInterface;
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Framework\Registry;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
$objectManager = Bootstrap::getObjectManager();
14+
/** @var ProductAttributeRepositoryInterface $productAttributeRepository */
15+
$productAttributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class);
16+
/** @var Registry $registry */
17+
$registry = $objectManager->get(Registry::class);
18+
$registry->unregister('isSecureArea');
19+
$registry->register('isSecureArea', true);
20+
try {
21+
$attribute = $productAttributeRepository->get('text_editor_attribute');
22+
$productAttributeRepository->delete($attribute);
23+
} catch (NoSuchEntityException $e) {
24+
//Attribute already deleted.
25+
}
26+
$registry->unregister('isSecureArea');
27+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)