Skip to content

Commit 81fa344

Browse files
committed
Merge remote-tracking branch 'origin/MC-17878' into 2.3.3-develop-pr58
2 parents 02313eb + 8e89a1c commit 81fa344

File tree

13 files changed

+124
-17
lines changed

13 files changed

+124
-17
lines changed

app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Magento\Eav\Model\Entity\Attribute as EntityAttribute;
1212
use Magento\Framework\App\ObjectManager;
1313
use Magento\Framework\DB\Select;
14+
use Magento\Framework\Exception\CouldNotDeleteException;
1415
use Magento\Framework\Model\AbstractModel;
16+
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
1517

1618
/**
1719
* EAV attribute resource model
@@ -20,7 +22,7 @@
2022
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2123
* @since 100.0.2
2224
*/
23-
class Attribute extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
25+
class Attribute extends AbstractDb
2426
{
2527
/**
2628
* Eav Entity attributes cache
@@ -189,6 +191,23 @@ protected function _beforeSave(AbstractModel $object)
189191
return parent::_beforeSave($object);
190192
}
191193

194+
/**
195+
* @inheritdoc
196+
*
197+
* @param AbstractModel $attribute
198+
* @return AbstractDb
199+
* @throws CouldNotDeleteException
200+
*/
201+
protected function _beforeDelete(AbstractModel $attribute)
202+
{
203+
/** @var $attribute \Magento\Eav\Api\Data\AttributeInterface */
204+
if ($attribute->getId() && !$attribute->getIsUserDefined()) {
205+
throw new CouldNotDeleteException(__("The system attribute can't be deleted."));
206+
}
207+
208+
return parent::_beforeDelete($attribute);
209+
}
210+
192211
/**
193212
* Save additional attribute data after save attribute
194213
*

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeRepositoryTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7-
87
namespace Magento\Catalog\Api;
98

109
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
11-
use Magento\TestFramework\Helper\Bootstrap;
1210

11+
/**
12+
* API tests for \Magento\Catalog\Model\Product\Attribute\Repository.
13+
*/
1314
class ProductAttributeRepositoryTest extends \Magento\TestFramework\TestCase\WebapiAbstract
1415
{
1516
const SERVICE_NAME = 'catalogProductAttributeRepositoryV1';
@@ -130,6 +131,7 @@ public function testCreateWithExceptionIfAttributeAlreadyExists()
130131
try {
131132
$this->createAttribute($attributeCode);
132133
$this->fail("Expected exception");
134+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
133135
} catch (\SoapFault $e) {
134136
//Expects soap exception
135137
} catch (\Exception $e) {
@@ -320,6 +322,20 @@ public function testDeleteById()
320322
$this->assertTrue($this->deleteAttribute($attributeCode));
321323
}
322324

325+
/**
326+
* Trying to delete system attribute.
327+
*
328+
* @magentoApiDataFixture Magento/Catalog/_files/product_system_attribute.php
329+
* @expectedException \Exception
330+
* @expectedExceptionMessage The system attribute can't be deleted.
331+
* @return void
332+
*/
333+
public function testDeleteSystemAttributeById(): void
334+
{
335+
$attributeCode = 'test_attribute_code_333';
336+
$this->deleteAttribute($attributeCode);
337+
}
338+
323339
/**
324340
* @return void
325341
*/

dev/tests/integration/testsuite/Magento/Catalog/Model/Indexer/Product/Flat/ProcessorTest.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66
namespace Magento\Catalog\Model\Indexer\Product\Flat;
77

8+
use Magento\Catalog\Model\Product\Attribute\Repository;
9+
810
/**
9-
* Class FullTest
11+
* Integration tests for \Magento\Catalog\Model\Indexer\Product\Flat\Processor.
1012
*/
1113
class ProcessorTest extends \Magento\TestFramework\Indexer\TestCase
1214
{
@@ -64,22 +66,23 @@ public function testSaveAttribute()
6466
}
6567

6668
/**
67-
* @magentoDbIsolation enabled
69+
* @magentoDbIsolation disabled
6870
* @magentoAppIsolation enabled
6971
* @magentoAppArea adminhtml
70-
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
72+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_custom_attribute_in_flat.php
7173
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
7274
*/
7375
public function testDeleteAttribute()
7476
{
75-
/** @var $product \Magento\Catalog\Model\Product */
76-
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
77-
\Magento\Catalog\Model\Product::class
78-
);
79-
80-
/** @var \Magento\Catalog\Model\ResourceModel\Product $productResource */
81-
$productResource = $product->getResource();
82-
$productResource->getAttribute('media_gallery')->delete();
77+
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $model */
78+
$model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
79+
->get(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
80+
/** @var Repository $productAttributeRepository */
81+
$productAttributeRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
82+
->get(Repository::class);
83+
$productAttrubute = $productAttributeRepository->get('flat_attribute');
84+
$productAttributeId = $productAttrubute->getAttributeId();
85+
$model->load($productAttributeId)->delete();
8386

8487
$this->assertTrue($this->_processor->getIndexer()->isInvalid());
8588
}

dev/tests/integration/testsuite/Magento/Catalog/Model/Layer/Filter/_files/attribute_with_option.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'is_global' => 1,
2121
'frontend_input' => 'select',
2222
'is_filterable' => 1,
23+
'is_user_defined' => 1,
2324
'option' => ['value' => ['option_0' => [0 => 'Option Label']]],
2425
'backend_type' => 'int',
2526
]

dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Eav/AttributeTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Eav;
77

8+
/**
9+
* Test for \Magento\Catalog\Model\ResourceModel\Eav\Attribute.
10+
*/
811
class AttributeTest extends \PHPUnit\Framework\TestCase
912
{
1013
/**
@@ -19,6 +22,11 @@ protected function setUp()
1922
);
2023
}
2124

25+
/**
26+
* Test Create -> Read -> Update -> Delete attribute operations.
27+
*
28+
* @return void
29+
*/
2230
public function testCRUD()
2331
{
2432
$this->_model->setAttributeCode(
@@ -31,7 +39,7 @@ public function testCRUD()
3139
)->getId()
3240
)->setFrontendLabel(
3341
'test'
34-
);
42+
)->setIsUserDefined(1);
3543
$crud = new \Magento\TestFramework\Entity($this->_model, ['frontend_label' => uniqid()]);
3644
$crud->testCrud();
3745
}

