3 namespace Drupal\Tests\block\Kernel;
5 use Drupal\Core\Block\BlockPluginInterface;
6 use Drupal\Core\Form\FormState;
7 use Drupal\KernelTests\KernelTestBase;
10 * Tests that the block plugin can work properly without a supporting entity.
14 class BlockInterfaceTest extends KernelTestBase {
16 public static $modules = ['system', 'block', 'block_test', 'user'];
19 * Test configuration and subsequent form() and build() method calls.
21 * This test is attempting to test the existing block plugin api and all
22 * functionality that is expected to remain consistent. The arrays that are
23 * used for comparison can change, but only to include elements that are
24 * contained within BlockBase or the plugin being tested. Likely these
25 * comparison arrays should get smaller, not larger, as more form/build
26 * elements are moved into a more suitably responsible class.
28 * Instantiation of the plugin is the primary element being tested here. The
29 * subsequent method calls are just attempting to cause a failure if a
30 * dependency outside of the plugin configuration is required.
32 public function testBlockInterface() {
33 $manager = $this->container->get('plugin.manager.block');
35 'label' => 'Custom Display Message',
37 $expected_configuration = [
38 'id' => 'test_block_instantiation',
39 'label' => 'Custom Display Message',
40 'provider' => 'block_test',
41 'label_display' => BlockPluginInterface::BLOCK_LABEL_VISIBLE,
42 'display_message' => 'no message set',
44 // Initial configuration of the block at construction time.
45 /** @var $display_block \Drupal\Core\Block\BlockPluginInterface */
46 $display_block = $manager->createInstance('test_block_instantiation', $configuration);
47 $this->assertIdentical($display_block->getConfiguration(), $expected_configuration, 'The block was configured correctly.');
49 // Updating an element of the configuration.
50 $display_block->setConfigurationValue('display_message', 'My custom display message.');
51 $expected_configuration['display_message'] = 'My custom display message.';
52 $this->assertIdentical($display_block->getConfiguration(), $expected_configuration, 'The block configuration was updated correctly.');
53 $definition = $display_block->getPluginDefinition();
58 '#value' => 'block_test',
62 '#title' => t('Block description'),
63 '#plain_text' => $definition['admin_label'],
66 '#type' => 'textfield',
69 '#default_value' => 'Custom Display Message',
73 '#type' => 'checkbox',
74 '#title' => 'Display title',
75 '#default_value' => TRUE,
76 '#return_value' => 'visible',
78 'context_mapping' => [],
79 'display_message' => [
80 '#type' => 'textfield',
81 '#title' => t('Display message'),
82 '#default_value' => 'My custom display message.',
85 $form_state = new FormState();
86 // Ensure there are no form elements that do not belong to the plugin.
87 $actual_form = $display_block->buildConfigurationForm([], $form_state);
88 // Remove the visibility sections, as that just tests condition plugins.
89 unset($actual_form['visibility'], $actual_form['visibility_tabs']);
90 $this->assertIdentical($this->castSafeStrings($actual_form), $this->castSafeStrings($expected_form), 'Only the expected form elements were present.');
93 '#children' => 'My custom display message.',
95 // Ensure the build array is proper.
96 $this->assertIdentical($display_block->build(), $expected_build, 'The plugin returned the appropriate build array.');
98 // Ensure the machine name suggestion is correct. In truth, this is actually
99 // testing BlockBase's implementation, not the interface itself.
100 $this->assertIdentical($display_block->getMachineNameSuggestion(), 'displaymessage', 'The plugin returned the expected machine name suggestion.');