Skip to content

Commit ddc1b03

Browse files
committed
Move getCustomAttributeCodes from category model to resource model
1 parent 53e03b2 commit ddc1b03

File tree

7 files changed

+168
-97
lines changed

7 files changed

+168
-97
lines changed

app/code/Magento/Catalog/Api/Data/CategoryInterface.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@
1414
*/
1515
interface CategoryInterface extends \Magento\Framework\Api\CustomAttributesDataInterface
1616
{
17+
/**#@+
18+
* Constants defined for keys of data array
19+
*/
20+
const KEY_PARENT_ID = 'parent_id';
21+
const KEY_NAME = 'name';
22+
const KEY_IS_ACTIVE = 'is_active';
23+
const KEY_POSITION = 'position';
24+
const KEY_LEVEL = 'level';
25+
const KEY_UPDATED_AT = 'updated_at';
26+
const KEY_CREATED_AT = 'created_at';
27+
const KEY_PATH = 'path';
28+
const KEY_AVAILABLE_SORT_BY = 'available_sort_by';
29+
const KEY_INCLUDE_IN_MENU = 'include_in_menu';
30+
const KEY_PRODUCT_COUNT = 'product_count';
31+
const KEY_CHILDREN_DATA = 'children_data';
32+
33+
const ATTRIBUTES = [
34+
'id',
35+
self::KEY_PARENT_ID,
36+
self::KEY_NAME,
37+
self::KEY_IS_ACTIVE,
38+
self::KEY_POSITION,
39+
self::KEY_LEVEL,
40+
self::KEY_UPDATED_AT,
41+
self::KEY_CREATED_AT,
42+
self::KEY_AVAILABLE_SORT_BY,
43+
self::KEY_INCLUDE_IN_MENU,
44+
self::KEY_CHILDREN_DATA,
45+
];
46+
/**#@-*/
47+
1748
/**
1849
* @return int|null
1950
*/

app/code/Magento/Catalog/Model/Category.php

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Catalog\Model;
77

88
use Magento\Catalog\Api\CategoryRepositoryInterface;
9+
use Magento\Catalog\Api\Data\CategoryInterface;
910
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
1011
use Magento\Framework\Api\AttributeValueFactory;
1112
use Magento\Framework\Convert\ConvertArray;
@@ -69,23 +70,6 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
6970

7071
const CACHE_TAG = 'cat_c';
7172

72-
/**#@+
73-
* Constants
74-
*/
75-
const KEY_PARENT_ID = 'parent_id';
76-
const KEY_NAME = 'name';
77-
const KEY_IS_ACTIVE = 'is_active';
78-
const KEY_POSITION = 'position';
79-
const KEY_LEVEL = 'level';
80-
const KEY_UPDATED_AT = 'updated_at';
81-
const KEY_CREATED_AT = 'created_at';
82-
const KEY_PATH = 'path';
83-
const KEY_AVAILABLE_SORT_BY = 'available_sort_by';
84-
const KEY_INCLUDE_IN_MENU = 'include_in_menu';
85-
const KEY_PRODUCT_COUNT = 'product_count';
86-
const KEY_CHILDREN_DATA = 'children_data';
87-
/**#@-*/
88-
8973
/**#@-*/
9074
protected $_eventPrefix = 'catalog_category';
9175

@@ -118,6 +102,11 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
118102
*/
119103
protected $_urlRewrite;
120104

105+
/**
106+
* @var ResourceModel\Category
107+
*/
108+
protected $_resource;
109+
121110
/**
122111
* Use flat resource model flag
123112
*
@@ -142,21 +131,11 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
142131
/**
143132
* Attributes are that part of interface
144133
*
134+
* @deprecated
135+
* @see CategoryInterface::ATTRIBUTES
145136
* @var array
146137
*/
147-
protected $interfaceAttributes = [
148-
'id',
149-
self::KEY_PARENT_ID,
150-
self::KEY_NAME,
151-
self::KEY_IS_ACTIVE,
152-
self::KEY_POSITION,
153-
self::KEY_LEVEL,
154-
self::KEY_UPDATED_AT,
155-
self::KEY_CREATED_AT,
156-
self::KEY_AVAILABLE_SORT_BY,
157-
self::KEY_INCLUDE_IN_MENU,
158-
self::KEY_CHILDREN_DATA,
159-
];
138+
protected $interfaceAttributes = CategoryInterface::ATTRIBUTES;
160139

161140
/**
162141
* Category tree model
@@ -226,6 +205,7 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
226205
protected $categoryRepository;
227206

228207
/**
208+
* @deprecated not used anymore, related functionality has been moved to resource model
229209
* @var \Magento\Framework\Api\MetadataServiceInterface
230210
*/
231211
protected $metadataService;
@@ -323,11 +303,7 @@ protected function _construct()
323303
*/
324304
protected function getCustomAttributesCodes()
325305
{
326-
if ($this->customAttributesCodes === null) {
327-
$this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService);
328-
$this->customAttributesCodes = array_diff($this->customAttributesCodes, $this->interfaceAttributes);
329-
}
330-
return $this->customAttributesCodes;
306+
return $this->_resource->getCustomAttributesCodes();
331307
}
332308

