69afeb745d9ffbe85ecaf5bc75e722bedef9a7d6
[yaffs-website] / web / core / modules / system / tests / src / Functional / Cache / PageCacheTagsTestBase.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Cache;
4
5 use Drupal\Core\Url;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Component\Utility\SafeMarkup;
8
9 /**
10  * Provides helper methods for page cache tags tests.
11  */
12 abstract class PageCacheTagsTestBase extends BrowserTestBase {
13
14   /**
15    * {@inheritdoc}
16    *
17    * Always enable header dumping in page cache tags tests, this aids debugging.
18    */
19   protected $dumpHeaders = TRUE;
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26
27     // Enable page caching.
28     $config = $this->config('system.performance');
29     $config->set('cache.page.max_age', 3600);
30     $config->save();
31   }
32
33   /**
34    * Verify that when loading a given page, it's a page cache hit or miss.
35    *
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.
40    *
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.
44    */
45   protected function verifyPageCache(Url $url, $hit_or_miss, $tags = FALSE) {
46     $this->drupalGet($url);
47     $message = SafeMarkup::format('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);
49
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);
57       sort($tags);
58       $this->assertIdentical($cache_entry->tags, $tags);
59     }
60   }
61
62 }