Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / config / tests / src / Functional / ConfigEntityListTest.php
1 <?php
2
3 namespace Drupal\Tests\config\Functional;
4
5 use Drupal\config_test\Entity\ConfigTest;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Tests the listing of configuration entities.
11  *
12  * @group config
13  */
14 class ConfigEntityListTest extends BrowserTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['block', 'config_test'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28     // Delete the override config_test entity since it is not required by this
29     // test.
30     \Drupal::entityManager()->getStorage('config_test')->load('override')->delete();
31     $this->drupalPlaceBlock('local_actions_block');
32   }
33
34   /**
35    * Tests entity list builder methods.
36    */
37   public function testList() {
38     $controller = \Drupal::entityManager()->getListBuilder('config_test');
39
40     // Test getStorage() method.
41     $this->assertTrue($controller->getStorage() instanceof EntityStorageInterface, 'EntityStorage instance in storage.');
42
43     // Get a list of ConfigTest entities and confirm that it contains the
44     // ConfigTest entity provided by the config_test module.
45     // @see config_test.dynamic.dotted.default.yml
46     $list = $controller->load();
47     $this->assertEqual(count($list), 1, '1 ConfigTest entity found.');
48     $entity = $list['dotted.default'];
49     $this->assertTrue(!empty($entity), '"Default" ConfigTest entity ID found.');
50     $this->assertTrue($entity instanceof ConfigTest, '"Default" ConfigTest entity is an instance of ConfigTest.');
51
52     // Test getOperations() method.
53     $expected_operations = [
54       'edit' => [
55         'title' => t('Edit'),
56         'weight' => 10,
57         'url' => $entity->urlInfo(),
58       ],
59       'disable' => [
60         'title' => t('Disable'),
61         'weight' => 40,
62         'url' => $entity->urlInfo('disable'),
63       ],
64       'delete' => [
65         'title' => t('Delete'),
66         'weight' => 100,
67         'url' => $entity->urlInfo('delete-form'),
68       ],
69     ];
70
71     $actual_operations = $controller->getOperations($entity);
72     // Sort the operations to normalize link order.
73     uasort($actual_operations, ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
74     $this->assertEqual($expected_operations, $actual_operations, 'The operations are identical.');
75
76     // Test buildHeader() method.
77     $expected_items = [
78       'label' => 'Label',
79       'id' => 'Machine name',
80       'operations' => 'Operations',
81     ];
82     $actual_items = $controller->buildHeader();
83     $this->assertEqual($expected_items, $actual_items, 'Return value from buildHeader matches expected.');
84
85     // Test buildRow() method.
86     $build_operations = $controller->buildOperations($entity);
87     $expected_items = [
88       'label' => 'Default',
89       'id' => 'dotted.default',
90       'operations' => [
91         'data' => $build_operations,
92       ],
93     ];
94     $actual_items = $controller->buildRow($entity);
95     $this->assertEqual($expected_items, $actual_items, 'Return value from buildRow matches expected.');
96     // Test sorting.
97     $storage = $controller->getStorage();
98     $entity = $storage->create([
99       'id' => 'alpha',
100       'label' => 'Alpha',
101       'weight' => 1,
102     ]);
103     $entity->save();
104     $entity = $storage->create([
105       'id' => 'omega',
106       'label' => 'Omega',
107       'weight' => 1,
108     ]);
109     $entity->save();
110     $entity = $storage->create([
111       'id' => 'beta',
112       'label' => 'Beta',
113       'weight' => 0,
114     ]);
115     $entity->save();
116     $list = $controller->load();
117     $this->assertIdentical(array_keys($list), ['beta', 'dotted.default', 'alpha', 'omega']);
118
119     // Test that config entities that do not support status, do not have
120     // enable/disable operations.
121     $controller = $this->container->get('entity.manager')
122       ->getListBuilder('config_test_no_status');
123
124     $list = $controller->load();
125     $entity = $list['default'];
126
127     // Test getOperations() method.
128     $expected_operations = [
129       'edit' => [
130         'title' => t('Edit'),
131         'weight' => 10,
132         'url' => $entity->urlInfo(),
133       ],
134       'delete' => [
135         'title' => t('Delete'),
136         'weight' => 100,
137         'url' => $entity->urlInfo('delete-form'),
138       ],
139     ];
140
141     $actual_operations = $controller->getOperations($entity);
142     // Sort the operations to normalize link order.
143     uasort($actual_operations, ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
144     $this->assertEqual($expected_operations, $actual_operations, 'The operations are identical.');
145   }
146
147   /**
148    * Tests the listing UI.
149    */
150   public function testListUI() {
151     // Log in as an administrative user to access the full menu trail.
152     $this->drupalLogin($this->drupalCreateUser(['access administration pages', 'administer site configuration']));
153
154     // Get the list callback page.
155     $this->drupalGet('admin/structure/config_test');
156
157     // Test for the page title.
158     $this->assertTitle('Test configuration | Drupal');
159
160     // Test for the table.
161     $element = $this->xpath('//div[@class="layout-content"]//table');
162     $this->assertTrue($element, 'Configuration entity list table found.');
163
164     // Test the table header.
165     $elements = $this->xpath('//div[@class="layout-content"]//table/thead/tr/th');
166     $this->assertEqual(count($elements), 3, 'Correct number of table header cells found.');
167
168     // Test the contents of each th cell.
169     $expected_items = ['Label', 'Machine name', 'Operations'];
170     foreach ($elements as $key => $element) {
171       $this->assertIdentical($element->getText(), $expected_items[$key]);
172     }
173
174     // Check the number of table row cells.
175     $elements = $this->xpath('//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td');
176     $this->assertEqual(count($elements), 3, 'Correct number of table row cells found.');
177
178     // Check the contents of each row cell. The first cell contains the label,
179     // the second contains the machine name, and the third contains the
180     // operations list.
181     $this->assertIdentical($elements[0]->getText(), 'Default');
182     $this->assertIdentical($elements[1]->getText(), 'dotted.default');
183     $this->assertNotEmpty($elements[2]->find('xpath', '//ul'), 'Operations list found.');
184
185     // Add a new entity using the operations link.
186     $this->assertLink('Add test configuration');
187     $this->clickLink('Add test configuration');
188     $this->assertResponse(200);
189     $edit = [
190       'label' => 'Antelope',
191       'id' => 'antelope',
192       'weight' => 1,
193     ];
194     $this->drupalPostForm(NULL, $edit, t('Save'));
195
196     // Ensure that the entity's sort method was called.
197     $this->assertTrue(\Drupal::state()->get('config_entity_sort'), 'ConfigTest::sort() was called.');
198
199     // Confirm that the user is returned to the listing, and verify that the
200     // text of the label and machine name appears in the list (versus elsewhere
201     // on the page).
202     $this->assertFieldByXpath('//td', 'Antelope', "Label found for added 'Antelope' entity.");
203     $this->assertFieldByXpath('//td', 'antelope', "Machine name found for added 'Antelope' entity.");
204
205     // Edit the entity using the operations link.
206     $this->assertLinkByHref('admin/structure/config_test/manage/antelope');
207     $this->clickLink('Edit', 1);
208     $this->assertResponse(200);
209     $this->assertTitle('Edit Antelope | Drupal');
210     $edit = ['label' => 'Albatross', 'id' => 'albatross'];
211     $this->drupalPostForm(NULL, $edit, t('Save'));
212
213     // Confirm that the user is returned to the listing, and verify that the
214     // text of the label and machine name appears in the list (versus elsewhere
215     // on the page).
216     $this->assertFieldByXpath('//td', 'Albatross', "Label found for updated 'Albatross' entity.");
217     $this->assertFieldByXpath('//td', 'albatross', "Machine name found for updated 'Albatross' entity.");
218
219     // Delete the added entity using the operations link.
220     $this->assertLinkByHref('admin/structure/config_test/manage/albatross/delete');
221     $this->clickLink('Delete', 1);
222     $this->assertResponse(200);
223     $this->assertTitle('Are you sure you want to delete the test configuration Albatross? | Drupal');
224     $this->drupalPostForm(NULL, [], t('Delete'));
225
226     // Verify that the text of the label and machine name does not appear in
227     // the list (though it may appear elsewhere on the page).
228     $this->assertNoFieldByXpath('//td', 'Albatross', "No label found for deleted 'Albatross' entity.");
229     $this->assertNoFieldByXpath('//td', 'albatross', "No machine name found for deleted 'Albatross' entity.");
230
231     // Delete the original entity using the operations link.
232     $this->clickLink('Delete');
233     $this->assertResponse(200);
234     $this->assertTitle('Are you sure you want to delete the test configuration Default? | Drupal');
235     $this->drupalPostForm(NULL, [], t('Delete'));
236
237     // Verify that the text of the label and machine name does not appear in
238     // the list (though it may appear elsewhere on the page).
239     $this->assertNoFieldByXpath('//td', 'Default', "No label found for deleted 'Default' entity.");
240     $this->assertNoFieldByXpath('//td', 'dotted.default', "No machine name found for deleted 'Default' entity.");
241
242     // Confirm that the empty text is displayed.
243     $this->assertText('There is no Test configuration yet.');
244   }
245
246   /**
247    * Test paging.
248    */
249   public function testPager() {
250     $this->drupalLogin($this->drupalCreateUser(['administer site configuration']));
251
252     $storage = \Drupal::entityManager()->getListBuilder('config_test')->getStorage();
253
254     // Create 51 test entities.
255     for ($i = 1; $i < 52; $i++) {
256       $storage->create([
257         'id' => str_pad($i, 2, '0', STR_PAD_LEFT),
258         'label' => 'Test config entity ' . $i,
259         'weight' => $i,
260         'protected_property' => $i,
261       ])->save();
262     }
263
264     // Load the listing page.
265     $this->drupalGet('admin/structure/config_test');
266
267     // Item 51 should not be present.
268     $this->assertRaw('Test config entity 50', 'Config entity 50 is shown.');
269     $this->assertNoRaw('Test config entity 51', 'Config entity 51 is on the next page.');
270
271     // Browse to the next page.
272     $this->clickLink(t('Page 2'));
273     $this->assertNoRaw('Test config entity 50', 'Test config entity 50 is on the previous page.');
274     $this->assertRaw('dotted.default', 'Default config entity appears on page 2.');
275     $this->assertRaw('Test config entity 51', 'Test config entity 51 is on page 2.');
276   }
277
278 }