You are here

public function BlockContentTypeTest::testBlockContentTypeCreation in Drupal 8

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

Class

BlockContentTypeTest
Ensures that custom block type functions work correctly.

Namespace

Drupal\Tests\block_content\Functional

Code

public function testBlockContentTypeCreation() {

  // 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
    ->assertText('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
    ->drupalPostForm(NULL, $edit, t('Save'));
  $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
    ->assertEqual($block_type
    ->language()
    ->getId(), $default_langcode);

  // Create block types programmatically.
  $this
    ->createBlockContentType('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('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);
}