function DisplayBlockTest::testViewsBlockForm

Same name in other branches
  1. 9 core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php \Drupal\Tests\block\Functional\Views\DisplayBlockTest::testViewsBlockForm()
  2. 10 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()

Test the block form for a Views block.

File

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

Class

DisplayBlockTest
Tests the block display plugin.

Namespace

Drupal\Tests\block\Functional\Views

Code

public function testViewsBlockForm() {
    $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);
    $elements = $this->xpath('//input[@name="label"]');
    $this->assertTrue(empty($elements), 'The label field is not found for Views blocks.');
    // Test that the 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.
    $edit = [
        'region' => 'content',
    ];
    $this->drupalPostForm(NULL, $edit, t('Save block'));
    $storage = $this->container
        ->get('entity_type.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, $edit, 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 = [
        'region' => 'content',
    ];
    $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 = [
        'region' => 'content',
    ];
    $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.");
}

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