DomainListBuilderTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace Drupal\Tests\domain\Functional;
  3. /**
  4. * Tests behavior for the domain list builder.
  5. *
  6. * @group domain
  7. */
  8. class DomainListBuilderTest extends DomainTestBase {
  9. /**
  10. * Modules to enable.
  11. *
  12. * @var array
  13. */
  14. public static $modules = ['domain', 'user'];
  15. /**
  16. * {@inheritdoc}
  17. */
  18. protected function setUp() {
  19. parent::setUp();
  20. // Create 150 domains.
  21. $this->domainCreateTestDomains(150);
  22. }
  23. /**
  24. * Basic test setup.
  25. */
  26. public function testDomainListBuilder() {
  27. $admin = $this->drupalCreateUser([
  28. 'bypass node access',
  29. 'administer content types',
  30. 'administer node fields',
  31. 'administer node display',
  32. 'administer domains',
  33. ]);
  34. $this->drupalLogin($admin);
  35. $this->drupalGet('admin/config/domain');
  36. $this->assertSession()->statusCodeEquals(200);
  37. // Check that links are printed.
  38. foreach ($this->getPaginatedDomains() as $domain) {
  39. $href = 'admin/config/domain/edit/' . $domain->id();
  40. $this->assertSession()->linkByHrefExists($href, 0, 'Link found ' . $href);
  41. $this->assertSession()->assertEscaped($domain->label());
  42. // Check for pagination.
  43. $this->checkPagination();
  44. }
  45. // Go to page 2.
  46. $this->clickLink('Next');
  47. foreach ($this->getPaginatedDomains(1) as $domain) {
  48. $href = 'admin/config/domain/edit/' . $domain->id();
  49. $this->assertSession()->linkByHrefExists($href, 0, 'Link found ' . $href);
  50. $this->assertSession()->assertEscaped($domain->label());
  51. // Check for pagination.
  52. $this->checkPagination();
  53. }
  54. // Now login as a user with limited rights.
  55. $account = $this->drupalCreateUser([
  56. 'create article content',
  57. 'edit any article content',
  58. 'edit assigned domains',
  59. 'view domain list',
  60. ]);
  61. $ids = ['example_com', 'one_example_com'];
  62. $this->addDomainsToEntity('user', $account->id(), $ids, DOMAIN_ADMIN_FIELD);
  63. $user_storage = \Drupal::entityTypeManager()->getStorage('user');
  64. $user = $user_storage->load($account->id());
  65. $manager = \Drupal::service('domain.element_manager');
  66. $values = $manager->getFieldValues($user, DOMAIN_ADMIN_FIELD);
  67. $this->assert(count($values) == 2, 'User saved with two domain records.');
  68. $this->drupalLogin($account);
  69. $this->drupalGet('admin/config/domain');
  70. $this->assertSession()->statusCodeEquals(200);
  71. // Check that links are printed.
  72. $path = 'admin/config/domain';
  73. $this->drupalGet($path);
  74. foreach ($this->getPaginatedDomains() as $domain) {
  75. $href = 'admin/config/domain/edit/' . $domain->id();
  76. if (in_array($domain->id(), $ids)) {
  77. $this->assertSession()->linkByHrefExists($href, 0, 'Link found');
  78. $this->assertSession()->assertEscaped($domain->label());
  79. }
  80. else {
  81. $this->assertSession()->linkByHrefNotExists($href, 'Link not found');
  82. $this->assertSession()->assertEscaped($domain->label());
  83. }
  84. // Check for pagination.
  85. $this->checkPagination();
  86. }
  87. // Check access to the pages/routes.
  88. foreach ($this->getPaginatedDomains() as $domain) {
  89. $path = 'admin/config/domain/edit/' . $domain->id();
  90. $this->drupalGet($path);
  91. if (in_array($domain->id(), $ids)) {
  92. $this->assertSession()->statusCodeEquals(200);
  93. }
  94. else {
  95. $this->assertSession()->statusCodeEquals(403);
  96. }
  97. }
  98. // Go to page 2.
  99. $this->drupalGet('admin/config/domain');
  100. $this->clickLink('Next');
  101. foreach ($this->getPaginatedDomains(1) as $domain) {
  102. $href = 'admin/config/domain/edit/' . $domain->id();
  103. if (in_array($domain->id(), $ids)) {
  104. $this->assertSession()->linkByHrefExists($href, 0, 'Link found');
  105. $this->assertSession()->assertEscaped($domain->label());
  106. }
  107. else {
  108. $this->assertSession()->linkByHrefNotExists($href, 'Link not found');
  109. $this->assertSession()->assertEscaped($domain->label());
  110. }
  111. // Check for pagination.
  112. $this->checkPagination();
  113. }
  114. // Now login as a user with more limited rights.
  115. $account2 = $this->drupalCreateUser([
  116. 'create article content',
  117. 'edit any article content',
  118. 'edit assigned domains',
  119. 'view assigned domains',
  120. ]);
  121. $ids = ['example_com', 'one_example_com'];
  122. $this->addDomainsToEntity('user', $account2->id(), $ids, DOMAIN_ADMIN_FIELD);
  123. $user_storage = \Drupal::entityTypeManager()->getStorage('user');
  124. $user = $user_storage->load($account2->id());
  125. $manager = \Drupal::service('domain.element_manager');
  126. $values = $manager->getFieldValues($user, DOMAIN_ADMIN_FIELD);
  127. $this->assert(count($values) == 2, 'User saved with two domain records.');
  128. $this->drupalLogin($account2);
  129. $this->drupalGet('admin/config/domain');
  130. $this->assertSession()->statusCodeEquals(200);
  131. // Check that domains are listed and links are printed.
  132. $path = 'admin/config/domain';
  133. $this->drupalGet($path);
  134. foreach ($this->getPaginatedDomains() as $domain) {
  135. $href = 'admin/config/domain/edit/' . $domain->id();
  136. if (in_array($domain->id(), $ids)) {
  137. $this->assertSession()->linkByHrefExists($href, 0, 'Link found');
  138. $this->assertSession()->assertEscaped($domain->label());
  139. }
  140. else {
  141. $this->assertSession()->linkByHrefNotExists($href, 'Link not found');
  142. $this->assertSession()->assertNoEscaped($domain->label());
  143. }
  144. // Check for pagination.
  145. $this->checkNoPagination();
  146. }
  147. // Check access to the pages/routes.
  148. foreach ($this->getPaginatedDomains() as $domain) {
  149. $path = 'admin/config/domain/edit/' . $domain->id();
  150. $this->drupalGet($path);
  151. if (in_array($domain->id(), $ids)) {
  152. $this->assertSession()->statusCodeEquals(200);
  153. }
  154. else {
  155. $this->assertSession()->statusCodeEquals(403);
  156. }
  157. }
  158. }
  159. /**
  160. * Returns an array of domains, paginated and sorted by weight.
  161. *
  162. * @param int $page
  163. * The page number to return.
  164. */
  165. private function getPaginatedDomains($page = 0) {
  166. $limit = 50;
  167. $offset = $page * $limit;
  168. return array_slice($this->getDomainsSorted(), $offset, $limit);
  169. }
  170. /**
  171. * Checks that pagination links appear, as expected.
  172. */
  173. private function checkPagination() {
  174. foreach (['?page=0', '?page=1', '?page=2'] as $href) {
  175. $this->assertSession()->linkByHrefExists($href, 0, 'Link found');
  176. }
  177. }
  178. /**
  179. * Checks that pagination links do not appear, as expected.
  180. */
  181. private function checkNoPagination() {
  182. foreach (['?page=0', '?page=1', '?page=2'] as $href) {
  183. $this->assertSession()->linkByHrefNotExists($href, 0, 'Link not found');
  184. }
  185. }
  186. }