You are here

public function BlockContentCreationTest::testBlockContentCreation in Drupal 9

Same name and namespace in other branches
  1. 8 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 51

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
    ->drupalGet('block/add/basic');
  $this
    ->submitForm($edit, 'Save');

  // Check that the Basic block has been created.
  $this
    ->assertSession()
    ->pageTextContains('basic ' . $edit['info[0][value]'] . ' has been created.');

  // Check that the view mode setting is hidden because only one exists.
  $this
    ->assertSession()
    ->fieldNotExists('settings[view_mode]');

  // 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
    ->drupalGet('block/add/basic');
  $this
    ->submitForm($edit, 'Save');

  // Check that the Basic block has been created.
  $this
    ->assertSession()
    ->pageTextContains('A custom block with block description ' . $edit['info[0][value]'] . ' already exists.');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
}