This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,686 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\GraphQl\PageCache\GraphQLPageCacheAbstract;
use Magento\Eav\Model\AttributeRepository;
use Magento\Framework\ObjectManagerInterface;
use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Test\Fixture\CustomerAttribute;
use Magento\Eav\Api\Data\AttributeInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Fixture\Config as ConfigFixture;
use Magento\TestFramework\Fixture\DataFixture;
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;
use Magento\PageCache\Model\Config;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
/**
* Test caching for attributes form GraphQL query_CUSTOMER_REGISTER_ADDRESS.
*/
class AttributesFormCacheTest extends GraphQLPageCacheAbstract
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var AttributeInterface[]
*/
private $attributesToRemove;
private const QUERY_CUSTOMER_REGISTER_ADDRESS = <<<QRY
{
attributesForm(formCode: "customer_register_address") {
items {
code
}
errors {
type
message
}
}
}
QRY;
private const QUERY_CUSTOMER_EDIT_ADDRESS = <<<QRY
{
attributesForm(formCode: "customer_account_edit") {
items {
code
}
errors {
type
message
}
}
}
QRY;
/**
* @inheritdoc
*/
public function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->attributesToRemove = [];
parent::setUp();
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$eavAttributeRepo = $this->objectManager->get(AttributeRepository::class);
array_walk($this->attributesToRemove, function ($attribute) use ($eavAttributeRepo) {
$eavAttributeRepo->delete($attribute);
});
parent::tearDown();
}
/**
* Obtains cache ID header from response
*
* @param string $query
* @return string
*/
private function getCacheIdHeader(string $query, array $headers = []): string
{
$response = $this->graphQlQueryWithResponseHeaders(
$query,
[],
'',
$headers
);
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
return $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
}
#[
ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH),
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'used_in_forms' => ['customer_register_address']
],
'attribute_1'
)
]
public function testAttributesFormCacheMissAndHit()
{
/** @var AttributeInterface $attribute1 */
$attribute1 = DataFixtureStorageManager::getStorage()->get('attribute_1');
$cacheId = $this->getCacheIdHeader(self::QUERY_CUSTOMER_EDIT_ADDRESS);
/** First response should be a MISS */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
/** Second response should be a HIT and attribute should be present in a cached response */
$response = $this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
foreach ($response['body']['attributesForm']['items'] as $item) {
if (in_array($attribute1->getAttributeCode(), $item)) {
return;
}
}
$this->fail(sprintf(
"Attribute '%s' not found in query_CUSTOMER_REGISTER_ADDRESS response",
$attribute1->getAttributeCode()
));
}
#[
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(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'used_in_forms' => ['customer_register_address']
],
'attribute_1'
)
]
public function testAttributesFormCacheMissAndHitDifferentWebsites()
{
/** @var StoreInterface $store2 */
$store2 = DataFixtureStorageManager::getStorage()->get('store2');
/** @var AttributeInterface $attribute1 */
$attribute1 = DataFixtureStorageManager::getStorage()->get('attribute_1');
$cacheIdStore1 = $this->getCacheIdHeader(self::QUERY_CUSTOMER_EDIT_ADDRESS);
$response = $this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheIdStore1]
);
$this->assertContains($attribute1->getAttributeCode(), array_map(function ($attribute) {
return $attribute['code'];
}, $response['body']['attributesForm']['items']));
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheIdStore1]
);
// obtain CacheID for Store 2 - has to be different than for Store 1:
$cacheIdStore2 = $this->getCacheIdHeader(self::QUERY_CUSTOMER_EDIT_ADDRESS, ['Store' => $store2->getCode()]);
// First query execution for a different store should result in a cache miss, while second one should be a hit
$response = $this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[
'Store' => $store2->getCode(),
CacheIdCalculator::CACHE_ID_HEADER => $cacheIdStore2
]
);
$this->assertContains($attribute1->getAttributeCode(), array_map(function ($attribute) {
return $attribute['code'];
}, $response['body']['attributesForm']['items']));
$response = $this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[
'Store' => $store2->getCode(),
CacheIdCalculator::CACHE_ID_HEADER => $cacheIdStore2
]
);
}
#[
ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH),
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'used_in_forms' => ['customer_register_address']
],
'attribute_1'
)
]
public function testAttributesFormCacheInvalidateOnAttributeEdit()
{
/** @var AttributeInterface $attribute1 */
$attribute1 = DataFixtureStorageManager::getStorage()->get('attribute_1');
$cacheId = $this->getCacheIdHeader(self::QUERY_CUSTOMER_EDIT_ADDRESS);
/** First response should be a MISS */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
/** Second response should be a HIT */
$response = $this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
/** Modify attribute to invalidate cache */
$eavAttributeRepo = $this->objectManager->get(AttributeRepository::class);
$attribute1->setDefaultValue("default_value");
$eavAttributeRepo->save($attribute1);
/** Response after the change should be a MISS */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
/** Second response should be a HIT */
$response = $this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
foreach ($response['body']['attributesForm']['items'] as $item) {
if (in_array($attribute1->getAttributeCode(), $item)) {
return;
}
}
$this->fail(sprintf(
"Attribute '%s' not found in query_CUSTOMER_REGISTER_ADDRESS response",
$attribute1->getAttributeCode()
));
}
#[
ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH)
]
public function testAttributesFormCacheInvalidateOnAttributeCreate()
{
$cacheId = $this->getCacheIdHeader(self::QUERY_CUSTOMER_EDIT_ADDRESS);
/** First response should be a MISS */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
/** Second response should be a HIT */
$response = $this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
/** Create new attribute and assign it to customer_register_address */
$attributeCreate = $this->objectManager->get(CustomerAttribute::class);
$attribute = $attributeCreate->apply([
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'used_in_forms' => ['customer_register_address']
]);
$this->attributesToRemove[] = $attribute;
/** Response after the creation of new attribute should be a MISS */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
/** Second response should be a HIT */
$response = $this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
// verify created attribute is present in result
foreach ($response['body']['attributesForm']['items'] as $item) {
if (in_array($attribute->getAttributeCode(), $item)) {
return;
}
}
$this->fail(sprintf(
"Attribute '%s' not found in QUERY_CUSTOMER_REGISTER_ADDRESS response",
$attribute->getAttributeCode()
));
}
#[
ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH),
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'used_in_forms' => ['customer_register_address']
],
'attribute_1'
)
]
public function testAttributesFormCacheInvalidateOnAttributeDelete()
{
/** @var AttributeInterface $attribute1 */
$attribute1 = DataFixtureStorageManager::getStorage()->get('attribute_1');
$cacheId = $this->getCacheIdHeader(self::QUERY_CUSTOMER_EDIT_ADDRESS);
/** First response should be a MISS */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
/** Second response should be a HIT */
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
/** Delete attribute to invalidate cache */
$eavAttributeRepo = $this->objectManager->get(AttributeRepository::class);
$deletedAttributeCode = $attribute1->getAttributeCode();
$eavAttributeRepo->delete($attribute1);
/** First response should be a MISS and attribute should NOT be present in a cached response */
$response = $this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
foreach ($response['body']['attributesForm']['items'] as $item) {
if (in_array($deletedAttributeCode, $item)) {
$this->fail(sprintf(
"Deleted attribute '%s' found in cached query_CUSTOMER_REGISTER_ADDRESS response",
$deletedAttributeCode
));
}
}
/** Second response should be a HIT */
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
);
}
#[
ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH),
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
],
'attribute'
)
]
public function testAttributesFormCacheInvalidateOnAttributeAssignToForm()
{
/** @var AttributeInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
$eavAttributeRepo = $this->objectManager->get(AttributeRepository::class);
$queryEditCacheId = $this->getCacheIdHeader(self::QUERY_CUSTOMER_EDIT_ADDRESS);
$queryRegisterCacheId = $this->getCacheIdHeader(self::QUERY_CUSTOMER_REGISTER_ADDRESS);
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
/** Second response should be a HIT*/
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
/** Assign $attribute to the 'customer_account_edit' form */
$attribute->setData('used_in_forms', ['customer_account_edit']);
$eavAttributeRepo->save($attribute);
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
/** Non-affected "customer_register_address" form -> MISS, then cached and HIT */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
/** Add $attribute to the 'customer_register_address' form */
$attribute->setData('used_in_forms', ['customer_account_edit', 'customer_register_address']);
$eavAttributeRepo->save($attribute);
/** 'customer_register_address' form should be invalidated first now */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
/** Remove $attribute from the 'customer_account_edit' form */
$attribute->setData('used_in_forms', ['customer_register_address']);
$eavAttributeRepo->save($attribute);
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
/** Remove $attribute from remaining form(s) */
$attribute->setData('used_in_forms', []);
$eavAttributeRepo->save($attribute);
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
}
#[
ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH),
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'used_in_forms' => [
'customer_register_address',
'customer_account_edit'
]
],
'shared_attribute'
)
]
public function testAttributesFormCacheInvalidateOnDeletedSharedAttribute()
{
/** @var AttributeInterface $sharedAttribute */
$sharedAttribute = DataFixtureStorageManager::getStorage()->get('shared_attribute');
$queryEditCacheId = $this->getCacheIdHeader(self::QUERY_CUSTOMER_EDIT_ADDRESS);
$queryRegisterCacheId = $this->getCacheIdHeader(self::QUERY_CUSTOMER_REGISTER_ADDRESS);
/** First response should be a MISS from both queries */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
// /** Second response should be a HIT from both queries */
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
/** Delete attribute to invalidate both cached queries */
$eavAttributeRepo = $this->objectManager->get(AttributeRepository::class);
$deletedAttributeCode = $sharedAttribute->getAttributeCode();
$eavAttributeRepo->delete($sharedAttribute);
/** First response after deleting should be a MISS from both queries */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
/** Second response should be a HIT from both queries as they are both cached back */
$responseRegisterAddress = $this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
$responseEditAddress = $this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
// verify created attribute is NOT present in results
foreach ($responseRegisterAddress['body']['attributesForm']['items'] as $item) {
if (in_array($deletedAttributeCode, $item)) {
$this->fail(
sprintf(
"Attribute '%s' found in QUERY_CUSTOMER_REGISTER_ADDRESS response",
$deletedAttributeCode
)
);
}
}
foreach ($responseEditAddress['body']['attributesForm']['items'] as $item) {
if (in_array($deletedAttributeCode, $item)) {
$this->fail(
sprintf(
"Attribute '%s' found in QUERY_CUSTOMER_EDIT_ADDRESS response",
$deletedAttributeCode
)
);
}
}
}
#[
ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH),
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'used_in_forms' => [
'customer_account_edit'
]
],
'non_shared_attribute_2'
),
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'used_in_forms' => [
'customer_register_address'
]
],
'non_shared_attribute_1'
)
]
public function testAttributesFormCacheInvalidateOnDeletedNonSharedAttribute()
{
/** @var AttributeInterface $nonSharedAttribute1 */
$nonSharedAttribute1 = DataFixtureStorageManager::getStorage()->get('non_shared_attribute_1');
/** @var AttributeInterface $nonSharedAttribute2 */
$nonSharedAttribute2 = DataFixtureStorageManager::getStorage()->get('non_shared_attribute_2');
$queryEditCacheId = $this->getCacheIdHeader(self::QUERY_CUSTOMER_EDIT_ADDRESS);
$queryRegisterCacheId = $this->getCacheIdHeader(self::QUERY_CUSTOMER_REGISTER_ADDRESS);
$eavAttributeRepo = $this->objectManager->get(AttributeRepository::class);
/** First response should be a MISS from both queries */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
/** Second response should be a HIT from all queries */
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
/** Delete nonSharedAttribute1 to invalidate cache of 'customer_register_address' ONLY*/
$eavAttributeRepo->delete($nonSharedAttribute1);
/** First response from QUERY_CUSTOMER_REGISTER_ADDRESS after deleting $nonSharedAttribute1 should be a MISS */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
/** other cached queries should not be affected */
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
/** Second response should be a HIT from all queries */
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
/** Delete nonSharedAttribute2 to invalidate cache of 'customer_account_edit' ONLY*/
$eavAttributeRepo->delete($nonSharedAttribute2);
/** First response from QUERY_CUSTOMER_EDIT_ADDRESS after deleting $nonSharedAttribute2 should be a MISS */
$this->assertCacheMissAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
/** other cached queries should not be affected */
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
/** Second response should be a HIT from all queries */
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_REGISTER_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryRegisterCacheId]
);
$this->assertCacheHitAndReturnResponse(
self::QUERY_CUSTOMER_EDIT_ADDRESS,
[CacheIdCalculator::CACHE_ID_HEADER => $queryEditCacheId]
);
}
}
@@ -0,0 +1,183 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Test\Fixture\CustomerAttribute;
use Magento\Eav\Api\Data\AttributeInterface;
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;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test customer EAV form attributes metadata retrieval via GraphQL API
*/
class AttributesFormTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
attributesForm(formCode: "%s") {
items {
code
label
entity_type
frontend_input
is_required
default_value
is_unique
}
errors {
type
message
}
}
}
QRY;
#[
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'used_in_forms' => ['customer_register_address']
],
'attribute_1'
),
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'used_in_forms' => ['customer_address_edit']
],
'attribute_2'
)
]
public function testAttributesForm(): void
{
/** @var AttributeInterface $attribute1 */
$attribute1 = DataFixtureStorageManager::getStorage()->get('attribute_1');
/** @var AttributeInterface $attribute2 */
$attribute2 = DataFixtureStorageManager::getStorage()->get('attribute_2');
$result = $this->graphQlQuery(sprintf(self::QUERY, 'customer_register_address'));
$this->assertNotEmpty($result['attributesForm']['items']);
$codes = $this->getAttributeCodes($result['attributesForm']['items']);
$this->assertContains($attribute1->getAttributeCode(), $codes);
$this->assertContains('country_id', $codes);
$this->assertContains('region_id', $codes);
$this->assertNotContains($attribute2->getAttributeCode(), $codes);
}
public function testAttributesFormAdminHtmlForm(): void
{
$this->assertEquals(
[
'attributesForm' => [
'items' => [],
'errors' => [
[
'type' => 'ENTITY_NOT_FOUND',
'message' => 'Form "adminhtml_customer" could not be found.'
]
]
]
],
$this->graphQlQuery(sprintf(self::QUERY, 'adminhtml_customer'))
);
}
public function testAttributesFormDoesNotExist(): void
{
$this->assertEquals(
[
'attributesForm' => [
'items' => [],
'errors' => [
[
'type' => 'ENTITY_NOT_FOUND',
'message' => 'Form "not_existing_form" could not be found.'
]
]
]
],
$this->graphQlQuery(sprintf(self::QUERY, 'not_existing_form'))
);
}
#[
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(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'used_in_forms' => ['customer_register_address'],
'website_id' => '$website2.id$',
'scope_is_visible' => 1,
'is_visible' => 0,
],
'attribute_1'
),
]
public function testAttributesFormScope(): void
{
/** @var AttributeInterface $attribute1 */
$attribute1 = DataFixtureStorageManager::getStorage()->get('attribute_1');
$result = $this->graphQlQuery(sprintf(self::QUERY, 'customer_register_address'));
$this->assertNotEmpty($result['attributesForm']['items']);
$codes = $this->getAttributeCodes($result['attributesForm']['items']);
$this->assertNotContains($attribute1->getAttributeCode(), $codes);
/** @var StoreInterface $store */
$store = DataFixtureStorageManager::getStorage()->get('store2');
$result = $this->graphQlQuery(
sprintf(self::QUERY, 'customer_register_address'),
[],
'',
['Store' => $store->getCode()]
);
$this->assertNotEmpty($result['attributesForm']['items']);
$codes = $this->getAttributeCodes($result['attributesForm']['items']);
$this->assertContains(
$attribute1->getAttributeCode(),
$codes,
sprintf(
"Attribute '%s' not found in query response in website scope",
$attribute1->getAttributeCode()
)
);
}
/**
* Retrieve an array of attribute codes based on an array of attributes data
*
* @param array $attributes
* @return array
*/
private function getAttributeCodes(array $attributes): array
{
return array_map(
function (array $attribute) {
return $attribute['code'];
},
$attributes
);
}
}
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\Data\AttributeMetadataInterface;
use Magento\Customer\Test\Fixture\CustomerAttribute;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test catalog EAV attributes metadata retrieval via GraphQL API
*/
class BooleanTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
customAttributeMetadataV2(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
code
label
entity_type
frontend_input
is_required
default_value
is_unique
options {
label
value
}
}
errors {
type
message
}
}
}
QRY;
#[
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'frontend_input' => 'boolean',
'source_model' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean'
],
'attribute'
),
]
public function testMetadata(): void
{
/** @var AttributeMetadataInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getFrontendLabel(),
'entity_type' => 'CUSTOMER',
'frontend_input' => 'BOOLEAN',
'is_required' => false,
'default_value' => $attribute->getDefaultValue(),
'is_unique' => false,
'options' => [
[
'label' => 'Yes',
'value' => '1'
],
[
'label' => 'No',
'value' => '0'
]
]
]
],
'errors' => []
]
],
$result
);
}
}
@@ -0,0 +1,102 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Api\Data\AttributeMetadataInterface;
use Magento\Customer\Test\Fixture\CustomerAttribute;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test catalog EAV attributes metadata retrieval via GraphQL API
*/
class CustomerAddressAttributesTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
customAttributeMetadataV2(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
code
label
entity_type
frontend_input
frontend_class
is_required
default_value
is_unique
... on CustomerAttributeMetadata {
input_filter
validate_rules {
name
value
}
}
}
errors {
type
message
}
}
}
QRY;
#[
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'frontend_input' => 'date',
'frontend_class' => 'hidden-for-virtual',
'default_value' => '2023-03-22 00:00:00',
'input_filter' => 'DATE',
'validate_rules' =>
'{"DATE_RANGE_MIN":"1679443200","DATE_RANGE_MAX":"1679875200","INPUT_VALIDATION":"DATE"}'
],
'attribute'
),
]
public function testMetadata(): void
{
/** @var AttributeMetadataInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
$formattedValidationRules = Bootstrap::getObjectManager()->get(FormatValidationRulesCommand::class)->execute(
$attribute->getValidationRules()
);
$result = $this->graphQlQuery(
sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer_address')
);
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getFrontendLabel(),
'entity_type' => 'CUSTOMER_ADDRESS',
'frontend_input' => 'DATE',
'frontend_class' => 'hidden-for-virtual',
'is_required' => false,
'default_value' => $attribute->getDefaultValue(),
'is_unique' => false,
'input_filter' => $attribute->getInputFilter(),
'validate_rules' => $formattedValidationRules
]
],
'errors' => []
]
],
$result
);
}
}
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\Data\AttributeMetadataInterface;
use Magento\Customer\Test\Fixture\CustomerAttribute;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test catalog EAV attributes metadata retrieval via GraphQL API
*/
class DateTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
customAttributeMetadataV2(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
code
label
entity_type
frontend_input
is_required
default_value
is_unique
... on CustomerAttributeMetadata {
input_filter
validate_rules {
name
value
}
}
}
errors {
type
message
}
}
}
QRY;
#[
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'frontend_input' => 'date',
'default_value' => '2023-03-22 00:00:00',
'input_filter' => 'DATE',
'validate_rules' => '
{"DATE_RANGE_MIN":"1679443200","DATE_RANGE_MAX":"1679875200","INPUT_VALIDATION":"DATE"}
'
],
'attribute'
),
]
public function testMetadata(): void
{
/** @var AttributeMetadataInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
$formattedValidationRules = Bootstrap::getObjectManager()->get(FormatValidationRulesCommand::class)->execute(
$attribute->getValidationRules()
);
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getFrontendLabel(),
'entity_type' => 'CUSTOMER',
'frontend_input' => 'DATE',
'is_required' => false,
'default_value' => $attribute->getDefaultValue(),
'is_unique' => false,
'input_filter' => $attribute->getInputFilter(),
'validate_rules' => $formattedValidationRules
]
],
'errors' => []
]
],
$result
);
}
}
@@ -0,0 +1,92 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\Data\AttributeMetadataInterface;
use Magento\Customer\Test\Fixture\CustomerAttribute;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test catalog EAV attributes metadata retrieval via GraphQL API
*/
class FileTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
customAttributeMetadataV2(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
code
label
entity_type
frontend_input
is_required
default_value
is_unique
... on CustomerAttributeMetadata {
validate_rules {
name
value
}
}
}
errors {
type
message
}
}
}
QRY;
#[
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'frontend_input' => 'file',
'validate_rules' => '{"MAX_FILE_SIZE":"10000000","FILE_EXTENSIONS":"PDF"}'
],
'attribute'
)
]
public function testMetadata(): void
{
/** @var AttributeMetadataInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
$formattedValidationRules = Bootstrap::getObjectManager()->get(FormatValidationRulesCommand::class)->execute(
$attribute->getValidationRules()
);
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getFrontendLabel(),
'entity_type' => 'CUSTOMER',
'frontend_input' => 'FILE',
'is_required' => false,
'default_value' => $attribute->getDefaultValue(),
'is_unique' => false,
'validate_rules' => $formattedValidationRules
]
],
'errors' => []
]
],
$result
);
}
}
@@ -0,0 +1,29 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
/**
* Command to format validation rules appropriately for testing purposes.
*/
class FormatValidationRulesCommand
{
/**
* Execute command to format validation rules provided as argument.
*
* @param array $validationRules
* @return array
*/
public function execute(array $validationRules): array
{
$formattedValidationRules = [];
foreach ($validationRules as $key => $value) {
$formattedValidationRules[] = ['name' => $key, 'value' => $value];
}
return $formattedValidationRules;
}
}
@@ -0,0 +1,92 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\Data\AttributeMetadataInterface;
use Magento\Customer\Test\Fixture\CustomerAttribute;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test catalog EAV attributes metadata retrieval via GraphQL API
*/
class ImageTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
customAttributeMetadataV2(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
code
label
entity_type
frontend_input
is_required
default_value
is_unique
... on CustomerAttributeMetadata {
validate_rules {
name
value
}
}
}
errors {
type
message
}
}
}
QRY;
#[
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'frontend_input' => 'image',
'validate_rules' => '{"MAX_FILE_SIZE":"10000","MAX_IMAGE_WIDTH":"100"}'
],
'attribute'
)
]
public function testMetadata(): void
{
/** @var AttributeMetadataInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
$formattedValidationRules = Bootstrap::getObjectManager()->get(FormatValidationRulesCommand::class)->execute(
$attribute->getValidationRules()
);
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getFrontendLabel(),
'entity_type' => 'CUSTOMER',
'frontend_input' => 'IMAGE',
'is_required' => false,
'default_value' => $attribute->getDefaultValue(),
'is_unique' => false,
'validate_rules' => $formattedValidationRules
]
],
'errors' => []
]
],
$result
);
}
}
@@ -0,0 +1,103 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\Data\AttributeMetadataInterface;
use Magento\Customer\Test\Fixture\CustomerAttribute;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test catalog EAV attributes metadata retrieval via GraphQL API
*/
class MultilineTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
customAttributeMetadataV2(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
code
label
entity_type
frontend_input
is_required
default_value
is_unique
... on CustomerAttributeMetadata {
input_filter
multiline_count
sort_order
validate_rules {
name
value
}
}
}
errors {
type
message
}
}
}
QRY;
#[
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'frontend_input' => 'multiline',
'default_value' => 'this is line one
this is line two',
'input_filter' => 'STRIPTAGS',
'multiline_count' => 2,
'sort_order' => 3,
'validate_rules' => '{"MIN_TEXT_LENGTH":"100","MAX_TEXT_LENGTH":"200","INPUT_VALIDATION":"EMAIL"}',
],
'attribute'
)
]
public function testMetadata(): void
{
/** @var AttributeMetadataInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
$formattedValidationRules = Bootstrap::getObjectManager()->get(FormatValidationRulesCommand::class)->execute(
$attribute->getValidationRules()
);
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getFrontendLabel(),
'entity_type' => 'CUSTOMER',
'frontend_input' => 'MULTILINE',
'is_required' => false,
'default_value' => $attribute->getDefaultValue(),
'is_unique' => false,
'input_filter' => $attribute->getInputFilter(),
'multiline_count' => $attribute->getMultilineCount(),
'sort_order' => $attribute->getSortOrder(),
'validate_rules' => $formattedValidationRules
]
],
'errors' => []
]
],
$result
);
}
}
@@ -0,0 +1,134 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Eav\Api\Data\AttributeOptionInterface;
use Magento\Eav\Model\Entity\Attribute\Source\Table;
use Magento\Eav\Test\Fixture\Attribute;
use Magento\Eav\Test\Fixture\AttributeOption;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test catalog EAV attributes metadata retrieval via GraphQL API
*/
class MultiselectTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
customAttributeMetadataV2(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
code
default_value
options {
label
value
is_default
}
}
errors {
type
message
}
}
}
QRY;
#[
DataFixture(
Attribute::class,
[
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'frontend_input' => 'multiselect',
'source_model' => Table::class
],
'attribute'
),
DataFixture(
AttributeOption::class,
[
'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'attribute_code' => '$attribute.attribute_code$',
'sort_order' => 10
],
'option1'
),
DataFixture(
AttributeOption::class,
[
'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'attribute_code' => '$attribute.attribute_code$',
'sort_order' => 20,
'is_default' => true
],
'option2'
),
DataFixture(
AttributeOption::class,
[
'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'attribute_code' => '$attribute.attribute_code$',
'sort_order' => 30,
'is_default' => true
],
'option3'
),
]
public function testMetadata(): void
{
/** @var AttributeInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
/** @var AttributeOptionInterface $option1 */
$option1 = DataFixtureStorageManager::getStorage()->get('option1');
/** @var AttributeOptionInterface $option2 */
$option2 = DataFixtureStorageManager::getStorage()->get('option2');
/** @var AttributeOptionInterface $option3 */
$option3 = DataFixtureStorageManager::getStorage()->get('option3');
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'default_value' => $option3->getValue() . ',' . $option2->getValue(),
'options' => [
$this->getOptionData($option1),
$this->getOptionData($option2),
$this->getOptionData($option3)
]
]
],
'errors' => []
]
],
$result
);
$this->assertEquals($option2->getIsDefault(), true);
$this->assertEquals($option3->getIsDefault(), true);
}
/**
* @param AttributeOptionInterface $option
* @return array
*/
private function getOptionData(AttributeOptionInterface $option): array
{
return [
'label' => $option->getLabel(),
'value' => $option->getValue(),
'is_default' => $option->getIsDefault()
];
}
}
@@ -0,0 +1,99 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Eav\Api\Data\AttributeOptionInterface;
use Magento\Eav\Test\Fixture\Attribute;
use Magento\Eav\Test\Fixture\AttributeOption;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test catalog EAV attributes metadata retrieval via GraphQL API
*/
class SelectTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
customAttributeMetadataV2(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
code
options {
label
value
}
}
errors {
type
message
}
}
}
QRY;
#[
DataFixture(
Attribute::class,
[
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'frontend_input' => 'select'
],
'attribute'
),
DataFixture(
AttributeOption::class,
[
'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'attribute_code' => '$attribute.attribute_code$',
'sort_order' => 10
],
'option1'
),
DataFixture(
AttributeOption::class,
[
'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'attribute_code' => '$attribute.attribute_code$',
'sort_order' => 20
],
'option2'
),
]
public function testMetadata(): void
{
/** @var AttributeInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
/** @var AttributeOptionInterface $option1 */
$option1 = DataFixtureStorageManager::getStorage()->get('option1');
/** @var AttributeOptionInterface $option2 */
$option2 = DataFixtureStorageManager::getStorage()->get('option2');
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'options' => [
$option1->getData(),
$option2->getData()
]
]
],
'errors' => []
]
],
$result
);
}
}
@@ -0,0 +1,252 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Eav\Api\Data\AttributeOptionInterface;
use Magento\Eav\Test\Fixture\Attribute;
use Magento\Eav\Test\Fixture\AttributeOption;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Test\Fixture\Store;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test customer EAV attribute options retrieval via GraphQL API
*/
#[
DataFixture(Store::class, as: 'store_1'),
DataFixture(Store::class, as: 'store_2'),
DataFixture(
Attribute::class,
[
'frontend_labels' => [
[
'store_id' => 0,
'label' => 'height'
],
[
'store_id' => '$store_1.id$',
'label' => 'hair'
],
[
'store_id' => '$store_2.id$',
'label' => 'eyes'
],
],
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'frontend_input' => 'select'
],
'attribute'
),
DataFixture(
AttributeOption::class,
[
'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'attribute_code' => '$attribute.attribute_code$',
'sort_order' => 10,
'store_labels' => [
[
'store_id' => 0,
'label' => 'tall'
],
[
'store_id' => '$store_1.id$',
'label' => 'red'
],
[
'store_id' => '$store_2.id$',
'label' => 'green'
]
],
],
'option1'
),
DataFixture(
AttributeOption::class,
[
'entity_type' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'attribute_code' => '$attribute.attribute_code$',
'sort_order' => 20,
'store_labels' => [
[
'store_id' => 0,
'label' => 'short'
],
[
'store_id' => '$store_1.id$',
'label' => 'brown'
],
[
'store_id' => '$store_2.id$',
'label' => 'blue'
]
],
],
'option2'
),
]
class StoreViewOptionsTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
customAttributeMetadataV2(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
code
label
entity_type
frontend_input
is_required
default_value
is_unique
options {
label
}
}
errors {
type
message
}
}
}
QRY;
public function testAttributeLabelsNoStoreViews(): void
{
/** @var AttributeInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
/** @var AttributeOptionInterface $option1 */
$option1 = DataFixtureStorageManager::getStorage()->get('option1');
/** @var AttributeOptionInterface $option2 */
$option2 = DataFixtureStorageManager::getStorage()->get('option2');
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getDefaultFrontendLabel(),
'entity_type' => 'CUSTOMER',
'frontend_input' => 'SELECT',
'is_required' => false,
'default_value' => '',
'is_unique' => false,
'options' => [
[
'label' => $option1->getLabel()
],
[
'label' => $option2->getLabel()
],
]
]
],
'errors' => []
]
],
$this->graphQlQuery(
sprintf(
self::QUERY,
$attribute->getAttributeCode(),
'customer'
)
)
);
}
public function testAttributeLabelsMultipleStoreViews(): void
{
/** @var AttributeInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
/** @var StoreInterface $store1 */
$store1 = DataFixtureStorageManager::getStorage()->get('store_1');
/** @var StoreInterface $store2 */
$store2 = DataFixtureStorageManager::getStorage()->get('store_2');
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getStoreLabel($store1->getId()),
'entity_type' => 'CUSTOMER',
'frontend_input' => 'SELECT',
'is_required' => false,
'default_value' => '',
'is_unique' => false,
'options' => [
[
'label' => 'red'
],
[
'label' => 'brown'
],
]
]
],
'errors' => []
]
],
$this->graphQlQuery(
sprintf(
self::QUERY,
$attribute->getAttributeCode(),
'customer'
),
[],
'',
['Store' => $store1->getCode()]
)
);
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getStoreLabel($store2->getId()),
'entity_type' => 'CUSTOMER',
'frontend_input' => 'SELECT',
'is_required' => false,
'default_value' => '',
'is_unique' => false,
'options' => [
[
'label' => 'green'
],
[
'label' => 'blue'
],
]
]
],
'errors' => []
]
],
$this->graphQlQuery(
sprintf(
self::QUERY,
$attribute->getAttributeCode(),
'customer'
),
[],
'',
['Store' => $store2->getCode()]
)
);
}
}
@@ -0,0 +1,124 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Eav\Test\Fixture\Attribute;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test catalog EAV attributes metadata retrieval via GraphQL API
*/
class TextTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
customAttributeMetadataV2(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
code
label
entity_type
frontend_input
is_required
default_value
is_unique
}
errors {
type
message
}
}
}
QRY;
#[
DataFixture(
Attribute::class,
[
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER
],
'attribute'
)
]
public function testTextField(): void
{
/** @var AttributeInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getDefaultFrontendLabel(),
'entity_type' => 'CUSTOMER',
'frontend_input' => 'TEXT',
'is_required' => false,
'default_value' => $attribute->getDefaultValue(),
'is_unique' => false
]
],
'errors' => []
]
],
$this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'))
);
}
public function testErrorEntityNotFound(): void
{
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [],
'errors' => [
[
'type' => 'ENTITY_NOT_FOUND',
'message' => 'Entity "non_existing_entity_type" could not be found.'
]
]
]
],
$this->graphQlQuery(
sprintf(
self::QUERY,
'lastname',
'non_existing_entity_type'
)
)
);
}
public function testErrorAttributeNotFound(): void
{
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [],
'errors' => [
[
'type' => 'ATTRIBUTE_NOT_FOUND',
'message' => 'Attribute code "non_existing_code" could not be found.'
]
]
]
],
$this->graphQlQuery(
sprintf(
self::QUERY,
'non_existing_code',
'customer'
)
)
);
}
}
@@ -0,0 +1,97 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\Customer\Attribute;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\Data\AttributeMetadataInterface;
use Magento\Customer\Test\Fixture\CustomerAttribute;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test catalog EAV attributes metadata retrieval via GraphQL API
*/
class TextareaTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
customAttributeMetadataV2(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
code
label
entity_type
frontend_input
is_required
default_value
is_unique
... on CustomerAttributeMetadata {
input_filter
sort_order
}
}
errors {
type
message
}
}
}
QRY;
#[
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'frontend_input' => 'textarea',
'default_value' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus bibendum finibus' .
'quam, at vulputate quam feugiat tincidunt. Pellentesque venenatis nunc eget dolor' .
'dictum, vel ultricies orci facilisis. Sed hendrerit arcu tristique dui molestie, ' .
'sit amet scelerisque nibh scelerisque. Nulla sed tellus eget tellus volutpat ' .
'vestibulum. Mauris molestie erat sed odio maximus accumsan. Morbi velit felis, ' .
'tristique et lectus sollicitudin, laoreet aliquam nisl. Suspendisse vel ante at ' .
'metus mattis ultrices non nec libero. Cras odio nunc, eleifend vitae interdum a, ' .
'porttitor a dolor. Praesent mi odio, hendrerit quis consequat nec, vestibulum ' .
'vitae justo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin auctor' .
'ac quam id rhoncus. Proin vel orci eu justo cursus vestibulum.',
'input_filter' => 'ESCAPEHTML',
'sort_order' => 4,
],
'attribute'
)
]
public function testMetadata(): void
{
/** @var AttributeMetadataInterface $attribute */
$attribute = DataFixtureStorageManager::getStorage()->get('attribute');
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
$this->assertEquals(
[
'customAttributeMetadataV2' => [
'items' => [
[
'code' => $attribute->getAttributeCode(),
'label' => $attribute->getFrontendLabel(),
'entity_type' => 'CUSTOMER',
'frontend_input' => 'TEXTAREA',
'is_required' => false,
'default_value' => $attribute->getDefaultValue(),
'is_unique' => false,
'input_filter' => $attribute->getInputFilter(),
'sort_order' => $attribute->getSortOrder(),
]
],
'errors' => []
]
],
$result
);
}
}