ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\PageCache;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
/**
|
||||
* Test the caching works properly for products and categories
|
||||
*/
|
||||
class CacheTagTest extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->markTestSkipped(
|
||||
'This test will stay skipped until DEVOPS-4924 is resolved'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if Magento cache tags and debug headers for products are generated properly
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
|
||||
*/
|
||||
public function testCacheTagsAndCacheDebugHeaderForProducts()
|
||||
{
|
||||
$productSku='simple2';
|
||||
$query
|
||||
= <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items {
|
||||
id
|
||||
name
|
||||
sku
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
// Cache-debug should be a MISS when product is queried for first time
|
||||
$responseMiss = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseMiss['headers']);
|
||||
$this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
// Cache-debug should be a HIT for the second round
|
||||
$responseHit = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseHit['headers']);
|
||||
$this->assertEquals('HIT', $responseHit['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
|
||||
/** @var Product $product */
|
||||
$product = $productRepository->get($productSku, false, null, true);
|
||||
$product->setPrice(15);
|
||||
$productRepository->save($product);
|
||||
// Cache invalidation happens and cache-debug header value is a MISS after product update
|
||||
$responseMiss = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseMiss['headers']);
|
||||
$this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']);
|
||||
$this->assertArrayHasKey('X-Magento-Tags', $responseMiss['headers']);
|
||||
$expectedCacheTags = ['cat_p','cat_p_' . $product->getId(),'FPC'];
|
||||
$actualCacheTags = explode(',', $responseMiss['headers']['X-Magento-Tags']);
|
||||
foreach ($expectedCacheTags as $expectedCacheTag) {
|
||||
$this->assertContains($expectedCacheTag, $actualCacheTags);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if X-Magento-Tags for categories are generated properly
|
||||
*
|
||||
* Also tests the use case for cache invalidation
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_in_multiple_categories.php
|
||||
*/
|
||||
public function testCacheTagForCategoriesWithProduct()
|
||||
{
|
||||
$firstProductSku = 'simple333';
|
||||
$secondProductSku = 'simple444';
|
||||
$categoryId ='4';
|
||||
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
|
||||
/** @var Product $firstProduct */
|
||||
$firstProduct = $productRepository->get($firstProductSku, false, null, true);
|
||||
/** @var Product $secondProduct */
|
||||
$secondProduct = $productRepository->get($secondProductSku, false, null, true);
|
||||
|
||||
$categoryQueryVariables =[
|
||||
'id' => $categoryId,
|
||||
'pageSize'=> 10,
|
||||
'currentPage' => 1
|
||||
];
|
||||
|
||||
$product1Query = $this->getProductQuery($firstProductSku);
|
||||
$product2Query =$this->getProductQuery($secondProductSku);
|
||||
$categoryQuery = $this->getCategoryQuery();
|
||||
|
||||
// cache-debug header value should be a MISS when category is loaded first time
|
||||
$responseMiss = $this->graphQlQueryWithResponseHeaders($categoryQuery, $categoryQueryVariables);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseMiss['headers']);
|
||||
$this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']);
|
||||
$this->assertArrayHasKey('X-Magento-Tags', $responseMiss['headers']);
|
||||
$actualCacheTags = explode(',', $responseMiss['headers']['X-Magento-Tags']);
|
||||
$expectedCacheTags =
|
||||
[
|
||||
'cat_c',
|
||||
'cat_c_' . $categoryId,
|
||||
'cat_p',
|
||||
'cat_p_' . $firstProduct->getId(),
|
||||
'cat_p_' . $secondProduct->getId(),
|
||||
'FPC'
|
||||
];
|
||||
$this->assertEquals($expectedCacheTags, $actualCacheTags);
|
||||
|
||||
// Cache-debug header should be a MISS for product 1 on first request
|
||||
$responseFirstProduct = $this->graphQlQueryWithResponseHeaders($product1Query);
|
||||
$this->assertEquals('MISS', $responseFirstProduct['headers']['X-Magento-Cache-Debug']);
|
||||
// Cache-debug header should be a MISS for product 2 during first load
|
||||
$responseSecondProduct = $this->graphQlQueryWithResponseHeaders($product2Query);
|
||||
$this->assertEquals('MISS', $responseSecondProduct['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
$firstProduct->setPrice(20);
|
||||
$productRepository->save($firstProduct);
|
||||
// cache-debug header value should be MISS after updating product1 and reloading the Category
|
||||
$responseMissCategory = $this->graphQlQueryWithResponseHeaders($categoryQuery, $categoryQueryVariables);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseMissCategory['headers']);
|
||||
$this->assertEquals('MISS', $responseMissCategory['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
// cache-debug should be a MISS for product 1 after it is updated - cache invalidation
|
||||
$responseMissFirstProduct = $this->graphQlQueryWithResponseHeaders($product1Query);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseMissFirstProduct['headers']);
|
||||
$this->assertEquals('MISS', $responseMissFirstProduct['headers']['X-Magento-Cache-Debug']);
|
||||
// Cache-debug header should be a HIT for product 2
|
||||
$responseHitSecondProduct = $this->graphQlQueryWithResponseHeaders($product2Query);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseHitSecondProduct['headers']);
|
||||
$this->assertEquals('HIT', $responseHitSecondProduct['headers']['X-Magento-Cache-Debug']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Product query
|
||||
*
|
||||
* @param string $productSku
|
||||
* @return string
|
||||
*/
|
||||
private function getProductQuery(string $productSku): string
|
||||
{
|
||||
$productQuery = <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items {
|
||||
id
|
||||
name
|
||||
sku
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
return $productQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get category query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getCategoryQuery(): string
|
||||
{
|
||||
$categoryQueryString = <<<QUERY
|
||||
query GetCategoryQuery(\$id: Int!, \$pageSize: Int!, \$currentPage: Int!) {
|
||||
category(id: \$id) {
|
||||
id
|
||||
description
|
||||
name
|
||||
product_count
|
||||
products(pageSize: \$pageSize, currentPage: \$currentPage) {
|
||||
items {
|
||||
id
|
||||
name
|
||||
url_key
|
||||
}
|
||||
total_count
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
return $categoryQueryString;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\PageCache\Cms;
|
||||
|
||||
use Magento\Cms\Model\Block;
|
||||
use Magento\Cms\Model\BlockRepository;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
/**
|
||||
* Test the caching works properly for CMS Blocks
|
||||
*/
|
||||
class BlockCacheTest extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->markTestSkipped(
|
||||
'This test will stay skipped until DEVOPS-4924 is resolved'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that X-Magento-Tags are correct
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Cms/_files/block.php
|
||||
*/
|
||||
public function testCacheTagsHaveExpectedValue()
|
||||
{
|
||||
$blockIdentifier = 'fixture_block';
|
||||
$blockRepository = Bootstrap::getObjectManager()->get(BlockRepository::class);
|
||||
$block = $blockRepository->getById($blockIdentifier);
|
||||
$blockId = $block->getId();
|
||||
$query = $this->getBlockQuery([$blockIdentifier]);
|
||||
|
||||
//cache-debug should be a MISS on first request
|
||||
$response = $this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$this->assertArrayHasKey('X-Magento-Tags', $response['headers']);
|
||||
$actualTags = explode(',', $response['headers']['X-Magento-Tags']);
|
||||
$expectedTags = ["cms_b", "cms_b_{$blockId}", "cms_b_{$blockIdentifier}", "FPC"];
|
||||
$this->assertEquals($expectedTags, $actualTags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the second request for the same block will return a cached result
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Cms/_files/block.php
|
||||
*/
|
||||
public function testCacheIsUsedOnSecondRequest()
|
||||
{
|
||||
$blockIdentifier = 'fixture_block';
|
||||
$query = $this->getBlockQuery([$blockIdentifier]);
|
||||
|
||||
//cache-debug should be a MISS on first request
|
||||
$responseMiss = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseMiss['headers']);
|
||||
$this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
//cache-debug should be a HIT on second request
|
||||
$responseHit = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseHit['headers']);
|
||||
$this->assertEquals('HIT', $responseHit['headers']['X-Magento-Cache-Debug']);
|
||||
//cached data should be correct
|
||||
$this->assertNotEmpty($responseHit['body']);
|
||||
$this->assertArrayNotHasKey('errors', $responseHit['body']);
|
||||
$blocks = $responseHit['body']['cmsBlocks']['items'];
|
||||
$this->assertEquals($blockIdentifier, $blocks[0]['identifier']);
|
||||
$this->assertEquals('CMS Block Title', $blocks[0]['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that cache is invalidated when block is updated
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Cms/_files/blocks.php
|
||||
* @magentoApiDataFixture Magento/Cms/_files/block.php
|
||||
*/
|
||||
public function testCacheIsInvalidatedOnBlockUpdate()
|
||||
{
|
||||
$fixtureBlockIdentifier = 'fixture_block';
|
||||
$enabledBlockIdentifier = 'enabled_block';
|
||||
$fixtureBlockQuery = $this->getBlockQuery([$fixtureBlockIdentifier]);
|
||||
$enabledBlockQuery = $this->getBlockQuery([$enabledBlockIdentifier]);
|
||||
|
||||
//cache-debug should be a MISS on first request
|
||||
$fixtureBlockMiss = $this->graphQlQueryWithResponseHeaders($fixtureBlockQuery);
|
||||
$this->assertEquals('MISS', $fixtureBlockMiss['headers']['X-Magento-Cache-Debug']);
|
||||
$enabledBlockMiss = $this->graphQlQueryWithResponseHeaders($enabledBlockQuery);
|
||||
$this->assertEquals('MISS', $enabledBlockMiss['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
//cache-debug should be a HIT on second request
|
||||
$fixtureBlockHit = $this->graphQlQueryWithResponseHeaders($fixtureBlockQuery);
|
||||
$this->assertEquals('HIT', $fixtureBlockHit['headers']['X-Magento-Cache-Debug']);
|
||||
$enabledBlockHit = $this->graphQlQueryWithResponseHeaders($enabledBlockQuery);
|
||||
$this->assertEquals('HIT', $enabledBlockHit['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
$newBlockContent = 'New block content!!!';
|
||||
$this->updateBlockContent($fixtureBlockIdentifier, $newBlockContent);
|
||||
|
||||
//cache-debug should be a MISS after update the block
|
||||
$fixtureBlockMiss = $this->graphQlQueryWithResponseHeaders($fixtureBlockQuery);
|
||||
$this->assertEquals('MISS', $fixtureBlockMiss['headers']['X-Magento-Cache-Debug']);
|
||||
$enabledBlockHit = $this->graphQlQueryWithResponseHeaders($enabledBlockQuery);
|
||||
$this->assertEquals('HIT', $enabledBlockHit['headers']['X-Magento-Cache-Debug']);
|
||||
//updated block data should be correct
|
||||
$this->assertNotEmpty($fixtureBlockMiss['body']);
|
||||
$blocks = $fixtureBlockMiss['body']['cmsBlocks']['items'];
|
||||
$this->assertArrayNotHasKey('errors', $fixtureBlockMiss['body']);
|
||||
$this->assertEquals($fixtureBlockIdentifier, $blocks[0]['identifier']);
|
||||
$this->assertEquals('CMS Block Title', $blocks[0]['title']);
|
||||
$this->assertEquals($newBlockContent, $blocks[0]['content']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the content of a CMS block
|
||||
*
|
||||
* @param $identifier
|
||||
* @param $newContent
|
||||
* @return Block
|
||||
*/
|
||||
private function updateBlockContent($identifier, $newContent): Block
|
||||
{
|
||||
$blockRepository = Bootstrap::getObjectManager()->get(BlockRepository::class);
|
||||
$block = $blockRepository->getById($identifier);
|
||||
$block->setContent($newContent);
|
||||
$blockRepository->save($block);
|
||||
|
||||
return $block;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cmsBlocks query
|
||||
*
|
||||
* @param array $identifiers
|
||||
* @return string
|
||||
*/
|
||||
private function getBlockQuery(array $identifiers): string
|
||||
{
|
||||
$identifiersString = implode(',', $identifiers);
|
||||
$query = <<<QUERY
|
||||
{
|
||||
cmsBlocks(identifiers: ["$identifiersString"]) {
|
||||
items {
|
||||
title
|
||||
identifier
|
||||
content
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\PageCache\Cms;
|
||||
|
||||
use Magento\Cms\Model\GetPageByIdentifier;
|
||||
use Magento\Cms\Model\PageRepository;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
/**
|
||||
* Test the caching works properly for CMS Pages
|
||||
*/
|
||||
class PageCacheTest extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* @var GetPageByIdentifier
|
||||
*/
|
||||
private $pageByIdentifier;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->markTestSkipped(
|
||||
'This test will stay skipped until DEVOPS-4924 is resolved'
|
||||
);
|
||||
$this->pageByIdentifier = Bootstrap::getObjectManager()->get(GetPageByIdentifier::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that X-Magento-Tags are correct
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Cms/_files/pages.php
|
||||
*/
|
||||
public function testCacheTagsHaveExpectedValue()
|
||||
{
|
||||
$pageIdentifier = 'page100';
|
||||
$page = $this->pageByIdentifier->execute($pageIdentifier, 0);
|
||||
$pageId = (int) $page->getId();
|
||||
|
||||
$query = $this->getPageQuery($pageId);
|
||||
|
||||
//cache-debug should be a MISS on first request
|
||||
$response = $this->graphQlQueryWithResponseHeaders($query);
|
||||
|
||||
$this->assertArrayHasKey('X-Magento-Tags', $response['headers']);
|
||||
$actualTags = explode(',', $response['headers']['X-Magento-Tags']);
|
||||
$expectedTags = ["cms_p", "cms_p_{$pageId}", "FPC"];
|
||||
$this->assertEquals($expectedTags, $actualTags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the second request for the same page will return a cached result
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Cms/_files/pages.php
|
||||
*/
|
||||
public function testCacheIsUsedOnSecondRequest()
|
||||
{
|
||||
$pageIdentifier = 'page100';
|
||||
$page = $this->pageByIdentifier->execute($pageIdentifier, 0);
|
||||
$pageId = (int) $page->getId();
|
||||
|
||||
$query = $this->getPageQuery($pageId);
|
||||
|
||||
//cache-debug should be a MISS on first request
|
||||
$responseMiss = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseMiss['headers']);
|
||||
$this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
//cache-debug should be a HIT on second request
|
||||
$responseHit = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseHit['headers']);
|
||||
$this->assertEquals('HIT', $responseHit['headers']['X-Magento-Cache-Debug']);
|
||||
//cached data should be correct
|
||||
$this->assertNotEmpty($responseHit['body']);
|
||||
$this->assertArrayNotHasKey('errors', $responseHit['body']);
|
||||
$pageData = $responseHit['body']['cmsPage'];
|
||||
$this->assertEquals('Cms Page 100', $pageData['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that cache is invalidated when page is updated
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Cms/_files/pages.php
|
||||
*/
|
||||
public function testCacheIsInvalidatedOnPageUpdate()
|
||||
{
|
||||
$page100Identifier = 'page100';
|
||||
$page100 = $this->pageByIdentifier->execute($page100Identifier, 0);
|
||||
$page100Id = (int) $page100->getId();
|
||||
$pageBlankIdentifier = 'page_design_blank';
|
||||
$pageBlank = $this->pageByIdentifier->execute($pageBlankIdentifier, 0);
|
||||
$pageBlankId = (int) $pageBlank->getId();
|
||||
|
||||
$page100Query = $this->getPageQuery($page100Id);
|
||||
$pageBlankQuery = $this->getPageQuery($pageBlankId);
|
||||
|
||||
//cache-debug should be a MISS on first request
|
||||
$page100Miss = $this->graphQlQueryWithResponseHeaders($page100Query);
|
||||
$this->assertEquals('MISS', $page100Miss['headers']['X-Magento-Cache-Debug']);
|
||||
$pageBlankMiss = $this->graphQlQueryWithResponseHeaders($pageBlankQuery);
|
||||
$this->assertEquals('MISS', $pageBlankMiss['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
//cache-debug should be a HIT on second request
|
||||
$page100Hit = $this->graphQlQueryWithResponseHeaders($page100Query);
|
||||
$this->assertEquals('HIT', $page100Hit['headers']['X-Magento-Cache-Debug']);
|
||||
$pageBlankHit = $this->graphQlQueryWithResponseHeaders($pageBlankQuery);
|
||||
$this->assertEquals('HIT', $pageBlankHit['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
$pageRepository = Bootstrap::getObjectManager()->get(PageRepository::class);
|
||||
$newPageContent = 'New page content for blank page.';
|
||||
$pageBlank->setContent($newPageContent);
|
||||
$pageRepository->save($pageBlank);
|
||||
|
||||
//cache-debug should be a MISS after updating the page
|
||||
$pageBlankMiss = $this->graphQlQueryWithResponseHeaders($pageBlankQuery);
|
||||
$this->assertEquals('MISS', $pageBlankMiss['headers']['X-Magento-Cache-Debug']);
|
||||
$page100Hit = $this->graphQlQueryWithResponseHeaders($page100Query);
|
||||
$this->assertEquals('HIT', $page100Hit['headers']['X-Magento-Cache-Debug']);
|
||||
//updated page data should be correct
|
||||
$this->assertNotEmpty($pageBlankMiss['body']);
|
||||
$pageData = $pageBlankMiss['body']['cmsPage'];
|
||||
$this->assertArrayNotHasKey('errors', $pageBlankMiss['body']);
|
||||
$this->assertEquals('Cms Page Design Blank', $pageData['title']);
|
||||
$this->assertEquals($newPageContent, $pageData['content']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get page query
|
||||
*
|
||||
* @param int $pageId
|
||||
* @return string
|
||||
*/
|
||||
private function getPageQuery(int $pageId): string
|
||||
{
|
||||
$query = <<<QUERY
|
||||
{
|
||||
cmsPage(id: $pageId) {
|
||||
title
|
||||
url_key
|
||||
content
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\PageCache;
|
||||
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
/**
|
||||
* Abstract class for GraphQL page cache test
|
||||
*/
|
||||
class GraphQLPageCacheAbstract extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* Assert that we obtain a cache MISS when sending the provided query & headers.
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $headers
|
||||
* @return array
|
||||
*/
|
||||
protected function assertCacheMissAndReturnResponse(string $query, array $headers) :array
|
||||
{
|
||||
$responseMiss = $this->graphQlQueryWithResponseHeaders($query, [], '', $headers);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseMiss['headers']);
|
||||
$this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']);
|
||||
return $responseMiss;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that we obtain a cache HIT when sending the provided query & headers.
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $headers
|
||||
* @return array
|
||||
*/
|
||||
protected function assertCacheHitAndReturnResponse(string $query, array $headers) :array
|
||||
{
|
||||
$responseHit = $this->graphQlQueryWithResponseHeaders($query, [], '', $headers);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseHit['headers']);
|
||||
$this->assertEquals('HIT', $responseHit['headers']['X-Magento-Cache-Debug']);
|
||||
return $responseHit;
|
||||
}
|
||||
}
|
||||
+313
@@ -0,0 +1,313 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\PageCache;
|
||||
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
/**
|
||||
* This test considers FPC caching but it can't check the headers since we don't guarantee that test if in dev mode.
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
class ProductInMultipleStoresCacheTest extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
/** @var \Magento\Store\Model\Store $store */
|
||||
$store = ObjectManager::getInstance()->get(\Magento\Store\Model\Store::class);
|
||||
$storeCodeFromFixture = 'fixture_second_store';
|
||||
|
||||
/** @var \Magento\Config\Model\ResourceModel\Config $configResource */
|
||||
$configResource = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
|
||||
/** @var \Magento\Config\App\Config\Type\System $config */
|
||||
$config = ObjectManager::getInstance()->get(\Magento\Config\App\Config\Type\System::class);
|
||||
|
||||
$configResource->saveConfig(
|
||||
\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_DEFAULT,
|
||||
'EUR',
|
||||
\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES,
|
||||
$store->load($storeCodeFromFixture)->getWebsiteId()
|
||||
);
|
||||
|
||||
// allow USD & EUR currency
|
||||
$configResource->saveConfig(
|
||||
\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_ALLOW,
|
||||
'EUR,USD',
|
||||
\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES,
|
||||
$store->load($storeCodeFromFixture)->getWebsiteId()
|
||||
);
|
||||
|
||||
// allow USD & EUR currency
|
||||
$configResource->saveConfig(
|
||||
\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_ALLOW,
|
||||
'EUR,USD'
|
||||
);
|
||||
|
||||
// configuration cache clean is required to reload currency setting
|
||||
$config->clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
/** @var \Magento\Config\App\Config\Type\System $config */
|
||||
$config = ObjectManager::getInstance()->get(\Magento\Config\App\Config\Type\System::class);
|
||||
/** @var \Magento\Config\Model\ResourceModel\Config $configResource */
|
||||
$configResource = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
|
||||
|
||||
// restore allow USD currency
|
||||
$configResource->saveConfig(
|
||||
\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_ALLOW,
|
||||
'USD'
|
||||
);
|
||||
|
||||
// configuration cache clean is required to reload currency setting
|
||||
$config->clean();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a non existing or non existing currency
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Store/_files/second_website_with_second_currency.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function testProductFromSpecificAndDefaultStoreWithMultiCurrencyNonExisting()
|
||||
{
|
||||
$productSku = 'simple';
|
||||
|
||||
$query = <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items {
|
||||
id
|
||||
name
|
||||
price {
|
||||
minimalPrice {
|
||||
amount {
|
||||
value
|
||||
currency
|
||||
}
|
||||
}
|
||||
}
|
||||
sku
|
||||
type_id
|
||||
... on PhysicalProductInterface {
|
||||
weight
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
//test non existing currency
|
||||
$headerMap = ['Store' => 'default', 'Content-Currency' => 'someNonExistentCurrency'];
|
||||
$this->expectExceptionMessage('GraphQL response contains errors: Please correct the target currency');
|
||||
$this->graphQlQuery($query, [], '', $headerMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a non existing or non allowed currency
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Store/_files/second_website_with_second_currency.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function testProductFromSpecificAndDefaultStoreWithMultiCurrencyNotAllowed()
|
||||
{
|
||||
$productSku = 'simple';
|
||||
|
||||
$query = <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items {
|
||||
id
|
||||
name
|
||||
price {
|
||||
minimalPrice {
|
||||
amount {
|
||||
value
|
||||
currency
|
||||
}
|
||||
}
|
||||
}
|
||||
sku
|
||||
type_id
|
||||
... on PhysicalProductInterface {
|
||||
weight
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$storeCodeFromFixture = 'fixture_second_store';
|
||||
|
||||
//test not allowed existing currency
|
||||
$headerMap = ['Store' => $storeCodeFromFixture, 'Content-Currency' => 'CAD'];
|
||||
$this->expectExceptionMessage(
|
||||
'GraphQL response contains errors: Please correct the target currency'
|
||||
);
|
||||
$this->graphQlQuery($query, [], '', $headerMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a product from a custom and default store, with cache with repeating queries asserting different results.
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Store/_files/second_website_with_second_currency.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function testProductFromSpecificAndDefaultStoreWithMultiCurrency()
|
||||
{
|
||||
$productSku = 'simple';
|
||||
|
||||
$query = <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items {
|
||||
id
|
||||
name
|
||||
price {
|
||||
minimalPrice {
|
||||
amount {
|
||||
value
|
||||
currency
|
||||
}
|
||||
}
|
||||
}
|
||||
sku
|
||||
type_id
|
||||
... on PhysicalProductInterface {
|
||||
weight
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
/** @var \Magento\Store\Model\Store $store */
|
||||
$store = ObjectManager::getInstance()->get(\Magento\Store\Model\Store::class);
|
||||
$storeCodeFromFixture = 'fixture_second_store';
|
||||
$storeId = $store->load($storeCodeFromFixture)->getStoreId();
|
||||
|
||||
/** @var \Magento\Catalog\Model\Product $product */
|
||||
$product = ObjectManager::getInstance()->get(\Magento\Catalog\Model\Product::class);
|
||||
$product->load($product->getIdBySku($productSku));
|
||||
|
||||
$website = ObjectManager::getInstance()->get(\Magento\Store\Model\Website::class);
|
||||
/** @var $website \Magento\Store\Model\Website */
|
||||
$website->load('test', 'code');
|
||||
$product->setWebsiteIds([1, $website->getId()]);
|
||||
|
||||
// change product name for custom store
|
||||
$productNameInFixtureStore = 'Product\'s Name in Fixture Store';
|
||||
$product->setName($productNameInFixtureStore)->setStoreId($storeId)->save();
|
||||
|
||||
// test store header only, query is cached at this point in EUR
|
||||
$headerMap = ['Store' => $storeCodeFromFixture];
|
||||
$response = $this->graphQlQuery($query, [], '', $headerMap);
|
||||
$this->assertEquals(
|
||||
$productNameInFixtureStore,
|
||||
$response['products']['items'][0]['name'],
|
||||
'Product name in fixture store is invalid.'
|
||||
);
|
||||
$this->assertEquals(
|
||||
'EUR',
|
||||
$response['products']['items'][0]['price']['minimalPrice']['amount']['currency'],
|
||||
'Currency code EUR in fixture ' . $storeCodeFromFixture . ' is unexpected'
|
||||
);
|
||||
|
||||
// test cached store + currency header in Euros
|
||||
$headerMap = ['Store' => $storeCodeFromFixture, 'Content-Currency' => 'EUR'];
|
||||
$response = $this->graphQlQuery($query, [], '', $headerMap);
|
||||
$this->assertEquals(
|
||||
$productNameInFixtureStore,
|
||||
$response['products']['items'][0]['name'],
|
||||
'Product name in fixture ' . $storeCodeFromFixture . ' is invalid.'
|
||||
);
|
||||
$this->assertEquals(
|
||||
'EUR',
|
||||
$response['products']['items'][0]['price']['minimalPrice']['amount']['currency'],
|
||||
'Currency code EUR in fixture ' . $storeCodeFromFixture . ' is unexpected'
|
||||
);
|
||||
|
||||
// test non cached store + currency header in USD
|
||||
$headerMap = ['Store' => $storeCodeFromFixture, 'Content-Currency' => 'USD'];
|
||||
$response = $this->graphQlQuery($query, [], '', $headerMap);
|
||||
$this->assertEquals(
|
||||
$productNameInFixtureStore,
|
||||
$response['products']['items'][0]['name'],
|
||||
'Product name in fixture store is invalid.'
|
||||
);
|
||||
$this->assertEquals(
|
||||
'USD',
|
||||
$response['products']['items'][0]['price']['minimalPrice']['amount']['currency'],
|
||||
'Currency code USD in fixture ' . $storeCodeFromFixture . ' is unexpected'
|
||||
);
|
||||
|
||||
// test non cached store + currency header in USD not cached
|
||||
$headerMap = ['Store' => 'default', 'Content-Currency' => 'USD'];
|
||||
$response = $this->graphQlQuery($query, [], '', $headerMap);
|
||||
$this->assertEquals(
|
||||
'Simple Product',
|
||||
$response['products']['items'][0]['name'],
|
||||
'Product name in fixture store is invalid.'
|
||||
);
|
||||
$this->assertEquals(
|
||||
'USD',
|
||||
$response['products']['items'][0]['price']['minimalPrice']['amount']['currency'],
|
||||
'Currency code USD in fixture store default is unexpected'
|
||||
);
|
||||
|
||||
// test non cached store + currency header in USD not cached
|
||||
$headerMap = ['Store' => 'default', 'Content-Currency' => 'EUR'];
|
||||
$response = $this->graphQlQuery($query, [], '', $headerMap);
|
||||
$this->assertEquals(
|
||||
'Simple Product',
|
||||
$response['products']['items'][0]['name'],
|
||||
'Product name in fixture store is invalid.'
|
||||
);
|
||||
$this->assertEquals(
|
||||
'EUR',
|
||||
$response['products']['items'][0]['price']['minimalPrice']['amount']['currency'],
|
||||
'Currency code EUR in fixture store default is unexpected'
|
||||
);
|
||||
|
||||
// test non cached store + currency header in USD cached
|
||||
$headerMap = ['Store' => 'default'];
|
||||
$response = $this->graphQlQuery($query, [], '', $headerMap);
|
||||
$this->assertEquals(
|
||||
'Simple Product',
|
||||
$response['products']['items'][0]['name'],
|
||||
'Product name in fixture store is invalid.'
|
||||
);
|
||||
$this->assertEquals(
|
||||
'USD',
|
||||
$response['products']['items'][0]['price']['minimalPrice']['amount']['currency'],
|
||||
'Currency code USD in fixture store default is unexpected'
|
||||
);
|
||||
|
||||
// test cached response store + currency header with non existing currency, and no valid response, no cache
|
||||
$headerMap = ['Store' => $storeCodeFromFixture, 'Content-Currency' => 'SOMECURRENCY'];
|
||||
$this->expectExceptionMessage(
|
||||
'GraphQL response contains errors: Please correct the target currency'
|
||||
);
|
||||
$this->graphQlQuery($query, [], '', $headerMap);
|
||||
}
|
||||
}
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\PageCache\Quote\Guest;
|
||||
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
/**
|
||||
* Test cart queries are not cached
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/products.php
|
||||
*/
|
||||
class CartCacheTest extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->markTestSkipped(
|
||||
'This test will stay skipped until DEVOPS-4924 is resolved'
|
||||
);
|
||||
}
|
||||
|
||||
public function testCartIsNotCached()
|
||||
{
|
||||
$qty = 2;
|
||||
$sku = 'simple';
|
||||
$cartId = $this->createEmptyCart();
|
||||
$this->addSimpleProductToCart($cartId, $qty, $sku);
|
||||
|
||||
$getCartQuery = $this->getCartQuery($cartId);
|
||||
$responseMiss = $this->graphQlQueryWithResponseHeaders($getCartQuery);
|
||||
$this->assertArrayHasKey('cart', $responseMiss['body']);
|
||||
$this->assertArrayHasKey('items', $responseMiss['body']['cart']);
|
||||
$this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
/** Cache debug header value is still a MISS for any subsequent request */
|
||||
$responseMissNext = $this->graphQlQueryWithResponseHeaders($getCartQuery);
|
||||
$this->assertEquals('MISS', $responseMissNext['headers']['X-Magento-Cache-Debug']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a guest cart which generates a maskedQuoteId
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function createEmptyCart(): string
|
||||
{
|
||||
$query =
|
||||
<<<QUERY
|
||||
mutation
|
||||
{
|
||||
createEmptyCart
|
||||
}
|
||||
QUERY;
|
||||
|
||||
$response = $this->graphQlMutation($query);
|
||||
$maskedQuoteId = $response['createEmptyCart'];
|
||||
return $maskedQuoteId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add simple product to the cart using the maskedQuoteId
|
||||
*
|
||||
* @param string $maskedCartId
|
||||
* @param int $qty
|
||||
* @param string $sku
|
||||
*/
|
||||
private function addSimpleProductToCart(string $maskedCartId, int $qty, string $sku): void
|
||||
{
|
||||
$addProductToCartQuery =
|
||||
<<<QUERY
|
||||
mutation {
|
||||
addSimpleProductsToCart(
|
||||
input: {
|
||||
cart_id: "{$maskedCartId}"
|
||||
cart_items: [
|
||||
{
|
||||
data: {
|
||||
qty: $qty
|
||||
sku: "$sku"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
) {
|
||||
cart {
|
||||
items {
|
||||
qty
|
||||
product {
|
||||
sku
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
$response = $this->graphQlMutation($addProductToCartQuery);
|
||||
self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cart query string
|
||||
*
|
||||
* @param string $maskedQuoteId
|
||||
* @return string
|
||||
*/
|
||||
private function getCartQuery(string $maskedQuoteId): string
|
||||
{
|
||||
return <<<QUERY
|
||||
{
|
||||
cart(cart_id: "{$maskedQuoteId}") {
|
||||
items {
|
||||
id
|
||||
qty
|
||||
product {
|
||||
sku
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
}
|
||||
}
|
||||
+529
@@ -0,0 +1,529 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\PageCache\UrlRewrite;
|
||||
|
||||
use Magento\Catalog\Api\CategoryRepositoryInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Framework\Exception\AlreadyExistsException;
|
||||
use Magento\GraphQl\PageCache\GraphQLPageCacheAbstract;
|
||||
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\UrlRewrite\Model\ResourceModel\UrlRewrite as UrlRewriteResourceModel;
|
||||
use Magento\UrlRewrite\Model\UrlFinderInterface;
|
||||
use Magento\UrlRewrite\Model\UrlRewrite as UrlRewriteModel;
|
||||
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite as UrlRewriteService;
|
||||
|
||||
/**
|
||||
* Test caching works for url route.
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class RouteCacheTest extends GraphQLPageCacheAbstract
|
||||
{
|
||||
/** @var ObjectManager */
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if target_path(relative_url) is resolved for Product entity
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
|
||||
*/
|
||||
public function testProductUrlResolver()
|
||||
{
|
||||
$productSku = 'p002';
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$productRepository->get($productSku, false, null, true);
|
||||
|
||||
$routeQuery = $this->getRouteQuery($this->getProductUrlKey($productSku));
|
||||
$response = $this->graphQlQueryWithResponseHeaders($routeQuery);
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
|
||||
$cacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse($routeQuery, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
|
||||
// Verify we obtain a cache HIT the second time around for this X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse($routeQuery, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the use case where non seo friendly is provided as resolver input in the Query
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
|
||||
*/
|
||||
public function testProductUrlWithNonSeoFriendlyUrlInput()
|
||||
{
|
||||
$productSku = 'p002';
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$productRepository->get($productSku, false, null, true);
|
||||
|
||||
$actualUrls = $this->getProductUrlRewriteData($productSku);
|
||||
$nonSeoFriendlyPath = $actualUrls->getTargetPath();
|
||||
|
||||
$routeQuery = $this->getRouteQuery($nonSeoFriendlyPath);
|
||||
$response = $this->graphQlQueryWithResponseHeaders($routeQuery);
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
|
||||
$cacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse($routeQuery, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
|
||||
// Verify we obtain a cache HIT the second time around for this X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse($routeQuery, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the use case where url_key of the existing product is changed and verify final url is redirected correctly
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_with_category.php
|
||||
*/
|
||||
public function testProductUrlRewriteResolver()
|
||||
{
|
||||
$productSku = 'in-stock-product';
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$product = $productRepository->get($productSku, false, null, true);
|
||||
$this->getProductUrlKey($productSku);
|
||||
$renamedKey = 'simple-product-in-stock-new';
|
||||
$suffix = '.html';
|
||||
$product->setUrlKey($renamedKey)->setData('save_rewrites_history', true)->save();
|
||||
$newUrlPath = $renamedKey . $suffix;
|
||||
|
||||
$routeQuery = $this->getRouteQuery($newUrlPath);
|
||||
$response = $this->graphQlQueryWithResponseHeaders($routeQuery);
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
|
||||
$cacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse($routeQuery, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
|
||||
// Verify we obtain a cache HIT the second time around for this X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse($routeQuery, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for custom type which point to the valid product/category/cms page.
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
|
||||
*/
|
||||
public function testGetNonExistentUrlRewrite()
|
||||
{
|
||||
$productSku = 'p002';
|
||||
$urlPath = 'non-exist-product.html';
|
||||
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$productRepository->get($productSku, false, null, true);
|
||||
|
||||
/** @var UrlRewriteModel $urlRewriteModel */
|
||||
$urlRewriteModel = $this->objectManager->create(UrlRewriteModel::class);
|
||||
$urlRewriteModel->load($urlPath, 'request_path');
|
||||
|
||||
$routeQuery = $this->getRouteQuery($urlPath);
|
||||
$response = $this->graphQlQueryWithResponseHeaders($routeQuery);
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
|
||||
$cacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse($routeQuery, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
|
||||
// Verify we obtain a cache HIT the second time around for this X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse($routeQuery, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for category entity
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
|
||||
*/
|
||||
public function testCategoryUrlResolver()
|
||||
{
|
||||
$productSku = 'p002';
|
||||
$categoryUrlPath = 'cat-1.html';
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$product = $productRepository->get($productSku, false, null, true);
|
||||
$storeId = $product->getStoreId();
|
||||
|
||||
/** @var UrlFinderInterface $urlFinder */
|
||||
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
|
||||
$actualUrls = $urlFinder->findOneByData(
|
||||
[
|
||||
'request_path' => $categoryUrlPath,
|
||||
'store_id' => $storeId
|
||||
]
|
||||
);
|
||||
$categoryId = $actualUrls->getEntityId();
|
||||
$categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
|
||||
$categoryRepository->get($categoryId);
|
||||
|
||||
$query
|
||||
= <<<QUERY
|
||||
{
|
||||
category(id:{$categoryId}) {
|
||||
url_key
|
||||
url_suffix
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
$response = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
|
||||
$cacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
|
||||
// Verify we obtain a cache HIT the second time around for this X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/Cms/_files/pages.php
|
||||
*/
|
||||
public function testCMSPageUrlResolver()
|
||||
{
|
||||
/** @var \Magento\Cms\Model\Page $page */
|
||||
$page = $this->objectManager->get(\Magento\Cms\Model\Page::class);
|
||||
$page->load('page100');
|
||||
$page->getData();
|
||||
|
||||
/** @var \Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $urlPathGenerator */
|
||||
$urlPathGenerator = $this->objectManager->get(\Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator::class);
|
||||
|
||||
/** @param \Magento\Cms\Api\Data\PageInterface $page */
|
||||
$targetPath = $urlPathGenerator->getCanonicalUrlPath($page);
|
||||
|
||||
$routeQuery = $this->getRouteQuery($targetPath);
|
||||
$response = $this->graphQlQueryWithResponseHeaders($routeQuery);
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
|
||||
$cacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse($routeQuery, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
|
||||
// Verify we obtain a cache HIT the second time around for this X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse($routeQuery, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $urlKey
|
||||
* @return string
|
||||
*/
|
||||
public function getRouteQuery(string $urlKey): string
|
||||
{
|
||||
$routeQuery
|
||||
= <<<QUERY
|
||||
{
|
||||
route(url:"{$urlKey}")
|
||||
{
|
||||
__typename
|
||||
...on SimpleProduct {
|
||||
name
|
||||
sku
|
||||
relative_url
|
||||
redirect_code
|
||||
type
|
||||
}
|
||||
...on CategoryTree {
|
||||
name
|
||||
uid
|
||||
relative_url
|
||||
redirect_code
|
||||
type
|
||||
}
|
||||
...on CmsPage {
|
||||
title
|
||||
url_key
|
||||
page_layout
|
||||
content
|
||||
content_heading
|
||||
relative_url
|
||||
redirect_code
|
||||
type
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
return $routeQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $productSku
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getProductUrlKey(string $productSku): string
|
||||
{
|
||||
$query
|
||||
= <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items {
|
||||
url_key
|
||||
url_suffix
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
$response = $this->graphQlQuery($query);
|
||||
return $response['products']['items'][0]['url_key'] . $response['products']['items'][0]['url_suffix'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $productSku
|
||||
* @return UrlRewriteService
|
||||
* @throws \Magento\Framework\Exception\NoSuchEntityException
|
||||
*/
|
||||
private function getProductUrlRewriteData($productSku): UrlRewriteService
|
||||
{
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$product = $productRepository->get($productSku, false, null, true);
|
||||
$storeId = $product->getStoreId();
|
||||
|
||||
$urlPath = $this->getProductUrlKey($productSku);
|
||||
|
||||
/** @var UrlFinderInterface $urlFinder */
|
||||
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
|
||||
/** @var UrlRewriteService $actualUrls */
|
||||
$actualUrls = $urlFinder->findOneByData(
|
||||
[
|
||||
'request_path' => $urlPath,
|
||||
'store_id' => $storeId
|
||||
]
|
||||
);
|
||||
return $actualUrls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for url rewrite to clean cache on rewrites update
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_with_category.php
|
||||
* @magentoApiDataFixture Magento/Cms/_files/pages.php
|
||||
*
|
||||
* @dataProvider urlRewriteEntitiesDataProvider
|
||||
* @param string $requestPath
|
||||
* @throws AlreadyExistsException
|
||||
*/
|
||||
public function testUrlRewriteCleansCacheOnChange(string $requestPath)
|
||||
{
|
||||
|
||||
/** @var UrlRewriteResourceModel $urlRewriteResourceModel */
|
||||
$urlRewriteResourceModel = $this->objectManager->create(UrlRewriteResourceModel::class);
|
||||
$storeId = 1;
|
||||
$query = function ($requestUrl) {
|
||||
return <<<QUERY
|
||||
{
|
||||
route(url:"{$requestUrl}")
|
||||
{
|
||||
relative_url
|
||||
type
|
||||
redirect_code
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
};
|
||||
|
||||
// warming up route API response cache for entity and validate proper response
|
||||
$apiResponse = $this->graphQlQueryWithResponseHeaders($query($requestPath));
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $apiResponse['headers']);
|
||||
$cacheId = $apiResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse($query($requestPath), [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
|
||||
// Verify we obtain a cache HIT the second time around for this X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse($query($requestPath), [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
|
||||
$this->assertEquals($requestPath, $apiResponse['body']['route']['relative_url']);
|
||||
|
||||
$urlRewrite = $this->getUrlRewriteModelByRequestPath($requestPath, $storeId);
|
||||
|
||||
// renaming entity request path and validating that API will not return cached response
|
||||
$urlRewrite->setRequestPath('test' . $requestPath);
|
||||
$urlRewriteResourceModel->save($urlRewrite);
|
||||
$apiResponse = $this->assertCacheMissAndReturnResponse(
|
||||
$query($requestPath),
|
||||
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
|
||||
);
|
||||
$this->assertNull($apiResponse['body']['route']);
|
||||
|
||||
// rolling back changes
|
||||
$urlRewrite->setRequestPath($requestPath);
|
||||
$urlRewriteResourceModel->save($urlRewrite);
|
||||
}
|
||||
|
||||
public function urlRewriteEntitiesDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'simple-product-in-stock.html'
|
||||
],
|
||||
[
|
||||
'category-1.html'
|
||||
],
|
||||
[
|
||||
'page100'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for custom url rewrite to clean cache on update combinations
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/product_with_category.php
|
||||
* @magentoApiDataFixture Magento/Cms/_files/pages.php
|
||||
*
|
||||
* @throws AlreadyExistsException
|
||||
*/
|
||||
public function testUrlRewriteCleansCacheForCustomRewrites()
|
||||
{
|
||||
/** @var UrlRewriteResourceModel $urlRewriteResourceModel */
|
||||
$urlRewriteResourceModel = $this->objectManager->create(UrlRewriteResourceModel::class);
|
||||
$storeId = 1;
|
||||
$query = function ($requestUrl) {
|
||||
return <<<QUERY
|
||||
{
|
||||
route(url:"{$requestUrl}")
|
||||
{
|
||||
relative_url
|
||||
type
|
||||
redirect_code
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
};
|
||||
|
||||
$customRequestPath = 'test.html';
|
||||
$customSecondRequestPath = 'test2.html';
|
||||
$entitiesRequestPaths = [
|
||||
'simple-product-in-stock.html',
|
||||
'category-1.html',
|
||||
'page100'
|
||||
];
|
||||
|
||||
// create custom url rewrite
|
||||
$urlRewriteModel = $this->objectManager->create(UrlRewriteModel::class);
|
||||
$urlRewriteModel->setEntityType('custom')
|
||||
->setRedirectType(302)
|
||||
->setStoreId($storeId)
|
||||
->setDescription(null)
|
||||
->setIsAutogenerated(0);
|
||||
|
||||
// create second custom url rewrite and target it to previous one to check
|
||||
// if proper final target url will be resolved
|
||||
$secondUrlRewriteModel = $this->objectManager->create(UrlRewriteModel::class);
|
||||
$secondUrlRewriteModel->setEntityType('custom')
|
||||
->setRedirectType(302)
|
||||
->setStoreId($storeId)
|
||||
->setRequestPath($customSecondRequestPath)
|
||||
->setTargetPath($customRequestPath)
|
||||
->setDescription(null)
|
||||
->setIsAutogenerated(0);
|
||||
$urlRewriteResourceModel->save($secondUrlRewriteModel);
|
||||
|
||||
foreach ($entitiesRequestPaths as $entityRequestPath) {
|
||||
// updating custom rewrite for each entity
|
||||
$urlRewriteModel->setRequestPath($customRequestPath)
|
||||
->setTargetPath($entityRequestPath);
|
||||
$urlRewriteResourceModel->save($urlRewriteModel);
|
||||
|
||||
// confirm that API returns non-cached response for the first custom rewrite
|
||||
$apiResponse = $this->graphQlQueryWithResponseHeaders($query($customRequestPath));
|
||||
$this->assertEquals($entityRequestPath, $apiResponse['body']['route']['relative_url']);
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $apiResponse['headers']);
|
||||
$cacheId = $apiResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse(
|
||||
$query($customRequestPath),
|
||||
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
|
||||
);
|
||||
|
||||
// Verify we obtain a cache HIT the second time around for this X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse(
|
||||
$query($customRequestPath),
|
||||
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
|
||||
);
|
||||
|
||||
// confirm that API returns non-cached response for the second custom rewrite
|
||||
$apiResponse = $this->graphQlQueryWithResponseHeaders($query($customSecondRequestPath));
|
||||
$this->assertEquals($entityRequestPath, $apiResponse['body']['route']['relative_url']);
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $apiResponse['headers']);
|
||||
$cacheId = $apiResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse(
|
||||
$query($customSecondRequestPath),
|
||||
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
|
||||
);
|
||||
|
||||
// Verify we obtain a cache HIT the second time around for this X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse(
|
||||
$query($customSecondRequestPath),
|
||||
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
|
||||
);
|
||||
}
|
||||
|
||||
$urlRewriteResourceModel->delete($secondUrlRewriteModel);
|
||||
|
||||
// delete custom rewrite and validate that API will not return cached response
|
||||
$urlRewriteResourceModel->delete($urlRewriteModel);
|
||||
$apiResponse = $this->assertCacheMissAndReturnResponse(
|
||||
$query($customRequestPath),
|
||||
[CacheIdCalculator::CACHE_ID_HEADER => $cacheId]
|
||||
);
|
||||
$this->assertNull($apiResponse['body']['route']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return UrlRewrite model instance by request_path
|
||||
*
|
||||
* @param string $requestPath
|
||||
* @param int $storeId
|
||||
* @return UrlRewriteModel
|
||||
*/
|
||||
private function getUrlRewriteModelByRequestPath(string $requestPath, int $storeId): UrlRewriteModel
|
||||
{
|
||||
/** @var UrlFinderInterface $urlFinder */
|
||||
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
|
||||
|
||||
/** @var UrlRewriteService $urlRewriteService */
|
||||
$urlRewriteService = $urlFinder->findOneByData(
|
||||
[
|
||||
'request_path' => $requestPath,
|
||||
'store_id' => $storeId
|
||||
]
|
||||
);
|
||||
|
||||
/** @var UrlRewriteModel $urlRewrite */
|
||||
$urlRewrite = $this->objectManager->create(UrlRewriteModel::class);
|
||||
$urlRewrite->load($urlRewriteService->getUrlRewriteId());
|
||||
|
||||
return $urlRewrite;
|
||||
}
|
||||
}
|
||||
+195
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\PageCache\UrlRewrite;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
use Magento\UrlRewrite\Model\UrlFinderInterface;
|
||||
|
||||
/**
|
||||
* Test caching works for url resolver.
|
||||
*/
|
||||
class UrlResolverCacheTest extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->markTestSkipped(
|
||||
'This test will stay skipped until DEVOPS-4924 is resolved'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that X-Magento-tags and cache debug headers are correct for product urlResolver
|
||||
*
|
||||
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
|
||||
*/
|
||||
public function testCacheTagsForProducts()
|
||||
{
|
||||
$productSku = 'p002';
|
||||
$urlKey = 'p002.html';
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
|
||||
/** @var Product $product */
|
||||
$product = $productRepository->get($productSku, false, null, true);
|
||||
$urlResolverQuery = $this->getUrlResolverQuery($urlKey);
|
||||
$responseMiss = $this->graphQlQueryWithResponseHeaders($urlResolverQuery);
|
||||
$this->assertArrayHasKey('X-Magento-Tags', $responseMiss['headers']);
|
||||
$actualTags = explode(',', $responseMiss['headers']['X-Magento-Tags']);
|
||||
$expectedTags = ["cat_p", "cat_p_{$product->getId()}", "FPC"];
|
||||
$this->assertEquals($expectedTags, $actualTags);
|
||||
|
||||
//cache-debug should be a MISS on first request
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseMiss['headers']);
|
||||
$this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
//cache-debug should be a HIT on second request
|
||||
$responseHit = $this->graphQlQueryWithResponseHeaders($urlResolverQuery);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseHit['headers']);
|
||||
$this->assertEquals('HIT', $responseHit['headers']['X-Magento-Cache-Debug']);
|
||||
//cached data should be correct
|
||||
$this->assertNotEmpty($responseHit['body']);
|
||||
$this->assertArrayNotHasKey('errors', $responseHit['body']);
|
||||
$this->assertEquals('PRODUCT', $responseHit['body']['urlResolver']['type']);
|
||||
}
|
||||
/**
|
||||
* Tests that X-Magento-tags and cache debug headers are correct for category urlResolver
|
||||
*
|
||||
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
|
||||
*/
|
||||
public function testCacheTagsForCategory()
|
||||
{
|
||||
$categoryUrlKey = 'cat-1.html';
|
||||
$productSku = 'p002';
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
|
||||
/** @var Product $product */
|
||||
$product = $productRepository->get($productSku, false, null, true);
|
||||
$storeId = $product->getStoreId();
|
||||
|
||||
/** @var UrlFinderInterface $urlFinder */
|
||||
$urlFinder = Bootstrap::getObjectManager()->get(UrlFinderInterface::class);
|
||||
$actualUrls = $urlFinder->findOneByData(
|
||||
[
|
||||
'request_path' => $categoryUrlKey,
|
||||
'store_id' => $storeId
|
||||
]
|
||||
);
|
||||
$categoryId = $actualUrls->getEntityId();
|
||||
$query = $this->getUrlResolverQuery($categoryUrlKey);
|
||||
$responseMiss = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey('X-Magento-Tags', $responseMiss['headers']);
|
||||
$actualTags = explode(',', $responseMiss['headers']['X-Magento-Tags']);
|
||||
$expectedTags = ["cat_c", "cat_c_{$categoryId}", "FPC"];
|
||||
$this->assertEquals($expectedTags, $actualTags);
|
||||
|
||||
//cache-debug should be a MISS on first request
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseMiss['headers']);
|
||||
$this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
//cache-debug should be a HIT on second request
|
||||
$responseHit = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseHit['headers']);
|
||||
$this->assertEquals('HIT', $responseHit['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
//verify cached data is correct
|
||||
$this->assertNotEmpty($responseHit['body']);
|
||||
$this->assertArrayNotHasKey('errors', $responseHit['body']);
|
||||
$this->assertEquals('CATEGORY', $responseHit['body']['urlResolver']['type']);
|
||||
}
|
||||
/**
|
||||
* Test that X-Magento-Tags Cache debug headers are correct for cms page url resolver
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Cms/_files/pages.php
|
||||
*/
|
||||
public function testUrlResolverCachingForCMSPage()
|
||||
{
|
||||
/** @var \Magento\Cms\Model\Page $page */
|
||||
$page = Bootstrap::getObjectManager()->get(\Magento\Cms\Model\Page::class);
|
||||
$page->load('page100');
|
||||
$cmsPageId = $page->getId();
|
||||
$requestPath = $page->getIdentifier();
|
||||
|
||||
$query = $this->getUrlResolverQuery($requestPath);
|
||||
$responseMiss = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey('X-Magento-Tags', $responseMiss['headers']);
|
||||
$actualTags = explode(',', $responseMiss['headers']['X-Magento-Tags']);
|
||||
$expectedTags = ["cms_p", "cms_p_{$cmsPageId}", "FPC"];
|
||||
$this->assertEquals($expectedTags, $actualTags);
|
||||
|
||||
//cache-debug should be a MISS on first request
|
||||
$this->assertArrayHasKey('X-Magento-Cache-Debug', $responseMiss['headers']);
|
||||
$this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
//cache-debug should be a HIT on second request
|
||||
$responseHit = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertEquals('HIT', $responseHit['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
//verify cached data is correct
|
||||
$this->assertNotEmpty($responseHit['body']);
|
||||
$this->assertArrayNotHasKey('errors', $responseHit['body']);
|
||||
$this->assertEquals('CMS_PAGE', $responseHit['body']['urlResolver']['type']);
|
||||
}
|
||||
/**
|
||||
* Tests that cache is invalidated when url key is updated and access the original request path
|
||||
*
|
||||
* @magentoApiDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
|
||||
*/
|
||||
public function testCacheIsInvalidatedForUrlResolver()
|
||||
{
|
||||
$productSku = 'p002';
|
||||
$urlKey = 'p002.html';
|
||||
$urlResolverQuery = $this->getUrlResolverQuery($urlKey);
|
||||
$responseMiss = $this->graphQlQueryWithResponseHeaders($urlResolverQuery);
|
||||
//cache-debug should be a MISS on first request
|
||||
$this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
//cache-debug should be a HIT on second request
|
||||
$urlResolverQuery = $this->getUrlResolverQuery($urlKey);
|
||||
$responseHit = $this->graphQlQueryWithResponseHeaders($urlResolverQuery);
|
||||
$this->assertEquals('HIT', $responseHit['headers']['X-Magento-Cache-Debug']);
|
||||
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
|
||||
/** @var Product $product */
|
||||
$product = $productRepository->get($productSku, false, null, true);
|
||||
$product->setUrlKey('p002-new.html')->save();
|
||||
|
||||
//cache-debug should be a MISS after updating the url key and accessing the same requestPath or urlKey
|
||||
$urlResolverQuery = $this->getUrlResolverQuery($urlKey);
|
||||
$responseMiss = $this->graphQlQueryWithResponseHeaders($urlResolverQuery);
|
||||
$this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get url resolver query
|
||||
*
|
||||
* @param $urlKey
|
||||
* @return string
|
||||
*/
|
||||
private function getUrlResolverQuery(string $urlKey): string
|
||||
{
|
||||
$query = <<<QUERY
|
||||
{
|
||||
urlResolver(url:"{$urlKey}")
|
||||
{
|
||||
id
|
||||
relative_url
|
||||
canonical_url
|
||||
type
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\PageCache;
|
||||
|
||||
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
|
||||
|
||||
/**
|
||||
* Test that caching works properly for Varnish when using the X-Magento-Cache-Id
|
||||
*/
|
||||
class VarnishTest extends GraphQLPageCacheAbstract
|
||||
{
|
||||
/**
|
||||
* Test that we obtain cache MISS/HIT when expected for a guest.
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
|
||||
*/
|
||||
public function testCacheResultForGuest()
|
||||
{
|
||||
$productSku='simple2';
|
||||
$query = $this->getProductQuery($productSku);
|
||||
|
||||
// Obtain the X-Magento-Cache-Id from the response which will be used as the cache key
|
||||
$response = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
|
||||
$cacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
|
||||
// Verify we obtain a cache HIT the second time around for this X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $cacheId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that changing the Store header returns different cache results.
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/Store/_files/second_store.php
|
||||
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
|
||||
*/
|
||||
public function testCacheResultForGuestWithStoreHeader()
|
||||
{
|
||||
$productSku = 'simple2';
|
||||
$query = $this->getProductQuery($productSku);
|
||||
|
||||
// Verify caching works as expected without a Store header
|
||||
$response = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
|
||||
$defaultStoreCacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId]);
|
||||
// Verify we obtain a cache HIT the second time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId]);
|
||||
|
||||
// Obtain a new X-Magento-Cache-Id using after updating the Store header
|
||||
$secondStoreResponse = $this->graphQlQueryWithResponseHeaders(
|
||||
$query,
|
||||
[],
|
||||
'',
|
||||
[
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId,
|
||||
'Store' => 'fixture_second_store'
|
||||
]
|
||||
);
|
||||
$secondStoreCacheId = $secondStoreResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search by this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse($query, [
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $secondStoreCacheId,
|
||||
'Store' => 'fixture_second_store'
|
||||
]);
|
||||
|
||||
// Verify we obtain a cache HIT the second time around with the Store header
|
||||
$this->assertCacheHitAndReturnResponse($query, [
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $secondStoreCacheId,
|
||||
'Store' => 'fixture_second_store'
|
||||
]);
|
||||
|
||||
// Verify we still obtain a cache HIT for the default store
|
||||
$this->assertCacheHitAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that changing the Content-Currency header returns different cache results.
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/Store/_files/multiple_currencies.php
|
||||
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
|
||||
*/
|
||||
public function testCacheResultForGuestWithCurrencyHeader()
|
||||
{
|
||||
$productSku = 'simple_product';
|
||||
$query = $this->getProductQuery($productSku);
|
||||
|
||||
// Verify caching works as expected without a Content-Currency header
|
||||
$response = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
|
||||
$defaultCurrencyCacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse(
|
||||
$query,
|
||||
[CacheIdCalculator::CACHE_ID_HEADER => $defaultCurrencyCacheId]
|
||||
);
|
||||
// Verify we obtain a cache HIT the second time we search the cache using this X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultCurrencyCacheId]);
|
||||
|
||||
// Obtain a new X-Magento-Cache-Id using after updating the Content-Currency header
|
||||
$secondCurrencyResponse = $this->graphQlQueryWithResponseHeaders(
|
||||
$query,
|
||||
[],
|
||||
'',
|
||||
[
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $defaultCurrencyCacheId,
|
||||
'Content-Currency' => 'EUR'
|
||||
]
|
||||
);
|
||||
$secondCurrencyCacheId = $secondCurrencyResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we obtain a cache MISS the first time we search by this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse($query, [
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $secondCurrencyCacheId,
|
||||
'Content-Currency' => 'EUR'
|
||||
]);
|
||||
|
||||
// Verify we obtain a cache HIT the second time around with the changed currency header
|
||||
$this->assertCacheHitAndReturnResponse($query, [
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $secondCurrencyCacheId,
|
||||
'Content-Currency' => 'EUR'
|
||||
]);
|
||||
|
||||
// Verify we still obtain a cache HIT for the default currency ( no Content-Currency header)
|
||||
$this->assertCacheHitAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultCurrencyCacheId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a request with a cache id which differs from the one returned by the response is not cacheable.
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/Store/_files/second_store.php
|
||||
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
|
||||
*/
|
||||
public function testCacheResultForGuestWithOutdatedCacheId()
|
||||
{
|
||||
$productSku = 'simple_product';
|
||||
$query = $this->getProductQuery($productSku);
|
||||
|
||||
// Verify caching with no headers in the request
|
||||
$response = $this->graphQlQueryWithResponseHeaders($query);
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
|
||||
$defaultCacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
$this->assertCacheMissAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultCacheId]);
|
||||
$this->assertCacheHitAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultCacheId]);
|
||||
|
||||
// Obtain a new X-Magento-Cache-Id using after updating the request with Store header
|
||||
$responseWithStore = $this->graphQlQueryWithResponseHeaders(
|
||||
$query,
|
||||
[],
|
||||
'',
|
||||
[
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $defaultCacheId,
|
||||
'Store' => 'fixture_second_store'
|
||||
]
|
||||
);
|
||||
$storeCacheId = $responseWithStore['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
|
||||
// Verify we still get a cache MISS since the cache id in the request doesn't match the cache id from response
|
||||
$this->assertCacheMissAndReturnResponse($query, [
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $defaultCacheId,
|
||||
'Store' => 'fixture_second_store'
|
||||
]);
|
||||
|
||||
// Verify we get a cache MISS first time with the updated cache id
|
||||
$this->assertCacheMissAndReturnResponse($query, [
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $storeCacheId,
|
||||
'Store' => 'fixture_second_store'
|
||||
]);
|
||||
|
||||
// Verify we obtain a cache HIT second time around with the updated cache id
|
||||
$this->assertCacheHitAndReturnResponse($query, [
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $storeCacheId,
|
||||
'Store' => 'fixture_second_store'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that we obtain cache MISS/HIT when expected for a customer.
|
||||
*
|
||||
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
|
||||
* @magentoApiDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
|
||||
*/
|
||||
public function testCacheResultForCustomer()
|
||||
{
|
||||
$productSku = 'simple_product';
|
||||
$query = $this->getProductQuery($productSku);
|
||||
|
||||
$email = 'customer@example.com';
|
||||
$password = 'password';
|
||||
$generateToken = $this->generateCustomerToken($email, $password);
|
||||
$tokenResponse = $this->graphQlMutationWithResponseHeaders($generateToken);
|
||||
|
||||
// Obtain the X-Magento-Cache-id from the response and authorization token - customer logs in
|
||||
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $tokenResponse['headers']);
|
||||
$cacheIdCustomer = $tokenResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
$customerToken = $tokenResponse['body']['generateCustomerToken']['token'];
|
||||
|
||||
// Verify we obtain cache MISS the first time we search by this X-Magento-Cache-Id
|
||||
$this->assertCacheMissAndReturnResponse($query, [
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer,
|
||||
'Authorization' => 'Bearer ' . $customerToken
|
||||
]);
|
||||
|
||||
// Verify we obtain cache HIT second time using the same X-Magento-Cache-Id
|
||||
$this->assertCacheHitAndReturnResponse($query, [
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer,
|
||||
'Authorization' => 'Bearer ' . $customerToken
|
||||
]);
|
||||
$revokeTokenQuery = $this->revokeCustomerToken();
|
||||
|
||||
// Verify that once customer logs out, X-Magento-Cache-Id will be that of an unregistered user
|
||||
$revokeTokenResponse = $this->graphQlMutationWithResponseHeaders(
|
||||
$revokeTokenQuery,
|
||||
[],
|
||||
'',
|
||||
[
|
||||
CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer,
|
||||
'Authorization' => 'Bearer ' . $customerToken
|
||||
]
|
||||
);
|
||||
|
||||
$cacheIdGuest = $revokeTokenResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
|
||||
$this->assertNotEquals($cacheIdCustomer, $cacheIdGuest);
|
||||
|
||||
//Verify that omitting the Auth token doesn't send cached content for a logged-in customer
|
||||
$this->assertCacheMissAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer]);
|
||||
$this->assertCacheMissAndReturnResponse($query, [CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get product query
|
||||
*
|
||||
* @param string $productSku
|
||||
* @return string
|
||||
*/
|
||||
private function getProductQuery(string $productSku): string
|
||||
{
|
||||
$productQuery = <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "{$productSku}"}})
|
||||
{
|
||||
items {
|
||||
id
|
||||
name
|
||||
sku
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
|
||||
return $productQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @param string $password
|
||||
* @return string
|
||||
*/
|
||||
private function generateCustomerToken(string $email, string $password) : string
|
||||
{
|
||||
return <<<MUTATION
|
||||
mutation {
|
||||
generateCustomerToken(
|
||||
email: "{$email}"
|
||||
password: "{$password}"
|
||||
) {
|
||||
token
|
||||
}
|
||||
}
|
||||
MUTATION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function revokeCustomerToken() : string
|
||||
{
|
||||
return <<<MUTATION
|
||||
mutation {
|
||||
revokeCustomerToken
|
||||
{ result }
|
||||
}
|
||||
MUTATION;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user