DomainAdminElementTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Drupal\Tests\domain\Functional;
  3. /**
  4. * Tests behavior for the domain admin field element.
  5. *
  6. * @group domain
  7. */
  8. class DomainAdminElementTest extends DomainTestBase {
  9. /**
  10. * Modules to enable.
  11. *
  12. * @var array
  13. */
  14. public static $modules = ['domain', 'field', 'field_ui', 'user'];
  15. /**
  16. * {@inheritdoc}
  17. */
  18. protected function setUp() {
  19. parent::setUp();
  20. // Create 5 domains.
  21. $this->domainCreateTestDomains(5);
  22. }
  23. /**
  24. * Basic test setup.
  25. */
  26. public function testDomainAccessElement() {
  27. $admin = $this->drupalCreateUser([
  28. 'bypass node access',
  29. 'administer content types',
  30. 'administer users',
  31. 'administer domains',
  32. ]);
  33. $this->drupalLogin($admin);
  34. $this->drupalGet('admin/people/create');
  35. $this->assertSession()->statusCodeEquals(200);
  36. // Create a user through the form.
  37. $this->fillField('name', 'testuser');
  38. $this->fillField('mail', 'test@example.com');
  39. $this->fillField('pass[pass1]', 'test');
  40. $this->fillField('pass[pass2]', 'test');
  41. // We expect to find 5 domain options. We set two as selected.
  42. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  43. $count = 0;
  44. $ids = ['example_com', 'one_example_com', 'two_example_com'];
  45. foreach ($domains as $domain) {
  46. $locator = DOMAIN_ADMIN_FIELD . '[' . $domain->id() . ']';
  47. $this->findField($locator);
  48. if (in_array($domain->id(), $ids)) {
  49. $this->checkField($locator);
  50. }
  51. }
  52. // Save the form.
  53. $this->pressButton('edit-submit');
  54. $this->assertSession()->statusCodeEquals(200);
  55. $storage = \Drupal::entityTypeManager()->getStorage('user');
  56. $user = $storage->load(3);
  57. // Check that two values are set.
  58. $manager = \Drupal::service('domain.element_manager');
  59. $values = $manager->getFieldValues($user, DOMAIN_ADMIN_FIELD);
  60. $this->assert(count($values) == 3, 'User saved with three domain records.');
  61. // Now login as a user with limited rights.
  62. $account = $this->drupalCreateUser([
  63. 'administer users',
  64. 'assign domain administrators',
  65. ]);
  66. $ids = ['example_com', 'one_example_com'];
  67. $this->addDomainsToEntity('user', $account->id(), $ids, DOMAIN_ADMIN_FIELD);
  68. $tester = $storage->load($account->id());
  69. $values = $manager->getFieldValues($tester, DOMAIN_ADMIN_FIELD);
  70. $this->assert(count($values) == 2, 'User saved with two domain records.');
  71. $storage->resetCache([$account->id()]);
  72. $this->drupalLogin($account);
  73. $this->drupalGet('user/' . $user->id() . '/edit');
  74. $this->assertSession()->statusCodeEquals(200);
  75. foreach ($domains as $domain) {
  76. $locator = DOMAIN_ADMIN_FIELD . '[' . $domain->id() . ']';
  77. $this->findField($locator);
  78. if ($domain->id() == 'example_com') {
  79. $this->checkField($locator);
  80. }
  81. elseif ($domain->id() == 'one_example_com') {
  82. $this->uncheckField($locator);
  83. }
  84. else {
  85. $this->assertSession()->fieldNotExists($locator);
  86. }
  87. }
  88. // Save the form.
  89. $this->pressButton('edit-submit');
  90. $this->assertSession()->statusCodeEquals(200);
  91. // Now, check the user.
  92. $storage->resetCache([$user->id()]);
  93. $user = $storage->load($user->id());
  94. // Check that two values are set.
  95. $values = $manager->getFieldValues($user, DOMAIN_ADMIN_FIELD);
  96. $this->assert(count($values) == 2, 'User saved with two domain records.');
  97. // Test the case presented in https://www.drupal.org/node/2841962.
  98. $config = \Drupal::configFactory()->getEditable('user.settings');
  99. $config->set('verify_mail', 0);
  100. $config->set('register', USER_REGISTER_VISITORS);
  101. $config->save();
  102. $this->drupalLogout();
  103. $this->drupalGet('user/register');
  104. $this->assertSession()->statusCodeEquals(200);
  105. $this->assertSession()->responseNotContains('Domain administrator');
  106. foreach ($domains as $domain) {
  107. $locator = DOMAIN_ADMIN_FIELD . '[' . $domain->id() . ']';
  108. $this->assertSession()->fieldNotExists($locator);
  109. }
  110. // Create a user through the form.
  111. $this->fillField('name', 'testuser2');
  112. $this->fillField('mail', 'test2@example.com');
  113. // In 8.3, this field is not present?
  114. if (!empty($this->findField('pass[pass1]'))) {
  115. $this->fillField('pass[pass1]', 'test');
  116. $this->fillField('pass[pass2]', 'test');
  117. }
  118. // Save the form.
  119. $this->pressButton('edit-submit');
  120. $this->assertSession()->statusCodeEquals(200);
  121. }
  122. }