333309
/**

app/code/Magento/Catalog/Model/ResourceModel/Category.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*/
1212
namespace Magento\Catalog\Model\ResourceModel;
1313

14+
use Magento\Catalog\Api\CategoryAttributeRepositoryInterface;
15+
use Magento\Catalog\Api\Data\CategoryInterface;
16+
use Magento\Framework\App\ObjectManager;
1417
use Magento\Framework\EntityManager\EntityManager;
1518

1619
/**
@@ -82,6 +85,16 @@ class Category extends AbstractResource
8285
*/
8386
protected $aggregateCount;
8487

88+
/**
89+
* @var CategoryAttributeRepositoryInterface
90+
*/
91+
protected $metadataService;
92+
93+
/**
94+
* @var string[]
95+
*/
96+
private $customAttributesCodes;
97+
8598
/**
8699
* Category constructor.
87100
* @param \Magento\Eav\Model\Entity\Context $context
@@ -92,6 +105,7 @@ class Category extends AbstractResource
92105
* @param Category\CollectionFactory $categoryCollectionFactory
93106
* @param array $data
94107
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
108+
* @param CategoryAttributeRepositoryInterface|null $metaDataService
95109
*/
96110
public function __construct(
97111
\Magento\Eav\Model\Entity\Context $context,
@@ -101,7 +115,8 @@ public function __construct(
101115
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory,
102116
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
103117
$data = [],
104-
\Magento\Framework\Serialize\Serializer\Json $serializer = null
118+
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
119+
CategoryAttributeRepositoryInterface $metaDataService = null
105120
) {
106121
parent::__construct(
107122
$context,
@@ -113,8 +128,21 @@ public function __construct(
113128
$this->_categoryCollectionFactory = $categoryCollectionFactory;
114129
$this->_eventManager = $eventManager;
115130
$this->connectionName = 'catalog';
116-
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
131+
$this->serializer = $serializer ?: ObjectManager::getInstance()
117132
->get(\Magento\Framework\Serialize\Serializer\Json::class);
133+
$this->metadataService = $metaDataService ?? ObjectManager::getInstance()
134+
->get(CategoryAttributeRepositoryInterface::class);
135+
}
136+
137+
public function getCustomAttributesCodes()
138+
{
139+
if ($this->customAttributesCodes === null) {
140+
$this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService);
141+
$this->customAttributesCodes = array_values(
142+
array_diff($this->customAttributesCodes, CategoryInterface::ATTRIBUTES)
143+
);
144+
}
145+
return $this->customAttributesCodes;
118146
}
119147

120148
/**
@@ -1060,7 +1088,7 @@ public function save(\Magento\Framework\Model\AbstractModel $object)
10601088
private function getEntityManager()
10611089
{
10621090
if (null === $this->entityManager) {
1063-
$this->entityManager = \Magento\Framework\App\ObjectManager::getInstance()
1091+
$this->entityManager = ObjectManager::getInstance()
10641092
->get(\Magento\Framework\EntityManager\EntityManager::class);
10651093
}
10661094
return $this->entityManager;
@@ -1072,7 +1100,7 @@ private function getEntityManager()
10721100
private function getAggregateCount()
10731101
{
10741102
if (null === $this->aggregateCount) {
1075-
$this->aggregateCount = \Magento\Framework\App\ObjectManager::getInstance()
1103+
$this->aggregateCount = ObjectManager::getInstance()
10761104
->get(\Magento\Catalog\Model\ResourceModel\Category\AggregateCount::class);
10771105
}
10781106
return $this->aggregateCount;

app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\Catalog\Test\Unit\Model;
88

9+
use Magento\Catalog\Api\CategoryAttributeRepositoryInterface;
910
use Magento\Catalog\Model\Indexer;
1011
use Magento\Catalog\Model\Category;
1112

@@ -105,11 +106,6 @@ class CategoryTest extends \PHPUnit\Framework\TestCase
105106
*/
106107
private $indexerRegistry;
107108

108-
/**
109-
* @var \Magento\Catalog\Api\CategoryAttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
110-
*/
111-
private $metadataServiceMock;
112-
113109
/**
114110
* @var \PHPUnit_Framework_MockObject_MockObject
115111
*/
@@ -155,9 +151,6 @@ protected function setUp()
155151
$this->resource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category::class);
156152
$this->indexerRegistry = $this->createPartialMock(\Magento\Framework\Indexer\IndexerRegistry::class, ['get']);
157153

158-
$this->metadataServiceMock = $this->createMock(
159-
\Magento\Catalog\Api\CategoryAttributeRepositoryInterface::class
160-
);
161154
$this->attributeValueFactory = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class)
162155
->disableOriginalConstructor()->getMock();
163156

