You are here

function BlockTest::testBlock in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/block/src/Tests/BlockTest.php \Drupal\block\Tests\BlockTest::testBlock()

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

File

core/modules/block/src/Tests/BlockTest.php, line 137
Contains \Drupal\block\Tests\BlockTest.

Class

BlockTest
Tests basic block functionality.

Namespace

Drupal\block\Tests

Code

function testBlock() {

  // Place page title block to test error messages.
  $this
    ->drupalPlaceBlock('page_title_block');

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

  // Set block title to confirm that interface works and override any custom titles.
  $this
    ->drupalPostForm('admin/structure/block/add/' . $block['id'] . '/' . $block['theme'], array(
    'settings[label]' => $block['settings[label]'],
    'id' => $block['id'],
    '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 = Block::load($block['id']);
  $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['id'] . '][region]'] = -1;
  $this
    ->drupalPostForm('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-' . str_replace('_', '-', strtolower($block['id'])),
  ));
  $this
    ->assertNoFieldByXPath($xpath, FALSE, 'Block found in no regions.');

  // Test deleting the block from the edit form.
  $this
    ->drupalGet('admin/structure/block/manage/' . $block['id']);
  $this
    ->clickLink(t('Delete'));
  $this
    ->assertRaw(t('Are you sure you want to delete the block %name?', array(
    '%name' => $block['settings[label]'],
  )));
  $this
    ->drupalPostForm(NULL, array(), t('Delete'));
  $this
    ->assertRaw(t('The block %name has been deleted.', array(
    '%name' => $block['settings[label]'],
  )));

  // Test deleting a block via "Configure block" link.
  $block = $this
    ->drupalPlaceBlock('system_powered_by_block');
  $this
    ->drupalGet('admin/structure/block/manage/' . $block
    ->id(), array(
    'query' => array(
      'destination' => 'admin',
    ),
  ));
  $this
    ->clickLink(t('Delete'));
  $this
    ->assertRaw(t('Are you sure you want to delete the block %name?', array(
    '%name' => $block
      ->label(),
  )));
  $this
    ->drupalPostForm(NULL, array(), t('Delete'));
  $this
    ->assertRaw(t('The block %name has been deleted.', array(
    '%name' => $block
      ->label(),
  )));
  $this
    ->assertUrl('admin');
  $this
    ->assertNoRaw($block
    ->id());
}