3 namespace Drupal\config\Tests;
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\config_test\Entity\ConfigTest;
7 use Drupal\Core\Entity\EntityStorageInterface;
10 * Tests the listing of configuration entities.
14 class ConfigEntityListTest extends WebTestBase {
21 public static $modules = ['block', 'config_test'];
26 protected function setUp() {
28 // Delete the override config_test entity since it is not required by this
30 \Drupal::entityManager()->getStorage('config_test')->load('override')->delete();
31 $this->drupalPlaceBlock('local_actions_block');
35 * Tests entity list builder methods.
37 public function testList() {
38 $controller = \Drupal::entityManager()->getListBuilder('config_test');
40 // Test getStorage() method.
41 $this->assertTrue($controller->getStorage() instanceof EntityStorageInterface, 'EntityStorage instance in storage.');
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.');
52 // Test getOperations() method.
53 $expected_operations = [
57 'url' => $entity->urlInfo(),
60 'title' => t('Disable'),
62 'url' => $entity->urlInfo('disable'),
65 'title' => t('Delete'),
67 'url' => $entity->urlInfo('delete-form'),
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.');
76 // Test buildHeader() method.
79 'id' => 'Machine name',
80 'operations' => 'Operations',
82 $actual_items = $controller->buildHeader();
83 $this->assertEqual($expected_items, $actual_items, 'Return value from buildHeader matches expected.');
85 // Test buildRow() method.
86 $build_operations = $controller->buildOperations($entity);
89 'id' => 'dotted.default',
91 'data' => $build_operations,
94 $actual_items = $controller->buildRow($entity);
95 $this->assertEqual($expected_items, $actual_items, 'Return value from buildRow matches expected.');
97 $storage = $controller->getStorage();
98 $entity = $storage->create([
104 $entity = $storage->create([
110 $entity = $storage->create([
116 $list = $controller->load();
117 $this->assertIdentical(array_keys($list), ['beta', 'dotted.default', 'alpha', 'omega']);
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');
124 $list = $controller->load();
125 $entity = $list['default'];
127 // Test getOperations() method.
128 $expected_operations = [
130 'title' => t('Edit'),
132 'url' => $entity->urlInfo(),
135 'title' => t('Delete'),
137 'url' => $entity->urlInfo('delete-form'),
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.');
148 * Tests the listing UI.
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']));
154 // Get the list callback page.
155 $this->drupalGet('admin/structure/config_test');
157 // Test for the page title.
158 $this->assertTitle('Test configuration | Drupal');
160 // Test for the table.
161 $element = $this->xpath('//div[@class="layout-content"]//table');
162 $this->assertTrue($element, 'Configuration entity list table found.');
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.');
168 // Test the contents of each th cell.
169 $expected_items = ['Label', 'Machine name', 'Operations'];
170 foreach ($elements as $key => $element) {
171 $this->assertIdentical((string) $element[0], $expected_items[$key]);
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.');
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
181 $this->assertIdentical((string) $elements[0], 'Default');
182 $this->assertIdentical((string) $elements[1], 'dotted.default');
183 $this->assertTrue($elements[2]->children()->xpath('//ul'), 'Operations list found.');
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);
190 'label' => 'Antelope',
194 $this->drupalPostForm(NULL, $edit, t('Save'));
196 // Ensure that the entity's sort method was called.
197 $this->assertTrue(\Drupal::state()->get('config_entity_sort'), 'ConfigTest::sort() was called.');
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
202 $this->assertFieldByXpath('//td', 'Antelope', "Label found for added 'Antelope' entity.");
203 $this->assertFieldByXpath('//td', 'antelope', "Machine name found for added 'Antelope' entity.");
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'));
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
216 $this->assertFieldByXpath('//td', 'Albatross', "Label found for updated 'Albatross' entity.");
217 $this->assertFieldByXpath('//td', 'albatross', "Machine name found for updated 'Albatross' entity.");
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'));
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.");
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'));
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.");
242 // Confirm that the empty text is displayed.
243 $this->assertText('There is no Test configuration yet.');
249 public function testPager() {
250 $this->drupalLogin($this->drupalCreateUser(['administer site configuration']));
252 $storage = \Drupal::entityManager()->getListBuilder('config_test')->getStorage();
254 // Create 51 test entities.
255 for ($i = 1; $i < 52; $i++) {
257 'id' => str_pad($i, 2, '0', STR_PAD_LEFT),
258 'label' => 'Test config entity ' . $i,
260 'protected_property' => $i,
264 // Load the listing page.
265 $this->drupalGet('admin/structure/config_test');
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.');
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.');