function RenderCacheIntegrationTest::assertCacheTagsForFieldBasedView

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/RenderCacheIntegrationTest.php \Drupal\Tests\views\Kernel\RenderCacheIntegrationTest::assertCacheTagsForFieldBasedView()
  2. 8.9.x core/modules/views/tests/src/Kernel/RenderCacheIntegrationTest.php \Drupal\Tests\views\Kernel\RenderCacheIntegrationTest::assertCacheTagsForFieldBasedView()
  3. 10 core/modules/views/tests/src/Kernel/RenderCacheIntegrationTest.php \Drupal\Tests\views\Kernel\RenderCacheIntegrationTest::assertCacheTagsForFieldBasedView()

Tests cache tags on output & result cache items for a field-based view.

@internal

Parameters

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

3 calls to RenderCacheIntegrationTest::assertCacheTagsForFieldBasedView()
RenderCacheIntegrationTest::testFieldBasedViewCacheTagsWithCachePluginNone in core/modules/views/tests/src/Kernel/RenderCacheIntegrationTest.php
Tests a field-based view's cache tags when using the "none" cache plugin.
RenderCacheIntegrationTest::testFieldBasedViewCacheTagsWithCachePluginTag in core/modules/views/tests/src/Kernel/RenderCacheIntegrationTest.php
Tests a field-based view's cache tags when using the "tag" cache plugin.
RenderCacheIntegrationTest::testFieldBasedViewCacheTagsWithCachePluginTime in core/modules/views/tests/src/Kernel/RenderCacheIntegrationTest.php
Tests a field-based view's cache tags when using the "time" cache plugin.

File

core/modules/views/tests/src/Kernel/RenderCacheIntegrationTest.php, line 95

Class

RenderCacheIntegrationTest
Tests the general integration between views and the render cache.

Namespace

Drupal\Tests\views\Kernel

Code

protected function assertCacheTagsForFieldBasedView(bool $do_assert_views_caches) : void {
    $view = Views::getView('entity_test_fields');
    // Empty result (no entities yet).
    $base_tags = [
        'config:views.view.entity_test_fields',
        'entity_test_list',
    ];
    $this->assertViewsCacheTags($view, $base_tags, $do_assert_views_caches, $base_tags);
    $this->assertViewsCacheTagsFromStaticRenderArray($view, $base_tags, $do_assert_views_caches);
    // Non-empty result (1 entity).
    
    /** @var \Drupal\Core\Entity\EntityInterface[] $entities */
    $entities[] = $entity = EntityTest::create();
    $entity->save();
    $tags_with_entity = Cache::mergeTags($base_tags, $entities[0]->getCacheTags());
    $this->assertViewsCacheTags($view, $tags_with_entity, $do_assert_views_caches, $tags_with_entity);
    $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_with_entity, $do_assert_views_caches);
    // Paged result (more entities than the items-per-page limit).
    for ($i = 0; $i < 5; $i++) {
        $entities[] = $entity = EntityTest::create();
        $entity->save();
    }
    // Test pager.
    // Page 1.
    \Drupal::request()->query
        ->set('page', 0);
    $tags_page_1 = Cache::mergeTags($base_tags, $entities[1]->getCacheTags());
    $tags_page_1 = Cache::mergeTags($tags_page_1, $entities[2]->getCacheTags());
    $tags_page_1 = Cache::mergeTags($tags_page_1, $entities[3]->getCacheTags());
    $tags_page_1 = Cache::mergeTags($tags_page_1, $entities[4]->getCacheTags());
    $tags_page_1 = Cache::mergeTags($tags_page_1, $entities[5]->getCacheTags());
    $this->assertViewsCacheTags($view, $tags_page_1, $do_assert_views_caches, $tags_page_1);
    $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_page_1, $do_assert_views_caches);
    $view->destroy();
    // Page 2.
    $view->setCurrentPage(1);
    \Drupal::request()->query
        ->set('page', 1);
    $tags_page_2 = Cache::mergeTags($base_tags, $entities[0]->getCacheTags());
    $this->assertViewsCacheTags($view, $tags_page_2, $do_assert_views_caches, $tags_page_2);
    $view->destroy();
    // Ensure that invalidation works on both pages.
    // Page 2.
    $view->setCurrentPage(1);
    \Drupal::request()->query
        ->set('page', 1);
    $entities[0]->name->value = $random_name = $this->randomMachineName();
    $entities[0]->save();
    $build = $this->assertViewsCacheTags($view, $tags_page_2, $do_assert_views_caches, $tags_page_2);
    // @todo Static render arrays don't support different pages yet, see
    //   https://www.drupal.org/node/2500701.
    // $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_page_2, $do_assert_views_caches);
    $this->assertStringContainsString($random_name, (string) $build['#markup']);
    $view->destroy();
    // Page 1.
    $view->setCurrentPage(0);
    \Drupal::request()->query
        ->set('page', 0);
    $entities[1]->name->value = $random_name = $this->randomMachineName();
    $entities[1]->save();
    $build = $this->assertViewsCacheTags($view, $tags_page_1, $do_assert_views_caches, $tags_page_1);
    $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_page_1, $do_assert_views_caches);
    $this->assertStringContainsString($random_name, (string) $build['#markup']);
    $view->destroy();
    // Setup arguments to ensure that render caching also varies by them.
    // Custom assert for a single result row.
    $single_entity_assertions = function (array $build, EntityInterface $entity) {
        $this->setRawContent($build['#markup']);
        $result = $this->cssSelect('div.views-row');
        $count = count($result);
        $this->assertEquals(1, $count);
        $this->assertEquals((string) $entity->id(), (string) $result[0]->div->span);
    };
    // Execute the view once with a static renderable and one with a full
    // prepared render array.
    $tags_argument = Cache::mergeTags($base_tags, $entities[0]->getCacheTags());
    $view->setArguments([
        $entities[0]->id(),
    ]);
    $build = $this->assertViewsCacheTags($view, $tags_argument, $do_assert_views_caches, $tags_argument);
    $single_entity_assertions($build, $entities[0]);
    $view->setArguments([
        $entities[0]->id(),
    ]);
    $build = $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_argument, $do_assert_views_caches);
    $single_entity_assertions($build, $entities[0]);
    // Set a different argument and ensure that the result is different.
    $tags2_argument = Cache::mergeTags($base_tags, $entities[1]->getCacheTags());
    $view->setArguments([
        $entities[1]->id(),
    ]);
    $build = $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags2_argument, $do_assert_views_caches);
    $single_entity_assertions($build, $entities[1]);
    $view->destroy();
}

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