assertViewsCacheTags example


  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);

    


    $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);
    $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']);
    $this->assertPageCacheContextsAndTags(
      Url::fromRoute('view.frontpage.page_1'),
      $cache_contexts,
      $expected_tags
    );
Home | Imprint | This part of the site doesn't use cookies.