You are here

public function BlockUiTest::testBlockAdminUiPage in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/block/tests/src/Functional/BlockUiTest.php \Drupal\Tests\block\Functional\BlockUiTest::testBlockAdminUiPage()

Tests block admin page exists and functions correctly.

File

core/modules/block/tests/src/Functional/BlockUiTest.php, line 112

Class

BlockUiTest
Tests that the block configuration UI exists and stores data correctly.

Namespace

Drupal\Tests\block\Functional

Code

public 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->blockValues as $delta => $values) {
    $block = $this->blocks[$delta];
    $label = $block
      ->label();
    $element = $this
      ->xpath('//*[@id="blocks"]/tbody/tr[' . $values['tr'] . ']/td[1]/text()');
    $this
      ->assertEquals($element[0]
      ->getText(), $label, 'The "' . $label . '" block title is set inside the ' . $values['settings']['region'] . ' region.');

    // Look for a test block region select form element.
    $this
      ->assertSession()
      ->fieldExists('blocks[' . $values['settings']['id'] . '][region]');

    // Move the test block to the header region.
    $edit['blocks[' . $values['settings']['id'] . '][region]'] = 'header';

    // Look for a test block weight select form element.
    $this
      ->assertSession()
      ->fieldExists('blocks[' . $values['settings']['id'] . '][weight]');

    // Change the test block's weight.
    $edit['blocks[' . $values['settings']['id'] . '][weight]'] = $values['test_weight'];
  }
  $this
    ->drupalGet('admin/structure/block');
  $this
    ->submitForm($edit, 'Save blocks');
  foreach ($this->blockValues as $values) {

    // Check if the region and weight settings changes have persisted.
    $this
      ->assertTrue($this
      ->assertSession()
      ->optionExists('edit-blocks-' . $values['settings']['id'] . '-region', 'header')
      ->isSelected());
    $this
      ->assertTrue($this
      ->assertSession()
      ->optionExists('edit-blocks-' . $values['settings']['id'] . '-weight', $values['test_weight'])
      ->isSelected());
  }

  // Add a block with a machine name the same as a region name.
  $this
    ->drupalPlaceBlock('system_powered_by_block', [
    'region' => 'header',
    'id' => 'header',
  ]);
  $this
    ->drupalGet('admin/structure/block');
  $element = $this
    ->xpath('//tr[contains(@class, :class)]', [
    ':class' => 'region-title-header',
  ]);
  $this
    ->assertTrue(!empty($element));

  // Ensure hidden themes do not appear in the UI. Enable another non base
  // theme and place the local tasks block.
  $this
    ->assertTrue(\Drupal::service('theme_handler')
    ->themeExists('classy'), 'The classy base theme is enabled');
  $this
    ->drupalPlaceBlock('local_tasks_block', [
    'region' => 'header',
  ]);
  \Drupal::service('theme_installer')
    ->install([
    'stable',
    'stark',
  ]);
  $this
    ->drupalGet('admin/structure/block');
  $theme_handler = \Drupal::service('theme_handler');
  $this
    ->assertSession()
    ->linkExists($theme_handler
    ->getName('classy'));
  $this
    ->assertSession()
    ->linkExists($theme_handler
    ->getName('stark'));
  $this
    ->assertSession()
    ->linkNotExists($theme_handler
    ->getName('stable'));

  // Ensure that a hidden theme cannot use the block demo page.
  $this
    ->drupalGet('admin/structure/block/list/stable');
  $this
    ->assertSession()
    ->statusCodeEquals(404);

  // Ensure that a hidden theme set as the admin theme can use the block demo
  // page.
  \Drupal::configFactory()
    ->getEditable('system.theme')
    ->set('admin', 'stable')
    ->save();
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
  $this
    ->drupalPlaceBlock('local_tasks_block', [
    'region' => 'header',
    'theme' => 'stable',
  ]);
  $this
    ->drupalGet('admin/structure/block');
  $this
    ->assertSession()
    ->linkExists($theme_handler
    ->getName('stable'));
  $this
    ->drupalGet('admin/structure/block/list/stable');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
}