PageCacheTagsTestBase.php 1.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

/**
 * @file
 * Contains \Drupal\system\Tests\Cache\PageCacheTagsTestBase.
 */

namespace Drupal\system\Tests\Cache;

use Drupal\simpletest\WebTestBase;
use Drupal\Component\Utility\String;

/**
 * Provides helper methods for page cache tags tests.
 */
abstract class PageCacheTagsTestBase extends WebTestBase {

18
19
20
21
22
23
24
  /**
   * {@inheritdoc}
   *
   * Always enable header dumping in page cache tags tests, this aids debugging.
   */
  protected $dumpHeaders = TRUE;

25
26
27
  /**
   * {@inheritdoc}
   */
28
  protected function setUp() {
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    parent::setUp();

    // Enable page caching.
    $config = \Drupal::config('system.performance');
    $config->set('cache.page.use_internal', 1);
    $config->set('cache.page.max_age', 3600);
    $config->save();
  }

  /**
   * Verify that when loading a given page, it's a page cache hit or miss.
   *
   * @param string $path
   *   The page at this path will be loaded.
   * @param string $hit_or_miss
   *   'HIT' if a page cache hit is expected, 'MISS' otherwise.
   *
   * @param array|FALSE $tags
   *   When expecting a page cache hit, you may optionally specify an array of
   *   expected cache tags. While FALSE, the cache tags will not be verified.
   */
  protected function verifyPageCache($path, $hit_or_miss, $tags = FALSE) {
    $this->drupalGet($path);
    $message = String::format('Page cache @hit_or_miss for %path.', array('@hit_or_miss' => $hit_or_miss, '%path' => $path));
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), $hit_or_miss, $message);

    if ($hit_or_miss === 'HIT' && is_array($tags)) {
56
      $cid_parts = array(_url($path, array('absolute' => TRUE)), 'html');
57
      $cid = implode(':', $cid_parts);
58
      $cache_entry = \Drupal::cache('render')->get($cid);
59
      sort($cache_entry->tags);
60
      $tags = array_unique($tags);
61
      sort($tags);
62
63
64
65
66
      $this->assertIdentical($cache_entry->tags, $tags);
    }
  }

}