function CacheWebTest::testCacheOutputOnPage

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Functional/Plugin/CacheWebTest.php \Drupal\Tests\views\Functional\Plugin\CacheWebTest::testCacheOutputOnPage()
  2. 8.9.x core/modules/views/tests/src/Functional/Plugin/CacheWebTest.php \Drupal\Tests\views\Functional\Plugin\CacheWebTest::testCacheOutputOnPage()
  3. 10 core/modules/views/tests/src/Functional/Plugin/CacheWebTest.php \Drupal\Tests\views\Functional\Plugin\CacheWebTest::testCacheOutputOnPage()

Tests the output caching on an actual page.

File

core/modules/views/tests/src/Functional/Plugin/CacheWebTest.php, line 53

Class

CacheWebTest
Tests pluggable caching for views via a web test.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testCacheOutputOnPage() : void {
    $view = Views::getView('test_display');
    $view->storage
        ->setStatus(TRUE);
    $view->setDisplay('page_1');
    $view->display_handler
        ->overrideOption('cache', [
        'type' => 'time',
        'options' => [
            'results_lifespan' => '3600',
            'output_lifespan' => '3600',
        ],
    ]);
    $view->save();
    $this->container
        ->get('router.builder')
        ->rebuildIfNeeded();
    
    /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
    $render_cache = \Drupal::service('render_cache');
    $cache_element = DisplayPluginBase::buildBasicRenderable('test_display', 'page_1');
    $cache_element['#cache'] += [
        'contexts' => $this->container
            ->getParameter('renderer.config')['required_cache_contexts'],
    ];
    $this->assertFalse($render_cache->get($cache_element));
    $this->drupalGet('test-display');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertNotEmpty($render_cache->get($cache_element));
    $cache_tags = [
        'config:user.role.anonymous',
        'config:views.view.test_display',
        'node_list',
        'rendered',
    ];
    $this->assertCacheTags($cache_tags);
    $this->drupalGet('test-display');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertNotEmpty($render_cache->get($cache_element));
    $this->assertCacheTags($cache_tags);
}

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