function BlockContentCreationTest::testBlockDelete

Same name in other branches
  1. 8.9.x core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php \Drupal\Tests\block_content\Functional\BlockContentCreationTest::testBlockDelete()
  2. 10 core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php \Drupal\Tests\block_content\Functional\BlockContentCreationTest::testBlockDelete()
  3. 11.x core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php \Drupal\Tests\block_content\Functional\BlockContentCreationTest::testBlockDelete()

Tests deleting a block.

File

core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php, line 217

Class

BlockContentCreationTest
Create a block and test saving it.

Namespace

Drupal\Tests\block_content\Functional

Code

public function testBlockDelete() {
    // Create a block.
    $edit = [];
    $edit['info[0][value]'] = $this->randomMachineName(8);
    $body = $this->randomMachineName(16);
    $edit['body[0][value]'] = $body;
    $this->drupalGet('block/add/basic');
    $this->submitForm($edit, 'Save');
    // Place the block.
    $instance = [
        'id' => mb_strtolower($edit['info[0][value]']),
        'settings[label]' => $edit['info[0][value]'],
        'region' => 'sidebar_first',
    ];
    $block = BlockContent::load(1);
    $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')
        ->get('default');
    $this->drupalGet($url);
    $this->submitForm($instance, 'Save block');
    $block = BlockContent::load(1);
    // Test getInstances method.
    $this->assertCount(1, $block->getInstances());
    // Navigate to home page.
    $this->drupalGet('');
    $this->assertSession()
        ->pageTextContains($body);
    // Delete the block.
    $this->drupalGet('block/1/delete');
    $this->assertSession()
        ->pageTextContains('This will also remove 1 placed block instance.');
    $this->submitForm([], 'Delete');
    $this->assertSession()
        ->pageTextContains('The custom block ' . $edit['info[0][value]'] . ' has been deleted.');
    // Create another block and force the plugin cache to flush.
    $edit2 = [];
    $edit2['info[0][value]'] = $this->randomMachineName(8);
    $body2 = $this->randomMachineName(16);
    $edit2['body[0][value]'] = $body2;
    $this->drupalGet('block/add/basic');
    $this->submitForm($edit2, 'Save');
    $this->assertSession()
        ->responseNotContains('Error message');
    // Create another block with no instances, and test we don't get a
    // confirmation message about deleting instances.
    $edit3 = [];
    $edit3['info[0][value]'] = $this->randomMachineName(8);
    $body = $this->randomMachineName(16);
    $edit3['body[0][value]'] = $body;
    $this->drupalGet('block/add/basic');
    $this->submitForm($edit3, 'Save');
    // Show the delete confirm form.
    $this->drupalGet('block/3/delete');
    $this->assertSession()
        ->pageTextNotContains('This will also remove');
}

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