function BlockUiTest::testBlockAdminUiPage

Same name in other branches
  1. 9 core/modules/block/tests/src/Functional/BlockUiTest.php \Drupal\Tests\block\Functional\BlockUiTest::testBlockAdminUiPage()
  2. 10 core/modules/block/tests/src/Functional/BlockUiTest.php \Drupal\Tests\block\Functional\BlockUiTest::testBlockAdminUiPage()
  3. 11.x core/modules/block/tests/src/Functional/BlockUiTest.php \Drupal\Tests\block\Functional\BlockUiTest::testBlockAdminUiPage()

Test block admin page exists and functions correctly.

File

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

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->assertField('blocks[' . $values['settings']['id'] . '][region]', 'The block "' . $values['label'] . '" has a region assignment field.');
        // 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->assertField('blocks[' . $values['settings']['id'] . '][weight]', 'The block "' . $values['label'] . '" has a weight assignment field.');
        // Change the test block's weight.
        $edit['blocks[' . $values['settings']['id'] . '][weight]'] = $values['test_weight'];
    }
    $this->drupalPostForm('admin/structure/block', $edit, t('Save blocks'));
    foreach ($this->blockValues as $values) {
        // Check if the region and weight settings changes have persisted.
        $this->assertOptionSelected('edit-blocks-' . $values['settings']['id'] . '-region', 'header', 'The block "' . $label . '" has the correct region assignment (header).');
        $this->assertOptionSelected('edit-blocks-' . $values['settings']['id'] . '-weight', $values['test_weight'], 'The block "' . $label . '" has the correct weight assignment (' . $values['test_weight'] . ').');
    }
    // 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);
}

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