3 namespace Drupal\Tests\config\Functional;
5 use Drupal\Core\Routing\RedirectDestinationTrait;
6 use Drupal\config_test\Entity\ConfigTest;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests the listing of configuration entities.
15 class ConfigEntityListTest extends BrowserTestBase {
17 use RedirectDestinationTrait;
24 public static $modules = ['block', 'config_test'];
29 protected function setUp() {
31 // Delete the override config_test entity since it is not required by this
33 \Drupal::entityManager()->getStorage('config_test')->load('override')->delete();
34 $this->drupalPlaceBlock('local_actions_block');
38 * Tests entity list builder methods.
40 public function testList() {
41 $controller = \Drupal::entityManager()->getListBuilder('config_test');
43 // Test getStorage() method.
44 $this->assertTrue($controller->getStorage() instanceof EntityStorageInterface, 'EntityStorage instance in storage.');
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.');
55 // Test getOperations() method.
56 $expected_operations = [
60 'url' => $entity->toUrl()->setOption('query', $this->getRedirectDestination()->getAsArray()),
63 'title' => t('Disable'),
65 'url' => $entity->toUrl('disable')->setOption('query', $this->getRedirectDestination()->getAsArray()),
68 'title' => t('Delete'),
70 'url' => $entity->toUrl('delete-form')->setOption('query', $this->getRedirectDestination()->getAsArray()),
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.');
79 // Test buildHeader() method.
82 'id' => 'Machine name',
83 'operations' => 'Operations',
85 $actual_items = $controller->buildHeader();
86 $this->assertEqual($expected_items, $actual_items, 'Return value from buildHeader matches expected.');
88 // Test buildRow() method.
89 $build_operations = $controller->buildOperations($entity);
92 'id' => 'dotted.default',
94 'data' => $build_operations,
97 $actual_items = $controller->buildRow($entity);
98 $this->assertEqual($expected_items, $actual_items, 'Return value from buildRow matches expected.');
100 $storage = $controller->getStorage();
101 $entity = $storage->create([
107 $entity = $storage->create([
113 $entity = $storage->create([
119 $list = $controller->load();
120 $this->assertIdentical(array_keys($list), ['beta', 'dotted.default', 'alpha', 'omega']);
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');
127 $list = $controller->load();
128 $entity = $list['default'];
130 // Test getOperations() method.
131 $expected_operations = [
133 'title' => t('Edit'),
135 'url' => $entity->toUrl()->setOption('query', $this->getRedirectDestination()->getAsArray()),
138 'title' => t('Delete'),
140 'url' => $entity->toUrl('delete-form')->setOption('query', $this->getRedirectDestination()->getAsArray()),
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.');
151 * Tests the listing UI.
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']));
157 // Get the list callback page.
158 $this->drupalGet('admin/structure/config_test');
160 // Test for the page title.
161 $this->assertTitle('Test configuration | Drupal');
163 // Test for the table.
164 $element = $this->xpath('//div[@class="layout-content"]//table');
165 $this->assertTrue($element, 'Configuration entity list table found.');
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.');
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]);
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.');
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
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.');
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);
193 'label' => 'Antelope',
197 $this->drupalPostForm(NULL, $edit, t('Save'));
199 // Ensure that the entity's sort method was called.
200 $this->assertTrue(\Drupal::state()->get('config_entity_sort'), 'ConfigTest::sort() was called.');
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
205 $this->assertFieldByXpath('//td', 'Antelope', "Label found for added 'Antelope' entity.");
206 $this->assertFieldByXpath('//td', 'antelope', "Machine name found for added 'Antelope' entity.");
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'));
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
219 $this->assertFieldByXpath('//td', 'Albatross', "Label found for updated 'Albatross' entity.");
220 $this->assertFieldByXpath('//td', 'albatross', "Machine name found for updated 'Albatross' entity.");
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'));
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.");
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'));
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.");
245 // Confirm that the empty text is displayed.
246 $this->assertText('There are no test configuration entities yet.');
252 public function testPager() {
253 $this->drupalLogin($this->drupalCreateUser(['administer site configuration']));
255 $storage = \Drupal::entityManager()->getListBuilder('config_test')->getStorage();
257 // Create 51 test entities.
258 for ($i = 1; $i < 52; $i++) {
260 'id' => str_pad($i, 2, '0', STR_PAD_LEFT),
261 'label' => 'Test config entity ' . $i,
263 'protected_property' => $i,
267 // Load the listing page.
268 $this->drupalGet('admin/structure/config_test');
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.');
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.');