function DisplayBlockTest::testBlockCategory

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

Tests default and custom block categories.

File

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

Class

DisplayBlockTest
Tests the block display plugin.

Namespace

Drupal\Tests\block\Functional\Views

Code

public function testBlockCategory() {
    $this->drupalLogin($this->drupalCreateUser([
        'administer views',
        'administer blocks',
    ]));
    // Create a new view in the UI.
    $edit = [];
    $edit['label'] = $this->randomString();
    $edit['id'] = strtolower($this->randomMachineName());
    $edit['show[wizard_key]'] = 'standard:views_test_data';
    $edit['description'] = $this->randomString();
    $edit['block[create]'] = TRUE;
    $edit['block[style][row_plugin]'] = 'fields';
    $this->drupalPostForm('admin/structure/views/add', $edit, t('Save and edit'));
    $pattern = '//tr[.//td[text()=:category] and .//td//a[contains(@href, :href)]]';
    // Test that the block was given a default category corresponding to its
    // base table.
    $arguments = [
        ':href' => Url::fromRoute('block.admin_add', [
            'plugin_id' => 'views_block:' . $edit['id'] . '-block_1',
            'theme' => 'classy',
        ])->toString(),
        ':category' => 'Lists (Views)',
    ];
    $this->drupalGet('admin/structure/block');
    $this->clickLink('Place block');
    $elements = $this->xpath($pattern, $arguments);
    $this->assertTrue(!empty($elements), 'The test block appears in the category for its base table.');
    // Duplicate the block before changing the category.
    $this->drupalPostForm('admin/structure/views/view/' . $edit['id'] . '/edit/block_1', [], t('Duplicate @display_title', [
        '@display_title' => 'Block',
    ]));
    $this->assertUrl('admin/structure/views/view/' . $edit['id'] . '/edit/block_2');
    // Change the block category to a random string.
    $this->drupalGet('admin/structure/views/view/' . $edit['id'] . '/edit/block_1');
    $link = $this->xpath('//a[@id="views-block-1-block-category" and normalize-space(text())=:category]', $arguments);
    $this->assertTrue(!empty($link));
    $this->clickLink(t('Lists (Views)'));
    $category = $this->randomString();
    $this->drupalPostForm(NULL, [
        'block_category' => $category,
    ], t('Apply'));
    // Duplicate the block after changing the category.
    $this->drupalPostForm(NULL, [], t('Duplicate @display_title', [
        '@display_title' => 'Block',
    ]));
    $this->assertUrl('admin/structure/views/view/' . $edit['id'] . '/edit/block_3');
    $this->drupalPostForm(NULL, [], t('Save'));
    // Test that the blocks are listed under the correct categories.
    $arguments[':category'] = $category;
    $this->drupalGet('admin/structure/block');
    $this->clickLink('Place block');
    $elements = $this->xpath($pattern, $arguments);
    $this->assertTrue(!empty($elements), 'The test block appears in the custom category.');
    $arguments = [
        ':href' => Url::fromRoute('block.admin_add', [
            'plugin_id' => 'views_block:' . $edit['id'] . '-block_2',
            'theme' => 'classy',
        ])->toString(),
        ':category' => 'Lists (Views)',
    ];
    $elements = $this->xpath($pattern, $arguments);
    $this->assertTrue(!empty($elements), 'The first duplicated test block remains in the original category.');
    $arguments = [
        ':href' => Url::fromRoute('block.admin_add', [
            'plugin_id' => 'views_block:' . $edit['id'] . '-block_3',
            'theme' => 'classy',
        ])->toString(),
        ':category' => $category,
    ];
    $elements = $this->xpath($pattern, $arguments);
    $this->assertTrue(!empty($elements), 'The second duplicated test block appears in the custom category.');
}

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