function BlockTest::testBlock

Same name and namespace in other branches
  1. 9 core/modules/block/tests/src/Functional/BlockTest.php \Drupal\Tests\block\Functional\BlockTest::testBlock()
  2. 8.9.x core/modules/block/tests/src/Functional/BlockTest.php \Drupal\Tests\block\Functional\BlockTest::testBlock()
  3. 11.x core/modules/block/tests/src/Functional/BlockTest.php \Drupal\Tests\block\Functional\BlockTest::testBlock()

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

File

core/modules/block/tests/src/Functional/BlockTest.php, line 212

Class

BlockTest
Tests basic block functionality.

Namespace

Drupal\Tests\block\Functional

Code

public function testBlock() : void {
  // Place page title block to test error messages.
  $this->drupalPlaceBlock('page_title_block');
  // Disable the block.
  $this->drupalGet('admin/structure/block');
  $this->clickLink('Disable');
  // Select the 'Powered by Drupal' block to be configured and moved.
  $block = [];
  $block['id'] = 'system_powered_by_block';
  $block['settings[label]'] = $this->randomMachineName(8);
  $block['settings[label_display]'] = TRUE;
  $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->drupalGet('admin/structure/block/add/' . $block['id'] . '/' . $block['theme']);
  $this->submitForm([
    'settings[label]' => $block['settings[label]'],
    'settings[label_display]' => $block['settings[label_display]'],
    'id' => $block['id'],
    'region' => $block['region'],
  ], 'Save block');
  $this->assertSession()
    ->statusMessageContains('The block configuration has been saved.', 'status');
  // Check to see if the block was created by checking its configuration.
  $instance = Block::load($block['id']);
  $this->assertEquals($block['settings[label]'], $instance->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);
  }
  // Disable the block.
  $this->drupalGet('admin/structure/block');
  $this->clickLink('Disable');
  // Confirm that the block is now listed as disabled.
  $this->assertSession()
    ->statusMessageContains('The block settings have been updated.', 'status');
  // Confirm that the block instance title and markup are not displayed.
  $this->drupalGet('node');
  $this->assertSession()
    ->pageTextNotContains($block['settings[label]']);
  // Check for <div id="block-my-block-instance-name"> if the machine name
  // is my_block_instance_name.
  $xpath = $this->assertSession()
    ->buildXPathQuery('//div[@id=:id]/*', [
    ':id' => 'block-' . str_replace('_', '-', strtolower($block['id'])),
  ]);
  $this->assertSession()
    ->elementNotExists('xpath', $xpath);
  $pages = [
    '',
    '<front>',
    '/valid-page',
    'user/login',
  ];
  // Test error when not including forward slash.
  $this->drupalGet('admin/structure/block/manage/' . $block['id']);
  $this->submitForm([
    'visibility[request_path][pages]' => implode("\n", $pages),
  ], 'Save block');
  $this->assertSession()
    ->pageTextContains('The path user/login requires a leading forward slash when used with the Pages setting.');
  // Test deleting the block from the edit form.
  $this->drupalGet('admin/structure/block/manage/' . $block['id']);
  $this->clickLink('Remove block');
  $this->assertSession()
    ->pageTextContains('Are you sure you want to remove the block ' . $block['settings[label]'] . ' from the Footer region?');
  $this->submitForm([], 'Remove');
  $this->assertSession()
    ->statusMessageContains('The block ' . $block['settings[label]'] . ' has been removed from the Footer region.', 'status');
  // Test deleting a block via "Configure block" link.
  $block = $this->drupalPlaceBlock('system_powered_by_block', [
    'region' => 'left_sidebar',
  ]);
  $this->drupalGet('admin/structure/block/manage/' . $block->id(), [
    'query' => [
      'destination' => 'admin',
    ],
  ]);
  $this->clickLink('Remove block');
  $this->assertSession()
    ->pageTextContains('Are you sure you want to remove the block ' . $block->label() . ' from the Left sidebar region?');
  $this->submitForm([], 'Remove');
  $this->assertSession()
    ->statusMessageContains('The block ' . $block->label() . ' has been removed from the Left sidebar region.', 'status');
  $this->assertSession()
    ->addressEquals('admin');
  $this->assertSession()
    ->responseNotContains($block->id());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.