function AssertPageCacheContextsAndTagsTrait::assertCacheContexts

Same name in other branches
  1. 8.9.x core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php \Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait::assertCacheContexts()
  2. 8.9.x core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php \Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait::assertCacheContexts()
  3. 10 core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php \Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait::assertCacheContexts()
  4. 11.x 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.

19 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.
CommentRssTest::testCommentRss in core/modules/comment/tests/src/Functional/CommentRssTest.php
Tests comments as part of an RSS feed.
ContentTranslationUITestBase::doTestBasicTranslation in core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php
Tests the basic translation workflow.
DisplayBlockTest::testBlockEmptyRendering in core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php
Tests the various testcases of empty block rendering.
DisplayPageWebTest::testArguments in core/modules/views/tests/src/Functional/Plugin/DisplayPageWebTest.php
Tests arguments.

... See full list

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;
}

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