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