Skip to content

Commit b61dd4a

Browse files
committed
Merge remote-tracking branch 'origin/MC-31361' into 2.4.0-develop-pr17-2
2 parents d6129f0 + 7eb5025 commit b61dd4a

File tree

5 files changed

+169
-17
lines changed

5 files changed

+169
-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: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,48 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Eav;
77

8+
use Magento\Eav\Api\AttributeRepositoryInterface;
9+
use Magento\Eav\Api\Data\AttributeInterface;
10+
use Magento\Eav\Model\Config;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
814
/**
915
* Test for \Magento\Catalog\Model\ResourceModel\Eav\Attribute.
1016
*/
1117
class AttributeTest extends \PHPUnit\Framework\TestCase
1218
{
1319
/**
14-
* @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute
20+
* @var ObjectManagerInterface
21+
*/
22+
private $objectManager;
23+
24+
/**
25+
* @var Attribute
26+
*/
27+
private $model;
28+
29+
/**
30+
* @var AttributeRepositoryInterface
31+
*/
32+
private $attributeRepository;
33+
34+
/**
35+
* @var int|string
1536
*/
16-
protected $_model;
37+
private $catalogProductEntityType;
1738

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

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

0 commit comments

Comments
 (0)