function BlockContentTypeTest::testBlockContentTypeCreation

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

Tests creating a block type programmatically and via a form.

File

core/modules/block_content/tests/src/Functional/BlockContentTypeTest.php, line 74

Class

BlockContentTypeTest
Ensures that block type functions work correctly.

Namespace

Drupal\Tests\block_content\Functional

Code

public function testBlockContentTypeCreation() : void {
    // Log in a test user.
    $this->drupalLogin($this->adminUser);
    // Test the page with no block-types.
    $this->drupalGet('block/add');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains('You have not created any block types yet');
    $this->clickLink('block type creation page');
    // Create a block type via the user interface.
    $edit = [
        'id' => 'foo',
        'label' => 'title for foo',
    ];
    $this->submitForm($edit, 'Save and manage fields');
    // Asserts that form submit redirects to the expected manage fields page.
    $this->assertSession()
        ->addressEquals('admin/structure/block-content/manage/' . $edit['id'] . '/fields');
    $block_type = BlockContentType::load('foo');
    $this->assertInstanceOf(BlockContentType::class, $block_type);
    $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('block_content', 'foo');
    $this->assertTrue(isset($field_definitions['body']), 'Body field created when using the UI to create block content types.');
    // Check that the block type was created in site default language.
    $default_langcode = \Drupal::languageManager()->getDefaultLanguage()
        ->getId();
    $this->assertEquals($block_type->language()
        ->getId(), $default_langcode);
    // Create block types programmatically.
    $this->createBlockContentType([
        'id' => 'basic',
    ], TRUE);
    $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('block_content', 'basic');
    $this->assertTrue(isset($field_definitions['body']), "Body field for 'basic' block type created when using the testing API to create block content types.");
    $this->createBlockContentType([
        'id' => 'other',
    ]);
    $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('block_content', 'other');
    $this->assertFalse(isset($field_definitions['body']), "Body field for 'other' block type not created when using the testing API to create block content types.");
    $block_type = BlockContentType::load('other');
    $this->assertInstanceOf(BlockContentType::class, $block_type);
    $this->drupalGet('block/add/' . $block_type->id());
    $this->assertSession()
        ->statusCodeEquals(200);
}

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