function BlockContentCreationTest::testBlockDelete

Same name in other branches
  1. 9 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()

Test deleting a block.

File

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

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->drupalPostForm('block/add/basic', $edit, t('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->drupalPostForm($url, $instance, t('Save block'));
    $block = BlockContent::load(1);
    // Test getInstances method.
    $this->assertCount(1, $block->getInstances());
    // Navigate to home page.
    $this->drupalGet('');
    $this->assertText($body);
    // Delete the block.
    $this->drupalGet('block/1/delete');
    $this->assertText(\Drupal::translation()->formatPlural(1, 'This will also remove 1 placed block instance.', 'This will also remove @count placed block instance.'));
    $this->drupalPostForm(NULL, [], 'Delete');
    $this->assertRaw(t('The custom block %name has been deleted.', [
        '%name' => $edit['info[0][value]'],
    ]));
    // 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->drupalPostForm('block/add/basic', $edit2, t('Save'));
    $this->assertNoRaw('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->drupalPostForm('block/add/basic', $edit3, t('Save'));
    // Show the delete confirm form.
    $this->drupalGet('block/3/delete');
    $this->assertNoText('This will also remove');
}

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