function PageCacheTest::testPageCacheTags

Same name and namespace in other branches
  1. 10 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCacheTags()
  2. 11.x core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCacheTags()
  3. 9 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCacheTags()

Test 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 55

Class

PageCacheTest
Enables the page cache and tests it with various HTTP requests.

Namespace

Drupal\Tests\page_cache\Functional

Code

public function testPageCacheTags() {
  $config = $this->config('system.performance');
  $config->set('cache.page.max_age', 300);
  $config->save();
  $path = 'system-test/cache_tags_page';
  $tags = [
    'system_test_cache_tags_page',
  ];
  $this->drupalGet($path);
  $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
  // Verify a cache hit, but also the presence of the correct cache tags.
  $this->drupalGet($path);
  $this->assertEqual($this->drupalGetHeader('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->assertIdentical($cache_entry->tags, $expected_tags);
  Cache::invalidateTags($tags);
  $this->drupalGet($path);
  $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.