comparison core/modules/block/tests/src/Kernel/BlockInterfaceTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\Tests\block\Kernel;
4
5 use Drupal\Core\Block\BlockPluginInterface;
6 use Drupal\Core\Form\FormState;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10 * Tests that the block plugin can work properly without a supporting entity.
11 *
12 * @group block
13 */
14 class BlockInterfaceTest extends KernelTestBase {
15
16 public static $modules = ['system', 'block', 'block_test', 'user'];
17
18 /**
19 * Test configuration and subsequent form() and build() method calls.
20 *
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.
27 *
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.
31 */
32 public function testBlockInterface() {
33 $manager = $this->container->get('plugin.manager.block');
34 $configuration = [
35 'label' => 'Custom Display Message',
36 ];
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',
43 ];
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.');
48
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();
54
55 $expected_form = [
56 'provider' => [
57 '#type' => 'value',
58 '#value' => 'block_test',
59 ],
60 'admin_label' => [
61 '#type' => 'item',
62 '#title' => t('Block description'),
63 '#plain_text' => $definition['admin_label'],
64 ],
65 'label' => [
66 '#type' => 'textfield',
67 '#title' => 'Title',
68 '#maxlength' => 255,
69 '#default_value' => 'Custom Display Message',
70 '#required' => TRUE,
71 ],
72 'label_display' => [
73 '#type' => 'checkbox',
74 '#title' => 'Display title',
75 '#default_value' => TRUE,
76 '#return_value' => 'visible',
77 ],
78 'context_mapping' => [],
79 'display_message' => [
80 '#type' => 'textfield',
81 '#title' => t('Display message'),
82 '#default_value' => 'My custom display message.',
83 ],
84 ];
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.');
91
92 $expected_build = [
93 '#children' => 'My custom display message.',
94 ];
95 // Ensure the build array is proper.
96 $this->assertIdentical($display_block->build(), $expected_build, 'The plugin returned the appropriate build array.');
97
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.');
101 }
102
103 }