init
This commit is contained in:
+470
@@ -0,0 +1,470 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright 2023 Adobe
|
||||
* All Rights Reserved.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\CmsGraphQl\Model\Resolver;
|
||||
|
||||
use Magento\Cms\Api\BlockRepositoryInterface;
|
||||
use Magento\Cms\Api\Data\BlockInterface;
|
||||
use Magento\Cms\Model\Block;
|
||||
use Magento\Cms\Test\Fixture\Block as BlockFixture;
|
||||
use Magento\CmsGraphQl\Model\Resolver\Blocks;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\Calculator\ProviderInterface;
|
||||
use Magento\GraphQlResolverCache\Model\Resolver\Result\Type as GraphQlResolverCache;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Store\Test\Fixture\Store;
|
||||
use Magento\TestFramework\Fixture\Config;
|
||||
use Magento\TestFramework\Fixture\DataFixture;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorage;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\GraphQl\ResolverCacheAbstract;
|
||||
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
|
||||
use Magento\Widget\Model\Template\FilterEmulate;
|
||||
|
||||
/**
|
||||
* Test for cms block resolver cache
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class BlockTest extends ResolverCacheAbstract
|
||||
{
|
||||
/**
|
||||
* @var BlockRepositoryInterface
|
||||
*/
|
||||
private $blockRepository;
|
||||
|
||||
/**
|
||||
* @var GraphQlResolverCache
|
||||
*/
|
||||
private $graphQlResolverCache;
|
||||
|
||||
/**
|
||||
* @var FilterEmulate
|
||||
*/
|
||||
private $widgetFilter;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var DataFixtureStorage
|
||||
*/
|
||||
private $fixtures;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = ObjectManager::getInstance();
|
||||
$this->blockRepository = $objectManager->get(BlockRepositoryInterface::class);
|
||||
$this->graphQlResolverCache = $objectManager->get(GraphQlResolverCache::class);
|
||||
$this->widgetFilter = $objectManager->get(FilterEmulate::class);
|
||||
$this->storeManager = $objectManager->get(StoreManagerInterface::class);
|
||||
$this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage();
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
#[
|
||||
DataFixture(BlockFixture::class, as: 'guest_block')
|
||||
]
|
||||
public function testCmsSingleBlockResolverCacheAndInvalidationAsGuest()
|
||||
{
|
||||
$block = $this->fixtures->get('guest_block');
|
||||
|
||||
$query = $this->getQuery([
|
||||
$block->getIdentifier(),
|
||||
]);
|
||||
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$cacheIdentityString = $this->getResolverCacheKeyFromBlocks([$block]);
|
||||
|
||||
$cacheEntry = $this->graphQlResolverCache->load($cacheIdentityString);
|
||||
$cacheEntryDecoded = json_decode($cacheEntry, true);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
$this->generateExpectedDataFromBlocks([$block]),
|
||||
$cacheEntryDecoded
|
||||
);
|
||||
|
||||
$this->assertTagsByCacheIdentityAndBlocks(
|
||||
$cacheIdentityString,
|
||||
[$block]
|
||||
);
|
||||
|
||||
// assert that cache is invalidated after block content change
|
||||
$block->setContent('New Content');
|
||||
$this->blockRepository->save($block);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->test($cacheIdentityString),
|
||||
'Cache entry should be invalidated after block content change'
|
||||
);
|
||||
}
|
||||
|
||||
#[
|
||||
DataFixture(BlockFixture::class, as: 'block', count: 2)
|
||||
]
|
||||
public function testCmsMultipleBlockResolverCacheAndInvalidationAsGuest()
|
||||
{
|
||||
$block1 = $this->fixtures->get('block1');
|
||||
$block2 = $this->fixtures->get('block2');
|
||||
|
||||
$query = $this->getQuery([
|
||||
$block1->getIdentifier(),
|
||||
$block2->getIdentifier(),
|
||||
]);
|
||||
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$cacheKey = $this->getResolverCacheKeyFromBlocks([
|
||||
$block1,
|
||||
$block2,
|
||||
]);
|
||||
|
||||
$cacheEntry = $this->graphQlResolverCache->load($cacheKey);
|
||||
$cacheEntryDecoded = json_decode($cacheEntry, true);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
$this->generateExpectedDataFromBlocks([$block1, $block2]),
|
||||
$cacheEntryDecoded
|
||||
);
|
||||
|
||||
$this->assertTagsByCacheIdentityAndBlocks(
|
||||
$cacheKey,
|
||||
[$block1, $block2]
|
||||
);
|
||||
|
||||
// assert that cache is invalidated after a single block content change
|
||||
$block2->setContent('New Content');
|
||||
$this->blockRepository->save($block2);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->test($cacheKey),
|
||||
'Cache entry should be invalidated after block content change'
|
||||
);
|
||||
}
|
||||
|
||||
#[
|
||||
DataFixture(BlockFixture::class, as: 'deleted_block')
|
||||
]
|
||||
public function testCmsBlockResolverCacheInvalidatesWhenBlockGetsDeleted()
|
||||
{
|
||||
$block = $this->fixtures->get('deleted_block');
|
||||
|
||||
$query = $this->getQuery([
|
||||
$block->getIdentifier(),
|
||||
]);
|
||||
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$cacheKey = $this->getResolverCacheKeyFromBlocks([$block]);
|
||||
|
||||
$cacheEntry = $this->graphQlResolverCache->load($cacheKey);
|
||||
$cacheEntryDecoded = json_decode($cacheEntry, true);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
$this->generateExpectedDataFromBlocks([$block]),
|
||||
$cacheEntryDecoded
|
||||
);
|
||||
|
||||
$this->assertTagsByCacheIdentityAndBlocks(
|
||||
$cacheKey,
|
||||
[$block]
|
||||
);
|
||||
|
||||
// assert that cache is invalidated after block deletion
|
||||
$this->blockRepository->delete($block);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->test($cacheKey),
|
||||
'Cache entry should be invalidated after block deletion'
|
||||
);
|
||||
}
|
||||
|
||||
#[
|
||||
DataFixture(BlockFixture::class, as: 'enabled_block')
|
||||
]
|
||||
public function testCmsBlockResolverCacheInvalidatesWhenBlockGetsDisabled()
|
||||
{
|
||||
$block = $this->fixtures->get('enabled_block');
|
||||
|
||||
$query = $this->getQuery([
|
||||
$block->getIdentifier(),
|
||||
]);
|
||||
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$cacheKey = $this->getResolverCacheKeyFromBlocks([$block]);
|
||||
|
||||
$cacheEntry = $this->graphQlResolverCache->load($cacheKey);
|
||||
$cacheEntryDecoded = json_decode($cacheEntry, true);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
$this->generateExpectedDataFromBlocks([$block]),
|
||||
$cacheEntryDecoded
|
||||
);
|
||||
|
||||
$this->assertTagsByCacheIdentityAndBlocks(
|
||||
$cacheKey,
|
||||
[$block]
|
||||
);
|
||||
|
||||
// assert that cache is invalidated after block disablement
|
||||
$block->setIsActive(false);
|
||||
$this->blockRepository->save($block);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->test($cacheKey),
|
||||
'Cache entry should be invalidated after block disablement'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LocalizedException
|
||||
* @throws \Zend_Cache_Exception
|
||||
*/
|
||||
#[
|
||||
DataFixture(BlockFixture::class, as: 'block'),
|
||||
DataFixture(Store::class, as: 'second_store'),
|
||||
]
|
||||
public function testCmsBlockResolverCacheIsInvalidatedAfterChangingItsStoreView()
|
||||
{
|
||||
/** @var Block $block */
|
||||
$block = $this->fixtures->get('block');
|
||||
|
||||
/** @var \Magento\Store\Model\Store $store */
|
||||
$store = $this->fixtures->get('second_store');
|
||||
|
||||
$query = $this->getQuery([
|
||||
$block->getIdentifier(),
|
||||
]);
|
||||
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$cacheKey = $this->getResolverCacheKeyFromBlocks([$block]);
|
||||
|
||||
$cacheEntry = $this->graphQlResolverCache->load($cacheKey);
|
||||
$cacheEntryDecoded = json_decode($cacheEntry, true);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
$cacheEntryDecoded,
|
||||
$this->generateExpectedDataFromBlocks([$block])
|
||||
);
|
||||
|
||||
$this->assertTagsByCacheIdentityAndBlocks(
|
||||
(string)$cacheKey,
|
||||
[$block]
|
||||
);
|
||||
|
||||
// assert that cache is invalidated after changing block's store view
|
||||
$secondStoreViewId = $store->getId();
|
||||
$block->setStoreId($secondStoreViewId);
|
||||
$this->blockRepository->save($block);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->test($cacheKey),
|
||||
'Cache entry should be invalidated after changing block\'s store view'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @return void
|
||||
*/
|
||||
public function testCmsBlockResolverCacheDoesNotSaveNonExistentCmsBlock()
|
||||
{
|
||||
$nonExistentBlock = ObjectManager::getInstance()->create(BlockInterface::class);
|
||||
$nonExistentBlock->setIdentifier('non-existent-block');
|
||||
|
||||
$query = $this->getQuery([$nonExistentBlock->getIdentifier()]);
|
||||
|
||||
try {
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->fail('Expected exception was not thrown');
|
||||
} catch (ResponseContainsErrorsException $e) {
|
||||
// expected exception
|
||||
}
|
||||
|
||||
$cacheIdentityString = $this->getResolverCacheKeyFromBlocks([$nonExistentBlock]);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->load($cacheIdentityString)
|
||||
);
|
||||
}
|
||||
|
||||
#[
|
||||
Config('system/full_page_cache/caching_application', '2'),
|
||||
DataFixture(BlockFixture::class, as: 'block', count: 2)
|
||||
]
|
||||
public function testCmsBlockResolverCacheRetainsEntriesThatHaveNotBeenUpdated()
|
||||
{
|
||||
// query block1
|
||||
$block1 = $this->fixtures->get('block1');
|
||||
|
||||
$queryBlock1 = $this->getQuery([
|
||||
$block1->getIdentifier(),
|
||||
]);
|
||||
|
||||
$this->graphQlQueryWithResponseHeaders($queryBlock1);
|
||||
|
||||
$cacheKeyBlock1 = $this->getResolverCacheKeyFromBlocks([$block1]);
|
||||
|
||||
// query block2
|
||||
$block2 = $this->fixtures->get('block2');
|
||||
|
||||
$queryBlock2 = $this->getQuery([
|
||||
$block2->getIdentifier(),
|
||||
]);
|
||||
|
||||
$this->graphQlQueryWithResponseHeaders($queryBlock2);
|
||||
|
||||
$cacheKeyBlock2 = $this->getResolverCacheKeyFromBlocks([$block2]);
|
||||
|
||||
// assert both cache entries are present
|
||||
$this->assertIsNumeric(
|
||||
$this->graphQlResolverCache->test($cacheKeyBlock1),
|
||||
'Cache entry for block1 should be present'
|
||||
);
|
||||
|
||||
$this->assertIsNumeric(
|
||||
$this->graphQlResolverCache->test($cacheKeyBlock2),
|
||||
'Cache entry for block2 should be present'
|
||||
);
|
||||
|
||||
// assert that cache is invalidated after block1 update
|
||||
$block1->setContent('Updated content');
|
||||
$this->blockRepository->save($block1);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->test($cacheKeyBlock1),
|
||||
'Cache entry for block1 should be invalidated after block1 update'
|
||||
);
|
||||
|
||||
// assert that cache is not invalidated after block1 update
|
||||
$this->assertIsNumeric(
|
||||
$this->graphQlResolverCache->test($cacheKeyBlock2),
|
||||
'Cache entry for block2 should be present after block1 update'
|
||||
);
|
||||
}
|
||||
|
||||
private function getQuery(array $identifiers): string
|
||||
{
|
||||
$identifiersStr = $this->getQuotedBlockIdentifiersListAsString($identifiers);
|
||||
|
||||
return <<<QUERY
|
||||
{
|
||||
cmsBlocks(identifiers: [$identifiersStr]) {
|
||||
items {
|
||||
title
|
||||
content
|
||||
identifier
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $identifiers
|
||||
* @return string
|
||||
*/
|
||||
private function getQuotedBlockIdentifiersListAsString(array $identifiers): string
|
||||
{
|
||||
return implode(',', array_map(function (string $identifier) {
|
||||
return "\"$identifier\"";
|
||||
}, $identifiers));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BlockInterface[] $blocks
|
||||
* @return array
|
||||
*/
|
||||
private function generateExpectedDataFromBlocks(array $blocks): array
|
||||
{
|
||||
$expectedBlockData = [];
|
||||
|
||||
foreach ($blocks as $block) {
|
||||
$expectedBlockData[$block->getIdentifier()] = [
|
||||
'block_id' => $block->getId(),
|
||||
'identifier' => $block->getIdentifier(),
|
||||
'title' => $block->getTitle(),
|
||||
'content' => $this->widgetFilter->filterDirective($block->getContent()),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'items' => $expectedBlockData,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $cacheIdentityString
|
||||
* @param BlockInterface[] $blocks
|
||||
* @return void
|
||||
* @throws \Zend_Cache_Exception
|
||||
*/
|
||||
private function assertTagsByCacheIdentityAndBlocks(string $cacheIdentityString, array $blocks): void
|
||||
{
|
||||
$lowLevelFrontendCache = $this->graphQlResolverCache->getLowLevelFrontend();
|
||||
$cacheIdPrefix = $lowLevelFrontendCache->getOption('cache_id_prefix');
|
||||
$metadatas = $lowLevelFrontendCache->getMetadatas($cacheIdentityString);
|
||||
$tags = $metadatas['tags'];
|
||||
|
||||
$expectedTags = [
|
||||
$cacheIdPrefix . strtoupper(GraphQlResolverCache::CACHE_TAG),
|
||||
$cacheIdPrefix . 'MAGE',
|
||||
];
|
||||
|
||||
foreach ($blocks as $block) {
|
||||
$expectedTags[] = $cacheIdPrefix . strtoupper(Block::CACHE_TAG) . '_' . $block->getId();
|
||||
$expectedTags[] = $cacheIdPrefix . strtoupper(Block::CACHE_TAG . '_' . $block->getIdentifier());
|
||||
}
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
$expectedTags,
|
||||
$tags
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $response
|
||||
* @param BlockInterface[] $blocks
|
||||
* @return string
|
||||
*/
|
||||
private function getResolverCacheKeyFromBlocks(array $blocks): string
|
||||
{
|
||||
$resolverMock = $this->getMockBuilder(Blocks::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
/** @var ProviderInterface $cacheKeyCalculatorProvider */
|
||||
$cacheKeyCalculatorProvider = ObjectManager::getInstance()->get(ProviderInterface::class);
|
||||
$cacheKeyFactor = $cacheKeyCalculatorProvider
|
||||
->getKeyCalculatorForResolver($resolverMock)
|
||||
->calculateCacheKey();
|
||||
|
||||
$blockIdentifiers = array_map(function (BlockInterface $block) {
|
||||
return $block->getIdentifier();
|
||||
}, $blocks);
|
||||
|
||||
$cacheKeyQueryPayloadMetadata = sprintf(Blocks::class . '\Interceptor%s', json_encode([
|
||||
'identifiers' => $blockIdentifiers,
|
||||
]));
|
||||
|
||||
$cacheKeyParts = [
|
||||
GraphQlResolverCache::CACHE_TAG,
|
||||
$cacheKeyFactor,
|
||||
sha1($cacheKeyQueryPayloadMetadata)
|
||||
];
|
||||
|
||||
// strtoupper is called in \Magento\Framework\Cache\Frontend\Adapter\Zend::_unifyId
|
||||
return strtoupper(implode('_', $cacheKeyParts));
|
||||
}
|
||||
}
|
||||
+536
@@ -0,0 +1,536 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\CmsGraphQl\Model\Resolver;
|
||||
|
||||
use Magento\Cms\Api\Data\PageInterface;
|
||||
use Magento\Cms\Model\Page as CmsPage;
|
||||
use Magento\Cms\Model\PageRepository;
|
||||
use Magento\CmsGraphQl\Model\Resolver\Page;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Framework\Api\SearchCriteriaBuilder;
|
||||
use Magento\Framework\App\Cache\Frontend\Factory as CacheFrontendFactory;
|
||||
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\Calculator\ProviderInterface;
|
||||
use Magento\GraphQlResolverCache\Model\Resolver\Result\Type as GraphQlResolverCache;
|
||||
use Magento\Integration\Api\CustomerTokenServiceInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\GraphQl\ResolverCacheAbstract;
|
||||
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class PageTest extends ResolverCacheAbstract
|
||||
{
|
||||
/**
|
||||
* @var GraphQlResolverCache
|
||||
*/
|
||||
private $graphQlResolverCache;
|
||||
|
||||
/**
|
||||
* @var PageRepository
|
||||
*/
|
||||
private $pageRepository;
|
||||
|
||||
/**
|
||||
* @var SearchCriteriaBuilder
|
||||
*/
|
||||
private $searchCriteriaBuilder;
|
||||
|
||||
/**
|
||||
* @var CustomerTokenServiceInterface
|
||||
*/
|
||||
private $customerTokenService;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var CustomerRepositoryInterface
|
||||
*/
|
||||
private $customerRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = ObjectManager::getInstance();
|
||||
|
||||
$this->graphQlResolverCache = $objectManager->get(GraphQlResolverCache::class);
|
||||
$this->pageRepository = $objectManager->get(PageRepository::class);
|
||||
$this->searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
|
||||
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
|
||||
$this->storeManager = $objectManager->get(StoreManagerInterface::class);
|
||||
$this->customerRepository = $objectManager->get(CustomerRepositoryInterface::class);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoDataFixture Magento/Cms/Fixtures/page_list.php
|
||||
* @return void
|
||||
*/
|
||||
public function testCmsPageResolverCacheAndInvalidationAsGuest()
|
||||
{
|
||||
$page = $this->getPageByTitle('Page with 1column layout');
|
||||
|
||||
$query = $this->getQuery($page->getIdentifier());
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$cacheKey = $this->getResolverCacheKeyForPage($page);
|
||||
|
||||
$cacheEntry = $this->graphQlResolverCache->load($cacheKey);
|
||||
$cacheEntryDecoded = json_decode($cacheEntry, true);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
$this->generateExpectedDataFromPage($page),
|
||||
$cacheEntryDecoded
|
||||
);
|
||||
|
||||
$this->assertTagsByCacheKeyAndPage($cacheKey, $page);
|
||||
|
||||
// update CMS page and assert cache is invalidated
|
||||
$page->setContent('something different');
|
||||
$this->pageRepository->save($page);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->test($cacheKey),
|
||||
'Cache entry still exists for CMS page'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Cms/Fixtures/page_list.php
|
||||
* @return void
|
||||
*/
|
||||
public function testCmsPageResolverCacheAndInvalidationAsCustomer()
|
||||
{
|
||||
$customer = $this->customerRepository->get('customer@example.com');
|
||||
$this->mockCustomerUserInfoContext($customer);
|
||||
|
||||
$authHeader = [
|
||||
'Authorization' => 'Bearer ' . $this->customerTokenService->createCustomerAccessToken(
|
||||
'customer@example.com',
|
||||
'password'
|
||||
)
|
||||
];
|
||||
|
||||
$page = $this->getPageByTitle('Page with 1column layout');
|
||||
$query = $this->getQuery($page->getIdentifier());
|
||||
$this->graphQlQueryWithResponseHeaders(
|
||||
$query,
|
||||
[],
|
||||
'',
|
||||
$authHeader
|
||||
);
|
||||
|
||||
$cacheKey = $this->getResolverCacheKeyForPage($page);
|
||||
|
||||
$cacheEntry = $this->graphQlResolverCache->load($cacheKey);
|
||||
$cacheEntryDecoded = json_decode($cacheEntry, true);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
$this->generateExpectedDataFromPage($page),
|
||||
$cacheEntryDecoded
|
||||
);
|
||||
|
||||
$this->assertTagsByCacheKeyAndPage($cacheKey, $page);
|
||||
|
||||
// update CMS page and assert cache is invalidated
|
||||
$page->setIdentifier('1-column-page-different-identifier');
|
||||
$this->pageRepository->save($page);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->test($cacheKey),
|
||||
'Cache entry still exists for CMS page'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoDataFixture Magento/Cms/Fixtures/page_list.php
|
||||
* @return void
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function testCmsPageResolverCacheWithPostRequest()
|
||||
{
|
||||
$page = $this->getPageByTitle('Page with 1column layout');
|
||||
|
||||
$getGraphQlClient = new \ReflectionMethod($this, 'getGraphQlClient');
|
||||
$getGraphQlClient->setAccessible(true);
|
||||
|
||||
$query = $this->getQuery($page->getIdentifier());
|
||||
$getGraphQlClient->invoke($this)->postWithResponseHeaders($query);
|
||||
|
||||
$cacheKey = $this->getResolverCacheKeyForPage($page);
|
||||
|
||||
$cacheEntry = $this->graphQlResolverCache->load($cacheKey);
|
||||
$cacheEntryDecoded = json_decode($cacheEntry, true);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
$this->generateExpectedDataFromPage($page),
|
||||
$cacheEntryDecoded
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Cms/Fixtures/page_list.php
|
||||
* @return void
|
||||
*/
|
||||
public function testCmsPageResolverCacheGeneratesSeparateEntriesBasedOnArgumentsAndContext()
|
||||
{
|
||||
$titles = ['Page with 1column layout', 'Page with unavailable layout'];
|
||||
|
||||
$authHeader = [
|
||||
'Authorization' => 'Bearer ' . $this->customerTokenService->createCustomerAccessToken(
|
||||
'customer@example.com',
|
||||
'password'
|
||||
)
|
||||
];
|
||||
|
||||
$customer = $this->customerRepository->get('customer@example.com');
|
||||
|
||||
foreach ($titles as $title) {
|
||||
$page = $this->getPageByTitle($title);
|
||||
|
||||
// query $page as guest
|
||||
$query = $this->getQuery($page->getIdentifier());
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$this->mockGuestUserInfoContext();
|
||||
$resolverCacheKeyForGuestQuery = $this->getResolverCacheKeyForPage($page);
|
||||
|
||||
$cacheEntry = $this->graphQlResolverCache->load($resolverCacheKeyForGuestQuery);
|
||||
$cacheEntryDecoded = json_decode($cacheEntry, true);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
$this->generateExpectedDataFromPage($page),
|
||||
$cacheEntryDecoded
|
||||
);
|
||||
|
||||
$this->assertTagsByCacheKeyAndPage($resolverCacheKeyForGuestQuery, $page);
|
||||
|
||||
$resolverCacheKeys[] = $resolverCacheKeyForGuestQuery;
|
||||
|
||||
// query $page as customer
|
||||
$query = $this->getQuery($page->getIdentifier());
|
||||
$this->graphQlQueryWithResponseHeaders(
|
||||
$query,
|
||||
[],
|
||||
'',
|
||||
$authHeader
|
||||
);
|
||||
|
||||
$this->mockCustomerUserInfoContext($customer);
|
||||
$resolverCacheKeyForUserQuery = $this->getResolverCacheKeyForPage($page);
|
||||
|
||||
$cacheEntry = $this->graphQlResolverCache->load($resolverCacheKeyForUserQuery);
|
||||
$cacheEntryDecoded = json_decode($cacheEntry, true);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
$this->generateExpectedDataFromPage($page),
|
||||
$cacheEntryDecoded
|
||||
);
|
||||
|
||||
$this->assertTagsByCacheKeyAndPage($resolverCacheKeyForUserQuery, $page);
|
||||
|
||||
$resolverCacheKeys[] = $resolverCacheKeyForUserQuery;
|
||||
}
|
||||
|
||||
// assert that every cache key is unique
|
||||
$this->assertCount(count($resolverCacheKeys), array_unique($resolverCacheKeys));
|
||||
|
||||
foreach ($resolverCacheKeys as $cacheKey) {
|
||||
$this->assertNotFalse($this->graphQlResolverCache->load($cacheKey));
|
||||
}
|
||||
|
||||
// invalidate first page and assert first two cache keys (guest and user) are invalidated,
|
||||
// while the rest are not
|
||||
$page = $this->getPageByTitle($titles[0]);
|
||||
$page->setMetaDescription('whatever');
|
||||
$this->pageRepository->save($page);
|
||||
|
||||
list($page1GuestKey, $page1UserKey, $page2GuestKey, $page2UserKey) = $resolverCacheKeys;
|
||||
|
||||
$this->assertFalse($this->graphQlResolverCache->load($page1GuestKey));
|
||||
$this->assertFalse($this->graphQlResolverCache->load($page1UserKey));
|
||||
|
||||
$this->assertNotFalse($this->graphQlResolverCache->load($page2GuestKey));
|
||||
$this->assertNotFalse($this->graphQlResolverCache->load($page2UserKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoDataFixture Magento/Cms/Fixtures/page_list.php
|
||||
* @return void
|
||||
* @throws \Magento\Framework\Exception\CouldNotDeleteException
|
||||
*/
|
||||
public function testCmsPageResolverCacheInvalidatesWhenPageGetsDeleted()
|
||||
{
|
||||
// cache page1
|
||||
$page1 = $this->getPageByTitle('Page with 1column layout');
|
||||
|
||||
$query = $this->getQuery($page1->getIdentifier());
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$cacheKeyPage1 = $this->getResolverCacheKeyForPage($page1);
|
||||
|
||||
$this->assertIsNumeric(
|
||||
$this->graphQlResolverCache->test($cacheKeyPage1)
|
||||
);
|
||||
|
||||
// cache page2
|
||||
$page2 = $this->getPageByTitle('Page with unavailable layout');
|
||||
|
||||
$query = $this->getQuery($page2->getIdentifier());
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$cacheKeyPage2 = $this->getResolverCacheKeyForPage($page2);
|
||||
|
||||
$this->assertIsNumeric(
|
||||
$this->graphQlResolverCache->test($cacheKeyPage2)
|
||||
);
|
||||
|
||||
// delete page1 and assert cache is invalidated
|
||||
$this->pageRepository->delete($page1);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->test($cacheKeyPage1),
|
||||
'Cache entry still exists for deleted CMS page'
|
||||
);
|
||||
|
||||
// assert page2 cache entry still exists
|
||||
$this->assertIsNumeric(
|
||||
$this->graphQlResolverCache->test($cacheKeyPage2)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoDataFixture Magento/Cms/Fixtures/page_list.php
|
||||
* @return void
|
||||
*/
|
||||
public function testCmsPageResolverCacheInvalidatesWhenPageGetsDisabled()
|
||||
{
|
||||
// cache page1
|
||||
$page1 = $this->getPageByTitle('Page with 1column layout');
|
||||
|
||||
$query = $this->getQuery($page1->getIdentifier());
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$cacheKeyPage1 = $this->getResolverCacheKeyForPage($page1);
|
||||
|
||||
$this->assertIsNumeric(
|
||||
$this->graphQlResolverCache->test($cacheKeyPage1)
|
||||
);
|
||||
|
||||
// cache page2
|
||||
$page2 = $this->getPageByTitle('Page with unavailable layout');
|
||||
|
||||
$query = $this->getQuery($page2->getIdentifier());
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$cacheKeyPage2 = $this->getResolverCacheKeyForPage($page2);
|
||||
|
||||
$this->assertIsNumeric(
|
||||
$this->graphQlResolverCache->test($cacheKeyPage2)
|
||||
);
|
||||
|
||||
// disable page 1
|
||||
$page1->setIsActive(false);
|
||||
$this->pageRepository->save($page1);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->test($cacheKeyPage1),
|
||||
'Cache entry still exists for disabled CMS page'
|
||||
);
|
||||
|
||||
// assert page2 cache entry still exists
|
||||
$this->assertIsNumeric(
|
||||
$this->graphQlResolverCache->test($cacheKeyPage2)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoDataFixture Magento/Cms/Fixtures/page_list.php
|
||||
* @return void
|
||||
*/
|
||||
public function testCmsPageResolverCacheDoesNotSaveNonExistentCmsPage()
|
||||
{
|
||||
$nonExistentPage = ObjectManager::getInstance()->create(PageInterface::class);
|
||||
$nonExistentPage->setIdentifier('non-existent-page');
|
||||
|
||||
$query = $this->getQuery($nonExistentPage->getIdentifier());
|
||||
|
||||
try {
|
||||
$this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->fail('Expected exception was not thrown');
|
||||
} catch (ResponseContainsErrorsException $e) {
|
||||
// expected exception
|
||||
}
|
||||
|
||||
$cacheKey = $this->getResolverCacheKeyForPage($nonExistentPage);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->load($cacheKey)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoDataFixture Magento/Store/_files/second_store.php
|
||||
* @magentoDataFixture Magento/Cms/Fixtures/page_list.php
|
||||
* @return void
|
||||
*/
|
||||
public function testCmsResolverCacheIsInvalidatedAfterChangingItsStoreView()
|
||||
{
|
||||
/** @var \Magento\Cms\Model\Page $page */
|
||||
$page = $this->getPageByTitle('Page with 1column layout');
|
||||
|
||||
// query first page in default store and assert cache entry is created; use default store header
|
||||
$query = $this->getQuery($page->getIdentifier());
|
||||
|
||||
$this->graphQlQueryWithResponseHeaders(
|
||||
$query
|
||||
);
|
||||
|
||||
$cacheKey = $this->getResolverCacheKeyForPage($page);
|
||||
|
||||
$this->assertIsNumeric(
|
||||
$this->graphQlResolverCache->test($cacheKey)
|
||||
);
|
||||
|
||||
// change store id of page
|
||||
$secondStoreViewId = $this->storeManager->getStore('fixture_second_store')->getId();
|
||||
$page->setStoreId($secondStoreViewId);
|
||||
$this->pageRepository->save($page);
|
||||
|
||||
// assert cache entry is invalidated
|
||||
$this->assertFalse(
|
||||
$this->graphQlResolverCache->test($cacheKey)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that resolver cache is saved with default TTL
|
||||
*
|
||||
* @magentoDataFixture Magento/Cms/Fixtures/page_list.php
|
||||
* @return void
|
||||
*/
|
||||
public function testCacheExpirationTimeUsesDefaultDirective()
|
||||
{
|
||||
$page = $this->getPageByTitle('Page with 1column layout');
|
||||
$query = $this->getQuery($page->getIdentifier());
|
||||
$this->graphQlQueryWithResponseHeaders(
|
||||
$query
|
||||
);
|
||||
|
||||
$cacheKey = $this->getResolverCacheKeyForPage($page);
|
||||
|
||||
$lowLevelFrontendCache = $this->graphQlResolverCache->getLowLevelFrontend();
|
||||
$metadatas = $lowLevelFrontendCache->getMetadatas($cacheKey);
|
||||
|
||||
$this->assertEquals(
|
||||
$metadatas['mtime'] + CacheFrontendFactory::DEFAULT_LIFETIME,
|
||||
$metadatas['expire']
|
||||
);
|
||||
}
|
||||
|
||||
private function generateExpectedDataFromPage(PageInterface $page): array
|
||||
{
|
||||
return [
|
||||
'page_id' => $page->getId(),
|
||||
'identifier' => $page->getIdentifier(),
|
||||
'url_key' => $page->getIdentifier(),
|
||||
'title' => $page->getTitle(),
|
||||
'content' => $page->getContent(),
|
||||
'content_heading' => $page->getContentHeading(),
|
||||
'page_layout' => $page->getPageLayout(),
|
||||
'meta_keywords' => $page->getMetaKeywords(),
|
||||
'meta_title' => $page->getMetaTitle(),
|
||||
'meta_description' => $page->getMetaDescription(),
|
||||
];
|
||||
}
|
||||
|
||||
private function assertTagsByCacheKeyAndPage(string $cacheKey, PageInterface $page): void
|
||||
{
|
||||
$lowLevelFrontendCache = $this->graphQlResolverCache->getLowLevelFrontend();
|
||||
$cacheIdPrefix = $lowLevelFrontendCache->getOption('cache_id_prefix');
|
||||
$metadatas = $lowLevelFrontendCache->getMetadatas($cacheKey);
|
||||
$tags = $metadatas['tags'];
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
[
|
||||
$cacheIdPrefix . strtoupper(CmsPage::CACHE_TAG) . '_' . $page->getId(),
|
||||
$cacheIdPrefix . strtoupper(GraphQlResolverCache::CACHE_TAG),
|
||||
$cacheIdPrefix . 'MAGE',
|
||||
],
|
||||
$tags
|
||||
);
|
||||
}
|
||||
|
||||
private function getPageByTitle(string $title): PageInterface
|
||||
{
|
||||
$searchCriteria = $this->searchCriteriaBuilder
|
||||
->addFilter('title', $title)
|
||||
->create();
|
||||
|
||||
$pages = $this->pageRepository->getList($searchCriteria)->getItems();
|
||||
|
||||
/** @var PageInterface $page */
|
||||
$page = reset($pages);
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
private function getQuery(string $identifier): string
|
||||
{
|
||||
return <<<QUERY
|
||||
{
|
||||
cmsPage(identifier: "$identifier") {
|
||||
title
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create resolver key with key calculator retriever vis the actual key provider.
|
||||
*
|
||||
* @param PageInterface $page
|
||||
* @return string
|
||||
*/
|
||||
private function getResolverCacheKeyForPage(PageInterface $page): string
|
||||
{
|
||||
$resolverMock = $this->getMockBuilder(Page::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
/** @var ProviderInterface $cacheKeyCalculatorProvider */
|
||||
$cacheKeyCalculatorProvider = ObjectManager::getInstance()->get(ProviderInterface::class);
|
||||
$cacheKeyFactor = $cacheKeyCalculatorProvider->getKeyCalculatorForResolver($resolverMock)->calculateCacheKey();
|
||||
|
||||
$cacheKeyQueryPayloadMetadata = sprintf(Page::class . '\Interceptor%s', json_encode([
|
||||
'identifier' => $page->getIdentifier(),
|
||||
]));
|
||||
|
||||
$cacheKeyParts = [
|
||||
GraphQlResolverCache::CACHE_TAG,
|
||||
$cacheKeyFactor,
|
||||
sha1($cacheKeyQueryPayloadMetadata)
|
||||
];
|
||||
|
||||
// strtoupper is called in \Magento\Framework\Cache\Frontend\Adapter\Zend::_unifyId
|
||||
return strtoupper(implode('_', $cacheKeyParts));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user