public function BlockContentCacheTagsTest::testBlock in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php \Drupal\block_content\Tests\BlockContentCacheTagsTest::testBlock()
Tests that the block is cached with the correct contexts and tags.
File
- core/
modules/ block_content/ src/ Tests/ BlockContentCacheTagsTest.php, line 75 - Contains \Drupal\block_content\Tests\BlockContentCacheTagsTest.
Class
- BlockContentCacheTagsTest
- Tests the Custom Block entity's cache tags.
Namespace
Drupal\block_content\TestsCode
public function testBlock() {
$block = $this
->drupalPlaceBlock('block_content:' . $this->entity
->uuid());
$build = $this->container
->get('entity.manager')
->getViewBuilder('block')
->view($block, 'block');
// Render the block.
// @todo The request stack manipulation won't be necessary once
// https://www.drupal.org/node/2367555 is fixed and the
// corresponding $request->isMethodSafe() checks are removed from
// Drupal\Core\Render\Renderer.
$request_stack = $this->container
->get('request_stack');
$request_stack
->push(new Request());
$this->container
->get('renderer')
->renderRoot($build);
$request_stack
->pop();
// Expected keys, contexts, and tags for the block.
// @see \Drupal\block\BlockViewBuilder::viewMultiple()
$expected_block_cache_keys = [
'entity_view',
'block',
$block
->id(),
];
$expected_block_cache_contexts = [
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
];
$expected_block_cache_tags = Cache::mergeTags([
'block_view',
'rendered',
], $block
->getCacheTags());
$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);
}