You are here

protected function AssertPageCacheContextsAndTagsTrait::assertCacheContexts in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php \Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait::assertCacheContexts()
  2. 9 core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php \Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait::assertCacheContexts()

Ensures that some cache contexts are present in the current response.

Parameters

string[] $expected_contexts: The expected cache contexts.

string $message: (optional) A verbose message to output.

bool $include_default_contexts: (optional) Whether the default contexts should automatically be included.

Return value

bool Always returns TRUE.

5 calls to AssertPageCacheContextsAndTagsTrait::assertCacheContexts()
AssertPageCacheContextsAndTagsTrait::assertPageCacheContextsAndTags in core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php
Asserts page cache miss, then hit for the given URL; checks cache headers.
StyleSerializerTest::testRestRenderCaching in core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php
Tests REST export with views render caching enabled.
StyleSerializerTest::testRestViewExposedFilter in core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php
Tests the exposed filter works.
StyleSerializerTest::testSerializerResponses in core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php
Checks the behavior of the Serializer callback paths and row plugins.
TokenReplaceWebTest::testTokens in core/modules/system/tests/src/Functional/System/TokenReplaceWebTest.php
Tests a token replacement on an actual website.

File

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

Class

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

Namespace

Drupal\Tests\system\Functional\Cache

Code

protected function assertCacheContexts(array $expected_contexts, $message = NULL, $include_default_contexts = TRUE) {
  if ($include_default_contexts) {
    $default_contexts = [
      'languages:language_interface',
      'theme',
    ];

    // Add the user.permission context to the list of default contexts except
    // when user is already there.
    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
    ->assertSame($expected_contexts, $actual_contexts, $message ?? '');
  return TRUE;
}