|
7 | 7 |
|
8 | 8 | namespace Magento\GraphQl\PageCache;
|
9 | 9 |
|
| 10 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
10 | 11 | use Magento\Catalog\Api\ProductRepositoryInterface;
|
11 | 12 | use Magento\Catalog\Model\Product;
|
| 13 | +use Magento\Catalog\Test\Fixture\Category as CategoryFixture; |
| 14 | +use Magento\Cms\Model\BlockRepository; |
| 15 | +use Magento\Cms\Test\Fixture\Block as BlockFixture; |
12 | 16 | use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
|
| 17 | +use Magento\PageCache\Model\Config; |
| 18 | +use Magento\Store\Model\Store; |
| 19 | +use Magento\TestFramework\Fixture\Config as ConfigFixture; |
| 20 | +use Magento\TestFramework\Fixture\DataFixture; |
| 21 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 22 | +use Magento\TestFramework\Helper\Bootstrap; |
13 | 23 | use Magento\TestFramework\ObjectManager;
|
14 | 24 |
|
15 | 25 | /**
|
@@ -133,6 +143,140 @@ public function testCacheInvalidationForCategoriesWithProduct()
|
133 | 143 | );
|
134 | 144 | }
|
135 | 145 |
|
| 146 | + #[ |
| 147 | + ConfigFixture(Config::XML_PAGECACHE_TYPE, Config::VARNISH), |
| 148 | + DataFixture(BlockFixture::class, ['content' => 'Original Block content'], as: 'block'), |
| 149 | + DataFixture(CategoryFixture::class, as: 'category1'), |
| 150 | + DataFixture(CategoryFixture::class, ['description' => 'Original Category description'], as: 'category2'), |
| 151 | + ] |
| 152 | + public function testCacheInvalidationForCategoriesWithWidget(): void |
| 153 | + { |
| 154 | + $fixtures = DataFixtureStorageManager::getStorage(); |
| 155 | + $block = $fixtures->get('block'); |
| 156 | + $category1 = $fixtures->get('category1'); |
| 157 | + $category2 = $fixtures->get('category2'); |
| 158 | + $queryForCategory1 = $this->getCategoriesQuery((int) $category1->getId()); |
| 159 | + $queryForCategory2 = $this->getCategoriesQuery((int) $category2->getId()); |
| 160 | + |
| 161 | + $this->updateCategoryDescription((int) $category1->getId(), $this->getBlockWidget((int) $block->getId())); |
| 162 | + |
| 163 | + $responseCacheIdForCategory1 = $this->getQueryResponseCacheKey($queryForCategory1); |
| 164 | + // Verify we get MISS for category1 query in the first request |
| 165 | + $responseMissForCategory1 = $this->assertCacheMissAndReturnResponse( |
| 166 | + $queryForCategory1, |
| 167 | + [CacheIdCalculator::CACHE_ID_HEADER => $responseCacheIdForCategory1] |
| 168 | + ); |
| 169 | + |
| 170 | + // Verify we get HIT for category 1 query in the second request |
| 171 | + $responseHitForCategory1 = $this->assertCacheHitAndReturnResponse( |
| 172 | + $queryForCategory1, |
| 173 | + [CacheIdCalculator::CACHE_ID_HEADER => $responseCacheIdForCategory1] |
| 174 | + ); |
| 175 | + |
| 176 | + $this->assertEquals($responseMissForCategory1['body'], $responseHitForCategory1['body']); |
| 177 | + |
| 178 | + // Verify category1 description contains block content |
| 179 | + $this->assertCategoryDescription('Original Block content', $responseHitForCategory1); |
| 180 | + |
| 181 | + $responseCacheIdForCategory2 = $this->getQueryResponseCacheKey($queryForCategory2); |
| 182 | + // Verify we get MISS for category2 query in the first request |
| 183 | + $responseMissForCategory2 = $this->assertCacheMissAndReturnResponse( |
| 184 | + $queryForCategory2, |
| 185 | + [CacheIdCalculator::CACHE_ID_HEADER => $responseCacheIdForCategory2] |
| 186 | + ); |
| 187 | + |
| 188 | + // Verify we get HIT for category 2 query in the second request |
| 189 | + $responseHitForCategory2 = $this->assertCacheHitAndReturnResponse( |
| 190 | + $queryForCategory2, |
| 191 | + [CacheIdCalculator::CACHE_ID_HEADER => $responseCacheIdForCategory2] |
| 192 | + ); |
| 193 | + |
| 194 | + $this->assertEquals($responseMissForCategory2['body'], $responseHitForCategory2['body']); |
| 195 | + |
| 196 | + // Verify category2 description is the same as created |
| 197 | + $this->assertCategoryDescription('Original Category description', $responseHitForCategory2); |
| 198 | + |
| 199 | + // Update block content |
| 200 | + $newBlockContent = 'New block content!!!'; |
| 201 | + $this->updateBlockContent((int) $block->getId(), $newBlockContent); |
| 202 | + |
| 203 | + // Verify we get MISS for category1 query after block is updated |
| 204 | + $this->assertCacheMissAndReturnResponse( |
| 205 | + $queryForCategory1, |
| 206 | + [CacheIdCalculator::CACHE_ID_HEADER => $responseCacheIdForCategory1] |
| 207 | + ); |
| 208 | + |
| 209 | + // Verify we get HIT for category1 query in the second request after block is updated |
| 210 | + $responseHitForCategory1 = $this->assertCacheHitAndReturnResponse( |
| 211 | + $queryForCategory1, |
| 212 | + [CacheIdCalculator::CACHE_ID_HEADER => $responseCacheIdForCategory1] |
| 213 | + ); |
| 214 | + |
| 215 | + // Verify we get HIT for category2 query after block is updated |
| 216 | + $responseHitForCategory2 = $this->assertCacheHitAndReturnResponse( |
| 217 | + $queryForCategory2, |
| 218 | + [CacheIdCalculator::CACHE_ID_HEADER => $responseCacheIdForCategory2] |
| 219 | + ); |
| 220 | + |
| 221 | + // Verify the updated block data is returned in category1 query response |
| 222 | + $this->assertCategoryDescription($newBlockContent, $responseHitForCategory1); |
| 223 | + |
| 224 | + // Verify category2 description is the same as created |
| 225 | + $this->assertCategoryDescription('Original Category description', $responseHitForCategory2); |
| 226 | + } |
| 227 | + |
| 228 | + private function assertCategoryDescription(string $expected, array $response): void |
| 229 | + { |
| 230 | + $responseBody = $response['body']; |
| 231 | + $this->assertIsArray($responseBody); |
| 232 | + $this->assertArrayNotHasKey('errors', $responseBody); |
| 233 | + $this->assertStringContainsString($expected, $responseBody['categories']['items'][0]['description']); |
| 234 | + } |
| 235 | + |
| 236 | + private function getQueryResponseCacheKey(string $query): string |
| 237 | + { |
| 238 | + $response = $this->graphQlQueryWithResponseHeaders($query); |
| 239 | + $this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']); |
| 240 | + return $response['headers'][CacheIdCalculator::CACHE_ID_HEADER]; |
| 241 | + } |
| 242 | + |
| 243 | + private function updateBlockContent(int $id, string $content): void |
| 244 | + { |
| 245 | + $blockRepository = Bootstrap::getObjectManager()->get(BlockRepository::class); |
| 246 | + $block = $blockRepository->getById($id); |
| 247 | + $block->setContent($content); |
| 248 | + $blockRepository->save($block); |
| 249 | + } |
| 250 | + |
| 251 | + private function updateCategoryDescription(int $id, string $description): void |
| 252 | + { |
| 253 | + $categoryRepository = Bootstrap::getObjectManager()->get(CategoryRepositoryInterface::class); |
| 254 | + $category = $categoryRepository->get($id, Store::DEFAULT_STORE_ID); |
| 255 | + $category->setCustomAttribute('description', $description); |
| 256 | + $categoryRepository->save($category); |
| 257 | + } |
| 258 | + |
| 259 | + private function getBlockWidget(int $blockId): string |
| 260 | + { |
| 261 | + return "{{widget type=\"Magento\\Cms\\Block\\Widget\\Block\" " . |
| 262 | + "template=\"widget/static_block/default.phtml\" " . |
| 263 | + "block_id=\"$blockId\" " . |
| 264 | + "type_name=\"CMS Static Block\"}}"; |
| 265 | + } |
| 266 | + |
| 267 | + private function getCategoriesQuery(int $categoryId): string |
| 268 | + { |
| 269 | + return <<<QUERY |
| 270 | +{ |
| 271 | + categories(filters: {ids: {in: ["$categoryId"]}}) { |
| 272 | + items{ |
| 273 | + description |
| 274 | + } |
| 275 | + } |
| 276 | +} |
| 277 | +QUERY; |
| 278 | + } |
| 279 | + |
136 | 280 | /**
|
137 | 281 | * Get Product query
|
138 | 282 | *
|
|
0 commit comments