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,135 @@
<?php
/**
* Copyright 2021 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento\GraphQl\GraphQlCache\CacheIdFactorProviders\Customer;
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Group;
use Magento\Customer\Test\Fixture\Customer as CustomerFixture;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\Exception\EmailNotConfirmedException;
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorage;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test class for customerGroup CacheIdFactorProvider.
*/
class CustomerGroupProviderTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var Group
*/
private $customerGroup;
/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;
/**
* @var DataFixtureStorageManager
*/
private $fixtures;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->customerGroup = $objectManager->get(Group::class);
$this->customerRepository = $objectManager->get(CustomerRepositoryInterface::class);
$this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage();
}
/**
* Tests that cache id header changes based on customer group and remains consistent for same customer group
*/
#[
DataFixture(ProductFixture::class, as: 'product'),
DataFixture(CustomerFixture::class, as: 'customer'),
]
public function testCacheIdHeaderWithCustomerGroup()
{
$customerEmail = $this->fixtures->get('customer')->getEmail();
$query = <<<QUERY
{
products(filter: {sku: {eq: "{$this->fixtures->get('product')->getSku()}"}})
{
items {
id
name
sku
description {
html
}
}
}
}
QUERY;
$response = $this->graphQlQueryWithResponseHeaders($query, [], '', $this->getHeaderMap($customerEmail));
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
$cacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
$this->assertTrue((boolean)preg_match('/^[0-9a-f]{64}$/i', $cacheId));
$groupCode = 'Retailer';
$customer = $this->customerRepository->get($customerEmail);
$customerGroupId = $this->customerGroup->load($groupCode, 'customer_group_code')->getId();
// change the customer group of this customer from the default group
$customer->setGroupId($customerGroupId);
$this->customerRepository->save($customer);
$responseAfterCustomerGroupChange = $this->graphQlQueryWithResponseHeaders(
$query,
[],
'',
$this->getHeaderMap($customerEmail)
);
$cacheIdCustomerGroupChange = $responseAfterCustomerGroupChange['headers'][CacheIdCalculator::CACHE_ID_HEADER];
// Verify that the cache id generated is a 64 character long
$this->assertTrue((boolean)preg_match('/^[0-9a-f]{64}$/i', $cacheId));
// check that the cache ids generated before and after customer group changes are not equal
$this->assertNotEquals($cacheId, $cacheIdCustomerGroupChange);
//Change the customer groupId back to Default General
$customer->setGroupId(1);
$this->customerRepository->save($customer);
$responseDefaultCustomerGroup = $this->graphQlQueryWithResponseHeaders(
$query,
[],
'',
$this->getHeaderMap($customerEmail)
);
//Verify that the cache id is same as original $cacheId
$this->assertEquals(
$responseDefaultCustomerGroup['headers'][CacheIdCalculator::CACHE_ID_HEADER],
$cacheId
);
}
/**
* Authentication header map
*
* @param string $username
* @return array
*
* @throws AuthenticationException
* @throws EmailNotConfirmedException
*/
private function getHeaderMap(string $username): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, 'password');
return ['Authorization' => 'Bearer ' . $customerToken];
}
}
@@ -0,0 +1,122 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\GraphQlCache\CacheIdFactorProviders\Customer;
use Magento\Framework\Exception\AuthenticationException;
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Tax\Model\Calculation\Rate;
use Magento\Tax\Model\Calculation\RateRepository;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test class for tax rate CacheIdFactorProvider.
*/
class CustomerTaxRateProviderTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
/**
* @var Rate
*/
private $taxRate;
/**
* @var RateRepository
*/
private $taxRateRepository;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
$this->taxRate = $objectManager->get(Rate::class);
$this->taxRateRepository = $objectManager->get(RateRepository::class);
}
/**
* Tests that cache id header changes based on the customer tax rate and remains consistent for the
* same customer tax rate
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Customer/_files/customer_shipping_address_36104.php
* @magentoApiDataFixture Magento/Tax/_files/tax_rule_region_1_al.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
*/
public function testCacheIdHeaderWithCustomerTaxRate()
{
$query = <<<QUERY
{
products( filter:{})
{
total_count
items {
name
sku
price_range {
minimum_price {
final_price {value }
regular_price {value }
}
maximum_price{
final_price{ value }
regular_price{ value } }
}
}
}
}
QUERY;
$response = $this->graphQlQueryWithResponseHeaders($query, [], '', $this->getHeaderMap());
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
$cacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
$this->assertTrue((boolean)preg_match('/^[0-9a-f]{64}$/i', $cacheId));
//Change tax rate to different value
/** @var Rate $rate */
$rate = $this->taxRate->loadByCode('US-AL-*-Rate-1');
$rate->setRate(10);
$this->taxRateRepository->save($rate);
$responseAfterTaxRateChange = $this->graphQlQueryWithResponseHeaders($query, [], '', $this->getHeaderMap());
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $responseAfterTaxRateChange['headers']);
$cacheIdTaxRateChange = $responseAfterTaxRateChange['headers'][CacheIdCalculator::CACHE_ID_HEADER];
// Verify that the the cache id generated is a 64 character long
$this->assertTrue((boolean)preg_match('/^[0-9a-f]{64}$/i', $cacheId));
// check that the cache ids generated before and after tax rate changes are not equal
$this->assertNotEquals($cacheId, $cacheIdTaxRateChange);
//Change the tax rate back to original 7.5
/** @var Rate $rate */
$rate = $this->taxRate->loadByCode('US-AL-*-Rate-1');
$rate->setRate(7.5);
$this->taxRateRepository->save($rate);
$responseOriginalTaxRate = $this->graphQlQueryWithResponseHeaders($query, [], '', $this->getHeaderMap());
$cacheIdOriginalTaxRate = $responseOriginalTaxRate['headers'][CacheIdCalculator::CACHE_ID_HEADER];
//Verify that the cache id is same as original $cacheId
$this->assertEquals($cacheIdOriginalTaxRate, $cacheId);
}
/**
* Authentication header map
*
* @param string $username
* @param string $password
*
* @return array
*
* @throws AuthenticationException
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
return ['Authorization' => 'Bearer ' . $customerToken];
}
}
@@ -0,0 +1,145 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQl\GraphQlCache\CacheIdFactorProviders\Customer;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test class for IsLoggedIn CacheIdFactorProvider.
*/
class IsLoggedInProviderTest extends GraphQlAbstract
{
/**
* Tests cache is not generated for generateToken mutation and other post requests
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
*/
public function testCacheIdHeaderWithIsLoggedIn()
{
$email = 'customer@example.com';
$password = 'password';
$generateToken = <<<MUTATION
mutation{
generateCustomerToken
(
email:"{$email}",
password:"{$password}"
)
{
token
}
}
MUTATION;
$tokenResponse = $this->graphQlMutationWithResponseHeaders($generateToken);
// Verify that the cache is not generated for generate token mutation
$this->assertEquals('no-cache', $tokenResponse['headers']['Pragma']);
$this->assertEquals(
'no-store, no-cache, must-revalidate, max-age=0',
$tokenResponse['headers']['Cache-Control']
);
$this->assertArrayHasKey('generateCustomerToken', $tokenResponse['body']);
$customerToken = $tokenResponse['body']['generateCustomerToken']['token'];
$createEmptyCart = <<<MUTATION
mutation{createEmptyCart}
MUTATION;
$createCustomerCartResponse = $this->graphQlMutationWithResponseHeaders(
$createEmptyCart,
[],
'',
['Authorization' => 'Bearer ' . $customerToken]
);
//Verify that the cache is not generated for authorized mutation like createEmptyCart
$this->assertEquals('no-cache', $createCustomerCartResponse['headers']['Pragma']);
$this->assertEquals(
'no-store, no-cache, must-revalidate, max-age=0',
$createCustomerCartResponse['headers']['Cache-Control']
);
$cartId = $createCustomerCartResponse['body']['createEmptyCart'];
$createGuestCartResponse = $this->graphQlMutationWithResponseHeaders($createEmptyCart);
//Verify that cache is not generated for unauthorized post requests
$this->assertEquals('no-cache', $createGuestCartResponse['headers']['Pragma']);
$this->assertEquals(
'no-store, no-cache, must-revalidate, max-age=0',
$createGuestCartResponse['headers']['Cache-Control']
);
$addProductToCustomerCart = <<<MUTATION
mutation{
addSimpleProductsToCart
(input:{cart_id:"{$cartId}"
cart_items:{
data:{
quantity:2
sku:"simple_product"
} } } )
{
cart{ items{quantity product{sku}}}}}
MUTATION;
$addProductToCustomerCartResponse = $this->graphQlMutationWithResponseHeaders(
$addProductToCustomerCart,
[],
'',
['Authorization' => 'Bearer ' . $customerToken]
);
//Verify that cache is not generated for addSimpleProductsToCart mutation
$this->assertEquals('no-cache', $addProductToCustomerCartResponse['headers']['Pragma']);
$this->assertEquals(
'no-store, no-cache, must-revalidate, max-age=0',
$addProductToCustomerCartResponse['headers']['Cache-Control']
);
}
/**
* Tests that cache id header resets to the one for guest when a customer token is revoked
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
*/
public function testCacheIdHeaderAfterRevokeToken()
{
// Get the guest headers
$guestCartResponse = $this->graphQlMutationWithResponseHeaders('mutation{createEmptyCart}');
$this->assertEquals('no-cache', $guestCartResponse['headers']['Pragma']);
$this->assertEquals(
'no-store, no-cache, must-revalidate, max-age=0',
$guestCartResponse['headers']['Cache-Control']
);
// Get the customer token to send to the revoke mutation
$generateToken = <<<MUTATION
mutation{
generateCustomerToken(email:"customer@example.com", password:"password")
{token}
}
MUTATION;
$tokenResponse = $this->graphQlMutationWithResponseHeaders($generateToken);
$this->assertArrayHasKey('generateCustomerToken', $tokenResponse['body']);
$customerToken = $tokenResponse['body']['generateCustomerToken']['token'];
$this->assertEquals('no-cache', $tokenResponse['headers']['Pragma']);
$this->assertEquals(
'no-store, no-cache, must-revalidate, max-age=0',
$tokenResponse['headers']['Cache-Control']
);
// Revoke the token and check that cache is not generated
$revokeCustomerToken = "mutation{revokeCustomerToken{result}}";
$revokeResponse = $this->graphQlMutationWithResponseHeaders(
$revokeCustomerToken,
[],
'',
['Authorization' => 'Bearer ' . $customerToken]
);
$this->assertEquals('no-cache', $revokeResponse['headers']['Pragma']);
$this->assertEquals(
'no-store, no-cache, must-revalidate, max-age=0',
$revokeResponse['headers']['Cache-Control']
);
}
}
@@ -0,0 +1,42 @@
<?php
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GraphQl\GraphQlCache\CacheIdFactorProviders\Store;
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test class for currency CacheIdFactorProvider.
*/
class CurrencyProviderTest extends GraphQlAbstract
{
/**
* Tests that the cache id header changes based on the currency and remains consistent for the same currency.
*
* @magentoApiDataFixture Magento/Store/_files/multiple_currencies.php
*/
public function testCacheIdHeaderWithCurrency()
{
$query = 'query { products( filter: {sku: {eq: "simple1"}}) { items { name }}}';
$currency1Response = $this->graphQlQueryWithResponseHeaders($query, [], '', ['content-currency' => 'USD']);
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $currency1Response['headers']);
$currency1CacheId = $currency1Response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
$this->assertTrue((boolean)preg_match('/^[0-9a-f]{64}$/i', $currency1CacheId));
$currency2Response = $this->graphQlQueryWithResponseHeaders($query, [], '', ['content-currency' => 'EUR']);
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $currency2Response['headers']);
$currency2CacheId = $currency2Response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
$this->assertTrue((boolean)preg_match('/^[0-9a-f]{64}$/i', $currency2CacheId));
// Assert that currency1 and currency2 return different cache ids
$this->assertNotEquals($currency1CacheId, $currency2CacheId);
// Assert that currency1 returns the same cache id as before
$currency1Response = $this->graphQlQueryWithResponseHeaders($query, [], '', ['content-currency' => 'USD']);
$this->assertEquals($currency1CacheId, $currency1Response['headers'][CacheIdCalculator::CACHE_ID_HEADER]);
}
}
@@ -0,0 +1,42 @@
<?php
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GraphQl\GraphQlCache\CacheIdFactorProviders\Store;
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
use Magento\TestFramework\TestCase\GraphQlAbstract;
/**
* Test class for store CacheIdFactorProvider.
*/
class StoreProviderTest extends GraphQlAbstract
{
/**
* Tests that the cache id header changes based on the store and remains consistent for the same store.
*
* @magentoApiDataFixture Magento/Store/_files/second_store.php
*/
public function testCacheIdHeaderWithStore()
{
$query = 'query { products( filter: {sku: {eq: "simple1"}}) { items { name }}}';
$store1Response = $this->graphQlQueryWithResponseHeaders($query, [], '', ['store' => 'default']);
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $store1Response['headers']);
$store1CacheId = $store1Response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
$this->assertTrue((boolean)preg_match('/^[0-9a-f]{64}$/i', $store1CacheId));
$store2Response = $this->graphQlQueryWithResponseHeaders($query, [], '', ['store' => 'fixture_second_store']);
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $store2Response['headers']);
$store2CacheId = $store2Response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
$this->assertTrue((boolean)preg_match('/^[0-9a-f]{64}$/i', $store2CacheId));
// Assert that store1 and store2 return different cache ids
$this->assertNotEquals($store1CacheId, $store2CacheId);
// Assert that store1 returns the same cache id as before
$store1Response = $this->graphQlQueryWithResponseHeaders($query, [], '', ['store' => 'default']);
$this->assertEquals($store1CacheId, $store1Response['headers'][CacheIdCalculator::CACHE_ID_HEADER]);
}
}
@@ -0,0 +1,43 @@
<?php
/**
* Copyright 2023 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento\GraphQl\GraphQlCache;
use Magento\Customer\Test\Fixture\Customer;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;
class GraphQlTest extends GraphQlAbstract
{
#[
DataFixture(Customer::class, as: 'customer'),
]
public function testMutation(): void
{
/** @var \Magento\Customer\Model\Customer $customer */
$customer = DataFixtureStorageManager::getStorage()->get('customer');
$generateToken = <<<MUTATION
mutation{
generateCustomerToken
(
email:"{$customer->getEmail()}",
password: "password"
)
{
token
}
}
MUTATION;
$tokenResponse = $this->graphQlMutationWithResponseHeaders($generateToken);
$this->assertEquals('no-cache', $tokenResponse['headers']['Pragma']);
$this->assertEquals(
'no-store, no-cache, must-revalidate, max-age=0',
$tokenResponse['headers']['Cache-Control']
);
}
}