BlockInterfaceTest.php 5.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\block\Tests\BlockInterfaceTest.
 */

namespace Drupal\block\Tests;

use Drupal\simpletest\DrupalUnitTestBase;
11
use Drupal\block\BlockInterface;
12 13

/**
14 15 16
 * Tests that the block plugin can work properly without a supporting entity.
 *
 * @group block
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 */
class BlockInterfaceTest extends DrupalUnitTestBase {
  public static $modules = array('system', 'block', 'block_test', 'user');

  /**
   * Test configuration and subsequent form() and build() method calls.
   *
   * This test is attempting to test the existing block plugin api and all
   * functionality that is expected to remain consistent. The arrays that are
   * used for comparison can change, but only to include elements that are
   * contained within BlockBase or the plugin being tested. Likely these
   * comparison arrays should get smaller, not larger, as more form/build
   * elements are moved into a more suitably responsible class.
   *
   * Instantiation of the plugin is the primary element being tested here. The
   * subsequent method calls are just attempting to cause a failure if a
   * dependency outside of the plugin configuration is required.
   */
  public function testBlockInterface() {
    $manager = $this->container->get('plugin.manager.block');
    $configuration = array(
      'label' => 'Custom Display Message',
    );
    $expected_configuration = array(
41
      'visibility' => array(),
42
      'id' => 'test_block_instantiation',
43
      'label' => 'Custom Display Message',
44
      'provider' => 'block_test',
45
      'label_display' => BlockInterface::BLOCK_LABEL_VISIBLE,
46 47 48 49 50
      'cache' => array(
        'max_age' => 0,
        'contexts' => array(),
      ),
      'display_message' => 'no message set',
51 52
    );
    // Initial configuration of the block at construction time.
53
    /** @var $display_block \Drupal\block\BlockPluginInterface */
54
    $display_block = $manager->createInstance('test_block_instantiation', $configuration);
55
    $this->assertIdentical($display_block->getConfiguration(), $expected_configuration, 'The block was configured correctly.');
56 57

    // Updating an element of the configuration.
58
    $display_block->setConfigurationValue('display_message', 'My custom display message.');
59
    $expected_configuration['display_message'] = 'My custom display message.';
60
    $this->assertIdentical($display_block->getConfiguration(), $expected_configuration, 'The block configuration was updated correctly.');
61
    $definition = $display_block->getPluginDefinition();
62

63
    $period = array(0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400);
64
    $period = array_map(array(\Drupal::service('date'), 'formatInterval'), array_combine($period, $period));
65 66 67 68
    $period[0] = '<' . t('no caching') . '>';
    $period[\Drupal\Core\Cache\Cache::PERMANENT] = t('Forever');
    $contexts = \Drupal::service("cache_contexts")->getLabels();
    unset($contexts['cache_context.theme']);
69
    $expected_form = array(
70
      'provider' => array(
71 72 73
        '#type' => 'value',
        '#value' => 'block_test',
      ),
74 75 76 77 78
      'admin_label' => array(
        '#type' => 'item',
        '#title' => t('Block description'),
        '#markup' => $definition['admin_label'],
      ),
79 80 81 82 83 84 85 86 87 88 89 90 91
      'label' => array(
        '#type' => 'textfield',
        '#title' => 'Title',
        '#maxlength' => 255,
        '#default_value' => 'Custom Display Message',
        '#required' => TRUE,
      ),
      'label_display' => array(
        '#type' => 'checkbox',
        '#title' => 'Display title',
        '#default_value' => TRUE,
        '#return_value' => 'visible',
      ),
92
      'cache' => array(
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        '#type' => 'details',
        '#title' => t('Cache settings'),
        'max_age' => array(
          '#type' => 'select',
          '#title' => t('Maximum age'),
          '#description' => t('The maximum time this block may be cached.'),
          '#default_value' => 0,
          '#options' => $period,
        ),
        'contexts' => array(
          '#type' => 'checkboxes',
          '#title' => t('Vary by context'),
          '#description' => t('The contexts this cached block must be varied by.'),
          '#default_value' => array(),
          '#options' => $contexts,
          '#states' => array(
            'disabled' => array(
              ':input[name="settings[cache][max_age]"]' => array('value' => (string) 0),
            ),
          ),
        ),
114
      ),
115 116 117 118 119 120 121 122
      'display_message' => array(
        '#type' => 'textfield',
        '#title' => t('Display message'),
        '#default_value' => 'My custom display message.',
      ),
    );
    $form_state = array();
    // Ensure there are no form elements that do not belong to the plugin.
123 124 125 126
    $actual_form = $display_block->buildConfigurationForm(array(), $form_state);
    // Remove the visibility sections, as that just tests condition plugins.
    unset($actual_form['visibility'], $actual_form['visibility_tabs']);
    $this->assertIdentical($actual_form, $expected_form, 'Only the expected form elements were present.');
127 128

    $expected_build = array(
129
      '#children' => 'My custom display message.',
130 131 132
    );
    // Ensure the build array is proper.
    $this->assertIdentical($display_block->build(), $expected_build, 'The plugin returned the appropriate build array.');
133 134 135 136

    // Ensure the machine name suggestion is correct. In truth, this is actually
    // testing BlockBase's implementation, not the interface itself.
    $this->assertIdentical($display_block->getMachineNameSuggestion(), 'displaymessage', 'The plugin returned the expected machine name suggestion.');
137 138
  }
}