function AssertPageCacheContextsAndTagsTrait::assertPageCacheContextsAndTags

Same name in this branch
  1. 8.9.x core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php \Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait::assertPageCacheContextsAndTags()
Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php \Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait::assertPageCacheContextsAndTags()
  2. 10 core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php \Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait::assertPageCacheContextsAndTags()
  3. 11.x core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php \Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait::assertPageCacheContextsAndTags()

Asserts page cache miss, then hit for the given URL; checks cache headers.

Parameters

\Drupal\Core\Url $url: The URL to test.

string[] $expected_contexts: The expected cache contexts for the given URL.

string[] $expected_tags: The expected cache tags for the given URL.

File

core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php, line 81

Class

AssertPageCacheContextsAndTagsTrait
Provides test assertions for testing page-level cache contexts & tags.

Namespace

Drupal\system\Tests\Cache

Code

protected function assertPageCacheContextsAndTags(Url $url, array $expected_contexts, array $expected_tags) {
    $absolute_url = $url->setAbsolute()
        ->toString();
    sort($expected_contexts);
    sort($expected_tags);
    // Assert cache miss + expected cache contexts + tags.
    $this->drupalGet($absolute_url);
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
    $this->assertCacheTags($expected_tags);
    $this->assertCacheContexts($expected_contexts);
    // Assert cache hit + expected cache contexts + tags.
    $this->drupalGet($absolute_url);
    $this->assertCacheTags($expected_tags);
    $this->assertCacheContexts($expected_contexts);
    // Assert page cache item + expected cache tags.
    $cid_parts = [
        $url->setAbsolute()
            ->toString(),
        'html',
    ];
    $cid = implode(':', $cid_parts);
    $cache_entry = \Drupal::cache('page')->get($cid);
    sort($cache_entry->tags);
    $this->assertEqual($cache_entry->tags, $expected_tags);
    $this->debugCacheTags($cache_entry->tags, $expected_tags);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.