PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/config/src/Tests/ConfigEntityListTest.php

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