You are here

public function BlockContentCreationTest::testBlockContentCreationMultipleViewModes in Zircon Profile 8.0

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

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

File

core/modules/block_content/src/Tests/BlockContentCreationTest.php, line 89
Contains \Drupal\block_content\Tests\BlockContentCreationTest.

Class

BlockContentCreationTest
Create a block and test saving it.

Namespace

Drupal\block_content\Tests

Code

public function testBlockContentCreationMultipleViewModes() {

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

  // Create a block.
  $edit = array();
  $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(format_string('@block %name has been created.', array(
    '@block' => 'basic',
    '%name' => $edit['info[0][value]'],
  )), 'Basic block created.');

  // Save our block permanently
  $this
    ->drupalPostForm(NULL, NULL, 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 = array(
    '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['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"]/@value', 'test_view_mode', 'View mode changed to Test View Mode');

  // Check that the block exists in the database.
  $blocks = entity_load_multiple_by_properties('block_content', array(
    'info' => $edit['info[0][value]'],
  ));
  $block = reset($blocks);
  $this
    ->assertTrue($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(format_string('A custom block with block description %value already exists.', array(
    '%value' => $edit['info[0][value]'],
  )));
  $this
    ->assertResponse(200);
}