Backup of db before drupal security update
[yaffs-website] / web / core / modules / block_content / src / Tests / BlockContentListTest.php
1 <?php
2
3 namespace Drupal\block_content\Tests;
4
5 /**
6  * Tests the listing of custom blocks.
7  *
8  * Tests the fallback block content list when Views is disabled.
9  *
10  * @group block_content
11  * @see \Drupal\block\BlockContentListBuilder
12  * @see \Drupal\block_content\Tests\BlockContentListViewsTest
13  */
14 class BlockContentListTest extends BlockContentTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['block', 'block_content', 'config_translation'];
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 table.
34     $element = $this->xpath('//div[@class="layout-content"]//table');
35     $this->assertTrue($element, 'Configuration entity list table found.');
36
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.');
40
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[0], $expected_items[$key]);
45     }
46
47     $label = 'Antelope';
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);
54     $edit = [];
55     $edit['info[0][value]'] = $label;
56     $edit['body[0][value]'] = $this->randomMachineName(16);
57     $this->drupalPostForm(NULL, $edit, t('Save'));
58
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.');
62
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
68     // operations list.
69     $this->assertIdentical((string) $elements[0], $label);
70
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);
77     if (!empty($block)) {
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'));
84     }
85     else {
86       $this->fail('Did not find Albatross block in the database.');
87     }
88
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.');
92
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);
100
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.');
104
105     // Confirm that the empty text is displayed.
106     $this->assertText(t('There is no Custom block yet.'));
107   }
108
109 }