582a2f4f067b3ea574fc6933c1e0565f1300815d
[yaffs-website] / web / core / modules / system / src / Tests / Cache / PageCacheTagsTestBase.php
1 <?php
2
3 namespace Drupal\system\Tests\Cache;
4
5 use Drupal\Core\Url;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\Component\Utility\SafeMarkup;
8
9 /**
10  * Provides helper methods for page cache tags tests.
11  *
12  * @deprecated Scheduled for removal in Drupal 9.0.0.
13  *   Use \Drupal\Tests\system\Functional\Cache\PageCacheTagsTestBase instead.
14  */
15 abstract class PageCacheTagsTestBase extends WebTestBase {
16
17   /**
18    * {@inheritdoc}
19    *
20    * Always enable header dumping in page cache tags tests, this aids debugging.
21    */
22   protected $dumpHeaders = TRUE;
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
30     // Enable page caching.
31     $config = $this->config('system.performance');
32     $config->set('cache.page.max_age', 3600);
33     $config->save();
34   }
35
36   /**
37    * Verify that when loading a given page, it's a page cache hit or miss.
38    *
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.
43    *
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.
47    */
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);
52
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);
60       sort($tags);
61       $this->assertIdentical($cache_entry->tags, $tags);
62     }
63   }
64
65 }