function BlockContentCreationTest::testBlockContentCreation

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

Creates a "Basic page" block and verifies its consistency in the database.

File

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

Class

BlockContentCreationTest
Create a block and test saving it.

Namespace

Drupal\Tests\block_content\Functional

Code

public function testBlockContentCreation() {
    $this->drupalLogin($this->adminUser);
    // Create a block.
    $edit = [];
    $edit['info[0][value]'] = 'Test Block';
    $edit['body[0][value]'] = $this->randomMachineName(16);
    $this->drupalPostForm('block/add/basic', $edit, t('Save'));
    // Check that the Basic block has been created.
    $this->assertRaw(new FormattableMarkup('@block %name has been created.', [
        '@block' => 'basic',
        '%name' => $edit['info[0][value]'],
    ]), 'Basic block created.');
    // Check that the view mode setting is hidden because only one exists.
    $this->assertNoFieldByXPath('//select[@name="settings[view_mode]"]', NULL, 'View mode setting hidden because only one exists');
    // Check that the block exists in the database.
    $blocks = \Drupal::entityTypeManager()->getStorage('block_content')
        ->loadByProperties([
        'info' => $edit['info[0][value]'],
    ]);
    $block = reset($blocks);
    $this->assertNotEmpty($block, 'Custom Block found in database.');
    // Check that attempting to create another block with the same value for
    // 'info' returns an error.
    $this->drupalPostForm('block/add/basic', $edit, t('Save'));
    // Check that the Basic block has been created.
    $this->assertRaw(new FormattableMarkup('A custom block with block description %value already exists.', [
        '%value' => $edit['info[0][value]'],
    ]));
    $this->assertSession()
        ->statusCodeEquals(200);
}

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