function PageCacheTest::testPageCacheTags
Same name in other branches
- 9 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCacheTags()
- 8.9.x core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCacheTags()
- 11.x core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCacheTags()
Tests that cache tags are properly persisted.
Since tag based invalidation works, we know that our tag properly persisted.
File
-
core/
modules/ page_cache/ tests/ src/ Functional/ PageCacheTest.php, line 56
Class
- PageCacheTest
- Enables the page cache and tests it with various HTTP requests.
Namespace
Drupal\Tests\page_cache\FunctionalCode
public function testPageCacheTags() : void {
$this->enablePageCaching();
$path = 'system-test/cache_tags_page';
$tags = [
'system_test_cache_tags_page',
];
$this->drupalGet($path);
$this->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$this->drupalGet($path);
$this->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
$cid_parts = [
Url::fromRoute('system_test.cache_tags_page', [], [
'absolute' => TRUE,
])->toString(),
'',
];
$cid = implode(':', $cid_parts);
$cache_entry = \Drupal::cache('page')->get($cid);
sort($cache_entry->tags);
$expected_tags = [
'config:user.role.anonymous',
'http_response',
'pre_render',
'rendered',
'system_test_cache_tags_page',
];
$this->assertSame($expected_tags, $cache_entry->tags);
Cache::invalidateTags($tags);
$this->drupalGet($path);
$this->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.