BlockViewTest.php in Collapsiblock 3.x
File
tests/src/Kernel/BlockViewTest.php
View source
<?php
namespace Drupal\Tests\collapsiblock\Kernel;
use Drupal\block\Entity\Block;
class BlockViewTest extends CollapsiblockKernelTestBase {
protected $blockStorage;
protected $renderer;
protected function setUp() {
parent::setUp();
$this->blockStorage = $this->container
->get('entity_type.manager')
->getStorage('block');
$this->renderer = $this->container
->get('renderer');
}
public function testViewBlock() {
$entity = $this->blockStorage
->create([
'id' => 'test_block1',
'theme' => 'stark',
'plugin' => 'test_html',
]);
$entity
->save();
$entity = Block::load('test_block1');
$builder = \Drupal::entityTypeManager()
->getViewBuilder('block');
$build = $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($build));
}
public function testViewBlockWithUnexpectedBlockValue() {
$block_instance = $this->container
->get('plugin.manager.block')
->createInstance('test_html', []);
$base_id = $block_instance
->getBaseId();
$build = [
'#theme' => 'block',
'#attributes' => [],
'#configuration' => $block_instance
->getConfiguration(),
'#plugin_id' => $block_instance
->getPluginId(),
'#base_plugin_id' => $base_id,
'#derivative_plugin_id' => $block_instance
->getDerivativeId(),
'#id' => $block_instance
->getMachineNameSuggestion(),
'#block' => 'foo',
'content' => $block_instance
->build(),
];
$this->container
->get('module_handler')
->alter([
'block_view',
"block_view_{$base_id}",
], $build, $block_instance);
$expected = [];
$expected[] = '<div id="block-testhtmlblock">';
$expected[] = ' ';
$expected[] = ' ';
$expected[] = ' ';
$expected[] = ' </div>';
$expected[] = '';
$expected_output = implode("\n", $expected);
$this
->assertEquals($expected_output, $this->renderer
->renderRoot($build));
}
}