function BlockUiTest::testBlockAdminUiPage

Same name and namespace in other branches
  1. 9 core/modules/block/tests/src/Functional/BlockUiTest.php \Drupal\Tests\block\Functional\BlockUiTest::testBlockAdminUiPage()
  2. 8.9.x core/modules/block/tests/src/Functional/BlockUiTest.php \Drupal\Tests\block\Functional\BlockUiTest::testBlockAdminUiPage()
  3. 10 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 132

Class

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

Namespace

Drupal\Tests\block\Functional

Code

public function testBlockAdminUiPage() : void {
    // Visit the blocks admin ui.
    $this->drupalGet('admin/structure/block');
    // Look for the blocks table.
    $this->assertSession()
        ->elementExists('xpath', "//table[@id='blocks']");
    // Look for test blocks in the table.
    foreach ($this->blockValues as $delta => $values) {
        $block = $this->blocks[$delta];
        $label = $block->label();
        $this->assertSession()
            ->elementTextEquals('xpath', '//*[@id="blocks"]/tbody/tr[' . $values['tr'] . ']/td[1]/text()', $label);
        // 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');
    $this->assertSession()
        ->elementExists('xpath', '//tr[contains(@class, "region-title-header")]');
    // 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('stark'), 'The stark base theme is enabled');
    $this->drupalPlaceBlock('local_tasks_block', [
        'region' => 'header',
        'theme' => 'stark',
    ]);
    // We have to enable at least one extra theme that is not hidden so that
    // local tasks will show up. That's why we enable test_theme_theme.
    \Drupal::service('theme_installer')->install([
        'stable9',
        'test_theme_theme',
    ]);
    $this->drupalGet('admin/structure/block');
    $theme_handler = \Drupal::service('theme_handler');
    $this->assertSession()
        ->linkExists($theme_handler->getName('stark'));
    $this->assertSession()
        ->linkExists($theme_handler->getName('test_theme_theme'));
    $this->assertSession()
        ->linkNotExists($theme_handler->getName('stable9'));
    // Ensure that a hidden theme cannot use the block demo page.
    $this->drupalGet('admin/structure/block/list/stable9');
    $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', 'stable9')
        ->save();
    \Drupal::service('router.builder')->rebuildIfNeeded();
    $this->drupalPlaceBlock('local_tasks_block', [
        'region' => 'header',
        'theme' => 'stable9',
    ]);
    $this->drupalGet('admin/structure/block');
    $this->assertSession()
        ->linkExists($theme_handler->getName('stable9'));
    $this->drupalGet('admin/structure/block/list/stable9');
    $this->assertSession()
        ->statusCodeEquals(200);
}

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