function FrontPageTest::doTestFrontPageViewCacheTags

Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Functional/Views/FrontPageTest.php \Drupal\Tests\node\Functional\Views\FrontPageTest::doTestFrontPageViewCacheTags()
  2. 10 core/modules/node/tests/src/Functional/Views/FrontPageTest.php \Drupal\Tests\node\Functional\Views\FrontPageTest::doTestFrontPageViewCacheTags()
  3. 11.x core/modules/node/tests/src/Functional/Views/FrontPageTest.php \Drupal\Tests\node\Functional\Views\FrontPageTest::doTestFrontPageViewCacheTags()

Tests the cache tags on the front page.

Parameters

bool $do_assert_views_caches: Whether to check Views' result & output caches.

3 calls to FrontPageTest::doTestFrontPageViewCacheTags()
FrontPageTest::testCacheTagsWithCachePluginNone in core/modules/node/tests/src/Functional/Views/FrontPageTest.php
Tests the cache tags when using the "none" cache plugin.
FrontPageTest::testCacheTagsWithCachePluginTag in core/modules/node/tests/src/Functional/Views/FrontPageTest.php
Tests the cache tags when using the "tag" cache plugin.
FrontPageTest::testCacheTagsWithCachePluginTime in core/modules/node/tests/src/Functional/Views/FrontPageTest.php
Tests the cache tags when using the "time" cache plugin.

File

core/modules/node/tests/src/Functional/Views/FrontPageTest.php, line 243

Class

FrontPageTest
Tests the default frontpage provided by views.

Namespace

Drupal\Tests\node\Functional\Views

Code

protected function doTestFrontPageViewCacheTags($do_assert_views_caches) {
    $view = Views::getView('frontpage');
    $view->setDisplay('page_1');
    $cache_contexts = [
        // Cache contexts associated with the view.
'user.node_grants:view',
        'languages:' . LanguageInterface::TYPE_INTERFACE,
        // Cache contexts associated with the route's access checking.
'user.permissions',
        // Default cache contexts of the renderer.
'theme',
        'url.query_args',
        // Attached feed.
'url.site',
    ];
    $cache_context_tags = \Drupal::service('cache_contexts_manager')->convertTokensToKeys($cache_contexts)
        ->getCacheTags();
    // Test before there are any nodes.
    $empty_node_listing_cache_tags = [
        'config:views.view.frontpage',
        'node_list',
    ];
    $render_cache_tags = Cache::mergeTags($empty_node_listing_cache_tags, $cache_context_tags);
    $render_cache_tags = Cache::mergeTags($render_cache_tags, [
        'config:system.site',
    ]);
    $this->assertViewsCacheTags($view, $empty_node_listing_cache_tags, $do_assert_views_caches, $render_cache_tags);
    $expected_tags = Cache::mergeTags($empty_node_listing_cache_tags, $cache_context_tags);
    $expected_tags = Cache::mergeTags($expected_tags, [
        'http_response',
        'rendered',
        'config:user.role.anonymous',
        'config:system.site',
    ]);
    $this->assertPageCacheContextsAndTags(Url::fromRoute('view.frontpage.page_1'), $cache_contexts, $expected_tags);
    // Create some nodes on the frontpage view. Add more than 10 nodes in order
    // to enable paging.
    $this->drupalCreateContentType([
        'type' => 'article',
    ]);
    for ($i = 0; $i < 15; $i++) {
        $node = Node::create([
            'body' => [
                [
                    'value' => $this->randomMachineName(32),
                    'format' => filter_default_format(),
                ],
            ],
            'type' => 'article',
            'created' => $i,
            'title' => $this->randomMachineName(8),
            'nid' => $i + 1,
        ]);
        $node->enforceIsNew(TRUE);
        $node->save();
    }
    $cache_contexts = Cache::mergeContexts($cache_contexts, [
        'timezone',
    ]);
    // First page.
    $first_page_result_cache_tags = [
        'config:views.view.frontpage',
        'node_list',
        'node:6',
        'node:7',
        'node:8',
        'node:9',
        'node:10',
        'node:11',
        'node:12',
        'node:13',
        'node:14',
        'node:15',
    ];
    $cache_context_tags = \Drupal::service('cache_contexts_manager')->convertTokensToKeys($cache_contexts)
        ->getCacheTags();
    $first_page_output_cache_tags = Cache::mergeTags($first_page_result_cache_tags, $cache_context_tags);
    $first_page_output_cache_tags = Cache::mergeTags($first_page_output_cache_tags, [
        'config:filter.format.plain_text',
        'node_view',
        'user_view',
        'user:0',
    ]);
    $view->setDisplay('page_1');
    $view->setCurrentPage(0);
    $this->assertViewsCacheTags($view, $first_page_result_cache_tags, $do_assert_views_caches, $first_page_output_cache_tags);
    $this->assertPageCacheContextsAndTags(Url::fromRoute('view.frontpage.page_1'), $cache_contexts, Cache::mergeTags($first_page_output_cache_tags, [
        'http_response',
        'rendered',
        'config:user.role.anonymous',
    ]));
    // Second page.
    $this->assertPageCacheContextsAndTags(Url::fromRoute('view.frontpage.page_1', [], [
        'query' => [
            'page' => 1,
        ],
    ]), $cache_contexts, [
        // The cache tags for the listed nodes.
'node:1',
        'node:2',
        'node:3',
        'node:4',
        'node:5',
        // The rest.
'config:filter.format.plain_text',
        'config:views.view.frontpage',
        'node_list',
        'node_view',
        'user_view',
        'user:0',
        'http_response',
        'rendered',
        // FinishResponseSubscriber adds this cache tag to responses that have the
        // 'user.permissions' cache context for anonymous users.
'config:user.role.anonymous',
    ]);
    // Let's update a node title on the first page and ensure that the page
    // cache entry invalidates.
    $node = Node::load(10);
    $title = $node->getTitle() . 'a';
    $node->setTitle($title);
    $node->save();
    $this->drupalGet(Url::fromRoute('view.frontpage.page_1'));
    $this->assertText($title);
}

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