3 namespace Drupal\system\Tests\Cache;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\Component\Utility\SafeMarkup;
10 * Provides helper methods for page cache tags tests.
12 * @deprecated Scheduled for removal in Drupal 9.0.0.
13 * Use \Drupal\Tests\system\Functional\Cache\PageCacheTagsTestBase instead.
15 abstract class PageCacheTagsTestBase extends WebTestBase {
20 * Always enable header dumping in page cache tags tests, this aids debugging.
22 protected $dumpHeaders = TRUE;
27 protected function setUp() {
30 // Enable page caching.
31 $config = $this->config('system.performance');
32 $config->set('cache.page.max_age', 3600);
37 * Verify that when loading a given page, it's a page cache hit or miss.
39 * @param \Drupal\Core\Url $url
40 * The page for this URL will be loaded.
41 * @param string $hit_or_miss
42 * 'HIT' if a page cache hit is expected, 'MISS' otherwise.
44 * @param array|false $tags
45 * When expecting a page cache hit, you may optionally specify an array of
46 * expected cache tags. While FALSE, the cache tags will not be verified.
48 protected function verifyPageCache(Url $url, $hit_or_miss, $tags = FALSE) {
49 $this->drupalGet($url);
50 $message = SafeMarkup::format('Page cache @hit_or_miss for %path.', ['@hit_or_miss' => $hit_or_miss, '%path' => $url->toString()]);
51 $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), $hit_or_miss, $message);
53 if ($hit_or_miss === 'HIT' && is_array($tags)) {
54 $absolute_url = $url->setAbsolute()->toString();
55 $cid_parts = [$absolute_url, 'html'];
56 $cid = implode(':', $cid_parts);
57 $cache_entry = \Drupal::cache('render')->get($cid);
58 sort($cache_entry->tags);
59 $tags = array_unique($tags);
61 $this->assertIdentical($cache_entry->tags, $tags);