You are here

public function DisplayBlockTest::testBlockCategory 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::testBlockCategory()

Tests default and custom block categories.

File

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

Class

DisplayBlockTest
Tests the block display plugin.

Namespace

Drupal\block\Tests\Views

Code

public function testBlockCategory() {
  $this
    ->drupalLogin($this
    ->drupalCreateUser(array(
    'administer views',
    'administer blocks',
  )));

  // Create a new view in the UI.
  $edit = array();
  $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 = array(
    ':href' => \Drupal::Url('block.admin_add', array(
      'plugin_id' => 'views_block:' . $edit['id'] . '-block_1',
      'theme' => 'classy',
    )),
    ':category' => t('Lists (Views)'),
  );
  $this
    ->drupalGet('admin/structure/block');
  $this
    ->clickLinkPartialName('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', array(), t('Duplicate @display_title', array(
    '@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, array(
    'block_category' => $category,
  ), t('Apply'));

  // Duplicate the block after changing the category.
  $this
    ->drupalPostForm(NULL, array(), t('Duplicate @display_title', array(
    '@display_title' => 'Block',
  )));
  $this
    ->assertUrl('admin/structure/views/view/' . $edit['id'] . '/edit/block_3');
  $this
    ->drupalPostForm(NULL, array(), t('Save'));

  // Test that the blocks are listed under the correct categories.
  $arguments[':category'] = $category;
  $this
    ->drupalGet('admin/structure/block');
  $this
    ->clickLinkPartialName('Place block');
  $elements = $this
    ->xpath($pattern, $arguments);
  $this
    ->assertTrue(!empty($elements), 'The test block appears in the custom category.');
  $arguments = array(
    ':href' => \Drupal::Url('block.admin_add', array(
      'plugin_id' => 'views_block:' . $edit['id'] . '-block_2',
      'theme' => 'classy',
    )),
    ':category' => t('Lists (Views)'),
  );
  $elements = $this
    ->xpath($pattern, $arguments);
  $this
    ->assertTrue(!empty($elements), 'The first duplicated test block remains in the original category.');
  $arguments = array(
    ':href' => \Drupal::Url('block.admin_add', array(
      'plugin_id' => 'views_block:' . $edit['id'] . '-block_3',
      'theme' => 'classy',
    )),
    ':category' => $category,
  );
  $elements = $this
    ->xpath($pattern, $arguments);
  $this
    ->assertTrue(!empty($elements), 'The second duplicated test block appears in the custom category.');
}