function AssertPageCacheContextsAndTagsTrait::assertCacheContexts

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

21 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 test cases 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 132

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) : bool {
    if ($include_default_contexts) {
        $default_contexts = [
            'languages:language_interface',
            'theme',
        ];
        // Add the user based contexts to the list of default contexts except when
        // user is already there.
        if (!in_array('user', $expected_contexts)) {
            $default_contexts[] = 'user.permissions';
            if (!in_array('user.roles', $expected_contexts)) {
                // The system_page_attachments() hook is only called when dealing with
                // the HtmlRenderer, so check the Content-Type header.
                // @see \Drupal\Core\Render\MainContent\HtmlRenderer::invokePageAttachmentHooks()
                if ($this->getSession()
                    ->getResponseHeader('Content-Type') === 'text/html; charset=UTF-8') {
                    $default_contexts[] = 'user.roles:authenticated';
                }
            }
        }
        $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.