BlockContentListViewsTest.php 5.11 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\block_content\Functional;
4

5 6
use Drupal\block_content\Entity\BlockContent;

7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * Tests the Views-powered listing of custom blocks.
 *
 * @group block_content
 * @see \Drupal\block\BlockContentListBuilder
 * @see \Drupal\block_content\Tests\BlockContentListTest
 */
class BlockContentListViewsTest extends BlockContentTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
21 22 23 24 25 26
  protected static $modules = [
    'block',
    'block_content',
    'config_translation',
    'views',
  ];
27

28 29 30 31 32
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

33 34 35 36
  /**
   * Tests the custom block listing page.
   */
  public function testListing() {
37 38 39 40
    $this->drupalLogin($this->drupalCreateUser([
      'administer blocks',
      'translate configuration',
    ]));
41 42 43
    $this->drupalGet('admin/structure/block/block-content');

    // Test for the page title.
44
    $this->assertSession()->titleEquals('Custom block library | Drupal');
45

46
    // Test for the exposed filters.
47 48
    $this->assertSession()->fieldExists('info');
    $this->assertSession()->fieldExists('type');
49

50 51
    // Test for the table.
    $element = $this->xpath('//div[@class="layout-content"]//table');
52
    $this->assertNotEmpty($element, 'Views table found.');
53 54 55

    // Test the table header.
    $elements = $this->xpath('//div[@class="layout-content"]//table/thead/tr/th');
56
    $this->assertCount(4, $elements, 'Correct number of table header cells found.');
57 58

    // Test the contents of each th cell.
59
    $expected_items = ['Block description', 'Block type', 'Updated Sort ascending', 'Operations'];
60
    foreach ($elements as $key => $element) {
61 62
      if ($element->find('xpath', 'a')) {
        $this->assertIdentical(trim($element->find('xpath', 'a')->getText()), $expected_items[$key]);
63 64
      }
      else {
65
        $this->assertIdentical(trim($element->getText()), $expected_items[$key]);
66 67 68 69 70 71 72
      }
    }

    $label = 'Antelope';
    $new_label = 'Albatross';
    // Add a new entity using the operations link.
    $link_text = t('Add custom block');
73
    $this->assertSession()->linkExists($link_text);
74
    $this->clickLink($link_text);
75
    $this->assertSession()->statusCodeEquals(200);
76
    $edit = [];
77 78 79 80 81 82
    $edit['info[0][value]'] = $label;
    $edit['body[0][value]'] = $this->randomMachineName(16);
    $this->drupalPostForm(NULL, $edit, t('Save'));

    // Confirm that once the user returns to the listing, the text of the label
    // (versus elsewhere on the page).
83
    $this->assertSession()->elementTextContains('xpath', '//td/a', $label);
84 85 86

    // Check the number of table row cells.
    $elements = $this->xpath('//div[@class="layout-content"]//table/tbody/tr/td');
87
    $this->assertCount(4, $elements, 'Correct number of table row cells found.');
88 89 90
    // Check the contents of each row cell. The first cell contains the label,
    // the second contains the machine name, and the third contains the
    // operations list.
91
    $this->assertIdentical($elements[0]->find('xpath', 'a')->getText(), $label);
92 93 94

    // Edit the entity using the operations link.
    $blocks = $this->container
95
      ->get('entity_type.manager')
96
      ->getStorage('block_content')
97
      ->loadByProperties(['info' => $label]);
98 99
    $block = reset($blocks);
    if (!empty($block)) {
100
      $this->assertSession()->linkByHrefExists('block/' . $block->id());
101
      $this->clickLink(t('Edit'));
102
      $this->assertSession()->statusCodeEquals(200);
103
      $this->assertSession()->titleEquals("Edit custom block $label | Drupal");
104
      $edit = ['info[0][value]' => $new_label];
105 106 107 108 109 110 111 112
      $this->drupalPostForm(NULL, $edit, t('Save'));
    }
    else {
      $this->fail('Did not find Albatross block in the database.');
    }

    // Confirm that once the user returns to the listing, the text of the label
    // (versus elsewhere on the page).
113
    $this->assertSession()->elementTextContains('xpath', '//td/a', $new_label);
114 115

    // Delete the added entity using the operations link.
116
    $this->assertSession()->linkByHrefExists('block/' . $block->id() . '/delete');
117 118
    $delete_text = t('Delete');
    $this->clickLink($delete_text);
119
    $this->assertSession()->statusCodeEquals(200);
120
    $this->assertSession()->titleEquals("Are you sure you want to delete the custom block $new_label? | Drupal");
121
    $this->drupalPostForm(NULL, [], $delete_text);
122 123 124

    // Verify that the text of the label and machine name does not appear in
    // the list (though it may appear elsewhere on the page).
125
    $this->assertSession()->elementTextNotContains('xpath', '//td', $new_label);
126 127 128

    // Confirm that the empty text is displayed.
    $this->assertText('There are no custom blocks available.');
129
    $this->assertSession()->linkExists('custom block');
130 131 132 133 134 135 136 137 138 139 140 141 142

    $block_content = BlockContent::create([
      'info' => 'Non-reusable block',
      'type' => 'basic',
      'reusable' => FALSE,
    ]);
    $block_content->save();

    $this->drupalGet('admin/structure/block/block-content');
    // Confirm that the empty text is displayed.
    $this->assertSession()->pageTextContains('There are no custom blocks available.');
    // Confirm the non-reusable block is not on the page.
    $this->assertSession()->pageTextNotContains('Non-reusable block');
143 144 145
  }

}