protected function BlockViewBuilderTest::assertBlockRenderedWithExpectedCacheability in Drupal 8
Same name and namespace in other branches
- 9 core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php \Drupal\Tests\block\Kernel\BlockViewBuilderTest::assertBlockRenderedWithExpectedCacheability()
- 10 core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php \Drupal\Tests\block\Kernel\BlockViewBuilderTest::assertBlockRenderedWithExpectedCacheability()
Asserts that a block is built/rendered/cached with expected cacheability.
Parameters
string[] $expected_keys: The expected cache keys.
string[] $expected_contexts: The expected cache contexts.
string[] $expected_tags: The expected cache tags.
int $expected_max_age: The expected max-age.
1 call to BlockViewBuilderTest::assertBlockRenderedWithExpectedCacheability()
- BlockViewBuilderTest::testBlockViewBuilderBuildAlter in core/
modules/ block/ tests/ src/ Kernel/ BlockViewBuilderTest.php - Tests block build altering.
File
- core/
modules/ block/ tests/ src/ Kernel/ BlockViewBuilderTest.php, line 290
Class
- BlockViewBuilderTest
- Tests the block view builder.
Namespace
Drupal\Tests\block\KernelCode
protected function assertBlockRenderedWithExpectedCacheability(array $expected_keys, array $expected_contexts, array $expected_tags, $expected_max_age) {
$required_cache_contexts = [
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
];
// Check that the expected cacheability metadata is present in:
// - the built render array;
$build = $this
->getBlockRenderArray();
$this
->assertIdentical($expected_keys, $build['#cache']['keys']);
$this
->assertIdentical($expected_contexts, $build['#cache']['contexts']);
$this
->assertIdentical($expected_tags, $build['#cache']['tags']);
$this
->assertIdentical($expected_max_age, $build['#cache']['max-age']);
$this
->assertFalse(isset($build['#create_placeholder']));
// - the rendered render array;
$this->renderer
->renderRoot($build);
// - the render cache item.
$final_cache_contexts = Cache::mergeContexts($expected_contexts, $required_cache_contexts);
$cid = implode(':', $expected_keys) . ':' . implode(':', \Drupal::service('cache_contexts_manager')
->convertTokensToKeys($final_cache_contexts)
->getKeys());
$cache_item = $this->container
->get('cache.render')
->get($cid);
$this
->assertNotEmpty($cache_item, 'The block render element has been cached with the expected cache ID.');
$this
->assertIdentical(Cache::mergeTags($expected_tags, [
'rendered',
]), $cache_item->tags);
$this
->assertIdentical($final_cache_contexts, $cache_item->data['#cache']['contexts']);
$this
->assertIdentical($expected_tags, $cache_item->data['#cache']['tags']);
$this
->assertIdentical($expected_max_age, $cache_item->data['#cache']['max-age']);
$this->container
->get('cache.render')
->delete($cid);
}