Skip to content

Commit 655785c

Browse files
committed
MC-16108: EAV attribute is not cached
- Add integration tests;
1 parent 0da5ca4 commit 655785c

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed

dev/tests/integration/testsuite/Magento/Eav/Model/ConfigTest.php

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

8+
use Magento\Framework\App\Config\MutableScopeConfigInterface;
89
use Magento\Framework\DataObject;
910
use Magento\TestFramework\Helper\Bootstrap;
1011
use Magento\TestFramework\Helper\CacheCleaner;
1112

1213
/**
1314
* @magentoAppIsolation enabled
1415
* @magentoDbIsolation enabled
15-
* @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
1616
*/
1717
class ConfigTest extends \PHPUnit\Framework\TestCase
1818
{
@@ -27,6 +27,9 @@ protected function setUp()
2727
$this->config = $objectManager->get(Config::class);
2828
}
2929

30+
/**
31+
* @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
32+
*/
3033
public function testGetEntityAttributeCodes()
3134
{
3235
$entityType = 'test';
@@ -47,6 +50,9 @@ public function testGetEntityAttributeCodes()
4750
$this->assertEquals($entityAttributeCodes1, $entityAttributeCodes2);
4851
}
4952

53+
/**
54+
* @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
55+
*/
5056
public function testGetEntityAttributeCodesWithObject()
5157
{
5258
$entityType = 'test';
@@ -74,6 +80,9 @@ public function testGetEntityAttributeCodesWithObject()
7480
$this->assertEquals($entityAttributeCodes1, $entityAttributeCodes2);
7581
}
7682

83+
/**
84+
* @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
85+
*/
7786
public function testGetAttributes()
7887
{
7988
$entityType = 'test';
@@ -96,6 +105,9 @@ public function testGetAttributes()
96105
$this->assertEquals($attributes1, $attributes2);
97106
}
98107

