Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / block_content / tests / src / Functional / BlockContentListViewsTest.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Functional;
4
5 use Drupal\block_content\Entity\BlockContent;
6
7 /**
8  * Tests the Views-powered listing of custom blocks.
9  *
10  * @group block_content
11  * @see \Drupal\block\BlockContentListBuilder
12  * @see \Drupal\block_content\Tests\BlockContentListTest
13  */
14 class BlockContentListViewsTest extends BlockContentTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['block', 'block_content', 'config_translation', 'views'];
22
23   /**
24    * Tests the custom block listing page.
25    */
26   public function testListing() {
27     $this->drupalLogin($this->drupalCreateUser(['administer blocks', 'translate configuration']));
28     $this->drupalGet('admin/structure/block/block-content');
29
30     // Test for the page title.
31     $this->assertTitle(t('Custom block library') . ' | Drupal');
32
33     // Test for the exposed filters.
34     $this->assertFieldByName('info');
35     $this->assertFieldByName('type');
36
37     // Test for the table.
38     $element = $this->xpath('//div[@class="layout-content"]//table');
39     $this->assertTrue($element, 'Views table found.');
40
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.');
44
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]);
50       }
51       else {
52         $this->assertIdentical(trim($element->getText()), $expected_items[$key]);
53       }
54     }
55
56     $label = 'Antelope';
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);
63     $edit = [];
64     $edit['info[0][value]'] = $label;
65     $edit['body[0][value]'] = $this->randomMachineName(16);
66     $this->drupalPostForm(NULL, $edit, t('Save'));
67
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.');
71
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
77     // operations list.
78     $this->assertIdentical($elements[0]->find('xpath', 'a')->getText(), $label);
79
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);
86     if (!empty($block)) {
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'));
93     }
94     else {
95       $this->fail('Did not find Albatross block in the database.');
96     }
97
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.');
101
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);
109
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.');
113
114     // Confirm that the empty text is displayed.
115     $this->assertText('There are no custom blocks available.');
116     $this->assertLink('custom block');
117
118     $block_content = BlockContent::create([
119       'info' => 'Non-reusable block',
120       'type' => 'basic',
121       'reusable' => FALSE,
122     ]);
123     $block_content->save();
124
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');
130   }
131
132 }