DomainReferencesTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace Drupal\Tests\domain\Functional;
  3. /**
  4. * Tests behavior for hook_domain_references_alter().
  5. *
  6. * The module suite ships with two field types -- admin and editor. We want to
  7. * ensure that these are filtered properly by hook_domain_references_alter().
  8. *
  9. * @group domain
  10. */
  11. class DomainReferencesTest extends DomainTestBase {
  12. /**
  13. * Modules to enable.
  14. *
  15. * @var array
  16. */
  17. public static $modules = [
  18. 'domain',
  19. 'domain_access',
  20. 'field',
  21. 'field_ui',
  22. 'user',
  23. ];
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function setUp() {
  28. parent::setUp();
  29. // Create 5 domains.
  30. $this->domainCreateTestDomains(5);
  31. }
  32. /**
  33. * Basic test setup.
  34. */
  35. public function testDomainReferences() {
  36. // Create an admin user. This will be user 2.
  37. $admin = $this->drupalCreateUser([
  38. 'bypass node access',
  39. 'administer content types',
  40. 'administer users',
  41. 'administer domains',
  42. 'assign domain editors',
  43. ]);
  44. $this->drupalLogin($admin);
  45. $this->drupalGet('admin/people/create');
  46. $this->assertSession()->statusCodeEquals(200);
  47. // Create a user through the form. This will be user 3.
  48. $this->fillField('name', 'testuser');
  49. $this->fillField('mail', 'test@example.com');
  50. $this->fillField('pass[pass1]', 'test');
  51. $this->fillField('pass[pass2]', 'test');
  52. // We expect to find 5 domain options. We set three as selected.
  53. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  54. $ids = ['example_com', 'one_example_com', 'two_example_com'];
  55. $edit_ids = ['example_com', 'one_example_com'];
  56. foreach ($domains as $domain) {
  57. $locator = DOMAIN_ADMIN_FIELD . '[' . $domain->id() . ']';
  58. $this->findField($locator);
  59. if (in_array($domain->id(), $ids)) {
  60. $this->checkField($locator);
  61. }
  62. $locator = DOMAIN_ACCESS_FIELD . '[' . $domain->id() . ']';
  63. $this->findField($locator);
  64. if (in_array($domain->id(), $edit_ids)) {
  65. $this->checkField($locator);
  66. }
  67. }
  68. // Find the all affiliates field.
  69. $locator = DOMAIN_ACCESS_ALL_FIELD . '[value]';
  70. $this->findField($locator);
  71. // Save the form.
  72. $this->pressButton('edit-submit');
  73. $this->assertSession()->statusCodeEquals(200);
  74. // Load our test user.
  75. $storage = \Drupal::entityTypeManager()->getStorage('user');
  76. $testuser = $storage->load(3);
  77. // Check that three values are set.
  78. $manager = \Drupal::service('domain.element_manager');
  79. $values = $manager->getFieldValues($testuser, DOMAIN_ADMIN_FIELD);
  80. $this->assert(count($values) == 3, 'User saved with three domain admin records.');
  81. // Check that no access fields are set.
  82. $values = $manager->getFieldValues($testuser, DOMAIN_ACCESS_FIELD);
  83. $this->assert(count($values) == 2, 'User saved with two domain access records.');
  84. // Now login as a user with limited rights. This is user 4.
  85. $account = $this->drupalCreateUser([
  86. 'administer users',
  87. 'assign domain administrators',
  88. ]);
  89. // Set some domain assignments for this user.
  90. $ids = ['example_com', 'one_example_com'];
  91. $this->addDomainsToEntity('user', $account->id(), $ids, DOMAIN_ADMIN_FIELD);
  92. $limited_admin = $storage->load($account->id());
  93. $values = $manager->getFieldValues($limited_admin, DOMAIN_ADMIN_FIELD);
  94. $this->assert(count($values) == 2, 'User saved with two domain admin records.');
  95. // Check that no access fields are set.
  96. $values = $manager->getFieldValues($limited_admin, DOMAIN_ACCESS_FIELD);
  97. $this->assert(count($values) == 0, 'User saved with no domain access records.');
  98. // Now edit user 3 as user 4 with limited rights.
  99. $this->drupalLogin($account);
  100. $this->drupalGet('user/' . $testuser->id() . '/edit');
  101. $this->assertSession()->statusCodeEquals(200);
  102. foreach ($domains as $domain) {
  103. $locator = DOMAIN_ADMIN_FIELD . '[' . $domain->id() . ']';
  104. $this->findField($locator);
  105. if ($domain->id() == 'example_com') {
  106. $this->checkField($locator);
  107. }
  108. elseif ($domain->id() == 'one_example_com') {
  109. $this->uncheckField($locator);
  110. }
  111. else {
  112. $this->assertSession()->fieldNotExists($locator);
  113. }
  114. // No Domain Access field rights exist for this user.
  115. $locator = DOMAIN_ACCESS_FIELD . '[' . $domain->id() . ']';
  116. $this->assertSession()->fieldNotExists($locator);
  117. }
  118. // The all affiliates field should not be present..
  119. $locator = DOMAIN_ACCESS_ALL_FIELD . '[value]';
  120. $this->assertSession()->fieldNotExists($locator);
  121. // Save the form.
  122. $this->pressButton('edit-submit');
  123. $this->assertSession()->statusCodeEquals(200);
  124. // Now, check the user.
  125. $storage->resetCache([$testuser->id()]);
  126. $testuser = $storage->load($testuser->id());
  127. // Check that two values are set.
  128. $values = $manager->getFieldValues($testuser, DOMAIN_ADMIN_FIELD);
  129. $this->assert(count($values) == 2, 'User saved with two domain admin records.');
  130. // Check that no access fields are set.
  131. $values = $manager->getFieldValues($testuser, DOMAIN_ACCESS_FIELD);
  132. $this->assert(count($values) == 2, 'User saved with two domain access records.');
  133. // Now login as a user with different limited rights. This is user 5.
  134. $new_account = $this->drupalCreateUser([
  135. 'administer users',
  136. 'assign domain administrators',
  137. 'assign domain editors',
  138. ]);
  139. $ids = ['example_com', 'one_example_com'];
  140. $new_ids = ['one_example_com', 'four_example_com'];
  141. $this->addDomainsToEntity('user', $new_account->id(), $ids, DOMAIN_ADMIN_FIELD);
  142. $this->addDomainsToEntity('user', $new_account->id(), $new_ids, DOMAIN_ACCESS_FIELD);
  143. $new_admin = $storage->load($new_account->id());
  144. $values = $manager->getFieldValues($new_admin, DOMAIN_ADMIN_FIELD);
  145. $this->assert(count($values) == 2, 'User saved with two domain admin records.');
  146. $values = $manager->getFieldValues($new_admin, DOMAIN_ACCESS_FIELD);
  147. $this->assert(count($values) == 2, 'User saved with two domain access records.');
  148. // Now edit the user as someone with limited rights.
  149. $storage->resetCache([$new_admin->id()]);
  150. $this->drupalLogin($new_account);
  151. $this->drupalGet('user/' . $testuser->id() . '/edit');
  152. $this->assertSession()->statusCodeEquals(200);
  153. foreach ($domains as $domain) {
  154. $locator = DOMAIN_ADMIN_FIELD . '[' . $domain->id() . ']';
  155. $this->findField($locator);
  156. if ($domain->id() == 'example_com') {
  157. $this->checkField($locator);
  158. }
  159. elseif ($domain->id() == 'one_example_com') {
  160. $this->uncheckField($locator);
  161. }
  162. else {
  163. $this->assertSession()->fieldNotExists($locator);
  164. }
  165. // Some Domain Access field rights exist for this user. This adds
  166. // one to the count.
  167. $locator = DOMAIN_ACCESS_FIELD . '[' . $domain->id() . ']';
  168. if (in_array($domain->id(), $new_ids)) {
  169. $this->findField($locator);
  170. $this->checkField($locator);
  171. }
  172. else {
  173. $this->assertSession()->fieldNotExists($locator);
  174. }
  175. }
  176. // The all affiliates field should not be present..
  177. $locator = DOMAIN_ACCESS_ALL_FIELD . '[value]';
  178. $this->assertSession()->fieldNotExists($locator);
  179. // Save the form.
  180. $this->pressButton('edit-submit');
  181. $this->assertSession()->statusCodeEquals(200);
  182. // Now, check the user.
  183. $storage->resetCache([$testuser->id()]);
  184. $testuser = $storage->load($testuser->id());
  185. // Check that two values are set.
  186. $values = $manager->getFieldValues($testuser, DOMAIN_ADMIN_FIELD);
  187. $this->assert(count($values) == 2, 'User saved with two domain admin records.');
  188. $values = $manager->getFieldValues($testuser, DOMAIN_ACCESS_FIELD);
  189. $this->assert(count($values) == 3, 'User saved with three domain access records.');
  190. }
  191. }