function 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 TRUE if the assertion succeeded, FALSE otherwise.

2 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.
ContentTranslationUITestBase::doTestBasicTranslation in core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php
Tests the basic translation workflow.

File

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

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);
  $match = $actual_contexts === $expected_contexts;
  if (!$match) {
    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)));
  }
  $this->assertIdentical($actual_contexts, $expected_contexts, $message);
  // For compatibility with both BrowserTestBase and WebTestBase always return
  // a boolean.
  return $match;
}

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