View source
<?php
namespace Drupal\Tests\system\Functional\Cache;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Url;
trait AssertPageCacheContextsAndTagsTrait {
protected function enablePageCaching() {
$config = $this
->config('system.performance');
$config
->set('cache.page.max_age', 300);
$config
->save();
}
protected function getCacheHeaderValues($header_name) {
$header_value = $this
->drupalGetHeader($header_name);
if (empty($header_value)) {
return [];
}
else {
return explode(' ', $header_value);
}
}
protected function assertCacheContext($expected_cache_context) {
$cache_contexts = explode(' ', $this
->drupalGetHeader('X-Drupal-Cache-Contexts'));
$this
->assertContains($expected_cache_context, $cache_contexts, "'" . $expected_cache_context . "' is present in the X-Drupal-Cache-Contexts header.");
}
protected function assertNoCacheContext($not_expected_cache_context) {
$cache_contexts = explode(' ', $this
->drupalGetHeader('X-Drupal-Cache-Contexts'));
$this
->assertNotContains($not_expected_cache_context, $cache_contexts, "'" . $not_expected_cache_context . "' is not present in the X-Drupal-Cache-Contexts header.");
}
protected function assertPageCacheContextsAndTags(Url $url, array $expected_contexts, array $expected_tags) {
$absolute_url = $url
->setAbsolute()
->toString();
sort($expected_contexts);
sort($expected_tags);
$this
->drupalGet($absolute_url);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this
->assertCacheTags($expected_tags);
$this
->assertCacheContexts($expected_contexts);
$this
->drupalGet($absolute_url);
$this
->assertCacheTags($expected_tags);
$this
->assertCacheContexts($expected_contexts);
$cid_parts = [
$url
->setAbsolute()
->toString(),
'',
];
$cid = implode(':', $cid_parts);
$cache_entry = \Drupal::cache('page')
->get($cid);
sort($cache_entry->tags);
$this
->assertEqual($cache_entry->tags, $expected_tags);
}
protected function assertCacheTags(array $expected_tags, $include_default_tags = TRUE) {
if ($include_default_tags) {
if (\Drupal::currentUser()
->isAnonymous()) {
$expected_tags = Cache::mergeTags($expected_tags, [
'config:user.role.anonymous',
]);
}
$expected_tags[] = 'http_response';
}
$actual_tags = $this
->getCacheHeaderValues('X-Drupal-Cache-Tags');
$expected_tags = array_unique($expected_tags);
sort($expected_tags);
sort($actual_tags);
$this
->assertIdentical($actual_tags, $expected_tags);
}
protected function assertCacheContexts(array $expected_contexts, $message = NULL, $include_default_contexts = TRUE) {
if ($include_default_contexts) {
$default_contexts = [
'languages:language_interface',
'theme',
];
if (!in_array('user', $expected_contexts)) {
$default_contexts[] = 'user.permissions';
}
$expected_contexts = Cache::mergeContexts($expected_contexts, $default_contexts);
}
$actual_contexts = $this
->getCacheHeaderValues('X-Drupal-Cache-Contexts');
sort($expected_contexts);
sort($actual_contexts);
$this
->assertIdentical($actual_contexts, $expected_contexts, $message);
return $actual_contexts === $expected_contexts;
}
protected function assertCacheMaxAge($max_age) {
$cache_control_header = $this
->drupalGetHeader('Cache-Control');
$this
->assertStringContainsString('max-age:' . $max_age, $cache_control_header);
}
}