function BlockViewBuilderTest::verifyRenderCacheHandling

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

Verifies render cache handling of the block being tested.

See also

::testBlockViewBuilderCache()

1 call to BlockViewBuilderTest::verifyRenderCacheHandling()
BlockViewBuilderTest::testBlockViewBuilderCache in core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php
Tests block render cache handling.

File

core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php, line 150

Class

BlockViewBuilderTest
Tests the block view builder.

Namespace

Drupal\Tests\block\Kernel

Code

protected function verifyRenderCacheHandling() {
    
    /** @var \Drupal\Core\Cache\VariationCacheFactoryInterface $variation_cache_factory */
    $variation_cache_factory = $this->container
        ->get('variation_cache_factory');
    $cache_bin = $variation_cache_factory->get('render');
    // Force a request via GET so we can test the render cache.
    $request = \Drupal::request();
    $request_method = $request->server
        ->get('REQUEST_METHOD');
    $request->setMethod('GET');
    // Test that a cache entry is created.
    $build = $this->getBlockRenderArray();
    $cache_keys = [
        'entity_view',
        'block',
        'test_block',
    ];
    $this->renderer
        ->renderRoot($build);
    $this->assertNotEmpty($cache_bin->get($cache_keys, CacheableMetadata::createFromRenderArray($build)), 'The block render element has been cached.');
    // Re-save the block and check that the cache entry has been deleted.
    $this->block
        ->save();
    $this->assertFalse($cache_bin->get($cache_keys, CacheableMetadata::createFromRenderArray($build)), 'The block render cache entry has been cleared when the block was saved.');
    // Rebuild the render array (creating a new cache entry in the process) and
    // delete the block to check the cache entry is deleted.
    unset($build['#printed']);
    // Re-add the block because \Drupal\block\BlockViewBuilder::buildBlock()
    // removes it.
    $build['#block'] = $this->block;
    $this->renderer
        ->renderRoot($build);
    $this->assertNotEmpty($cache_bin->get($cache_keys, CacheableMetadata::createFromRenderArray($build)), 'The block render element has been cached.');
    $this->block
        ->delete();
    $this->assertFalse($cache_bin->get($cache_keys, CacheableMetadata::createFromRenderArray($build)), 'The block render cache entry has been cleared when the block was deleted.');
    // Restore the previous request method.
    $request->setMethod($request_method);
}

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