3 namespace Drupal\Tests\block_content\Functional;
5 use Drupal\block_content\Entity\BlockContent;
8 * Tests the Views-powered listing of custom blocks.
10 * @group block_content
11 * @see \Drupal\block\BlockContentListBuilder
12 * @see \Drupal\block_content\Tests\BlockContentListTest
14 class BlockContentListViewsTest extends BlockContentTestBase {
21 public static $modules = ['block', 'block_content', 'config_translation', 'views'];
24 * Tests the custom block listing page.
26 public function testListing() {
27 $this->drupalLogin($this->drupalCreateUser(['administer blocks', 'translate configuration']));
28 $this->drupalGet('admin/structure/block/block-content');
30 // Test for the page title.
31 $this->assertTitle(t('Custom block library') . ' | Drupal');
33 // Test for the exposed filters.
34 $this->assertFieldByName('info');
35 $this->assertFieldByName('type');
37 // Test for the table.
38 $element = $this->xpath('//div[@class="layout-content"]//table');
39 $this->assertTrue($element, 'Views table found.');
41 // Test the table header.
42 $elements = $this->xpath('//div[@class="layout-content"]//table/thead/tr/th');
43 $this->assertEqual(count($elements), 4, 'Correct number of table header cells found.');
45 // Test the contents of each th cell.
46 $expected_items = ['Block description', 'Block type', 'Updated Sort ascending', 'Operations'];
47 foreach ($elements as $key => $element) {
48 if ($element->find('xpath', 'a')) {
49 $this->assertIdentical(trim($element->find('xpath', 'a')->getText()), $expected_items[$key]);
52 $this->assertIdentical(trim($element->getText()), $expected_items[$key]);
57 $new_label = 'Albatross';
58 // Add a new entity using the operations link.
59 $link_text = t('Add custom block');
60 $this->assertLink($link_text);
61 $this->clickLink($link_text);
62 $this->assertResponse(200);
64 $edit['info[0][value]'] = $label;
65 $edit['body[0][value]'] = $this->randomMachineName(16);
66 $this->drupalPostForm(NULL, $edit, t('Save'));
68 // Confirm that once the user returns to the listing, the text of the label
69 // (versus elsewhere on the page).
70 $this->assertFieldByXpath('//td/a', $label, 'Label found for added block.');
72 // Check the number of table row cells.
73 $elements = $this->xpath('//div[@class="layout-content"]//table/tbody/tr/td');
74 $this->assertEqual(count($elements), 4, 'Correct number of table row cells found.');
75 // Check the contents of each row cell. The first cell contains the label,
76 // the second contains the machine name, and the third contains the
78 $this->assertIdentical($elements[0]->find('xpath', 'a')->getText(), $label);
80 // Edit the entity using the operations link.
81 $blocks = $this->container
82 ->get('entity.manager')
83 ->getStorage('block_content')
84 ->loadByProperties(['info' => $label]);
85 $block = reset($blocks);
87 $this->assertLinkByHref('block/' . $block->id());
88 $this->clickLink(t('Edit'));
89 $this->assertResponse(200);
90 $this->assertTitle(strip_tags(t('Edit custom block %label', ['%label' => $label]) . ' | Drupal'));
91 $edit = ['info[0][value]' => $new_label];
92 $this->drupalPostForm(NULL, $edit, t('Save'));
95 $this->fail('Did not find Albatross block in the database.');
98 // Confirm that once the user returns to the listing, the text of the label
99 // (versus elsewhere on the page).
100 $this->assertFieldByXpath('//td/a', $new_label, 'Label found for updated custom block.');
102 // Delete the added entity using the operations link.
103 $this->assertLinkByHref('block/' . $block->id() . '/delete');
104 $delete_text = t('Delete');
105 $this->clickLink($delete_text);
106 $this->assertResponse(200);
107 $this->assertTitle(strip_tags(t('Are you sure you want to delete the custom block %label?', ['%label' => $new_label]) . ' | Drupal'));
108 $this->drupalPostForm(NULL, [], $delete_text);
110 // Verify that the text of the label and machine name does not appear in
111 // the list (though it may appear elsewhere on the page).
112 $this->assertNoFieldByXpath('//td', $new_label, 'No label found for deleted custom block.');
114 // Confirm that the empty text is displayed.
115 $this->assertText('There are no custom blocks available.');
116 $this->assertLink('custom block');
118 $block_content = BlockContent::create([
119 'info' => 'Non-reusable block',
123 $block_content->save();
125 $this->drupalGet('admin/structure/block/block-content');
126 // Confirm that the empty text is displayed.
127 $this->assertSession()->pageTextContains('There are no custom blocks available.');
128 // Confirm the non-reusable block is not on the page.
129 $this->assertSession()->pageTextNotContains('Non-reusable block');