verifyRenderCache example

$expected_block_cache_tags = Cache::mergeTags($expected_block_cache_tags$block->getPlugin()->getCacheTags());

    // Expected contexts and tags for the BlockContent entity.     // @see \Drupal\Core\Entity\EntityViewBuilder::getBuildDefaults().     $expected_entity_cache_contexts = ['theme'];
    $expected_entity_cache_tags = Cache::mergeTags(['block_content_view']$this->entity->getCacheTags());
    $expected_entity_cache_tags = Cache::mergeTags($expected_entity_cache_tags$this->getAdditionalCacheTagsForEntity($this->entity));

    // Verify that what was render cached matches the above expectations.     $cid = $this->createCacheId($expected_block_cache_keys$expected_block_cache_contexts);
    $redirected_cid = $this->createCacheId($expected_block_cache_keys, Cache::mergeContexts($expected_block_cache_contexts$expected_entity_cache_contexts));
    $this->verifyRenderCache($cid, Cache::mergeTags($expected_block_cache_tags$expected_entity_cache_tags)($cid !== $redirected_cid) ? $redirected_cid : NULL);
  }

}


  /** * Tests that when creating a shortcut, the shortcut set tag is invalidated. */
  public function testEntityCreation() {
    // Create a cache entry that is tagged with a shortcut set cache tag.     $cache_tags = ['config:shortcut.set.default'];
    \Drupal::cache('render')->set('foo', 'bar', CacheBackendInterface::CACHE_PERMANENT, $cache_tags);

    // Verify a cache hit.     $this->verifyRenderCache('foo', $cache_tags);

    // Now create a shortcut entity in that shortcut set.     $this->createEntity();

    // Verify a cache miss.     $this->assertFalse(\Drupal::cache('render')->get('foo'), 'Creating a new shortcut invalidates the cache tag of the shortcut set.');
  }

  /** * Tests visibility and cacheability of shortcuts in the toolbar. */
  
$cid = $this->createCacheId($cache_keys$entity_cache_contexts);
    $access_cache_contexts = $this->getAccessCacheContextsForEntity($this->entity);
    $additional_cache_contexts = $this->getAdditionalCacheContextsForEntity($this->referencingEntity);
    $redirected_cid = NULL;
    if (count($access_cache_contexts) || count($additional_cache_contexts)) {
      $cache_contexts = Cache::mergeContexts($entity_cache_contexts$additional_cache_contexts);
      $cache_contexts = Cache::mergeContexts($cache_contexts$access_cache_contexts);
      $redirected_cid = $this->createCacheId($cache_keys$cache_contexts);
      $context_metadata = \Drupal::service('cache_contexts_manager')->convertTokensToKeys($cache_contexts);
      $referencing_entity_cache_tags = Cache::mergeTags($referencing_entity_cache_tags$context_metadata->getCacheTags());
    }
    $this->verifyRenderCache($cid$referencing_entity_cache_tags$redirected_cid);

    $this->verifyPageCache($non_referencing_entity_url, 'MISS');
    // Verify a cache hit, but also the presence of the correct cache tags.     $this->verifyPageCache($non_referencing_entity_url, 'HIT', Cache::mergeTags($non_referencing_entity_cache_tags$page_cache_tags));
    // Also verify the existence of an entity render cache entry.     $cache_keys = ['entity_view', 'entity_test', $this->nonReferencingEntity->id(), 'full'];
    $cid = $this->createCacheId($cache_keys$entity_cache_contexts);
    $this->verifyRenderCache($cid$non_referencing_entity_cache_tags);

    // Prime the page cache for the listing of referencing entities.     $this->verifyPageCache($listing_url, 'MISS');

    
    if (\Drupal::entityTypeManager()->getDefinition($entity_type)->isRenderCacheable()) {
      $cache_keys = ['entity_view', $entity_type$this->entity->id()$view_mode];
      $cid = $this->createCacheId($cache_keys$entity_cache_contexts);
      $redirected_cid = NULL;
      $additional_cache_contexts = $this->getAdditionalCacheContextsForEntity($this->entity);
      if (count($additional_cache_contexts)) {
        $redirected_cid = $this->createCacheId($cache_keys, Cache::mergeContexts($entity_cache_contexts$additional_cache_contexts));
      }
      $expected_cache_tags = Cache::mergeTags($cache_tag$view_cache_tag);
      $expected_cache_tags = Cache::mergeTags($expected_cache_tags$this->getAdditionalCacheTagsForEntity($this->entity));
      $expected_cache_tags = Cache::mergeTags($expected_cache_tags[$render_cache_tag]);
      $this->verifyRenderCache($cid$expected_cache_tags$redirected_cid);
    }

    // Verify that after modifying the entity, there is a cache miss.     $this->entity->save();
    $this->verifyPageCache($entity_url, 'MISS');

    // Verify a cache hit.     $this->verifyPageCache($entity_url, 'HIT');

    // Verify that after modifying the entity's display, there is a cache miss.     $entity_display = \Drupal::service('entity_display.repository')->getViewDisplay($entity_type$this->entity->bundle()$view_mode);
    
Home | Imprint | This part of the site doesn't use cookies.