function BlockViewBuilderTest::testBasicRendering

Same name and namespace in other branches
  1. 9 core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php \Drupal\Tests\block\Kernel\BlockViewBuilderTest::testBasicRendering()
  2. 8.9.x core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php \Drupal\Tests\block\Kernel\BlockViewBuilderTest::testBasicRendering()
  3. 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 75

Class

BlockViewBuilderTest
Tests the block view builder.

Namespace

Drupal\Tests\block\Kernel

Code

public function testBasicRendering() : void {
  \Drupal::keyValue('block_test')->set('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->assertSame($expected_output, (string) $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->assertSame($expected_output, (string) $this->renderer
    ->renderRoot($output));
}

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