Skip to content

Commit 4fed282

Browse files
committed
B2B-2259: customAttributeMetadata GraphQl query has no cache identity
- WebAPI test
1 parent 57755d2 commit 4fed282

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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\GraphQl\Eav;
9+
10+
use Magento\GraphQl\PageCache\GraphQLPageCacheAbstract;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
class CustomAttributesMetadataCacheTest extends GraphQLPageCacheAbstract
14+
{
15+
/**
16+
* @var \Magento\Framework\ObjectManagerInterface
17+
*/
18+
private $objectManager;
19+
20+
public function setUp(): void
21+
{
22+
$this->objectManager = Bootstrap::getObjectManager();
23+
parent::setUp();
24+
}
25+
26+
/**
27+
* @magentoApiDataFixture Magento/Catalog/_files/dropdown_attribute.php
28+
*
29+
* @return void
30+
*/
31+
public function testCacheHitMiss()
32+
{
33+
$query = $this->getAttributeQuery("dropdown_attribute", "catalog_product");
34+
$response = $this->assertCacheMissAndReturnResponse($query, []);
35+
$this->assertResponseFields(
36+
$response['body']['customAttributeMetadata']['items'][0],
37+
[
38+
'attribute_code' => 'dropdown_attribute',
39+
'attribute_type' => 'String',
40+
'entity_type' => 'catalog_product',
41+
'input_type' => 'select',
42+
]
43+
);
44+
$response = $this->assertCacheHitAndReturnResponse($query, []);
45+
$this->assertResponseFields(
46+
$response['body']['customAttributeMetadata']['items'][0],
47+
[
48+
'attribute_code' => 'dropdown_attribute',
49+
'attribute_type' => 'String',
50+
'entity_type' => 'catalog_product',
51+
'input_type' => 'select',
52+
]
53+
);
54+
}
55+
56+
/**
57+
* @magentoApiDataFixture Magento/Catalog/_files/dropdown_attribute.php
58+
* @magentoApiDataFixture Magento/Store/_files/store.php
59+
* @return void
60+
*/
61+
public function testCacheDifferentStores()
62+
{
63+
$query = $this->getAttributeQuery("dropdown_attribute", "catalog_product");
64+
/** @var \Magento\Eav\Model\AttributeRepository $eavAttributeRepo */
65+
$eavAttributeRepo = $this->objectManager->get(\Magento\Eav\Model\AttributeRepository::class);
66+
/** @var \Magento\Store\Model\StoreRepository $storeRepo */
67+
$storeRepo = $this->objectManager->get(\Magento\Store\Model\StoreRepository::class);
68+
69+
$stores = $storeRepo->getList();
70+
$attribute = $eavAttributeRepo->get("catalog_product", "dropdown_attribute");
71+
$options = $attribute->getOptions();
72+
73+
//prepare unique option values per-store
74+
$storeOptions = [];
75+
foreach ($options as $option) {
76+
$optionValues = $option->getData();
77+
if (!empty($optionValues['value'])) {
78+
$storeOptions['value'][$optionValues['value']] = [];
79+
foreach ($stores as $store) {
80+
$storeOptions['value'][$optionValues['value']][$store->getId()] = $store->getCode()
81+
. '_'
82+
. $optionValues['label'];
83+
}
84+
}
85+
}
86+
//save attribute with new option values
87+
$attribute->addData(['option' => $storeOptions]);
88+
$eavAttributeRepo->save($attribute);
89+
90+
// get attribute metadata for test store and assert it missed the cache
91+
$response = $this->assertCacheMissAndReturnResponse($query, ['Store' => 'test']);
92+
$options = $response['body']['customAttributeMetadata']['items'][0]['attribute_options'];
93+
$this->assertOptionValuesPerStore($storeOptions, 'test', $stores, $options);
94+
95+
// get attribute metadata for test store again and assert it has hit the cache
96+
$response = $this->assertCacheHitAndReturnResponse($query, ['Store' => 'test']);
97+
$options = $response['body']['customAttributeMetadata']['items'][0]['attribute_options'];
98+
$this->assertOptionValuesPerStore($storeOptions, 'test', $stores, $options);
99+
100+
101+
$response = $this->assertCacheMissAndReturnResponse($query, ['Store' => 'default']);
102+
$options = $response['body']['customAttributeMetadata']['items'][0]['attribute_options'];
103+
$this->assertOptionValuesPerStore($storeOptions, 'default', $stores, $options);
104+
105+
$response = $this->assertCacheHitAndReturnResponse($query, []);
106+
$options = $response['body']['customAttributeMetadata']['items'][0]['attribute_options'];
107+
$this->assertOptionValuesPerStore($storeOptions, 'default', $stores, $options);
108+
}
109+
110+
/**
111+
* Per-store assertion helper method.
112+
*
113+
* @param $storeOptions
114+
* @param $storeCode
115+
* @param $stores
116+
* @param $options
117+
* @return void
118+
*/
119+
private function assertOptionValuesPerStore($storeOptions, $storeCode, $stores, $options)
120+
{
121+
foreach ($options as $option) {
122+
$this->assertEquals(
123+
$storeOptions['value'][$option['value']][$stores[$storeCode]->getId()],
124+
$option['label']
125+
);
126+
}
127+
}
128+
129+
/**
130+
* @magentoApiDataFixture Magento/Catalog/_files/dropdown_attribute.php
131+
*
132+
* @return void
133+
*/
134+
public function testCacheInvalidation()
135+
{
136+
$query = $this->getAttributeQuery("dropdown_attribute", "catalog_product");
137+
// check cache missed on first query
138+
$response = $this->assertCacheMissAndReturnResponse($query, []);
139+
$this->assertResponseFields(
140+
$response['body']['customAttributeMetadata']['items'][0],
141+
[
142+
'attribute_code' => 'dropdown_attribute',
143+
'attribute_type' => 'String',
144+
'entity_type' => 'catalog_product',
145+
'input_type' => 'select',
146+
]
147+
);
148+
// assert cache hit on second query
149+
$this->assertCacheHitAndReturnResponse($query, []);
150+
/** @var \Magento\Eav\Model\AttributeRepository $eavAttributeRepo */
151+
$eavAttributeRepo = $this->objectManager->get(\Magento\Eav\Model\AttributeRepository::class);
152+
$attribute = $eavAttributeRepo->get("catalog_product", "dropdown_attribute");
153+
$attribute->setIsRequired(1);
154+
$eavAttributeRepo->save($attribute);
155+
// assert cache miss after changes
156+
$this->assertCacheMissAndReturnResponse($query, []);
157+
// assert cache hits on second query after changes
158+
$response = $this->assertCacheHitAndReturnResponse($query, []);
159+
$this->assertResponseFields(
160+
$response['body']['customAttributeMetadata']['items'][0],
161+
[
162+
'attribute_code' => 'dropdown_attribute',
163+
'attribute_type' => 'String',
164+
'entity_type' => 'catalog_product',
165+
'input_type' => 'select',
166+
]
167+
);
168+
}
169+
170+
private function getAttributeQuery(string $code, string $entityType) : string
171+
{
172+
return <<<QUERY
173+
{
174+
customAttributeMetadata(attributes:
175+
[
176+
{
177+
attribute_code:"{$code}",
178+
entity_type:"{$entityType}"
179+
}
180+
]
181+
)
182+
{
183+
items
184+
{
185+
attribute_code
186+
attribute_type
187+
entity_type
188+
input_type
189+
attribute_options{
190+
label
191+
value
192+
}
193+
}
194+
}
195+
}
196+
QUERY;
197+
}
198+
}

0 commit comments

Comments
 (0)