You are here

public function BlockViewBuilderTest::testBasicRendering in Drupal 9

Same name and namespace in other branches
  1. 8 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\Kernel

Code

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));
}