function PageCacheTest::testPageCacheTagsIndependentFromCacheabilityHeaders

Same name in other branches
  1. 8.9.x core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCacheTagsIndependentFromCacheabilityHeaders()
  2. 10 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCacheTagsIndependentFromCacheabilityHeaders()
  3. 11.x core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCacheTagsIndependentFromCacheabilityHeaders()

Tests that the page cache doesn't depend on cacheability headers.

File

core/modules/page_cache/tests/src/Functional/PageCacheTest.php, line 87

Class

PageCacheTest
Enables the page cache and tests it with various HTTP requests.

Namespace

Drupal\Tests\page_cache\Functional

Code

public function testPageCacheTagsIndependentFromCacheabilityHeaders() {
    // Disable the cacheability headers.
    $this->setContainerParameter('http.response.debug_cacheability_headers', FALSE);
    $this->rebuildContainer();
    $this->resetAll();
    $path = 'system-test/cache_tags_page';
    $tags = [
        'system_test_cache_tags_page',
    ];
    $this->drupalGet($path);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    // Verify a cache hit, but also the presence of the correct cache tags.
    $this->drupalGet($path);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    $cid_parts = [
        Url::fromRoute('system_test.cache_tags_page', [], [
            'absolute' => TRUE,
        ])->toString(),
        '',
    ];
    $cid = implode(':', $cid_parts);
    $cache_entry = \Drupal::cache('page')->get($cid);
    sort($cache_entry->tags);
    $expected_tags = [
        'config:user.role.anonymous',
        'http_response',
        'pre_render',
        'rendered',
        'system_test_cache_tags_page',
    ];
    $this->assertSame($expected_tags, $cache_entry->tags);
    Cache::invalidateTags($tags);
    $this->drupalGet($path);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
}

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