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