function BlockTest::testBlock

Test configuring and moving a module-define block to specific regions.

File

drupal/core/modules/block/lib/Drupal/block/Tests/BlockTest.php, line 101
Contains \Drupal\block\Tests\BlockTest.

Class

BlockTest
Provides testing for basic block module functionality.

Namespace

Drupal\block\Tests

Code

function testBlock() {

  // Select the 'Powered by Drupal' block to be configured and moved.
  $block = array();
  $block['id'] = 'system_powered_by_block';
  $block['settings[label]'] = $this
    ->randomName(8);
  $block['machine_name'] = strtolower($this
    ->randomName(8));
  $block['theme'] = config('system.theme')
    ->get('default');
  $block['region'] = 'header';

  // Set block title to confirm that interface works and override any custom titles.
  $this
    ->drupalPost('admin/structure/block/add/' . $block['id'] . '/' . $block['theme'], array(
    'settings[label]' => $block['settings[label]'],
    'machine_name' => $block['machine_name'],
    'region' => $block['region'],
  ), t('Save block'));
  $this
    ->assertText(t('The block configuration has been saved.'), 'Block title set.');

  // Check to see if the block was created by checking its configuration.
  $instance = entity_load('block', $block['theme'] . '.' . $block['machine_name']);
  $this
    ->assertEqual($instance
    ->label(), $block['settings[label]'], 'Stored block title found.');

  // Check whether the block can be moved to all available regions.
  foreach ($this->regions as $region) {
    $this
      ->moveBlockToRegion($block, $region);
  }

  // Set the block to the disabled region.
  $edit = array();
  $edit['blocks[' . $block['theme'] . '.' . $block['machine_name'] . '][region]'] = -1;
  $this
    ->drupalPost('admin/structure/block', $edit, t('Save blocks'));

  // Confirm that the block is now listed as disabled.
  $this
    ->assertText(t('The block settings have been updated.'), 'Block successfully move to disabled region.');

  // Confirm that the block instance title and markup are not displayed.
  $this
    ->drupalGet('node');
  $this
    ->assertNoText(t($block['settings[label]']));

  // Check for <div id="block-my-block-instance-name"> if the machine name
  // is my_block_instance_name.
  $xpath = $this
    ->buildXPathQuery('//div[@id=:id]/*', array(
    ':id' => 'block-' . strtr(strtolower($block['machine_name']), '-', '_'),
  ));
  $this
    ->assertNoFieldByXPath($xpath, FALSE, 'Block found in no regions.');
}