3 namespace Drupal\Tests\block_content\Functional;
6 * Tests the Views-powered listing of custom blocks.
9 * @see \Drupal\block\BlockContentListBuilder
10 * @see \Drupal\block_content\Tests\BlockContentListTest
12 class BlockContentListViewsTest extends BlockContentTestBase {
19 public static $modules = ['block', 'block_content', 'config_translation', 'views'];
22 * Tests the custom block listing page.
24 public function testListing() {
25 $this->drupalLogin($this->drupalCreateUser(['administer blocks', 'translate configuration']));
26 $this->drupalGet('admin/structure/block/block-content');
28 // Test for the page title.
29 $this->assertTitle(t('Custom block library') . ' | Drupal');
31 // Test for the exposed filters.
32 $this->assertFieldByName('info');
33 $this->assertFieldByName('type');
35 // Test for the table.
36 $element = $this->xpath('//div[@class="layout-content"]//table');
37 $this->assertTrue($element, 'Views table found.');
39 // Test the table header.
40 $elements = $this->xpath('//div[@class="layout-content"]//table/thead/tr/th');
41 $this->assertEqual(count($elements), 4, 'Correct number of table header cells found.');
43 // Test the contents of each th cell.
44 $expected_items = ['Block description', 'Block type', 'Updated Sort ascending', 'Operations'];
45 foreach ($elements as $key => $element) {
46 if ($element->find('xpath', 'a')) {
47 $this->assertIdentical(trim($element->find('xpath', 'a')->getText()), $expected_items[$key]);
50 $this->assertIdentical(trim($element->getText()), $expected_items[$key]);
55 $new_label = 'Albatross';
56 // Add a new entity using the operations link.
57 $link_text = t('Add custom block');
58 $this->assertLink($link_text);
59 $this->clickLink($link_text);
60 $this->assertResponse(200);
62 $edit['info[0][value]'] = $label;
63 $edit['body[0][value]'] = $this->randomMachineName(16);
64 $this->drupalPostForm(NULL, $edit, t('Save'));
66 // Confirm that once the user returns to the listing, the text of the label
67 // (versus elsewhere on the page).
68 $this->assertFieldByXpath('//td/a', $label, 'Label found for added block.');
70 // Check the number of table row cells.
71 $elements = $this->xpath('//div[@class="layout-content"]//table/tbody/tr/td');
72 $this->assertEqual(count($elements), 4, 'Correct number of table row cells found.');
73 // Check the contents of each row cell. The first cell contains the label,
74 // the second contains the machine name, and the third contains the
76 $this->assertIdentical($elements[0]->find('xpath', 'a')->getText(), $label);
78 // Edit the entity using the operations link.
79 $blocks = $this->container
80 ->get('entity.manager')
81 ->getStorage('block_content')
82 ->loadByProperties(['info' => $label]);
83 $block = reset($blocks);
85 $this->assertLinkByHref('block/' . $block->id());
86 $this->clickLink(t('Edit'));
87 $this->assertResponse(200);
88 $this->assertTitle(strip_tags(t('Edit custom block %label', ['%label' => $label]) . ' | Drupal'));
89 $edit = ['info[0][value]' => $new_label];
90 $this->drupalPostForm(NULL, $edit, t('Save'));
93 $this->fail('Did not find Albatross block in the database.');
96 // Confirm that once the user returns to the listing, the text of the label
97 // (versus elsewhere on the page).
98 $this->assertFieldByXpath('//td/a', $new_label, 'Label found for updated custom block.');
100 // Delete the added entity using the operations link.
101 $this->assertLinkByHref('block/' . $block->id() . '/delete');
102 $delete_text = t('Delete');
103 $this->clickLink($delete_text);
104 $this->assertResponse(200);
105 $this->assertTitle(strip_tags(t('Are you sure you want to delete the custom block %label?', ['%label' => $new_label]) . ' | Drupal'));
106 $this->drupalPostForm(NULL, [], $delete_text);
108 // Verify that the text of the label and machine name does not appear in
109 // the list (though it may appear elsewhere on the page).
110 $this->assertNoFieldByXpath('//td', $new_label, 'No label found for deleted custom block.');
112 // Confirm that the empty text is displayed.
113 $this->assertText('There are no custom blocks available.');
114 $this->assertLink('custom block');