108+
/**
109+
* @magentoDataFixture Magento/Eav/_files/attribute_for_search.php
110+
*/
99111
public function testGetAttribute()
100112
{
101113
$entityType = 'test';
@@ -109,4 +121,77 @@ public function testGetAttribute()
109121
$attribute2 = $this->config->getAttribute($entityType, 'attribute_for_search_1');
110122
$this->assertEquals($attribute1, $attribute2);
111123
}
124+
125+
/**
126+
* @magentoDataFixture Magento/Eav/_files/attribute_for_caching.php
127+
*/
128+
public function testGetAttributeWithCacheUserDefinedAttribute()
129+
{
130+
/** @var MutableScopeConfigInterface $mutableScopeConfig */
131+
$mutableScopeConfig = Bootstrap::getObjectManager()->get(MutableScopeConfigInterface::class);
132+
$mutableScopeConfig->setValue('dev/caching/cache_user_defined_attributes', 1);
133+
$entityType = 'catalog_product';
134+
$attribute = $this->config->getAttribute($entityType, 'foo');
135+
$this->assertEquals('foo', $attribute->getAttributeCode());
136+
$this->assertEquals('foo', $attribute->getFrontendLabel());
137+
$this->assertEquals('varchar', $attribute->getBackendType());
138+
$this->assertEquals(1, $attribute->getIsRequired());
139+
$this->assertEquals(1, $attribute->getIsUserDefined());
140+
$this->assertEquals(0, $attribute->getIsUnique());
141+
// Update attribute
142+
$eavSetupFactory = Bootstrap::getObjectManager()->create(\Magento\Eav\Setup\EavSetupFactory::class);
143+
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
144+
$eavSetup = $eavSetupFactory->create();
145+
$eavSetup->updateAttribute(
146+
\Magento\Catalog\Model\Product::ENTITY,
147+
'foo',
148+
[
149+
'frontend_label' => 'bar',
150+
]
151+
);
152+
// Check that attribute data has not changed
153+
$config = Bootstrap::getObjectManager()->create(\Magento\Eav\Model\Config::class);
154+
$updatedAttribute = $config->getAttribute($entityType, 'foo');
155+
$this->assertEquals('foo', $updatedAttribute->getFrontendLabel());
156+
// Clean cache
157+
CacheCleaner::cleanAll();
158+
$config = Bootstrap::getObjectManager()->create(\Magento\Eav\Model\Config::class);
159+
// Check that attribute data has changed
160+
$updatedAttributeAfterCacheClean = $config->getAttribute($entityType, 'foo');
161+
$this->assertEquals('bar', $updatedAttributeAfterCacheClean->getFrontendLabel());
162+
$mutableScopeConfig->setValue('dev/caching/cache_user_defined_attributes', 0);
163+
}
164+
165+
/**
166+
* @magentoDataFixture Magento/Eav/_files/attribute_for_caching.php
167+
*/
168+
public function testGetAttributeWithInitUserDefinedAttribute()
169+
{
170+
/** @var MutableScopeConfigInterface $mutableScopeConfig */
171+
$mutableScopeConfig = Bootstrap::getObjectManager()->get(MutableScopeConfigInterface::class);
172+
$mutableScopeConfig->setValue('dev/caching/cache_user_defined_attributes', 0);
173+
$entityType = 'catalog_product';
174+
$attribute = $this->config->getAttribute($entityType, 'foo');
175+
$this->assertEquals('foo', $attribute->getAttributeCode());
176+
$this->assertEquals('foo', $attribute->getFrontendLabel());
177+
$this->assertEquals('varchar', $attribute->getBackendType());
178+
$this->assertEquals(1, $attribute->getIsRequired());
179+
$this->assertEquals(1, $attribute->getIsUserDefined());
180+
$this->assertEquals(0, $attribute->getIsUnique());
181+
// Update attribute
182+
$eavSetupFactory = Bootstrap::getObjectManager()->create(\Magento\Eav\Setup\EavSetupFactory::class);
183+
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
184+
$eavSetup = $eavSetupFactory->create();
185+
$eavSetup->updateAttribute(
186+
\Magento\Catalog\Model\Product::ENTITY,
187+
'foo',
188+
[
189+
'frontend_label' => 'bar',
190+
]
191+
);
192+
// Check that attribute data has changed
193+
$config = Bootstrap::getObjectManager()->create(\Magento\Eav\Model\Config::class);
194+
$updatedAttributeAfterCacheClean = $config->getAttribute($entityType, 'foo');
195+
$this->assertEquals('bar', $updatedAttributeAfterCacheClean->getFrontendLabel());
196+
}
112197
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
9+
/** @var \Magento\Eav\Model\Entity\Type $entityType */
10+
$entityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)
11+
->loadByCode('catalog_product');
12+
$data = $entityType->getData();
13+
$entityTypeId = $entityType->getId();
14+
15+
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
16+
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
17+
$attributeSet->setData([
18+
'attribute_set_name' => 'test_attribute_set',
19+
'entity_type_id' => $entityTypeId,
20+
'sort_order' => 100,
21+
]);
22+
$attributeSet->validate();
23+
$attributeSet->save();
24+
25+
$attributeData = [
26+
[
27+
'attribute_code' => 'foo',
28+
'entity_type_id' => $entityTypeId,
29+
'backend_type' => 'varchar',
30+
'is_required' => 1,
31+
'is_user_defined' => 1,
32+
'is_unique' => 0,
33+
'frontend_label' => ['foo'],
34+
'attribute_set_id' => $entityType->getDefaultAttributeSetId()
35+
]
36+
];
37+
38+
foreach ($attributeData as $data) {
39+
/** @var \Magento\Eav\Model\Entity\Attribute $attribute */
40+
$attribute = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class);
41+
$attribute->setData($data);
42+
$attribute->save();
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
7+
8+
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
9+
$attribute = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
10+
$attribute->loadByCode(4, 'foo');
11+
12+
if ($attribute->getId()) {
13+
$attribute->delete();
14+
}
15+
16+
/** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
17+
$attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class)
18+
->load('test_attribute_set', 'attribute_set_name');
19+
if ($attributeSet->getId()) {
20+
$attributeSet->delete();
21+
}

0 commit comments

Comments
 (0)