3 namespace Drupal\Tests\system\Functional\Cache;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Component\Render\FormattableMarkup;
10 * Provides helper methods for page cache tags tests.
12 abstract class PageCacheTagsTestBase extends BrowserTestBase {
17 * Always enable header dumping in page cache tags tests, this aids debugging.
19 protected $dumpHeaders = TRUE;
24 protected function setUp() {
27 // Enable page caching.
28 $config = $this->config('system.performance');
29 $config->set('cache.page.max_age', 3600);
34 * Verify that when loading a given page, it's a page cache hit or miss.
36 * @param \Drupal\Core\Url $url
37 * The page for this URL will be loaded.
38 * @param string $hit_or_miss
39 * 'HIT' if a page cache hit is expected, 'MISS' otherwise.
41 * @param array|false $tags
42 * When expecting a page cache hit, you may optionally specify an array of
43 * expected cache tags. While FALSE, the cache tags will not be verified.
45 protected function verifyPageCache(Url $url, $hit_or_miss, $tags = FALSE) {
46 $this->drupalGet($url);
47 $message = new FormattableMarkup('Page cache @hit_or_miss for %path.', ['@hit_or_miss' => $hit_or_miss, '%path' => $url->toString()]);
48 $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), $hit_or_miss, $message);
50 if ($hit_or_miss === 'HIT' && is_array($tags)) {
51 $absolute_url = $url->setAbsolute()->toString();
52 $cid_parts = [$absolute_url, 'html'];
53 $cid = implode(':', $cid_parts);
54 $cache_entry = \Drupal::cache('page')->get($cid);
55 sort($cache_entry->tags);
56 $tags = array_unique($tags);
58 $this->assertIdentical($cache_entry->tags, $tags);