function BlockContentCreationTest::testBlockContentCreationMultipleViewModes

Same name and namespace in other branches
  1. 11.x core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php \Drupal\Tests\block_content\Functional\BlockContentCreationTest::testBlockContentCreationMultipleViewModes()
  2. 10 core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php \Drupal\Tests\block_content\Functional\BlockContentCreationTest::testBlockContentCreationMultipleViewModes()
  3. 9 core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php \Drupal\Tests\block_content\Functional\BlockContentCreationTest::testBlockContentCreationMultipleViewModes()
  4. 8.9.x 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 90

Class

BlockContentCreationTest
Create a block and test saving it.

Namespace

Drupal\Tests\block_content\Functional

Code

public function testBlockContentCreationMultipleViewModes() : void {
  // 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->submitForm($edit, 'Save');
  $this->assertSession()
    ->pageTextContains('Saved the ' . $edit['label'] . ' view mode.');
  $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 and configure');
  // Save our block permanently.
  $this->submitForm([
    'region' => 'content',
  ], 'Save block');
  // Set test_view_mode as a custom display to be available on the list.
  $storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
  $display = $storage->create([
    'targetEntityType' => 'block_content',
    'bundle' => 'basic',
    'mode' => 'test_view_mode',
    'status' => TRUE,
  ]);
  $display->save();
  // Go to the configure page and change the view mode.
  $this->drupalGet('admin/structure/block/manage/stark_testblock');
  // Test the available view mode options.
  // Verify that the default view mode is available.
  $this->assertSession()
    ->optionExists('edit-settings-view-mode', 'default');
  // Verify that the test view mode is available.
  $this->assertSession()
    ->optionExists('edit-settings-view-mode', 'test_view_mode');
  $view_mode['settings[view_mode]'] = 'test_view_mode';
  $this->submitForm($view_mode, 'Save block');
  // Check that the view mode setting is shown because more than one exists.
  $this->drupalGet('admin/structure/block/manage/stark_testblock');
  $this->assertSession()
    ->fieldExists('settings[view_mode]');
  // Change the view mode.
  $view_mode['region'] = 'content';
  $view_mode['settings[view_mode]'] = 'test_view_mode';
  $this->submitForm($view_mode, 'Save block');
  // Go to the configure page and verify the view mode has changed.
  $this->drupalGet('admin/structure/block/manage/stark_testblock');
  $this->assertSession()
    ->fieldValueEquals('settings[view_mode]', 'test_view_mode');
  // Check that the block exists in the database.
  $block = $this->getBlockByLabel($edit['info[0][value]']);
  $this->assertNotEmpty($block, 'Content Block found in database.');
}

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