Skip to content

Commit ce1a47e

Browse files
author
OlgaVasyltsun
committed
MC-31361: Remove ability to change entity type for attributes
1 parent 286fd52 commit ce1a47e

File tree

5 files changed

+167
-17
lines changed

5 files changed

+167
-17
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Save.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ public function execute()
258258
unset($data['apply_to']);
259259
}
260260

261+
unset($data['entity_type_id']);
262+
261263
$model->addData($data);
262264

263265
if (!$attributeId) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ public function beforeSave()
260260
);
261261
}
262262

263+
$this->validateEntityType();
264+
263265
$defaultValue = $this->getDefaultValue();
264266
$hasDefaultValue = (string)$defaultValue != '';
265267

@@ -535,4 +537,21 @@ public function __wakeup()
535537
$this->reservedAttributeList = $objectManager->get(\Magento\Catalog\Model\Product\ReservedAttributeList::class);
536538
$this->dateTimeFormatter = $objectManager->get(DateTimeFormatterInterface::class);
537539
}
540+
541+
/**
542+
* Entity type for existing attribute shouldn't be changed.
543+
*
544+
* @return void
545+
* @throws LocalizedException
546+
*/
547+
private function validateEntityType(): void
548+
{
549+
if ($this->getId() !== null) {
550+
$origEntityTypeId = $this->getOrigData('entity_type_id');
551+
552+
if (($origEntityTypeId !== null) && ((int)$this->getEntityTypeId() !== (int)$origEntityTypeId)) {
553+
throw new LocalizedException(__('Do not change entity type.'));
554+
}
555+
}
556+
}
538557
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function loadByCode(AbstractModel $object, $entityTypeId, $code)
107107

108108
if ($data) {
109109
$object->setData($data);
110+
$object->setOrigData('entity_type_id', $object->getEntityTypeId());
110111
$this->_afterLoad($object);
111112
return true;
112113
}

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

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,47 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Eav;
77

8+
use Magento\Eav\Api\AttributeRepositoryInterface;
9+
use Magento\Eav\Model\Config;
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
813
/**
914
* Test for \Magento\Catalog\Model\ResourceModel\Eav\Attribute.
1015
*/
1116
class AttributeTest extends \PHPUnit\Framework\TestCase
1217
{
1318
/**
14-
* @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute
19+
* @var ObjectManagerInterface
20+
*/
21+
private $objectManager;
22+
23+
/**
24+
* @var Attribute
25+
*/
26+
private $model;
27+
28+
/**
29+
* @var AttributeRepositoryInterface
30+
*/
31+
private $attributeRepository;
32+
33+
/**
34+
* @var int|string
1535
*/
16-
protected $_model;
36+
private $catalogProductEntityType;
1737

38+
/**
39+
* @inheritDoc
40+
*/
1841
protected function setUp()
1942
{
20-
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
21-
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
22-
);
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->model = $this->objectManager->get(Attribute::class);
45+
$this->attributeRepository = $this->objectManager->get(AttributeRepositoryInterface::class);
46+
$this->catalogProductEntityType = $this->objectManager->get(Config::class)
47+
->getEntityType('catalog_product')
48+
->getId();
2349
}
2450

2551
/**
@@ -29,18 +55,26 @@ protected function setUp()
2955
*/
3056
public function testCRUD()
3157
{
32-
$this->_model->setAttributeCode(
33-
'test'
34-
)->setEntityTypeId(
35-
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
36-
\Magento\Eav\Model\Config::class
37-
)->getEntityType(
38-
'catalog_product'
39-
)->getId()
40-
)->setFrontendLabel(
41-
'test'
42-
)->setIsUserDefined(1);
43-
$crud = new \Magento\TestFramework\Entity($this->_model, ['frontend_label' => uniqid()]);
58+
$this->model->setAttributeCode('test')
59+
->setEntityTypeId($this->catalogProductEntityType)
60+
->setFrontendLabel('test')
61+
->setIsUserDefined(1);
62+
$crud = new \Magento\TestFramework\Entity($this->model, ['frontend_label' => uniqid()]);
4463
$crud->testCrud();
4564
}
65+
66+
/**
67+
* @magentoDataFixture Magento/Catalog/_files/product_attribute.php
68+
*
69+
* @expectedException \Magento\Framework\Exception\LocalizedException
70+
* @expectedExceptionMessage Do not change entity type.
71+
*
72+
* @return void
73+
*/
74+
public function testAttributeSaveWithChangedEntityType(): void
75+
{
76+
$attribute = $this->attributeRepository->get($this->catalogProductEntityType, 'test_attribute_code_333');
77+
$attribute->setEntityTypeId(1);
78+
$attribute->save();
79+
}
4680
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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\Customer\Model;
9+
10+
use Magento\Eav\Api\AttributeRepositoryInterface;
11+
use Magento\Eav\Model\Config;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
15+
/**
16+
* Test for \Magento\Customer\Model\Attribute.
17+
*/
18+
class AttributeTest extends \PHPUnit\Framework\TestCase
19+
{
20+
/**
21+
* @var ObjectManagerInterface
22+
*/
23+
private $objectManager;
24+
25+
/**
26+
* @var Attribute
27+
*/
28+
private $model;
29+
30+
/**
31+
* @var AttributeRepositoryInterface
32+
*/
33+
private $attributeRepository;
34+
35+
/**
36+
* @var int|string
37+
*/
38+
private $customerEntityType;
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
protected function setUp()
44+
{
45+
$this->objectManager = Bootstrap::getObjectManager();
46+
$this->model = $this->objectManager->get(Attribute::class);
47+
$this->attributeRepository = $this->objectManager->get(AttributeRepositoryInterface::class);
48+
$this->customerEntityType = $this->objectManager->get(Config::class)
49+
->getEntityType('customer')
50+
->getId();
51+
}
52+
53+
/**
54+
* Test Create -> Read -> Update -> Delete attribute operations.
55+
*
56+
* @return void
57+
*/
58+
public function testCRUD(): void
59+
{
60+
$this->model->setAttributeCode('test')
61+
->setEntityTypeId($this->customerEntityType)
62+
->setFrontendLabel('test')
63+
->setIsUserDefined(1);
64+
$crud = new \Magento\TestFramework\Entity($this->model, ['frontend_label' => uniqid()]);
65+
$crud->testCrud();
66+
}
67+
68+
/**
69+
* @magentoDataFixture Magento/Customer/_files/attribute_user_defined_customer.php
70+
*
71+
* @expectedException \Magento\Framework\Exception\LocalizedException
72+
* @expectedExceptionMessage Do not change entity type.
73+
*
74+
* @return void
75+
*/
76+
public function testAttributeSaveWithChangedEntityType(): void
77+
{
78+
$attribute = $this->attributeRepository->get($this->customerEntityType, 'user_attribute');
79+
$attribute->setEntityTypeId(5);
80+
$attribute->save();
81+
}
82+
83+
/**
84+
* @magentoDataFixture Magento/Customer/_files/attribute_user_defined_customer.php
85+
*
86+
* @return void
87+
*/
88+
public function testAttributeSaveWithoutChangedEntityType(): void
89+
{
90+
$attribute = $this->attributeRepository->get($this->customerEntityType, 'user_attribute');
91+
$attribute->setSortOrder(1250);
92+
$attribute->save();
93+
}
94+
}

0 commit comments

Comments
 (0)