3 namespace Drupal\Tests\block_content\Functional;
5 use Drupal\block_content\Entity\BlockContent;
8 * Tests the listing of custom blocks.
10 * Tests the fallback block content list when Views is disabled.
12 * @group block_content
13 * @see \Drupal\block\BlockContentListBuilder
14 * @see \Drupal\block_content\Tests\BlockContentListViewsTest
16 class BlockContentListTest extends BlockContentTestBase {
23 public static $modules = ['block', 'block_content', 'config_translation'];
26 * Tests the custom block listing page.
28 public function testListing() {
29 $this->drupalLogin($this->drupalCreateUser(['administer blocks', 'translate configuration']));
30 $this->drupalGet('admin/structure/block/block-content');
32 // Test for the page title.
33 $this->assertTitle(t('Custom block library') . ' | Drupal');
35 // Test for the table.
36 $element = $this->xpath('//div[@class="layout-content"]//table');
37 $this->assertTrue($element, 'Configuration entity list table found.');
39 // Test the table header.
40 $elements = $this->xpath('//div[@class="layout-content"]//table/thead/tr/th');
41 $this->assertEqual(count($elements), 2, 'Correct number of table header cells found.');
43 // Test the contents of each th cell.
44 $expected_items = [t('Block description'), t('Operations')];
45 foreach ($elements as $key => $element) {
46 $this->assertEqual($element->getText(), $expected_items[$key]);
50 $new_label = 'Albatross';
51 // Add a new entity using the operations link.
52 $link_text = t('Add custom block');
53 $this->assertLink($link_text);
54 $this->clickLink($link_text);
55 $this->assertResponse(200);
57 $edit['info[0][value]'] = $label;
58 $edit['body[0][value]'] = $this->randomMachineName(16);
59 $this->drupalPostForm(NULL, $edit, t('Save'));
61 // Confirm that once the user returns to the listing, the text of the label
62 // (versus elsewhere on the page).
63 $this->assertFieldByXpath('//td', $label, 'Label found for added block.');
65 // Check the number of table row cells.
66 $elements = $this->xpath('//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td');
67 $this->assertEqual(count($elements), 2, 'Correct number of table row cells found.');
68 // Check the contents of each row cell. The first cell contains the label,
69 // the second contains the machine name, and the third contains the
71 $this->assertIdentical($elements[0]->getText(), $label);
73 // Edit the entity using the operations link.
74 $blocks = $this->container
75 ->get('entity.manager')
76 ->getStorage('block_content')
77 ->loadByProperties(['info' => $label]);
78 $block = reset($blocks);
80 $this->assertLinkByHref('block/' . $block->id());
81 $this->clickLink(t('Edit'));
82 $this->assertResponse(200);
83 $this->assertTitle(strip_tags(t('Edit custom block %label', ['%label' => $label]) . ' | Drupal'));
84 $edit = ['info[0][value]' => $new_label];
85 $this->drupalPostForm(NULL, $edit, t('Save'));
88 $this->fail('Did not find Albatross block in the database.');
91 // Confirm that once the user returns to the listing, the text of the label
92 // (versus elsewhere on the page).
93 $this->assertFieldByXpath('//td', $new_label, 'Label found for updated custom block.');
95 // Delete the added entity using the operations link.
96 $this->assertLinkByHref('block/' . $block->id() . '/delete');
97 $delete_text = t('Delete');
98 $this->clickLink($delete_text);
99 $this->assertResponse(200);
100 $this->assertTitle(strip_tags(t('Are you sure you want to delete the custom block %label?', ['%label' => $new_label]) . ' | Drupal'));
101 $this->drupalPostForm(NULL, [], $delete_text);
103 // Verify that the text of the label and machine name does not appear in
104 // the list (though it may appear elsewhere on the page).
105 $this->assertNoFieldByXpath('//td', $new_label, 'No label found for deleted custom block.');
107 // Confirm that the empty text is displayed.
108 $this->assertText(t('There are no custom blocks yet.'));
110 $block_content = BlockContent::create([
111 'info' => 'Non-reusable block',
115 $block_content->save();
117 $this->drupalGet('admin/structure/block/block-content');
118 // Confirm that the empty text is displayed.
119 $this->assertSession()->pageTextContains('There are no custom blocks yet.');
120 // Confirm the non-reusable block is not on the page.
121 $this->assertSession()->pageTextNotContains('Non-reusable block');