BlockUiTest.php 5.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php

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

namespace Drupal\block\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Tests the block configuration UI.
 */
class BlockUiTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
22
  public static $modules = array('block', 'block_test');
23 24 25 26 27 28 29 30 31 32 33

  protected $regions;

  /**
   * An administrative user to configure the test environment.
   */
  protected $adminUser;

  public static function getInfo() {
    return array(
      'name' => 'Block UI',
34
      'description' => 'Checks that the block configuration UI exists and stores data correctly.',
35 36 37 38 39 40 41 42 43 44 45 46
      'group' => 'Block',
    );
  }

  function setUp() {
    parent::setUp();
    // Create and log in an administrative user.
    $this->adminUser = $this->drupalCreateUser(array(
      'administer blocks',
      'access administration pages',
    ));
    $this->drupalLogin($this->adminUser);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    // Enable some test blocks.
    $this->testBlocks = array(
      array(
        'label' => 'Tools',
        'tr' => '5',
        'plugin_id' => 'system_menu_block:menu-tools',
        'settings' => array('region' => 'sidebar_second', 'machine_name' => 'tools'),
        'test_weight' => '-1',
      ),
      array(
        'label' => 'Powered by Drupal',
        'tr' => '12',
        'plugin_id' => 'system_powered_by_block',
        'settings' => array('region' => 'footer', 'machine_name' => 'powered'),
        'test_weight' => '0',
      ),
    );
    foreach ($this->testBlocks as $values) {
      $this->drupalPlaceBlock($values['plugin_id'], $values['settings']);
    }
68 69 70
  }

  /**
71
   * Test block admin page exists and functions correctly.
72
   */
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  function testBlockAdminUiPage() {
    // Visit the blocks admin ui.
    $this->drupalGet('admin/structure/block');
    // Look for the blocks table.
    $blocks_table = $this->xpath("//table[@id='blocks']");
    $this->assertTrue(!empty($blocks_table), 'The blocks table is being rendered.');
    // Look for test blocks in the table.
    foreach ($this->testBlocks as $values) {
      $element = $this->xpath('//*[@id="blocks"]/tbody/tr[' . $values['tr'] . ']/td[1]/text()');
      $this->assertTrue((string)$element[0] == $values['label'], 'The "' . $values['label'] . '" block title is set inside the ' . $values['settings']['region'] . ' region.');
      // Look for a test block region select form element.
      $this->assertField('blocks[stark.' . $values['settings']['machine_name'] . '][region]', 'The block "' . $values['label'] . '" has a region assignment field.');
      // Move the test block to the header region.
      $edit['blocks[stark.' . $values['settings']['machine_name'] . '][region]'] = 'header';
      // Look for a test block weight select form element.
      $this->assertField('blocks[stark.' . $values['settings']['machine_name'] . '][weight]', 'The block "' . $values['label'] . '" has a weight assignment field.');
      // Change the test block's weight.
      $edit['blocks[stark.' . $values['settings']['machine_name'] . '][weight]'] = $values['test_weight'];
    }
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
    foreach ($this->testBlocks as $values) {
      // Check if the region and weight settings changes have persisted.
      $this->assertOptionSelected(
        'edit-blocks-stark' . $values['settings']['machine_name']  . '-region',
        'header',
        'The block "' . $values['label'] . '" has the correct region assignment (header).'
      );
      $this->assertOptionSelected(
        'edit-blocks-stark' . $values['settings']['machine_name']  . '-weight',
        $values['test_weight'],
        'The block "' . $values['label'] . '" has the correct weight assignment (' . $values['test_weight'] . ').'
      );
    }
106
  }
107 108 109 110 111 112

  /**
   * Test block search.
   */
  function testBlockSearch() {
    $block = t('Administration');
113
    $blocks = drupal_json_decode($this->drupalGet('block/autocomplete', array('query' => array('q' => $block))));
114 115
    $this->assertEqual($blocks['system_menu_block:menu-admin'], $block, t('Can search for block with name !block.', array('!block' => $block)));
  }
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

  /**
   * Tests that the BlockFormController populates machine name correctly.
   */
  public function testMachineNameSuggestion() {
    $url = 'admin/structure/block/add/test_block_instantiation/stark';
    $this->drupalGet($url);
    $this->assertFieldByName('machine_name', 'displaymessage', 'Block form uses raw machine name suggestion when no instance already exists.');
    $this->drupalPost($url, array(), 'Save block');

    // Now, check to make sure the form starts by autoincrementing correctly.
    $this->drupalGet($url);
    $this->assertFieldByName('machine_name', 'displaymessage_2', 'Block form appends _2 to plugin-suggested machine name when an instance already exists.');
    $this->drupalPost($url, array(), 'Save block');

    // And verify that it continues working beyond just the first two.
    $this->drupalGet($url);
    $this->assertFieldByName('machine_name', 'displaymessage_3', 'Block form appends _3 to plugin-suggested machine name when two instances already exist.');
  }

136
}