You are here

protected function AssertPageCacheContextsAndTagsTrait::assertPageCacheContextsAndTags in Drupal 9

Same name and namespace in other branches
  1. 8 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()

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.

3 calls to AssertPageCacheContextsAndTagsTrait::assertPageCacheContextsAndTags()
FrontPageTest::doTestFrontPageViewCacheTags in core/modules/node/tests/src/Functional/Views/FrontPageTest.php
Tests the cache tags on the front page.
GlossaryTest::testGlossaryView in core/modules/views/tests/src/Functional/GlossaryTest.php
Tests the default glossary view.
PageCacheTagsIntegrationTest::testPageCacheTags in core/modules/page_cache/tests/src/Functional/PageCacheTagsIntegrationTest.php
Tests that cache tags are properly bubbled up to the page level.

File

core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php, line 70

Class

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

Namespace

Drupal\Tests\system\Functional\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
    ->assertSession()
    ->responseHeaderEquals('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(),
    '',
  ];
  $cid = implode(':', $cid_parts);
  $cache_entry = \Drupal::cache('page')
    ->get($cid);
  sort($cache_entry->tags);
  $this
    ->assertEquals($expected_tags, $cache_entry->tags);
}