You are here

public function DisplayBlockTest::testViewsBlockForm in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/block/src/Tests/Views/DisplayBlockTest.php \Drupal\block\Tests\Views\DisplayBlockTest::testViewsBlockForm()

Test the block form for a Views block.

File

core/modules/block/src/Tests/Views/DisplayBlockTest.php, line 184
Contains \Drupal\block\Tests\Views\DisplayBlockTest.

Class

DisplayBlockTest
Tests the block display plugin.

Namespace

Drupal\block\Tests\Views

Code

public function testViewsBlockForm() {
  $this
    ->drupalLogin($this
    ->drupalCreateUser(array(
    'administer blocks',
  )));
  $default_theme = $this
    ->config('system.theme')
    ->get('default');
  $this
    ->drupalGet('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme);
  $elements = $this
    ->xpath('//input[@name="label"]');
  $this
    ->assertTrue(empty($elements), 'The label field is not found for Views blocks.');

  // Test that that machine name field is hidden from display and has been
  // saved as expected from the default value.
  $this
    ->assertNoFieldById('edit-machine-name', 'views_block__test_view_block_1', 'The machine name is hidden on the views block form.');

  // Save the block.
  $this
    ->drupalPostForm(NULL, array(), t('Save block'));
  $storage = $this->container
    ->get('entity.manager')
    ->getStorage('block');
  $block = $storage
    ->load('views_block__test_view_block_block_1');

  // This will only return a result if our new block has been created with the
  // expected machine name.
  $this
    ->assertTrue(!empty($block), 'The expected block was loaded.');
  for ($i = 2; $i <= 3; $i++) {

    // Place the same block again and make sure we have a new ID.
    $this
      ->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, array(), t('Save block'));
    $block = $storage
      ->load('views_block__test_view_block_block_1_' . $i);

    // This will only return a result if our new block has been created with the
    // expected machine name.
    $this
      ->assertTrue(!empty($block), 'The expected block was loaded.');
  }

  // Tests the override capability of items per page.
  $this
    ->drupalGet('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme);
  $edit = array();
  $edit['settings[override][items_per_page]'] = 10;
  $this
    ->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, $edit, t('Save block'));
  $block = $storage
    ->load('views_block__test_view_block_block_1_4');
  $config = $block
    ->getPlugin()
    ->getConfiguration();
  $this
    ->assertEqual(10, $config['items_per_page'], "'Items per page' is properly saved.");
  $edit['settings[override][items_per_page]'] = 5;
  $this
    ->drupalPostForm('admin/structure/block/manage/views_block__test_view_block_block_1_4', $edit, t('Save block'));
  $block = $storage
    ->load('views_block__test_view_block_block_1_4');
  $config = $block
    ->getPlugin()
    ->getConfiguration();
  $this
    ->assertEqual(5, $config['items_per_page'], "'Items per page' is properly saved.");

  // Tests the override of the label capability.
  $edit = array();
  $edit['settings[views_label_checkbox]'] = 1;
  $edit['settings[views_label]'] = 'Custom title';
  $this
    ->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, $edit, t('Save block'));
  $block = $storage
    ->load('views_block__test_view_block_block_1_5');
  $config = $block
    ->getPlugin()
    ->getConfiguration();
  $this
    ->assertEqual('Custom title', $config['views_label'], "'Label' is properly saved.");
}