@@ -307,7 +300,7 @@ protected function getCategoryModel()
307300
'urlFinder' => $this->urlFinder,
308301
'resource' => $this->resource,
309302
'indexerRegistry' => $this->indexerRegistry,
310-
'metadataService' => $this->metadataServiceMock,
303+
'metadataService' => $this->createMock(CategoryAttributeRepositoryInterface::class),
311304
'customAttributeFactory' => $this->attributeValueFactory,
312305
]
313306
);
@@ -431,43 +424,39 @@ public function testReindexFlatDisabled(
431424

432425
public function testGetCustomAttributes()
433426
{
434-
$nameAttributeCode = 'name';
435-
$descriptionAttributeCode = 'description';
436-
$interfaceAttribute = $this->createMock(\Magento\Framework\Api\MetadataObjectInterface::class);
437-
$interfaceAttribute->expects($this->once())
438-
->method('getAttributeCode')
439-
->willReturn($nameAttributeCode);
440-
$descriptionAttribute = $this->createMock(\Magento\Framework\Api\MetadataObjectInterface::class);
441-
$descriptionAttribute->expects($this->once())
442-
->method('getAttributeCode')
443-
->willReturn($descriptionAttributeCode);
444-
$customAttributesMetadata = [$interfaceAttribute, $descriptionAttribute];
445-
446-
$this->metadataServiceMock->expects($this->once())
447-
->method('getCustomAttributesMetadata')
448-
->willReturn($customAttributesMetadata);
449-
$this->category->setData($nameAttributeCode, "sub");
450-
451-
//The color attribute is not set, expect empty custom attribute array
427+
$interfaceAttributeCode = 'name';
428+
$customAttributeCode = 'description';
429+
430+
$this->resource
431+
->method('getCustomAttributesCodes')
432+
->willReturn([$customAttributeCode]);
433+
$this->category->setData($interfaceAttributeCode, "sub");
434+
435+
//The description attribute is not set, expect empty custom attribute array
452436
$this->assertEquals([], $this->category->getCustomAttributes());
453437

454-
//Set the color attribute;
455-
$this->category->setData($descriptionAttributeCode, "description");
438+
//Set the description attribute;
439+
$initialCustomAttributeValue = "initial description";
440+
$this->category->setData($customAttributeCode, $initialCustomAttributeValue);
456441
$attributeValue = new \Magento\Framework\Api\AttributeValue();
457442
$attributeValue2 = new \Magento\Framework\Api\AttributeValue();
458443
$this->attributeValueFactory->expects($this->exactly(2))->method('create')
459444
->willReturnOnConsecutiveCalls($attributeValue, $attributeValue2);
460445
$this->assertEquals(1, count($this->category->getCustomAttributes()));
461-
$this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode));
462-
$this->assertEquals("description", $this->category->getCustomAttribute($descriptionAttributeCode)->getValue());
446+
$this->assertNotNull($this->category->getCustomAttribute($customAttributeCode));
447+
$this->assertEquals(
448+
$initialCustomAttributeValue,
449+
$this->category->getCustomAttribute($customAttributeCode)->getValue()
450+
);
463451

