You are here

public function DisplayBlockTest::testBlockCategory in Drupal 10

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

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
    ->drupalGet('admin/structure/views/add');
  $this
    ->submitForm($edit, 'Save and edit');
  $pattern = '//tr[.//td[text()=:category] and .//td//a[contains(@href, :href)]]';
  $arguments = [
    ':href' => Url::fromRoute('block.admin_add', [
      'plugin_id' => 'views_block:' . $edit['id'] . '-block_1',
      'theme' => 'stark',
    ])
      ->toString(),
    ':category' => 'Lists (Views)',
  ];

  // Test that the block was given a default category corresponding to its
  // base table.
  $this
    ->drupalGet('admin/structure/block');
  $this
    ->clickLink('Place block');
  $this
    ->assertSession()
    ->elementExists('xpath', $this
    ->assertSession()
    ->buildXPathQuery($pattern, $arguments));

  // Duplicate the block before changing the category.
  $this
    ->drupalGet('admin/structure/views/view/' . $edit['id'] . '/edit/block_1');
  $this
    ->submitForm([], 'Duplicate Block');
  $this
    ->assertSession()
    ->addressEquals('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');
  $this
    ->assertSession()
    ->elementTextEquals('named', [
    'id',
    'views-block-1-block-category',
  ], 'Lists (Views)');
  $this
    ->clickLink('Lists (Views)');
  $category = $this
    ->randomString();
  $this
    ->submitForm([
    'block_category' => $category,
  ], 'Apply');

  // Duplicate the block after changing the category.
  $this
    ->submitForm([], 'Duplicate Block');
  $this
    ->assertSession()
    ->addressEquals('admin/structure/views/view/' . $edit['id'] . '/edit/block_3');
  $this
    ->submitForm([], 'Save');

  // Test that the blocks are listed under the correct categories.
  $arguments[':category'] = $category;
  $this
    ->drupalGet('admin/structure/block');
  $this
    ->clickLink('Place block');
  $this
    ->assertSession()
    ->elementExists('xpath', $this
    ->assertSession()
    ->buildXPathQuery($pattern, $arguments));

  // Test that the first duplicated test block remains in the original
  // category.
  $arguments = [
    ':href' => Url::fromRoute('block.admin_add', [
      'plugin_id' => 'views_block:' . $edit['id'] . '-block_2',
      'theme' => 'stark',
    ])
      ->toString(),
    ':category' => 'Lists (Views)',
  ];
  $this
    ->assertSession()
    ->elementExists('xpath', $this
    ->assertSession()
    ->buildXPathQuery($pattern, $arguments));

  // Test that the second duplicated test block appears in the custom
  // category.
  $arguments = [
    ':href' => Url::fromRoute('block.admin_add', [
      'plugin_id' => 'views_block:' . $edit['id'] . '-block_3',
      'theme' => 'stark',
    ])
      ->toString(),
    ':category' => $category,
  ];
  $this
    ->assertSession()
    ->elementExists('xpath', $this
    ->assertSession()
    ->buildXPathQuery($pattern, $arguments));
}