3 namespace Drupal\Tests\block_content\Functional;
6 * Tests the listing of custom blocks.
8 * Tests the fallback block content list when Views is disabled.
10 * @group block_content
11 * @see \Drupal\block\BlockContentListBuilder
12 * @see \Drupal\block_content\Tests\BlockContentListViewsTest
14 class BlockContentListTest extends BlockContentTestBase {
21 public static $modules = ['block', 'block_content', 'config_translation'];
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 table.
34 $element = $this->xpath('//div[@class="layout-content"]//table');
35 $this->assertTrue($element, 'Configuration entity list table found.');
37 // Test the table header.
38 $elements = $this->xpath('//div[@class="layout-content"]//table/thead/tr/th');
39 $this->assertEqual(count($elements), 2, 'Correct number of table header cells found.');
41 // Test the contents of each th cell.
42 $expected_items = [t('Block description'), t('Operations')];
43 foreach ($elements as $key => $element) {
44 $this->assertEqual($element->getText(), $expected_items[$key]);
48 $new_label = 'Albatross';
49 // Add a new entity using the operations link.
50 $link_text = t('Add custom block');
51 $this->assertLink($link_text);
52 $this->clickLink($link_text);
53 $this->assertResponse(200);
55 $edit['info[0][value]'] = $label;
56 $edit['body[0][value]'] = $this->randomMachineName(16);
57 $this->drupalPostForm(NULL, $edit, t('Save'));
59 // Confirm that once the user returns to the listing, the text of the label
60 // (versus elsewhere on the page).
61 $this->assertFieldByXpath('//td', $label, 'Label found for added block.');
63 // Check the number of table row cells.
64 $elements = $this->xpath('//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td');
65 $this->assertEqual(count($elements), 2, 'Correct number of table row cells found.');
66 // Check the contents of each row cell. The first cell contains the label,
67 // the second contains the machine name, and the third contains the
69 $this->assertIdentical($elements[0]->getText(), $label);
71 // Edit the entity using the operations link.
72 $blocks = $this->container
73 ->get('entity.manager')
74 ->getStorage('block_content')
75 ->loadByProperties(['info' => $label]);
76 $block = reset($blocks);
78 $this->assertLinkByHref('block/' . $block->id());
79 $this->clickLink(t('Edit'));
80 $this->assertResponse(200);
81 $this->assertTitle(strip_tags(t('Edit custom block %label', ['%label' => $label]) . ' | Drupal'));
82 $edit = ['info[0][value]' => $new_label];
83 $this->drupalPostForm(NULL, $edit, t('Save'));
86 $this->fail('Did not find Albatross block in the database.');
89 // Confirm that once the user returns to the listing, the text of the label
90 // (versus elsewhere on the page).
91 $this->assertFieldByXpath('//td', $new_label, 'Label found for updated custom block.');
93 // Delete the added entity using the operations link.
94 $this->assertLinkByHref('block/' . $block->id() . '/delete');
95 $delete_text = t('Delete');
96 $this->clickLink($delete_text);
97 $this->assertResponse(200);
98 $this->assertTitle(strip_tags(t('Are you sure you want to delete the custom block %label?', ['%label' => $new_label]) . ' | Drupal'));
99 $this->drupalPostForm(NULL, [], $delete_text);
101 // Verify that the text of the label and machine name does not appear in
102 // the list (though it may appear elsewhere on the page).
103 $this->assertNoFieldByXpath('//td', $new_label, 'No label found for deleted custom block.');
105 // Confirm that the empty text is displayed.
106 $this->assertText(t('There is no Custom block yet.'));