function BlockTest::testBlock

Same name in other branches
  1. 9 core/modules/block/tests/src/Functional/BlockTest.php \Drupal\Tests\block\Functional\BlockTest::testBlock()
  2. 10 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()

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

File

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

Class

BlockTest
Tests basic block functionality.

Namespace

Drupal\Tests\block\Functional

Code

public function testBlock() {
    // 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->drupalPostForm('admin/structure/block/add/' . $block['id'] . '/' . $block['theme'], [
        'settings[label]' => $block['settings[label]'],
        'settings[label_display]' => $block['settings[label_display]'],
        'id' => $block['id'],
        'region' => $block['region'],
    ], t('Save block'));
    $this->assertText(t('The block configuration has been saved.'), 'Block title set.');
    // Check to see if the block was created by checking its configuration.
    $instance = Block::load($block['id']);
    $this->assertEqual($instance->label(), $block['settings[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->assertText(t('The block settings have been updated.'), 'Block successfully moved to disabled region.');
    // Confirm that the block instance title and markup are not displayed.
    $this->drupalGet('node');
    $this->assertNoText(t($block['settings[label]']));
    // Check for <div id="block-my-block-instance-name"> if the machine name
    // is my_block_instance_name.
    $xpath = $this->buildXPathQuery('//div[@id=:id]/*', [
        ':id' => 'block-' . str_replace('_', '-', strtolower($block['id'])),
    ]);
    $this->assertNoFieldByXPath($xpath, FALSE, 'Block found in no regions.');
    // Test deleting the block from the edit form.
    $this->drupalGet('admin/structure/block/manage/' . $block['id']);
    $this->clickLink(t('Remove block'));
    $this->assertRaw(t('Are you sure you want to remove the block @name?', [
        '@name' => $block['settings[label]'],
    ]));
    $this->drupalPostForm(NULL, [], t('Remove'));
    $this->assertRaw(t('The block %name has been removed.', [
        '%name' => $block['settings[label]'],
    ]));
    // Test deleting a block via "Configure block" link.
    $block = $this->drupalPlaceBlock('system_powered_by_block');
    $this->drupalGet('admin/structure/block/manage/' . $block->id(), [
        'query' => [
            'destination' => 'admin',
        ],
    ]);
    $this->clickLink(t('Remove block'));
    $this->assertRaw(t('Are you sure you want to remove the block @name?', [
        '@name' => $block->label(),
    ]));
    $this->drupalPostForm(NULL, [], t('Remove'));
    $this->assertRaw(t('The block %name has been removed.', [
        '%name' => $block->label(),
    ]));
    $this->assertUrl('admin');
    $this->assertNoRaw($block->id());
}

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