function BlockViewBuilderTest::testBasicRendering
Same name in other branches
- 8.9.x core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php \Drupal\Tests\block\Kernel\BlockViewBuilderTest::testBasicRendering()
- 10 core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php \Drupal\Tests\block\Kernel\BlockViewBuilderTest::testBasicRendering()
- 11.x core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php \Drupal\Tests\block\Kernel\BlockViewBuilderTest::testBasicRendering()
Tests the rendering of blocks.
File
-
core/
modules/ block/ tests/ src/ Kernel/ BlockViewBuilderTest.php, line 74
Class
- BlockViewBuilderTest
- Tests the block view builder.
Namespace
Drupal\Tests\block\KernelCode
public function testBasicRendering() {
\Drupal::state()->set('block_test.content', '');
$entity = $this->controller
->create([
'id' => 'test_block1',
'theme' => 'stark',
'plugin' => 'test_html',
]);
$entity->save();
// Test the rendering of a block.
$entity = Block::load('test_block1');
$builder = \Drupal::entityTypeManager()->getViewBuilder('block');
$output = $builder->view($entity, 'block');
$expected = [];
$expected[] = '<div id="block-test-block1">';
$expected[] = ' ';
$expected[] = ' ';
$expected[] = ' ';
$expected[] = ' </div>';
$expected[] = '';
$expected_output = implode("\n", $expected);
$this->assertEquals($expected_output, $this->renderer
->renderRoot($output));
// Reset the HTML IDs so that the next render is not affected.
Html::resetSeenIds();
// Test the rendering of a block with a given title.
$entity = $this->controller
->create([
'id' => 'test_block2',
'theme' => 'stark',
'plugin' => 'test_html',
'settings' => [
'label' => 'Powered by Bananas',
],
]);
$entity->save();
$output = $builder->view($entity, 'block');
$expected = [];
$expected[] = '<div id="block-test-block2">';
$expected[] = ' ';
$expected[] = ' <h2>Powered by Bananas</h2>';
$expected[] = ' ';
$expected[] = ' ';
$expected[] = ' </div>';
$expected[] = '';
$expected_output = implode("\n", $expected);
$this->assertEquals($expected_output, $this->renderer
->renderRoot($output));
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.