ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
@@ -0,0 +1,225 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQlCache\Controller;
use Magento\Framework\App\DeploymentConfig\Reader;
use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\App\Response\HttpInterface as HttpResponse;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Filesystem;
use Magento\Framework\Registry;
use Magento\GraphQl\Controller\GraphQl as GraphQlController;
use Magento\GraphQlCache\Model\CacheableQuery;
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
use Magento\PageCache\Model\Cache\Type as PageCache;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use PHPUnit\Framework\TestCase;
/**
* Abstract test class for Graphql cache tests
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
abstract class AbstractGraphqlCacheTest extends TestCase
{
/**
* @var ObjectManager
*/
protected $objectManager;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var string
*/
private $envConfigPath;
/**
* @var Reader
*/
private $envConfigReader;
/**
* @var PhpFormatter
*/
private $formatter;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->enablePageCachePlugin();
$this->enableCachebleQueryTestProxy();
}
/**
* If the cache id salt didn't exist in env.php before a GraphQL request it gets added. To prevent test failures
* due to a config getting changed (which is normally illegal), the salt needs to be removed from env.php after
* a test if it wasn't there before.
*
* @see \Magento\TestFramework\Isolation\DeploymentConfig
*
* @inheritdoc
*/
protected function runTest()
{
/** @var Reader $reader */
if (!$this->envConfigPath) {
/** @var ConfigFilePool $configFilePool */
$configFilePool = $this->objectManager->get(ConfigFilePool::class);
$this->envConfigPath = $configFilePool->getPath(ConfigFilePool::APP_ENV);
}
$this->envConfigReader = $this->envConfigReader ?: $this->objectManager->get(Reader::class);
$initialConfig = $this->envConfigReader->load(ConfigFilePool::APP_ENV);
try {
return parent::runTest();
} finally {
$this->formatter = $this->formatter ?: new PhpFormatter();
$this->filesystem = $this->filesystem ?: $this->objectManager->get(Filesystem::class);
$cacheSaltPathChunks = explode('/', CacheIdCalculator::SALT_CONFIG_PATH);
$currentConfig = $this->envConfigReader->load(ConfigFilePool::APP_ENV);
$resetConfig = $this->resetAddedSection($initialConfig, $currentConfig, $cacheSaltPathChunks);
$resetFileContents = $this->formatter->format($resetConfig);
$directoryWrite = $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG);
$directoryWrite->writeFile($this->envConfigPath, $resetFileContents);
}
}
protected function tearDown(): void
{
$this->disableCacheableQueryTestProxy();
$this->disablePageCachePlugin();
$this->flushPageCache();
}
protected function enablePageCachePlugin(): void
{
/** @var $registry Registry */
$registry = $this->objectManager->get(Registry::class);
$registry->register('use_page_cache_plugin', true, true);
}
protected function disablePageCachePlugin(): void
{
/** @var $registry Registry */
$registry = $this->objectManager->get(Registry::class);
$registry->unregister('use_page_cache_plugin');
}
protected function flushPageCache(): void
{
/** @var PageCache $fullPageCache */
$fullPageCache = $this->objectManager->get(PageCache::class);
$fullPageCache->clean();
}
/**
* Regarding the SuppressWarnings annotation below: the nested class below triggers a false rule match.
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
private function enableCachebleQueryTestProxy(): void
{
$cacheableQueryProxy = new class($this->objectManager) extends CacheableQuery {
/** @var CacheableQuery */
private $delegate;
public function __construct(ObjectManager $objectManager)
{
$this->reset($objectManager);
}
public function reset(ObjectManager $objectManager): void
{
$this->delegate = $objectManager->create(CacheableQuery::class);
}
public function getCacheTags(): array
{
return $this->delegate->getCacheTags();
}
public function addCacheTags(array $cacheTags): void
{
$this->delegate->addCacheTags($cacheTags);
}
public function isCacheable(): bool
{
return $this->delegate->isCacheable();
}
public function setCacheValidity(bool $cacheable): void
{
$this->delegate->setCacheValidity($cacheable);
}
public function shouldPopulateCacheHeadersWithTags(): bool
{
return $this->delegate->shouldPopulateCacheHeadersWithTags();
}
};
$this->objectManager->addSharedInstance($cacheableQueryProxy, CacheableQuery::class);
}
private function disableCacheableQueryTestProxy(): void
{
$this->resetQueryCacheTags();
$this->objectManager->removeSharedInstance(CacheableQuery::class);
}
protected function resetQueryCacheTags(): void
{
$this->objectManager->get(CacheableQuery::class)->reset($this->objectManager);
}
protected function dispatchGraphQlGETRequest(array $queryParams): HttpResponse
{
$this->resetQueryCacheTags();
/** @var HttpRequest $request */
$request = $this->objectManager->get(HttpRequest::class);
$request->setPathInfo('/graphql');
$request->setMethod('GET');
$request->setParams($queryParams);
// required for \Magento\Framework\App\PageCache\Identifier to generate the correct cache key
$request->setUri(implode('?', [$request->getPathInfo(), http_build_query($queryParams)]));
return $this->objectManager->create(GraphQlController::class)->dispatch($request);
}
/**
* Go over the current deployment config and unset a section that was not present in the pre-test deployment config
*
* @param array $initial
* @param array $current
* @param string[] $chunks
* @return array
*/
private function resetAddedSection(array $initial, array $current, array $chunks): array
{
if ($chunks) {
$chunk = array_shift($chunks);
if (!isset($initial[$chunk])) {
if (isset($current[$chunk])) {
unset($current[$chunk]);
}
} elseif (isset($current[$chunk]) && is_array($initial[$chunk]) && is_array($current[$chunk])) {
$current[$chunk] = $this->resetAddedSection($initial[$chunk], $current[$chunk], $chunks);
}
}
return $current;
}
}
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQlCache\Controller\Catalog;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\GraphQlCache\Controller\AbstractGraphqlCacheTest;
/**
* Tests cache debug headers and cache tag validation for a category with product query
*
* @magentoAppArea graphql
* @magentoCache full_page enabled
* @magentoDbIsolation disabled
*/
class CategoriesWithProductsCacheTest extends AbstractGraphqlCacheTest
{
/**
* Test cache tags and debug header for category with products querying for products and category
*
* @magentoDataFixture Magento/Catalog/_files/category_product.php
*/
public function testRequestCacheTagsForCategoryWithProducts(): void
{
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
/** @var ProductInterface $product */
$product = $productRepository->get('simple333');
$categoryId = 333;
$query =
<<<QUERY
query GetCategoryWithProducts(\$id: Int!, \$pageSize: Int!, \$currentPage: Int!) {
category(id: \$id) {
id
description
name
product_count
products(
pageSize: \$pageSize,
currentPage: \$currentPage) {
items {
id
name
attribute_set_id
url_key
sku
type_id
updated_at
url_key
url_path
}
total_count
}
}
}
QUERY;
$variables = [
'id' => $categoryId,
'pageSize'=> 10,
'currentPage' => 1
];
$queryParams = [
'query' => $query,
'variables' => json_encode($variables),
'operationName' => 'GetCategoryWithProducts'
];
$response = $this->dispatchGraphQlGETRequest($queryParams);
$this->assertEquals('MISS', $response->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$expectedCacheTags = ['cat_c', 'cat_c_' . $categoryId, 'cat_p', 'cat_p_' . $product->getId(), 'FPC'];
$actualCacheTags = explode(',', $response->getHeader('X-Magento-Tags')->getFieldValue());
$this->assertEquals($expectedCacheTags, $actualCacheTags);
}
}
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQlCache\Controller\Catalog;
use Magento\GraphQlCache\Controller\AbstractGraphqlCacheTest;
/**
* Tests cache debug headers and cache tag validation for a simple category query
*
* @magentoAppArea graphql
* @magentoCache full_page enabled
* @magentoDbIsolation disabled
*/
class CategoryCacheTest extends AbstractGraphqlCacheTest
{
/**
* Test cache tags and debug header for category and querying only for category
*
* @magentoDataFixture Magento/Catalog/_files/category_product.php
*/
public function testRequestCacheTagsForCategory(): void
{
$categoryId ='333';
$query
= <<<QUERY
{
category(id: $categoryId) {
id
name
url_key
description
product_count
}
}
QUERY;
$response = $this->dispatchGraphQlGETRequest(['query' => $query]);
$this->assertEquals('MISS', $response->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$actualCacheTags = explode(',', $response->getHeader('X-Magento-Tags')->getFieldValue());
$expectedCacheTags = ['cat_c','cat_c_' . $categoryId, 'FPC'];
$this->assertEquals($expectedCacheTags, $actualCacheTags);
}
}
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQlCache\Controller\Catalog;
use Magento\GraphQlCache\Controller\AbstractGraphqlCacheTest;
/**
* Test caching for categoryList query
*
* @magentoAppArea graphql
* @magentoCache full_page enabled
* @magentoDbIsolation disabled
*/
class CategoryListMultipleIdsCacheTest extends AbstractGraphqlCacheTest
{
/**
* Test request is served from cache. Expected cache tags are equal in both MISS and HIT cases.
*
* @magentoDataFixture Magento/Catalog/_files/category_tree.php
*/
public function testSecondRequestIsServedFromCacheOnMultipleIds()
{
$categoryId1 ='400';
$categoryId2 = '401';
$query
= <<<QUERY
{
categoryList(filters: {ids: {in: ["$categoryId1", "$categoryId2"]}}) {
id
name
url_key
description
product_count
}
}
QUERY;
$expectedCacheTags = ['cat_c', 'cat_c_' . $categoryId1, 'cat_c_' . $categoryId2, 'FPC'];
$response = $this->dispatchGraphQlGETRequest(['query' => $query]);
$this->assertEquals('MISS', $response->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$actualCacheTags = explode(',', $response->getHeader('X-Magento-Tags')->getFieldValue());
$this->assertEquals($expectedCacheTags, $actualCacheTags);
$cacheResponse = $this->dispatchGraphQlGETRequest(['query' => $query]);
$this->assertEquals('HIT', $cacheResponse->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$actualCacheTags = explode(',', $cacheResponse->getHeader('X-Magento-Tags')->getFieldValue());
$this->assertEquals($expectedCacheTags, $actualCacheTags);
}
}
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQlCache\Controller\Catalog;
use Magento\GraphQlCache\Controller\AbstractGraphqlCacheTest;
/**
* Test caching for categoryList query
*
* @magentoAppArea graphql
* @magentoCache full_page enabled
* @magentoDbIsolation disabled
*/
class CategoryListSingleIdCacheTest extends AbstractGraphqlCacheTest
{
/**
* Test request is served from cache. Expected cache tags are equal in both MISS and HIT cases.
*
* @magentoDataFixture Magento/Catalog/_files/category_product.php
*/
public function testSecondRequestIsServedFromCache()
{
$categoryId ='333';
$query
= <<<QUERY
{
categoryList(filters: {ids: {in: ["$categoryId"]}}) {
id
name
url_key
description
product_count
}
}
QUERY;
$expectedCacheTags = ['cat_c','cat_c_' . $categoryId, 'FPC'];
$response = $this->dispatchGraphQlGETRequest(['query' => $query]);
$this->assertEquals('MISS', $response->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$actualCacheTags = explode(',', $response->getHeader('X-Magento-Tags')->getFieldValue());
$this->assertEquals($expectedCacheTags, $actualCacheTags);
$cacheResponse = $this->dispatchGraphQlGETRequest(['query' => $query]);
$this->assertEquals('HIT', $cacheResponse->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$actualCacheTags = explode(',', $cacheResponse->getHeader('X-Magento-Tags')->getFieldValue());
$this->assertEquals($expectedCacheTags, $actualCacheTags);
}
}
@@ -0,0 +1,103 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQlCache\Controller\Catalog;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\GraphQlCache\Controller\AbstractGraphqlCacheTest;
/**
* Tests cache debug headers and cache tag validation for a deep nested category and product query
*
* @magentoAppArea graphql
* @magentoDbIsolation disabled
*/
class DeepNestedCategoriesAndProductsTest extends AbstractGraphqlCacheTest
{
/**
* Test cache tags and debug header for deep nested queries involving category and products
*
* @magentoCache all enabled
* @magentoDataFixture Magento/Catalog/_files/product_in_multiple_categories.php
*/
public function testDispatchForCacheHeadersOnDeepNestedQueries(): void
{
$baseCategoryId ='333';
$query
= <<<QUERY
{
category(id: $baseCategoryId) {
products {
items {
attribute_set_id
country_of_manufacture
created_at
description {
html
}
gift_message_available
id
categories {
name
url_path
available_sort_by
level
products {
items {
name
id
}
}
}
}
}
}
}
QUERY;
/** @var CategoryRepositoryInterface $categoryRepository */
$categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$resolvedCategoryIds = [];
$category = $categoryRepository->get($baseCategoryId);
$productIdsFromCategory = $category->getProductCollection()->getAllIds();
foreach ($productIdsFromCategory as $productId) {
$resolvedCategoryIds[] = $productRepository->getById($productId)->getCategoryIds();
}
$productIdsFromCategory = [$productIdsFromCategory];
$resolvedCategoryIds = array_merge(array_merge([], ...$resolvedCategoryIds), [$baseCategoryId]);
foreach ($resolvedCategoryIds as $categoryId) {
$category = $categoryRepository->get($categoryId);
$productIdsFromCategory[] = $category->getProductCollection()->getAllIds();
}
$uniqueProductIds = array_unique(array_merge([], ...$productIdsFromCategory));
$uniqueCategoryIds = array_unique($resolvedCategoryIds);
$expectedCacheTags = [];
foreach ($uniqueProductIds as $uniqueProductId) {
$expectedCacheTags[] = ['cat_p_' . $uniqueProductId];
}
foreach ($uniqueCategoryIds as $uniqueCategoryId) {
$expectedCacheTags[] = ['cat_c_' . $uniqueCategoryId];
}
$expectedCacheTags = array_merge(['cat_c', 'cat_p', 'FPC'], ...$expectedCacheTags);
$response = $this->dispatchGraphQlGETRequest(['query' => $query]);
$this->assertEquals('MISS', $response->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$actualCacheTags = explode(',', $response->getHeader('X-Magento-Tags')->getFieldValue());
$this->assertEmpty(
array_merge(
array_diff($expectedCacheTags, $actualCacheTags),
array_diff($actualCacheTags, $expectedCacheTags)
)
);
}
}
@@ -0,0 +1,171 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQlCache\Controller\Catalog;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\GraphQlCache\Controller\AbstractGraphqlCacheTest;
/**
* Tests cache debug headers and cache tag validation for a simple product query
*
* @magentoAppArea graphql
* @magentoCache full_page enabled
* @magentoDbIsolation disabled
*/
class ProductsCacheTest extends AbstractGraphqlCacheTest
{
/**
* Test request is dispatched and response is checked for debug headers and cache tags
*
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
*/
public function testRequestCacheTagsForProducts(): void
{
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$product = $productRepository->get('simple1');
$query
= <<<QUERY
{
products(filter: {sku: {eq: "simple1"}})
{
items {
id
name
sku
description {
html
}
}
}
}
QUERY;
$response = $this->dispatchGraphQlGETRequest(['query' => $query]);
$this->assertEquals('MISS', $response->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$actualCacheTags = explode(',', $response->getHeader('X-Magento-Tags')->getFieldValue());
$expectedCacheTags = ['cat_p', 'cat_p_' . $product->getId(), 'FPC'];
$this->assertEquals($expectedCacheTags, $actualCacheTags);
}
/**
* Test request is checked for debug headers and no cache tags for not existing product
*/
public function testRequestNoTagsForNonExistingProducts(): void
{
$query
= <<<QUERY
{
products(filter: {sku: {eq: "simple10"}})
{
items {
id
name
sku
description {
html
}
}
}
}
QUERY;
$response = $this->dispatchGraphQlGETRequest(['query' => $query]);
$this->assertEquals('MISS', $response->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$actualCacheTags = explode(',', $response->getHeader('X-Magento-Tags')->getFieldValue());
$expectedCacheTags = ['FPC'];
$this->assertEquals($expectedCacheTags, $actualCacheTags);
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
*/
public function testConsecutiveRequestsAreServedFromThePageCache(): void
{
$query
= <<<QUERY
{
products(filter: {sku: {eq: "simple1"}})
{
items {
id
name
sku
description {
html
}
}
}
}
QUERY;
$response1 = $this->dispatchGraphQlGETRequest(['query' => $query]);
$response2 = $this->dispatchGraphQlGETRequest(['query' => $query]);
$this->assertEquals('MISS', $response1->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$this->assertEquals('HIT', $response2->getHeader('X-Magento-Cache-Debug')->getFieldValue());
}
/**
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
*/
public function testDifferentProductsRequestsUseDifferentPageCacheRecords(): void
{
$queryTemplate
= <<<QUERY
{
products(filter: {sku: {eq: "%s"}})
{
items {
id
name
sku
description {
html
}
}
}
}
QUERY;
$responseProduct1 = $this->dispatchGraphQlGETRequest(['query' => sprintf($queryTemplate, 'simple1')]);
$responseProduct2 = $this->dispatchGraphQlGETRequest(['query' => sprintf($queryTemplate, 'simple2')]);
$this->assertEquals('MISS', $responseProduct1->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$this->assertEquals('MISS', $responseProduct2->getHeader('X-Magento-Cache-Debug')->getFieldValue());
}
/**
* Test response has category tags when products are filtered by category id
*
* @magentoDataFixture Magento/Catalog/_files/category_product.php
*/
public function testProductsFilterByCategoryHasCategoryTags(): void
{
$query
= <<<QUERY
{
products(filter: {category_id: {eq: "333"}})
{
items {
name
sku
}
}
}
QUERY;
$response = $this->dispatchGraphQlGETRequest(['query' => $query]);
$actualCacheTags = explode(',', $response->getHeader('X-Magento-Tags')->getFieldValue());
$expectedCacheTags = ['cat_c', 'cat_c_333'];
foreach ($expectedCacheTags as $cacheTag) {
$this->assertContains($cacheTag, $actualCacheTags);
}
}
}
@@ -0,0 +1,111 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQlCache\Controller\Cms;
use Magento\Cms\Api\Data\BlockInterface;
use Magento\Cms\Model\BlockRepository;
use Magento\Framework\App\Response\HttpInterface as HttpResponse;
use Magento\GraphQlCache\Controller\AbstractGraphqlCacheTest;
/**
* Test caching works for CMS blocks
*
* @magentoAppArea graphql
* @magentoCache full_page enabled
* @magentoDbIsolation disabled
*/
class BlockCacheTest extends AbstractGraphqlCacheTest
{
private function assertPageCacheMissWithTagsForCmsBlock(HttpResponse $response, BlockInterface $block): void
{
$this->assertEquals('MISS', $response->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$this->assertCmsBlockCacheTags($response, $block);
}
private function assertPageCacheHitWithTagsForCmsBlock(HttpResponse $response, BlockInterface $block): void
{
$this->assertEquals('HIT', $response->getHeader('X-Magento-Cache-Debug')->getFieldValue());
$this->assertCmsBlockCacheTags($response, $block);
}
private function assertCmsBlockCacheTags(HttpResponse $response, BlockInterface $block): void
{
$expectedCacheTags = ['cms_b', 'cms_b_' . $block->getId(), 'cms_b_' . $block->getIdentifier(), 'FPC'];
$rawActualCacheTags = $response->getHeader('X-Magento-Tags')->getFieldValue();
$actualCacheTags = explode(',', $rawActualCacheTags);
$this->assertEquals($expectedCacheTags, $actualCacheTags);
}
/**
* @magentoDataFixture Magento/Cms/_files/block.php
* @magentoDataFixture Magento/Cms/_files/blocks.php
*/
public function testCmsBlocksRequestHasCorrectTags(): void
{
/** @var BlockRepository $blockRepository */
$blockRepository = $this->objectManager->get(BlockRepository::class);
$block1Identifier = 'fixture_block';
$block1 = $blockRepository->getById($block1Identifier);
$block2Identifier = 'enabled_block';
$block2 = $blockRepository->getById($block2Identifier);
$queryBlock1
= <<<QUERY
{
cmsBlocks(identifiers: ["$block1Identifier"]) {
items {
title
identifier
content
}
}
}
QUERY;
$queryBlock2
= <<<QUERY
{
cmsBlocks(identifiers: ["$block2Identifier"]) {
items {
title
identifier
content
}
}
}
QUERY;
// check to see that the first entity gets a MISS when called the first time
$response = $this->dispatchGraphQlGETRequest(['query' => $queryBlock1]);
$this->assertPageCacheMissWithTagsForCmsBlock($response, $block1);
// check to see that the second entity gets a MISS when called the first time
$response = $this->dispatchGraphQlGETRequest(['query' => $queryBlock2]);
$this->assertPageCacheMissWithTagsForCmsBlock($response, $block2);
// check to see that the first entity gets a HIT when called the second time
$response = $this->dispatchGraphQlGETRequest(['query' => $queryBlock1]);
$this->assertPageCacheHitWithTagsForCmsBlock($response, $block1);
// check to see that the second entity gets a HIT when called the second time
$response = $this->dispatchGraphQlGETRequest(['query' => $queryBlock2]);
$this->assertPageCacheHitWithTagsForCmsBlock($response, $block2);
$block1->setTitle('something else that causes invalidation');
$blockRepository->save($block1);
// check to see that the first entity gets a MISS and it was invalidated
$response = $this->dispatchGraphQlGETRequest(['query' => $queryBlock1]);
$this->assertPageCacheMissWithTagsForCmsBlock($response, $block1);
// check to see that the first entity gets a HIT when called the second time
$response = $this->dispatchGraphQlGETRequest(['query' => $queryBlock1]);
$this->assertPageCacheHitWithTagsForCmsBlock($response, $block1);
}
}
@@ -0,0 +1,124 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQlCache\Controller\Cms;
use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Model\GetPageByIdentifier;
use Magento\Cms\Model\PageRepository;
use Magento\Framework\App\Response\HttpInterface as HttpResponse;
use Magento\GraphQlCache\Controller\AbstractGraphqlCacheTest;
/**
* Test caching works for CMS pages
*
* @magentoAppArea graphql
* @magentoCache full_page enabled
* @magentoDbIsolation disabled
*
*/
class CmsPageCacheTest extends AbstractGraphqlCacheTest
{
private function assertPageCacheMissWithTagsForCmsPage(string $pageId, string $name, HttpResponse $response): void
{
$this->assertEquals(
'MISS',
$response->getHeader('X-Magento-Cache-Debug')->getFieldValue(),
"expected MISS on page {$name} id {$pageId}"
);
$this->assertCmsPageCacheTags($pageId, $response);
}
private function assertPageCacheHitWithTagsForCmsPage(string $pageId, string $name, HttpResponse $response): void
{
$this->assertEquals(
'HIT',
$response->getHeader('X-Magento-Cache-Debug')->getFieldValue(),
"expected HIT on page {$name} id {$pageId}"
);
$this->assertCmsPageCacheTags($pageId, $response);
}
private function assertCmsPageCacheTags(string $pageId, HttpResponse $response): void
{
$requestedCacheTags = explode(',', $response->getHeader('X-Magento-Tags')->getFieldValue());
$expectedCacheTags = ['cms_p', 'cms_p_' . $pageId, 'FPC'];
$this->assertEquals($expectedCacheTags, $requestedCacheTags);
}
private function buildQuery(string $id): string
{
$queryCmsPage = <<<QUERY
{
cmsPage(id: $id) {
url_key
title
content
content_heading
page_layout
meta_title
meta_description
meta_keywords
}
}
QUERY;
return $queryCmsPage;
}
private function updateCmsPageTitle(string $pageId100, string $newTitle): void
{
/** @var PageRepository $pageRepository */
$pageRepository = $this->objectManager->get(PageRepository::class);
$page = $pageRepository->getById($pageId100);
$page->setTitle($newTitle);
$pageRepository->save($page);
}
/**
* @magentoDataFixture Magento/Cms/_files/pages.php
*/
public function testCmsPageRequestCacheTags(): void
{
/** @var PageInterface $cmsPage100 */
$cmsPage100 = $this->objectManager->get(GetPageByIdentifier::class)->execute('page100', 0);
$pageId100 = (string) $cmsPage100->getId();
/** @var PageInterface $cmsPageBlank */
$cmsPageBlank = $this->objectManager->get(GetPageByIdentifier::class)->execute('page_design_blank', 0);
$pageIdBlank = (string) $cmsPageBlank->getId();
$queryCmsPage100 = $this->buildQuery($pageId100);
$queryCmsPageBlank = $this->buildQuery($pageIdBlank);
// check to see that the first entity gets a MISS when called the first time
$response = $this->dispatchGraphQlGETRequest(['query' => $queryCmsPage100]);
$this->assertPageCacheMissWithTagsForCmsPage($pageId100, 'page100', $response);
// check to see that the second entity gets a MISS when called the first time
$response = $this->dispatchGraphQlGETRequest(['query' => $queryCmsPageBlank]);
$this->assertPageCacheMissWithTagsForCmsPage($pageIdBlank, 'pageBlank', $response);
// check to see that the first entity gets a HIT when called the second time
$response = $this->dispatchGraphQlGETRequest(['query' => $queryCmsPage100]);
$this->assertPageCacheHitWithTagsForCmsPage($pageId100, 'page100', $response);
// check to see that the second entity gets a HIT when called the second time
$response = $this->dispatchGraphQlGETRequest(['query' => $queryCmsPageBlank]);
$this->assertPageCacheHitWithTagsForCmsPage($pageIdBlank, 'pageBlank', $response);
// invalidate first entity
$this->updateCmsPageTitle($pageId100, 'something else that causes invalidation');
// check to see that the second entity gets a HIT to confirm only the first was invalidated
$response = $this->dispatchGraphQlGETRequest(['query' => $queryCmsPageBlank]);
$this->assertPageCacheHitWithTagsForCmsPage($pageIdBlank, 'pageBlank', $response);
// check to see that the first entity gets a MISS because it was invalidated
$response = $this->dispatchGraphQlGETRequest(['query' => $queryCmsPage100]);
$this->assertPageCacheMissWithTagsForCmsPage($pageId100, 'page100', $response);
}
}
@@ -0,0 +1,176 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\GraphQlCache\Controller\UrlRewrite;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Response\HttpInterface as HttpResponse;
use Magento\GraphQlCache\Controller\AbstractGraphqlCacheTest;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Api\GetPageByIdentifierInterface;
/**
* Test caching works for categoryUrlResolver
*
* @magentoAppArea graphql
* @magentoCache full_page enabled
* @magentoDbIsolation disabled
*/
class AllEntitiesUrlResolverCacheTest extends AbstractGraphqlCacheTest
{
private function assertCacheMISSWithTagsForCategory(string $categoryId, HttpResponse $response): void
{
$this->assertCacheMISS($response);
$this->assertCacheTags($categoryId, 'cat_c', $response);
}
private function assertCacheMISSWithTagsForProduct(string $productId, HttpResponse $response): void
{
$this->assertCacheMISS($response);
$this->assertCacheTags($productId, 'cat_p', $response);
}
private function assertCacheMISSWithTagsForCmsPage(string $pageId, HttpResponse $response): void
{
$this->assertCacheMISS($response);
$this->assertCacheTags($pageId, 'cms_p', $response);
}
private function assertCacheHITWithTagsForCategory(string $categoryId, HttpResponse $response): void
{
$this->assertCacheHIT($response);
$this->assertCacheTags($categoryId, 'cat_c', $response);
}
private function assertCacheHITWithTagsForProduct(string $productId, HttpResponse $response): void
{
$this->assertCacheHIT($response);
$this->assertCacheTags($productId, 'cat_p', $response);
}
private function assertCacheHITWithTagsForCmsPage(string $pageId, HttpResponse $response): void
{
$this->assertCacheHIT($response);
$this->assertCacheTags($pageId, 'cms_p', $response);
}
private function assertCacheMISS(HttpResponse $response): void
{
$this->assertEquals('MISS', $response->getHeader('X-Magento-Cache-Debug')->getFieldValue());
}
private function assertCacheHIT(HttpResponse $response): void
{
$this->assertEquals('HIT', $response->getHeader('X-Magento-Cache-Debug')->getFieldValue());
}
private function assertCacheTags(string $entityId, string $entityCacheTag, HttpResponse $response)
{
$expectedCacheTags = [$entityCacheTag, $entityCacheTag . '_' . $entityId, 'FPC'];
$rawActualCacheTags = $response->getHeader('X-Magento-Tags')->getFieldValue();
$actualCacheTags = explode(',', $rawActualCacheTags);
$this->assertEquals($expectedCacheTags, $actualCacheTags);
}
private function buildQuery(string $requestPath): string
{
$resolverQuery = <<<QUERY
{
urlResolver(url:"{$requestPath}")
{
id
relative_url
canonical_url
type
}
}
QUERY;
return $resolverQuery;
}
/**
* Tests that X-Magento-Tags and cache debug headers are correct for category urlResolver
*
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
* @magentoDataFixture Magento/Cms/_files/pages.php
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testAllEntitiesUrlResolverRequestHasCorrectTags(): void
{
$categoryUrlKey = 'cat-1.html';
$productUrlKey = 'p002.html';
$productSku = 'p002';
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
/** @var Product $product */
$product = $productRepository->get($productSku, false, null, true);
$storeId = (string) $product->getStoreId();
/** @var UrlFinderInterface $urlFinder */
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
$actualUrls = $urlFinder->findOneByData(
[
'request_path' => $categoryUrlKey,
'store_id' => $storeId
]
);
$categoryId = (string) $actualUrls->getEntityId();
$categoryQuery = $this->buildQuery($categoryUrlKey);
$productQuery = $this->buildQuery($productUrlKey);
/** @var GetPageByIdentifierInterface $page */
$page = $this->objectManager->get(GetPageByIdentifierInterface::class);
/** @var PageInterface $cmsPage */
$cmsPage = $page->execute('page100', 0);
$cmsPageId = (string) $cmsPage->getId();
$requestPath = $cmsPage->getIdentifier();
$pageQuery = $this->buildQuery($requestPath);
// query category for MISS
$response = $this->dispatchGraphQlGETRequest(['query' => $categoryQuery]);
$this->assertCacheMISSWithTagsForCategory($categoryId, $response);
// query product for MISS
$response = $this->dispatchGraphQlGETRequest(['query' => $productQuery]);
$this->assertCacheMISSWithTagsForProduct((string) $product->getId(), $response);
// query page for MISS
$response = $this->dispatchGraphQlGETRequest(['query' => $pageQuery]);
$this->assertCacheMISSWithTagsForCmsPage($cmsPageId, $response);
// query category for HIT
$response = $this->dispatchGraphQlGETRequest(['query' => $categoryQuery]);
$this->assertCacheHITWithTagsForCategory($categoryId, $response);
// query product for HIT
$response = $this->dispatchGraphQlGETRequest(['query' => $productQuery]);
$this->assertCacheHITWithTagsForProduct((string) $product->getId(), $response);
// query page for HIT
$response = $this->dispatchGraphQlGETRequest(['query' => $pageQuery]);
$this->assertCacheHITWithTagsForCmsPage($cmsPageId, $response);
$product->setUrlKey('something-else-that-invalidates-the-cache');
$productRepository->save($product);
$productQuery = $this->buildQuery('something-else-that-invalidates-the-cache.html');
// query category for MISS
$response = $this->dispatchGraphQlGETRequest(['query' => $categoryQuery]);
$this->assertCacheMISSWithTagsForCategory($categoryId, $response);
// query product for MISS
$response = $this->dispatchGraphQlGETRequest(['query' => $productQuery]);
$this->assertCacheMISSWithTagsForProduct((string) $product->getId(), $response);
// query page for HIT
$response = $this->dispatchGraphQlGETRequest(['query' => $pageQuery]);
$this->assertCacheHITWithTagsForCmsPage($cmsPageId, $response);
}
}