function DisplayBlockTest::testViewsBlockForm

Same name and namespace in other branches
  1. 9 core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php \Drupal\Tests\block\Functional\Views\DisplayBlockTest::testViewsBlockForm()
  2. 8.9.x core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php \Drupal\Tests\block\Functional\Views\DisplayBlockTest::testViewsBlockForm()
  3. 11.x core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php \Drupal\Tests\block\Functional\Views\DisplayBlockTest::testViewsBlockForm()

Tests the block form for a Views block.

File

core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php, line 199

Class

DisplayBlockTest
Tests the block display plugin.

Namespace

Drupal\Tests\block\Functional\Views

Code

public function testViewsBlockForm() : void {
  $this->drupalLogin($this->drupalCreateUser([
    '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);
  $this->assertSession()
    ->fieldNotExists('label');
  // Test that the machine name field is hidden from display and has been
  // saved as expected from the default value.
  $this->assertSession()
    ->fieldNotExists('edit-machine-name', NULL);
  // Save the block.
  $edit = [
    'region' => 'content',
  ];
  $this->submitForm($edit, 'Save block');
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('block');
  $block = $storage->load($default_theme . '_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->assertNotEmpty($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->drupalGet('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme);
    $this->submitForm($edit, 'Save block');
    $block = $storage->load($default_theme . '_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->assertNotEmpty($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 = [
    'region' => 'content',
  ];
  $edit['settings[override][items_per_page]'] = 10;
  $this->drupalGet('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme);
  $this->submitForm($edit, 'Save block');
  $block = $storage->load($default_theme . '_views_block__test_view_block_block_1_4');
  $config = $block->getPlugin()
    ->getConfiguration();
  $this->assertEquals(10, $config['items_per_page'], "'Items per page' is properly saved.");
  $edit['settings[override][items_per_page]'] = 5;
  $this->drupalGet('admin/structure/block/manage/' . $default_theme . '_views_block__test_view_block_block_1_4');
  $this->submitForm($edit, 'Save block');
  $block = $storage->load($default_theme . '_views_block__test_view_block_block_1_4');
  $config = $block->getPlugin()
    ->getConfiguration();
  $this->assertEquals(5, $config['items_per_page'], "'Items per page' is properly saved.");
  // Tests the override of the label capability.
  $edit = [
    'region' => 'content',
  ];
  $edit['settings[views_label_checkbox]'] = 1;
  $edit['settings[views_label]'] = 'Custom title';
  $this->drupalGet('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme);
  $this->submitForm($edit, 'Save block');
  $block = $storage->load($default_theme . '_views_block__test_view_block_block_1_5');
  $config = $block->getPlugin()
    ->getConfiguration();
  $this->assertEquals('Custom title', $config['views_label'], "'Label' is properly saved.");
}

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