Backup of db before drupal security update
[yaffs-website] / web / core / modules / block_content / src / Tests / BlockContentListViewsTest.php
1 <?php
2
3 namespace Drupal\block_content\Tests;
4
5 /**
6  * Tests the Views-powered listing of custom blocks.
7  *
8  * @group block_content
9  * @see \Drupal\block\BlockContentListBuilder
10  * @see \Drupal\block_content\Tests\BlockContentListTest
11  */
12 class BlockContentListViewsTest extends BlockContentTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['block', 'block_content', 'config_translation', 'views'];
20
21   /**
22    * Tests the custom block listing page.
23    */
24   public function testListing() {
25     $this->drupalLogin($this->drupalCreateUser(['administer blocks', 'translate configuration']));
26     $this->drupalGet('admin/structure/block/block-content');
27
28     // Test for the page title.
29     $this->assertTitle(t('Custom block library') . ' | Drupal');
30
31     // Test for the exposed filters.
32     $this->assertFieldByName('info');
33     $this->assertFieldByName('type');
34
35     // Test for the table.
36     $element = $this->xpath('//div[@class="layout-content"]//table');
37     $this->assertTrue($element, 'Views 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), 4, 'Correct number of table header cells found.');
42
43     // Test the contents of each th cell.
44     $expected_items = ['Block description', 'Block type', 'Updated', 'Operations'];
45     foreach ($elements as $key => $element) {
46       if ($element->xpath('a')) {
47         $this->assertIdentical(trim((string) $element->xpath('a')[0]), $expected_items[$key]);
48       }
49       else {
50         $this->assertIdentical(trim((string) $element[0]), $expected_items[$key]);
51       }
52     }
53
54     $label = 'Antelope';
55     $new_label = 'Albatross';
56     // Add a new entity using the operations link.
57     $link_text = t('Add custom block');
58     $this->assertLink($link_text);
59     $this->clickLink($link_text);
60     $this->assertResponse(200);
61     $edit = [];
62     $edit['info[0][value]'] = $label;
63     $edit['body[0][value]'] = $this->randomMachineName(16);
64     $this->drupalPostForm(NULL, $edit, t('Save'));
65
66     // Confirm that once the user returns to the listing, the text of the label
67     // (versus elsewhere on the page).
68     $this->assertFieldByXpath('//td/a', $label, 'Label found for added block.');
69
70     // Check the number of table row cells.
71     $elements = $this->xpath('//div[@class="layout-content"]//table/tbody/tr/td');
72     $this->assertEqual(count($elements), 4, 'Correct number of table row cells found.');
73     // Check the contents of each row cell. The first cell contains the label,
74     // the second contains the machine name, and the third contains the
75     // operations list.
76     $this->assertIdentical((string) $elements[0]->xpath('a')[0], $label);
77
78     // Edit the entity using the operations link.
79     $blocks = $this->container
80       ->get('entity.manager')
81       ->getStorage('block_content')
82       ->loadByProperties(['info' => $label]);
83     $block = reset($blocks);
84     if (!empty($block)) {
85       $this->assertLinkByHref('block/' . $block->id());
86       $this->clickLink(t('Edit'));
87       $this->assertResponse(200);
88       $this->assertTitle(strip_tags(t('Edit custom block %label', ['%label' => $label]) . ' | Drupal'));
89       $edit = ['info[0][value]' => $new_label];
90       $this->drupalPostForm(NULL, $edit, t('Save'));
91     }
92     else {
93       $this->fail('Did not find Albatross block in the database.');
94     }
95
96     // Confirm that once the user returns to the listing, the text of the label
97     // (versus elsewhere on the page).
98     $this->assertFieldByXpath('//td/a', $new_label, 'Label found for updated custom block.');
99
100     // Delete the added entity using the operations link.
101     $this->assertLinkByHref('block/' . $block->id() . '/delete');
102     $delete_text = t('Delete');
103     $this->clickLink($delete_text);
104     $this->assertResponse(200);
105     $this->assertTitle(strip_tags(t('Are you sure you want to delete the custom block %label?', ['%label' => $new_label]) . ' | Drupal'));
106     $this->drupalPostForm(NULL, [], $delete_text);
107
108     // Verify that the text of the label and machine name does not appear in
109     // the list (though it may appear elsewhere on the page).
110     $this->assertNoFieldByXpath('//td', $new_label, 'No label found for deleted custom block.');
111
112     // Confirm that the empty text is displayed.
113     $this->assertText('There are no custom blocks available.');
114     $this->assertLink('custom block');
115   }
116
117 }