You are here

public function BlockTest::testCustomBlock in MongoDB 7

Test creating custom block, moving it to a region and then deleting it.

File

mongodb_block_ui/src/Tests/BlockTest.php, line 57

Class

BlockTest
Class BlockTest.

Namespace

Drupal\mongodb_block_ui\Tests

Code

public function testCustomBlock() {

  // Confirm that the add block link appears on block overview pages.
  $this
    ->drupalGet('admin/structure/block');
  $this
    ->assertRaw(l(t('Add block'), 'admin/structure/block/add'), t('Add block link is present on block overview page for default theme.'));
  $this
    ->drupalGet('admin/structure/block/list/seven');
  $this
    ->assertRaw(l(t('Add block'), 'admin/structure/block/list/seven/add'), t('Add block link is present on block overview page for non-default theme.'));

  // Confirm that hidden regions are not shown as options for block placement
  // when adding a new block.
  theme_enable(array(
    'stark',
  ));
  $themes = list_themes();
  $this
    ->drupalGet('admin/structure/block/add');
  foreach ($themes as $key => $theme) {
    if ($theme->status) {
      foreach ($theme->info['regions_hidden'] as $hidden_region) {
        $elements = $this
          ->xpath('//select[@id=:id]//option[@value=:value]', array(
          ':id' => 'edit-regions-' . $key,
          ':value' => $hidden_region,
        ));
        $this
          ->assertFalse(isset($elements[0]), t('The hidden region @region is not available for @theme.', array(
          '@region' => $hidden_region,
          '@theme' => $key,
        )));
      }
    }
  }

  // Add a new custom block by filling out the input form on the
  // admin/structure/block/add page.
  $custom_block = array();
  $custom_block['info'] = $this
    ->randomName(8);
  $custom_block['title'] = $this
    ->randomName(8);
  $custom_block['body[value]'] = $this
    ->randomName(32);
  $this
    ->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));

  // Confirm that the custom block has been created, and then query the
  // created bid.
  $this
    ->assertText(t('The block has been created.'), t('Custom block successfully created.'));
  $bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(
    ':info' => $custom_block['info'],
  ))
    ->fetchField();

  // Check to see if the custom block was created by checking that it's in
  // the database..
  $this
    ->assertNotNull($bid, t('Custom block found in database'));

  // Check if the block can be moved to all available regions.
  $custom_block['module'] = 'block';
  $custom_block['delta'] = $bid;
  foreach ($this->regions as $region) {
    $this
      ->moveBlockToRegion($custom_block, $region);
  }

  // Verify presence of configure and delete links for custom block.
  $this
    ->drupalGet('admin/structure/block');
  $this
    ->assertRaw(l(t('configure'), 'admin/structure/block/manage/block/' . $bid . '/configure'), t('Custom block configure link found.'));
  $this
    ->assertRaw(l(t('delete'), 'admin/structure/block/manage/block/' . $bid . '/delete'), t('Custom block delete link found.'));

  // Set visibility only for authenticated users, to verify delete
  // functionality.
  $edit = array();
  $edit['roles[2]'] = TRUE;
  $this
    ->drupalPost('admin/structure/block/manage/block/' . $bid . '/configure', $edit, t('Save block'));

  // Delete the created custom block & verify that it's been deleted and no
  // longer appearing on the page.
  $this
    ->clickLink(t('delete'));
  $this
    ->drupalPost('admin/structure/block/manage/block/' . $bid . '/delete', array(), t('Delete'));
  $this
    ->assertRaw(t('The block %title has been removed.', array(
    '%title' => $custom_block['info'],
  )), t('Custom block successfully deleted.'));
  $this
    ->assertNoText(t($custom_block['title']), t('Custom block no longer appears on page.'));
  $count = db_query("SELECT 1 FROM {block_role} WHERE module = :module AND delta = :delta", array(
    ':module' => $custom_block['module'],
    ':delta' => $custom_block['delta'],
  ))
    ->fetchField();
  $this
    ->assertFalse($count, t('Table block_role being cleaned.'));
}