8919c05d00193c6354d84979aaa116725d35f1ce
[yaffs-website] / web / core / modules / block_content / tests / src / Functional / BlockContentListTest.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Functional;
4
5 use Drupal\block_content\Entity\BlockContent;
6
7 /**
8  * Tests the listing of custom blocks.
9  *
10  * Tests the fallback block content list when Views is disabled.
11  *
12  * @group block_content
13  * @see \Drupal\block\BlockContentListBuilder
14  * @see \Drupal\block_content\Tests\BlockContentListViewsTest
15  */
16 class BlockContentListTest extends BlockContentTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['block', 'block_content', 'config_translation'];
24
25   /**
26    * Tests the custom block listing page.
27    */
28   public function testListing() {
29     $this->drupalLogin($this->drupalCreateUser(['administer blocks', 'translate configuration']));
30     $this->drupalGet('admin/structure/block/block-content');
31
32     // Test for the page title.
33     $this->assertTitle(t('Custom block library') . ' | Drupal');
34
35     // Test for the table.
36     $element = $this->xpath('//div[@class="layout-content"]//table');
37     $this->assertTrue($element, 'Configuration entity list table found.');
38
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.');
42
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]);
47     }
48
49     $label = 'Antelope';
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);
56     $edit = [];
57     $edit['info[0][value]'] = $label;
58     $edit['body[0][value]'] = $this->randomMachineName(16);
59     $this->drupalPostForm(NULL, $edit, t('Save'));
60
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.');
64
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
70     // operations list.
71     $this->assertIdentical($elements[0]->getText(), $label);
72
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);
79     if (!empty($block)) {
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'));
86     }
87     else {
88       $this->fail('Did not find Albatross block in the database.');
89     }
90
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.');
94
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);
102
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.');
106
107     // Confirm that the empty text is displayed.
108     $this->assertText(t('There are no custom blocks yet.'));
109
110     $block_content = BlockContent::create([
111       'info' => 'Non-reusable block',
112       'type' => 'basic',
113       'reusable' => FALSE,
114     ]);
115     $block_content->save();
116
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');
122   }
123
124 }