You are here

public function BlockContentCreationTest::testBlockContentCreationMultipleViewModes in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php \Drupal\Tests\block_content\Functional\BlockContentCreationTest::testBlockContentCreationMultipleViewModes()

Creates a "Basic page" block with multiple view modes.

File

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

Class

BlockContentCreationTest
Create a block and test saving it.

Namespace

Drupal\Tests\block_content\Functional

Code

public function testBlockContentCreationMultipleViewModes() {

  // Add a new view mode and verify if it is selected as expected.
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'administer display modes',
  ]));
  $this
    ->drupalGet('admin/structure/display-modes/view/add/block_content');
  $edit = [
    'id' => 'test_view_mode',
    'label' => 'Test View Mode',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertRaw(t('Saved the %label view mode.', [
    '%label' => $edit['label'],
  ]));
  $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.');

  // Save our block permanently
  $this
    ->drupalPostForm(NULL, [
    'region' => 'content',
  ], t('Save block'));

  // Set test_view_mode as a custom display to be available on the list.
  $this
    ->drupalGet('admin/structure/block/block-content');
  $this
    ->drupalGet('admin/structure/block/block-content/types');
  $this
    ->clickLink(t('Manage display'));
  $this
    ->drupalGet('admin/structure/block/block-content/manage/basic/display');
  $custom_view_mode = [
    'display_modes_custom[test_view_mode]' => 1,
  ];
  $this
    ->drupalPostForm(NULL, $custom_view_mode, t('Save'));

  // Go to the configure page and change the view mode.
  $this
    ->drupalGet('admin/structure/block/manage/testblock');

  // Test the available view mode options.
  $this
    ->assertOption('edit-settings-view-mode', 'default', 'The default view mode is available.');
  $this
    ->assertOption('edit-settings-view-mode', 'test_view_mode', 'The test view mode is available.');
  $view_mode['settings[view_mode]'] = 'test_view_mode';
  $this
    ->drupalPostForm(NULL, $view_mode, t('Save block'));

  // Check that the view mode setting is shown because more than one exists.
  $this
    ->drupalGet('admin/structure/block/manage/testblock');
  $this
    ->assertFieldByXPath('//select[@name="settings[view_mode]"]', NULL, 'View mode setting shown because multiple exist');

  // Change the view mode.
  $view_mode['region'] = 'content';
  $view_mode['settings[view_mode]'] = 'test_view_mode';
  $this
    ->drupalPostForm(NULL, $view_mode, t('Save block'));

  // Go to the configure page and verify the view mode has changed.
  $this
    ->drupalGet('admin/structure/block/manage/testblock');
  $this
    ->assertFieldByXPath('//select[@name="settings[view_mode]"]/option[@selected="selected"]', 'test_view_mode', 'View mode changed to Test 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
    ->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);
}