dev/tests/integration/testsuite/Magento/Catalog/_files/category_attribute.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
1010
$attribute->setAttributeCode('test_attribute_code_666')
1111
->setEntityTypeId(3)
12-
->setIsGlobal(1);
12+
->setIsGlobal(1)
13+
->setIsUserDefined(1);
1314
$attribute->save();

dev/tests/integration/testsuite/Magento/Catalog/_files/product_attribute.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
$attribute->setAttributeCode('test_attribute_code_333')
1111
->setEntityTypeId(4)
1212
->setIsGlobal(1)
13-
->setPrice(95);
13+
->setPrice(95)
14+
->setIsUserDefined(1);
1415
$attribute->save();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
// phpcs:ignore Magento2.Security.IncludeFile
9+
require __DIR__ . '/product_attribute.php';
10+
/** @var $attributeRepository \Magento\Catalog\Model\Product\Attribute\Repository */
11+
$attributeRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
12+
->get(\Magento\Catalog\Model\Product\Attribute\Repository::class);
13+
/** @var $attribute \Magento\Eav\Api\Data\AttributeInterface */
14+
$attribute = $attributeRepository->get('test_attribute_code_333');
15+
16+
$attributeRepository->save($attribute->setIsUserDefined(0));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\Framework\Exception\NoSuchEntityException;
9+
10+
/** @var $attributeRepository \Magento\Catalog\Model\Product\Attribute\Repository */
11+
$attributeRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
12+
->get(\Magento\Catalog\Model\Product\Attribute\Repository::class);
13+
14+
try {
15+
/** @var $attribute \Magento\Eav\Api\Data\AttributeInterface */
16+
$attribute = $attributeRepository->get('test_attribute_code_333');
17+
$attributeRepository->save($attribute->setIsUserDefined(1));
18+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
19+
} catch (NoSuchEntityException $e) {
20+
}
21+
/** @var \Magento\Framework\Registry $registry */
22+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
23+
24+
$registry->unregister('isSecureArea');
25+
$registry->register('isSecureArea', true);
26+
27+
try {
28+
$attribute = $attributeRepository->get('test_attribute_code_333');
29+
if ($attribute->getId()) {
30+
$attribute->delete();
31+
}
32+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
33+
} catch (\Exception $e) {
34+
}
35+
36+
$registry->unregister('isSecureArea');
37+
$registry->register('isSecureArea', false);

dev/tests/integration/testsuite/Magento/Framework/Search/_files/date_attribute.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
2626
'is_global' => 1,
2727
'is_filterable' => 1,
28+
'is_user_defined' => 1,
2829
'backend_type' => 'datetime',
2930
'frontend_input' => 'date',
3031
'frontend_label' => 'Test Date',

0 commit comments

Comments
 (0)