|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\PageCache\Cms; |
| 9 | + |
| 10 | +use Magento\Cms\Model\GetPageByIdentifier; |
| 11 | +use Magento\Cms\Model\PageRepository; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test the caching works properly for CMS Pages |
| 17 | + */ |
| 18 | +class PageCacheTest extends GraphQlAbstract |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var GetPageByIdentifier |
| 22 | + */ |
| 23 | + private $pageByIdentifier; |
| 24 | + |
| 25 | + /** |
| 26 | + * @inheritdoc |
| 27 | + */ |
| 28 | + protected function setUp() |
| 29 | + { |
| 30 | + $this->markTestSkipped( |
| 31 | + 'This test will stay skipped until DEVOPS-4924 is resolved' |
| 32 | + ); |
| 33 | + $this->pageByIdentifier = Bootstrap::getObjectManager()->get(GetPageByIdentifier::class); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Test that X-Magento-Tags are correct |
| 38 | + * |
| 39 | + * @magentoApiDataFixture Magento/Cms/_files/pages.php |
| 40 | + */ |
| 41 | + public function testCacheTagsHaveExpectedValue() |
| 42 | + { |
| 43 | + $pageIdentifier = 'page100'; |
| 44 | + $page = $this->pageByIdentifier->execute($pageIdentifier, 0); |
| 45 | + $pageId = (int) $page->getId(); |
| 46 | + |
| 47 | + $query = $this->getPageQuery($pageId); |
| 48 | + |
| 49 | + //cache-debug should be a MISS on first request |
| 50 | + $response = $this->graphQlQueryWithResponseHeaders($query); |
| 51 | + |
| 52 | + $this->assertArrayHasKey('X-Magento-Tags', $response['headers']); |
| 53 | + $actualTags = explode(',', $response['headers']['X-Magento-Tags']); |
| 54 | + $expectedTags = ["cms_p", "cms_p_{$pageId}", "FPC"]; |
| 55 | + foreach ($expectedTags as $expectedTag) { |
| 56 | + $this->assertContains($expectedTag, $actualTags); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Test the second request for the same page will return a cached result |
| 62 | + * |
| 63 | + * @magentoApiDataFixture Magento/Cms/_files/pages.php |
| 64 | + */ |
| 65 | + public function testCacheIsUsedOnSecondRequest() |
| 66 | + { |
| 67 | + $pageIdentifier = 'page100'; |
| 68 | + $page = $this->pageByIdentifier->execute($pageIdentifier, 0); |
| 69 | + $pageId = (int) $page->getId(); |
| 70 | + |
| 71 | + $query = $this->getPageQuery($pageId); |
| 72 | + |
| 73 | + //cache-debug should be a MISS on first request |
| 74 | + $responseMiss = $this->graphQlQueryWithResponseHeaders($query); |
| 75 | + $this->assertArrayHasKey('X-Magento-Cache-Debug', $responseMiss['headers']); |
| 76 | + $this->assertEquals('MISS', $responseMiss['headers']['X-Magento-Cache-Debug']); |
| 77 | + |
| 78 | + //cache-debug should be a HIT on second request |
| 79 | + $responseHit = $this->graphQlQueryWithResponseHeaders($query); |
| 80 | + $this->assertArrayHasKey('X-Magento-Cache-Debug', $responseHit['headers']); |
| 81 | + $this->assertEquals('HIT', $responseHit['headers']['X-Magento-Cache-Debug']); |
| 82 | + //cached data should be correct |
| 83 | + $this->assertNotEmpty($responseHit['body']); |
| 84 | + $this->assertArrayNotHasKey('errors', $responseHit['body']); |
| 85 | + $pageData = $responseHit['body']['cmsPage']; |
| 86 | + $this->assertEquals('Cms Page 100', $pageData['title']); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Test that cache is invalidated when page is updated |
| 91 | + * |
| 92 | + * @magentoApiDataFixture Magento/Cms/_files/pages.php |
| 93 | + */ |
| 94 | + public function testCacheIsInvalidatedOnPageUpdate() |
| 95 | + { |
| 96 | + $page100Identifier = 'page100'; |
| 97 | + $page100 = $this->pageByIdentifier->execute($page100Identifier, 0); |
| 98 | + $page100Id = (int) $page100->getId(); |
| 99 | + $pageBlankIdentifier = 'page_design_blank'; |
| 100 | + $pageBlank = $this->pageByIdentifier->execute($pageBlankIdentifier, 0); |
| 101 | + $pageBlankId = (int) $pageBlank->getId(); |
| 102 | + |
| 103 | + $page100Query = $this->getPageQuery($page100Id); |
| 104 | + $pageBlankQuery = $this->getPageQuery($pageBlankId); |
| 105 | + |
| 106 | + //cache-debug should be a MISS on first request |
| 107 | + $page100Miss = $this->graphQlQueryWithResponseHeaders($page100Query); |
| 108 | + $this->assertEquals('MISS', $page100Miss['headers']['X-Magento-Cache-Debug']); |
| 109 | + $pageBlankMiss = $this->graphQlQueryWithResponseHeaders($pageBlankQuery); |
| 110 | + $this->assertEquals('MISS', $pageBlankMiss['headers']['X-Magento-Cache-Debug']); |
| 111 | + |
| 112 | + //cache-debug should be a HIT on second request |
| 113 | + $page100Hit = $this->graphQlQueryWithResponseHeaders($page100Query); |
| 114 | + $this->assertEquals('HIT', $page100Hit['headers']['X-Magento-Cache-Debug']); |
| 115 | + $pageBlankHit = $this->graphQlQueryWithResponseHeaders($pageBlankQuery); |
| 116 | + $this->assertEquals('HIT', $pageBlankHit['headers']['X-Magento-Cache-Debug']); |
| 117 | + |
| 118 | + $pageRepository = Bootstrap::getObjectManager()->get(PageRepository::class); |
| 119 | + $newPageContent = 'New page content for blank page.'; |
| 120 | + $pageBlank->setContent($newPageContent); |
| 121 | + $pageRepository->save($pageBlank); |
| 122 | + |
| 123 | + //cache-debug should be a MISS after updating the page |
| 124 | + $pageBlankMiss = $this->graphQlQueryWithResponseHeaders($pageBlankQuery); |
| 125 | + $this->assertEquals('MISS', $pageBlankMiss['headers']['X-Magento-Cache-Debug']); |
| 126 | + $page100Hit = $this->graphQlQueryWithResponseHeaders($page100Query); |
| 127 | + $this->assertEquals('HIT', $page100Hit['headers']['X-Magento-Cache-Debug']); |
| 128 | + //updated page data should be correct |
| 129 | + $this->assertNotEmpty($pageBlankMiss['body']); |
| 130 | + $pageData = $pageBlankMiss['body']['cmsPage']; |
| 131 | + $this->assertArrayNotHasKey('errors', $pageBlankMiss['body']); |
| 132 | + $this->assertEquals('Cms Page Design Blank', $pageData['title']); |
| 133 | + $this->assertEquals($newPageContent, $pageData['content']); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Get page query |
| 138 | + * |
| 139 | + * @param int $pageId |
| 140 | + * @return string |
| 141 | + */ |
| 142 | + private function getPageQuery(int $pageId): string |
| 143 | + { |
| 144 | + $query = <<<QUERY |
| 145 | +{ |
| 146 | + cmsPage(id: $pageId) { |
| 147 | + title |
| 148 | + url_key |
| 149 | + content |
| 150 | + } |
| 151 | +} |
| 152 | +QUERY; |
| 153 | + return $query; |
| 154 | + } |
| 155 | +} |
0 commit comments