You are here

protected function AssertPageCacheContextsAndTagsTrait::assertCacheContexts in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php \Drupal\system\Tests\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

TRUE if the assertion succeeded, FALSE otherwise.

16 calls to AssertPageCacheContextsAndTagsTrait::assertCacheContexts()
AssertPageCacheContextsAndTagsTrait::assertPageCacheContextsAndTags in core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php
Asserts page cache miss, then hit for the given URL; checks cache headers.
CommentRssTest::testCommentRss in core/modules/comment/src/Tests/CommentRssTest.php
Tests comments as part of an RSS feed.
ContentTranslationUITestBase::doTestBasicTranslation in core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php
Tests the basic translation workflow.
DisplayBlockTest::testBlockEmptyRendering in core/modules/block/src/Tests/Views/DisplayBlockTest.php
Tests the various testcases of empty block rendering.
DisplayPageWebTest::testArguments in core/modules/views/src/Tests/Plugin/DisplayPageWebTest.php
Tests arguments.

... See full list

File

core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php, line 131
Contains \Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait.

Class

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

Namespace

Drupal\system\Tests\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);
  $return = $this
    ->assertIdentical($actual_contexts, $expected_contexts, $message);
  if (!$return) {
    debug('Unwanted cache contexts in response: ' . implode(',', array_diff($actual_contexts, $expected_contexts)));
    debug('Missing cache contexts in response: ' . implode(',', array_diff($expected_contexts, $actual_contexts)));
  }
  return $return;
}