464452
//Change the attribute value, should reflect in getCustomAttribute
465-
$this->category->setData($descriptionAttributeCode, "new description");
453+
$newCustomAttributeValue = "new description";
454+
$this->category->setData($customAttributeCode, $newCustomAttributeValue);
466455
$this->assertEquals(1, count($this->category->getCustomAttributes()));
467-
$this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode));
456+
$this->assertNotNull($this->category->getCustomAttribute($customAttributeCode));
468457
$this->assertEquals(
469-
"new description",
470-
$this->category->getCustomAttribute($descriptionAttributeCode)->getValue()
458+
$newCustomAttributeValue,
459+
$this->category->getCustomAttribute($customAttributeCode)->getValue()
471460
);
472461
}
473462

app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,32 +1260,40 @@ public function testGetMediaGalleryImagesMerging()
12601260

12611261
public function testGetCustomAttributes()
12621262
{
1263-
$priceCode = 'price';
1264-
$colorAttributeCode = 'color';
1263+
$interfaceAttributeCode = 'price';
1264+
$customAttributeCode = 'color';
12651265

12661266
$this->resource
12671267
->method('getCustomAttributesCodes')
1268-
->willReturn([$colorAttributeCode]);
1269-
$this->model->setData($priceCode, 10);
1268+
->willReturn([$customAttributeCode]);
1269+
$this->model->setData($interfaceAttributeCode, 10);
12701270

12711271
//The color attribute is not set, expect empty custom attribute array
12721272
$this->assertEquals([], $this->model->getCustomAttributes());
12731273

12741274
//Set the color attribute;
1275-
$this->model->setData($colorAttributeCode, "red");
1275+
$initialCustomAttribueValue = "red";
1276+
$this->model->setData($customAttributeCode, $initialCustomAttribueValue);
12761277
$attributeValue = new \Magento\Framework\Api\AttributeValue();
12771278
$attributeValue2 = new \Magento\Framework\Api\AttributeValue();
12781279
$this->attributeValueFactory->expects($this->exactly(2))->method('create')
12791280
->willReturnOnConsecutiveCalls($attributeValue, $attributeValue2);
12801281
$this->assertEquals(1, count($this->model->getCustomAttributes()));
1281-
$this->assertNotNull($this->model->getCustomAttribute($colorAttributeCode));
1282-
$this->assertEquals("red", $this->model->getCustomAttribute($colorAttributeCode)->getValue());
1282+
$this->assertNotNull($this->model->getCustomAttribute($customAttributeCode));
1283+
$this->assertEquals(
1284+
$initialCustomAttribueValue,
1285+
$this->model->getCustomAttribute($customAttributeCode)->getValue()
1286+
);
12831287

12841288
//Change the attribute value, should reflect in getCustomAttribute
1285-
$this->model->setData($colorAttributeCode, "blue");
1289+
$newCustomAttributeValue = "blue";
1290+
$this->model->setData($customAttributeCode, $newCustomAttributeValue);
12861291
$this->assertEquals(1, count($this->model->getCustomAttributes()));
1287-
$this->assertNotNull($this->model->getCustomAttribute($colorAttributeCode));
1288-
$this->assertEquals("blue", $this->model->getCustomAttribute($colorAttributeCode)->getValue());
1292+
$this->assertNotNull($this->model->getCustomAttribute($customAttributeCode));
1293+
$this->assertEquals(
1294+
$newCustomAttributeValue,
1295+
$this->model->getCustomAttribute($customAttributeCode)->getValue()
1296+
);
12891297
}
12901298

12911299
/**

0 commit comments

Comments
 (0)