init
This commit is contained in:
+395
@@ -0,0 +1,395 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\Eav;
|
||||
|
||||
use Magento\Eav\Model\AttributeRepository;
|
||||
use Magento\GraphQl\PageCache\GraphQLPageCacheAbstract;
|
||||
use Magento\Store\Model\StoreRepository;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
|
||||
|
||||
/**
|
||||
* Test caching for custom attribute metadata GraphQL query.
|
||||
*/
|
||||
class CustomAttributesMetadataCacheTest extends GraphQLPageCacheAbstract
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/dropdown_attribute.php
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCacheHitMiss()
|
||||
{
|
||||
$query = $this->getAttributeQuery("dropdown_attribute", "catalog_product");
|
||||
$response = $this->assertCacheMissAndReturnResponse($query, []);
|
||||
$this->assertResponseFields(
|
||||
$response['body']['customAttributeMetadata']['items'][0],
|
||||
[
|
||||
'attribute_code' => 'dropdown_attribute',
|
||||
'attribute_type' => 'String',
|
||||
'entity_type' => 'catalog_product',
|
||||
'input_type' => 'select',
|
||||
]
|
||||
);
|
||||
$response = $this->assertCacheHitAndReturnResponse($query, []);
|
||||
$this->assertResponseFields(
|
||||
$response['body']['customAttributeMetadata']['items'][0],
|
||||
[
|
||||
'attribute_code' => 'dropdown_attribute',
|
||||
'attribute_type' => 'String',
|
||||
'entity_type' => 'catalog_product',
|
||||
'input_type' => 'select',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/dropdown_attribute.php
|
||||
* @magentoApiDataFixture Magento/Store/_files/store.php
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCacheDifferentStores()
|
||||
{
|
||||
$query = $this->getAttributeQuery("dropdown_attribute", "catalog_product");
|
||||
/** @var AttributeRepository $eavAttributeRepo */
|
||||
$eavAttributeRepo = $this->objectManager->get(AttributeRepository::class);
|
||||
/** @var StoreRepository $storeRepo */
|
||||
$storeRepo = $this->objectManager->get(StoreRepository::class);
|
||||
|
||||
$stores = $storeRepo->getList();
|
||||
$attribute = $eavAttributeRepo->get("catalog_product", "dropdown_attribute");
|
||||
$options = $attribute->getOptions();
|
||||
|
||||
//prepare unique option values per-store
|
||||
$storeOptions = [];
|
||||
foreach ($options as $option) {
|
||||
$optionValues = $option->getData();
|
||||
if (!empty($optionValues['value'])) {
|
||||
$storeOptions['value'][$optionValues['value']] = [];
|
||||
foreach ($stores as $store) {
|
||||
$storeOptions['value'][$optionValues['value']][$store->getId()] = $store->getCode()
|
||||
. '_'
|
||||
. $optionValues['label'];
|
||||
}
|
||||
}
|
||||
}
|
||||
//save attribute with new option values
|
||||
$attribute->addData(['option' => $storeOptions]);
|
||||
$eavAttributeRepo->save($attribute);
|
||||
|
||||
// get attribute metadata for test store and assert it missed the cache
|
||||
$response = $this->assertCacheMissAndReturnResponse($query, ['Store' => 'test']);
|
||||
$options = $response['body']['customAttributeMetadata']['items'][0]['attribute_options'];
|
||||
$this->assertOptionValuesPerStore($storeOptions, 'test', $stores, $options);
|
||||
|
||||
// get attribute metadata for test store again and assert it has hit the cache
|
||||
$response = $this->assertCacheHitAndReturnResponse($query, ['Store' => 'test']);
|
||||
$options = $response['body']['customAttributeMetadata']['items'][0]['attribute_options'];
|
||||
$this->assertOptionValuesPerStore($storeOptions, 'test', $stores, $options);
|
||||
|
||||
$response = $this->assertCacheMissAndReturnResponse($query, ['Store' => 'default']);
|
||||
$options = $response['body']['customAttributeMetadata']['items'][0]['attribute_options'];
|
||||
$this->assertOptionValuesPerStore($storeOptions, 'default', $stores, $options);
|
||||
|
||||
$response = $this->assertCacheHitAndReturnResponse($query, ['Store' => 'default']);
|
||||
$options = $response['body']['customAttributeMetadata']['items'][0]['attribute_options'];
|
||||
$this->assertOptionValuesPerStore($storeOptions, 'default', $stores, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert attribute option labels for each store provided.
|
||||
*
|
||||
* @param array $storeOptions
|
||||
* @param string $storeCode
|
||||
* @param \Magento\Store\Api\Data\StoreInterface[] $stores
|
||||
* @param array $options
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function assertOptionValuesPerStore($storeOptions, $storeCode, $stores, $options)
|
||||
{
|
||||
foreach ($options as $option) {
|
||||
$this->assertEquals(
|
||||
$storeOptions['value'][$option['value']][$stores[$storeCode]->getId()],
|
||||
$option['label']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/dropdown_attribute.php
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCacheInvalidation()
|
||||
{
|
||||
$query = $this->getAttributeQuery("dropdown_attribute", "catalog_product");
|
||||
// check cache missed on first query
|
||||
$response = $this->assertCacheMissAndReturnResponse($query, []);
|
||||
$this->assertResponseFields(
|
||||
$response['body']['customAttributeMetadata']['items'][0],
|
||||
[
|
||||
'attribute_code' => 'dropdown_attribute',
|
||||
'attribute_type' => 'String',
|
||||
'entity_type' => 'catalog_product',
|
||||
'input_type' => 'select',
|
||||
]
|
||||
);
|
||||
// assert cache hit on second query
|
||||
$this->assertCacheHitAndReturnResponse($query, []);
|
||||
/** @var AttributeRepository $eavAttributeRepo */
|
||||
$eavAttributeRepo = $this->objectManager->get(AttributeRepository::class);
|
||||
$attribute = $eavAttributeRepo->get("catalog_product", "dropdown_attribute");
|
||||
$attribute->setIsRequired(1);
|
||||
$eavAttributeRepo->save($attribute);
|
||||
// assert cache miss after changes
|
||||
$this->assertCacheMissAndReturnResponse($query, []);
|
||||
// assert cache hits on second query after changes
|
||||
$response = $this->assertCacheHitAndReturnResponse($query, []);
|
||||
$this->assertResponseFields(
|
||||
$response['body']['customAttributeMetadata']['items'][0],
|
||||
[
|
||||
'attribute_code' => 'dropdown_attribute',
|
||||
'attribute_type' => 'String',
|
||||
'entity_type' => 'catalog_product',
|
||||
'input_type' => 'select',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/dropdown_attribute.php
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCacheInvalidationOnAttributeDelete()
|
||||
{
|
||||
$query = $this->getAttributeQuery("dropdown_attribute", "catalog_product");
|
||||
// check cache missed on first query
|
||||
$response = $this->assertCacheMissAndReturnResponse($query, []);
|
||||
$this->assertResponseFields(
|
||||
$response['body']['customAttributeMetadata']['items'][0],
|
||||
[
|
||||
'attribute_code' => 'dropdown_attribute',
|
||||
'attribute_type' => 'String',
|
||||
'entity_type' => 'catalog_product',
|
||||
'input_type' => 'select',
|
||||
]
|
||||
);
|
||||
// assert cache hit on second query
|
||||
$this->assertCacheHitAndReturnResponse($query, []);
|
||||
/** @var AttributeRepository $eavAttributeRepo */
|
||||
$eavAttributeRepo = $this->objectManager->get(AttributeRepository::class);
|
||||
$attribute = $eavAttributeRepo->get("catalog_product", "dropdown_attribute");
|
||||
$eavAttributeRepo->delete($attribute);
|
||||
$this->assertQueryResultIsCacheMissWithError(
|
||||
$query,
|
||||
"GraphQL response contains errors: Internal server error"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/dropdown_attribute.php
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCacheMissingAttributeParam()
|
||||
{
|
||||
$query = $this->getAttributeQueryNoCode("catalog_product");
|
||||
// check cache missed on each query
|
||||
$this->assertQueryResultIsCacheMissWithError(
|
||||
$query,
|
||||
"Missing attribute_code for the input entity_type: catalog_product."
|
||||
);
|
||||
$this->assertQueryResultIsCacheMissWithError(
|
||||
$query,
|
||||
"Missing attribute_code for the input entity_type: catalog_product."
|
||||
);
|
||||
|
||||
$query = $this->getAttributeQueryNoEntityType("dropdown_attribute");
|
||||
// check cache missed on each query
|
||||
$this->assertQueryResultIsCacheMissWithError(
|
||||
$query,
|
||||
"Missing entity_type for the input attribute_code: dropdown_attribute."
|
||||
);
|
||||
$this->assertQueryResultIsCacheMissWithError(
|
||||
$query,
|
||||
"Missing entity_type for the input attribute_code: dropdown_attribute."
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that query produces an error and the cache is missed.
|
||||
*
|
||||
* @param string $query
|
||||
* @param string $expectedError
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function assertQueryResultIsCacheMissWithError(string $query, string $expectedError)
|
||||
{
|
||||
$caughtException = null;
|
||||
try {
|
||||
// query for response, expect response to be present in exception
|
||||
$this->graphQlQueryWithResponseHeaders($query, []);
|
||||
} catch (ResponseContainsErrorsException $exception) {
|
||||
$caughtException = $exception;
|
||||
}
|
||||
$this->assertInstanceOf(
|
||||
ResponseContainsErrorsException::class,
|
||||
$caughtException
|
||||
);
|
||||
// cannot use expectException because need to assert the headers
|
||||
$this->assertStringContainsString(
|
||||
$expectedError,
|
||||
$caughtException->getMessage()
|
||||
);
|
||||
// assert that it's a miss
|
||||
$this->assertEquals(
|
||||
$caughtException->getResponseHeaders()['X-Magento-Cache-Debug'],
|
||||
'MISS'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test cache invalidation when queried for attribute data of different entity types.
|
||||
* Required for GraphQL FPC use-case since there is no attribute ID provided in the result.
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCacheInvalidationMultiEntitySameCode()
|
||||
{
|
||||
$queryProduct = $this->getAttributeQuery("name", "catalog_product");
|
||||
$queryCategory = $this->getAttributeQuery("name", "catalog_category");
|
||||
// precache both product and category response
|
||||
$this->assertCacheMissAndReturnResponse($queryProduct, []);
|
||||
$this->assertCacheMissAndReturnResponse($queryCategory, []);
|
||||
$eavAttributeRepo = $this->objectManager->get(AttributeRepository::class);
|
||||
$attribute = $eavAttributeRepo->get("catalog_product", "name");
|
||||
$eavAttributeRepo->save($attribute);
|
||||
// assert that product is invalidated for the same code but category is not touched
|
||||
$this->assertCacheMissAndReturnResponse($queryProduct, []);
|
||||
$this->assertCacheHitAndReturnResponse($queryCategory, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and return GraphQL query for given entity type and code.
|
||||
*
|
||||
* @param string $code
|
||||
* @param string $entityType
|
||||
* @return string
|
||||
*/
|
||||
private function getAttributeQuery(string $code, string $entityType) : string
|
||||
{
|
||||
return <<<QUERY
|
||||
{
|
||||
customAttributeMetadata(attributes:
|
||||
[
|
||||
{
|
||||
attribute_code:"{$code}",
|
||||
entity_type:"{$entityType}"
|
||||
}
|
||||
]
|
||||
)
|
||||
{
|
||||
items
|
||||
{
|
||||
attribute_code
|
||||
attribute_type
|
||||
entity_type
|
||||
input_type
|
||||
attribute_options{
|
||||
label
|
||||
value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and return GraphQL query for given entity type with no code.
|
||||
*
|
||||
* @param string $entityType
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getAttributeQueryNoCode(string $entityType) : string
|
||||
{
|
||||
return <<<QUERY
|
||||
{
|
||||
customAttributeMetadata(attributes:
|
||||
[
|
||||
{
|
||||
entity_type:"{$entityType}"
|
||||
}
|
||||
]
|
||||
)
|
||||
{
|
||||
items
|
||||
{
|
||||
attribute_code
|
||||
entity_type
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and return GraphQL query for given code with no entity type.
|
||||
*
|
||||
* @param string $code
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getAttributeQueryNoEntityType(string $code) : string
|
||||
{
|
||||
return <<<QUERY
|
||||
{
|
||||
customAttributeMetadata(attributes:
|
||||
[
|
||||
{
|
||||
attribute_code:"{$code}"
|
||||
}
|
||||
]
|
||||
)
|
||||
{
|
||||
items
|
||||
{
|
||||
attribute_code
|
||||
entity_type
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
}
|
||||
+366
@@ -0,0 +1,366 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\Eav;
|
||||
|
||||
use Magento\Customer\Api\CustomerMetadataInterface;
|
||||
use Magento\Customer\Api\Data\AttributeMetadataInterface;
|
||||
use Magento\Eav\Api\Data\AttributeInterface;
|
||||
use Magento\Eav\Model\AttributeRepository;
|
||||
use Magento\Eav\Test\Fixture\Attribute;
|
||||
use Magento\GraphQl\PageCache\GraphQLPageCacheAbstract;
|
||||
use Magento\TestFramework\Fixture\Config as ConfigFixture;
|
||||
use Magento\TestFramework\Fixture\DataFixture;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
|
||||
use Magento\PageCache\Model\Config;
|
||||
use Magento\Store\Api\Data\StoreInterface;
|
||||
use Magento\Store\Test\Fixture\Group as StoreGroupFixture;
|
||||
use Magento\Store\Test\Fixture\Store as StoreFixture;
|
||||
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
|
||||
|
||||
/**
|
||||
* Test caching for custom attribute metadata GraphQL query.
|
||||
*/
|
||||
class CustomAttributesMetadataV2CacheTest extends GraphQLPageCacheAbstract
|
||||
{
|
||||
/**
|
||||
* @var AttributeRepository
|
||||
*/
|
||||
private $attributeRepository;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->attributeRepository = Bootstrap::getObjectManager()->get(AttributeRepository::class);
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
#[
|
||||
DataFixture(
|
||||
Attribute::class,
|
||||
[
|
||||
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
|
||||
'frontend_input' => 'text'
|
||||
],
|
||||
'attribute'
|
||||
),
|
||||
ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH)
|
||||
]
|
||||
public function testCacheHitMiss(): void
|
||||
{
|
||||
/** @var AttributeInterface $attribute */
|
||||
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
|
||||
|
||||
$query = $this->getAttributeQuery($attribute->getAttributeCode(), "customer");
|
||||
$response = $this->assertCacheMissAndReturnResponse($query, []);
|
||||
$assertionMap = [
|
||||
['response_field' => 'code', 'expected_value' => $attribute->getAttributeCode()],
|
||||
['response_field' => 'entity_type', 'expected_value' => 'CUSTOMER'],
|
||||
['response_field' => 'frontend_input', 'expected_value' => 'TEXT']
|
||||
];
|
||||
$this->assertResponseFields($response['body']['customAttributeMetadataV2']['items'][0], $assertionMap);
|
||||
$response = $this->assertCacheHitAndReturnResponse($query, []);
|
||||
$this->assertResponseFields($response['body']['customAttributeMetadataV2']['items'][0], $assertionMap);
|
||||
}
|
||||
|
||||
#[
|
||||
ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH),
|
||||
DataFixture(WebsiteFixture::class, as: 'website2'),
|
||||
DataFixture(StoreGroupFixture::class, ['website_id' => '$website2.id$'], 'store_group2'),
|
||||
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group2.id$'], 'store2'),
|
||||
DataFixture(
|
||||
Attribute::class,
|
||||
[
|
||||
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
|
||||
'frontend_input' => 'text'
|
||||
],
|
||||
'attribute'
|
||||
),
|
||||
]
|
||||
public function testCacheMissAndHitDifferentStores(): void
|
||||
{
|
||||
/** @var StoreInterface $store2 */
|
||||
$store2 = DataFixtureStorageManager::getStorage()->get('store2');
|
||||
|
||||
/** @var AttributeMetadataInterface $attribute */
|
||||
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
|
||||
|
||||
$query = $this->getAttributeQuery($attribute->getAttributeCode(), "customer");
|
||||
$response = $this->assertCacheMissAndReturnResponse($query, []);
|
||||
$assertionMap = [
|
||||
['response_field' => 'code', 'expected_value' => $attribute->getAttributeCode()],
|
||||
['response_field' => 'entity_type', 'expected_value' => 'CUSTOMER'],
|
||||
['response_field' => 'frontend_input', 'expected_value' => 'TEXT']
|
||||
];
|
||||
$this->assertResponseFields($response['body']['customAttributeMetadataV2']['items'][0], $assertionMap);
|
||||
$response = $this->assertCacheHitAndReturnResponse($query, []);
|
||||
$this->assertResponseFields($response['body']['customAttributeMetadataV2']['items'][0], $assertionMap);
|
||||
|
||||
// First query execution for a different store should result in a cache miss, while second one should be a hit
|
||||
$response = $this->assertCacheMissAndReturnResponse($query, ['Store' => $store2->getCode()]);
|
||||
$this->assertResponseFields($response['body']['customAttributeMetadataV2']['items'][0], $assertionMap);
|
||||
$response = $this->assertCacheHitAndReturnResponse($query, ['Store' => $store2->getCode()]);
|
||||
$this->assertResponseFields($response['body']['customAttributeMetadataV2']['items'][0], $assertionMap);
|
||||
}
|
||||
|
||||
#[
|
||||
DataFixture(
|
||||
Attribute::class,
|
||||
[
|
||||
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
|
||||
'frontend_input' => 'text'
|
||||
],
|
||||
'attribute_1'
|
||||
),
|
||||
DataFixture(
|
||||
Attribute::class,
|
||||
[
|
||||
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
|
||||
'frontend_input' => 'boolean'
|
||||
],
|
||||
'attribute_2'
|
||||
),
|
||||
ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH)
|
||||
]
|
||||
public function testCacheInvalidation(): void
|
||||
{
|
||||
/** @var AttributeInterface $attribute */
|
||||
$attribute = DataFixtureStorageManager::getStorage()->get('attribute_1');
|
||||
|
||||
/** @var AttributeInterface $attribute2 */
|
||||
$attribute2 = DataFixtureStorageManager::getStorage()->get('attribute_2');
|
||||
|
||||
$query = $this->getAttributeQuery($attribute->getAttributeCode(), "customer");
|
||||
// check cache missed on first query
|
||||
$this->assertCacheMissAndReturnResponse($query, []);
|
||||
// assert cache hit on second query
|
||||
$this->assertCacheHitAndReturnResponse($query, []);
|
||||
|
||||
$attribute->setIsRequired(true);
|
||||
$this->attributeRepository->save($attribute);
|
||||
// assert cache miss after changes
|
||||
$this->assertCacheMissAndReturnResponse($query, []);
|
||||
|
||||
$attribute2->setIsRequired(true);
|
||||
$this->attributeRepository->save($attribute2);
|
||||
|
||||
// assert cache hits on second query after changes, and cache is not invalidated when another entity changed
|
||||
$this->assertCacheHitAndReturnResponse($query, []);
|
||||
}
|
||||
|
||||
#[
|
||||
DataFixture(
|
||||
Attribute::class,
|
||||
[
|
||||
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
|
||||
'frontend_input' => 'text'
|
||||
],
|
||||
'attribute'
|
||||
),
|
||||
ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH)
|
||||
]
|
||||
public function testCacheInvalidationOnAttributeDelete()
|
||||
{
|
||||
/** @var AttributeInterface $attribute */
|
||||
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
|
||||
$attributeCode = $attribute->getAttributeCode();
|
||||
|
||||
$query = $this->getAttributeQuery($attributeCode, "customer");
|
||||
|
||||
// check cache missed on first query
|
||||
$response = $this->assertCacheMissAndReturnResponse($query, []);
|
||||
$assertionMap = [
|
||||
['response_field' => 'code', 'expected_value' => $attributeCode],
|
||||
['response_field' => 'entity_type', 'expected_value' => 'CUSTOMER'],
|
||||
['response_field' => 'frontend_input', 'expected_value' => 'TEXT']
|
||||
];
|
||||
$this->assertResponseFields($response['body']['customAttributeMetadataV2']['items'][0], $assertionMap);
|
||||
|
||||
// assert cache hit on second query
|
||||
$response = $this->assertCacheHitAndReturnResponse($query, []);
|
||||
$this->assertResponseFields($response['body']['customAttributeMetadataV2']['items'][0], $assertionMap);
|
||||
|
||||
$this->attributeRepository->delete($attribute);
|
||||
$assertionMap = [
|
||||
['response_field' => 'type', 'expected_value' => 'ATTRIBUTE_NOT_FOUND'],
|
||||
['response_field' => 'message', 'expected_value' => sprintf(
|
||||
'Attribute code "%s" could not be found.',
|
||||
$attributeCode
|
||||
)]
|
||||
];
|
||||
$response = $this->assertCacheMissAndReturnResponse($query, []);
|
||||
$this->assertResponseFields($response['body']['customAttributeMetadataV2']['errors'][0], $assertionMap);
|
||||
}
|
||||
|
||||
#[
|
||||
DataFixture(
|
||||
Attribute::class,
|
||||
[
|
||||
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
|
||||
'frontend_input' => 'text'
|
||||
],
|
||||
'attribute'
|
||||
),
|
||||
ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH)
|
||||
]
|
||||
public function testCacheMissingAttributeParam(): void
|
||||
{
|
||||
/** @var AttributeInterface $attribute */
|
||||
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
|
||||
|
||||
$query = $this->getAttributeQueryNoCode("customer");
|
||||
// check cache missed on each query
|
||||
$this->assertQueryResultIsCacheMissWithError(
|
||||
$query,
|
||||
"Missing attribute_code for the input entity_type: customer."
|
||||
);
|
||||
$this->assertQueryResultIsCacheMissWithError(
|
||||
$query,
|
||||
"Missing attribute_code for the input entity_type: customer."
|
||||
);
|
||||
|
||||
$query = $this->getAttributeQueryNoEntityType($attribute->getAttributeCode());
|
||||
// check cache missed on each query
|
||||
$this->assertQueryResultIsCacheMissWithError(
|
||||
$query,
|
||||
sprintf("Missing entity_type for the input attribute_code: %s.", $attribute->getAttributeCode())
|
||||
);
|
||||
$this->assertQueryResultIsCacheMissWithError(
|
||||
$query,
|
||||
sprintf("Missing entity_type for the input attribute_code: %s.", $attribute->getAttributeCode())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that query produces an error and the cache is missed.
|
||||
*
|
||||
* @param string $query
|
||||
* @param string $expectedError
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function assertQueryResultIsCacheMissWithError(string $query, string $expectedError)
|
||||
{
|
||||
$caughtException = null;
|
||||
try {
|
||||
// query for response, expect response to be present in exception
|
||||
$this->graphQlQueryWithResponseHeaders($query, []);
|
||||
} catch (ResponseContainsErrorsException $exception) {
|
||||
$caughtException = $exception;
|
||||
}
|
||||
$this->assertInstanceOf(
|
||||
ResponseContainsErrorsException::class,
|
||||
$caughtException
|
||||
);
|
||||
// cannot use expectException because need to assert the headers
|
||||
$this->assertStringContainsString(
|
||||
$expectedError,
|
||||
$caughtException->getMessage()
|
||||
);
|
||||
// assert that it's a miss
|
||||
$this->assertEquals(
|
||||
'MISS',
|
||||
$caughtException->getResponseHeaders()['X-Magento-Cache-Debug']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and return GraphQL query for given entity type and code.
|
||||
*
|
||||
* @param string $code
|
||||
* @param string $entityType
|
||||
* @return string
|
||||
*/
|
||||
private function getAttributeQuery(string $code, string $entityType) : string
|
||||
{
|
||||
return <<<QUERY
|
||||
{
|
||||
customAttributeMetadataV2(attributes: [{attribute_code:"{$code}", entity_type:"{$entityType}"}]) {
|
||||
items {
|
||||
code
|
||||
label
|
||||
entity_type
|
||||
frontend_input
|
||||
is_required
|
||||
default_value
|
||||
is_unique
|
||||
options {
|
||||
label
|
||||
value
|
||||
}
|
||||
}
|
||||
errors {
|
||||
type
|
||||
message
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and return GraphQL query for given entity type with no code.
|
||||
*
|
||||
* @param string $entityType
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getAttributeQueryNoCode(string $entityType) : string
|
||||
{
|
||||
return <<<QUERY
|
||||
{
|
||||
customAttributeMetadata(attributes:
|
||||
[
|
||||
{
|
||||
entity_type:"{$entityType}"
|
||||
}
|
||||
]
|
||||
)
|
||||
{
|
||||
items
|
||||
{
|
||||
attribute_code
|
||||
entity_type
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare and return GraphQL query for given code with no entity type.
|
||||
*
|
||||
* @param string $code
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getAttributeQueryNoEntityType(string $code) : string
|
||||
{
|
||||
return <<<QUERY
|
||||
{
|
||||
customAttributeMetadata(attributes:
|
||||
[
|
||||
{
|
||||
attribute_code:"{$code}"
|
||||
}
|
||||
]
|
||||
)
|
||||
{
|
||||
items
|
||||
{
|
||||
attribute_code
|
||||
entity_type
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user