Same filename and directory in other branches
- 8.9.x core/modules/block/tests/src/Kernel/BlockInterfaceTest.php
- 9 core/modules/block/tests/src/Kernel/BlockInterfaceTest.php
Namespace
Drupal\Tests\block\Kernel
File
core/modules/block/tests/src/Kernel/BlockInterfaceTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\block\Kernel;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormState;
use Drupal\KernelTests\KernelTestBase;
class BlockInterfaceTest extends KernelTestBase {
protected static $modules = [
'system',
'block',
'block_test',
'user',
];
public function testBlockInterface() {
$manager = $this->container
->get('plugin.manager.block');
$configuration = [
'label' => 'Custom Display Message',
];
$expected_configuration = [
'id' => 'test_block_instantiation',
'label' => 'Custom Display Message',
'label_display' => BlockPluginInterface::BLOCK_LABEL_VISIBLE,
'provider' => 'block_test',
'display_message' => 'no message set',
];
$display_block = $manager
->createInstance('test_block_instantiation', $configuration);
$this
->assertSame($expected_configuration, $display_block
->getConfiguration(), 'The block was configured correctly.');
$display_block
->setConfigurationValue('display_message', 'My custom display message.');
$expected_configuration['display_message'] = 'My custom display message.';
$this
->assertSame($expected_configuration, $display_block
->getConfiguration(), 'The block configuration was updated correctly.');
$definition = $display_block
->getPluginDefinition();
$expected_form = [
'provider' => [
'#type' => 'value',
'#value' => 'block_test',
],
'admin_label' => [
'#type' => 'item',
'#title' => 'Block description',
'#plain_text' => $definition['admin_label'],
],
'label' => [
'#type' => 'textfield',
'#title' => 'Title',
'#maxlength' => 255,
'#default_value' => 'Custom Display Message',
'#required' => TRUE,
],
'label_display' => [
'#type' => 'checkbox',
'#title' => 'Display title',
'#default_value' => TRUE,
'#return_value' => 'visible',
],
'context_mapping' => [],
'display_message' => [
'#type' => 'textfield',
'#title' => 'Display message',
'#default_value' => 'My custom display message.',
],
];
$form_state = new FormState();
$actual_form = $display_block
->buildConfigurationForm([], $form_state);
unset($actual_form['visibility'], $actual_form['visibility_tabs']);
$this
->assertEquals($expected_form, $actual_form, 'Only the expected form elements were present.');
$expected_build = [
'#children' => 'My custom display message.',
];
$this
->assertSame($expected_build, $display_block
->build(), 'The plugin returned the appropriate build array.');
$this
->assertSame('displaymessage', $display_block
->getMachineNameSuggestion(), 'The plugin returned the expected machine name suggestion.');
}
}
Classes
Name |
Description |
BlockInterfaceTest |
Tests that the block plugin can work properly without a supporting entity. |