DomainAccessFieldTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace Drupal\Tests\domain_access\Functional;
  3. use Drupal\node\Entity\NodeType;
  4. use Drupal\Tests\domain\Functional\DomainTestBase;
  5. /**
  6. * Tests the domain access entity reference field type.
  7. *
  8. * @group domain_access
  9. */
  10. class DomainAccessFieldTest extends DomainTestBase {
  11. /**
  12. * Modules to enable.
  13. *
  14. * @var array
  15. */
  16. public static $modules = [
  17. 'domain',
  18. 'domain_access',
  19. 'field',
  20. 'field_ui',
  21. 'user',
  22. ];
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function setUp() {
  27. parent::setUp();
  28. // Create 5 domains.
  29. $this->domainCreateTestDomains(5);
  30. }
  31. /**
  32. * Tests that the fields are accessed properly.
  33. */
  34. public function testDomainAccessFields() {
  35. $label = 'Send to all affiliates';
  36. // Test a user who can access all domain settings.
  37. $user1 = $this->drupalCreateUser(['create article content', 'publish to any domain']);
  38. $this->drupalLogin($user1);
  39. // Visit the article creation page.
  40. $this->drupalGet('node/add/article');
  41. $this->assertResponse(200, 'Article creation found.');
  42. // Check for the form options.
  43. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  44. foreach ($domains as $domain) {
  45. $this->assertText($domain->label(), 'Domain form item found.');
  46. }
  47. $this->assertText($label, 'All affiliates field found.');
  48. // Test a user who can access some domain settings.
  49. $user2 = $this->drupalCreateUser(['create article content', 'publish to any assigned domain']);
  50. $active_domain = array_rand($domains, 1);
  51. $this->addDomainsToEntity('user', $user2->id(), $active_domain, DOMAIN_ACCESS_FIELD);
  52. $this->drupalLogin($user2);
  53. // Visit the article creation page.
  54. $this->drupalGet('node/add/article');
  55. $this->assertResponse(200, 'Article creation found.');
  56. // Check for the form options.
  57. foreach ($domains as $domain) {
  58. if ($domain->id() == $active_domain) {
  59. $this->assertRaw('>' . $domain->label() . '</label>', 'Domain form item found.');
  60. }
  61. else {
  62. $this->assertNoRaw('>' . $domain->label() . '</label>', 'Domain form item not found.');
  63. }
  64. }
  65. $this->assertNoText($label, 'All affiliates field not found.');
  66. // Test a user who can access no domain settings.
  67. $user3 = $this->drupalCreateUser(['create article content']);
  68. $this->drupalLogin($user3);
  69. // Visit the article creation page.
  70. $this->drupalGet('node/add/article');
  71. $this->assertResponse(200, 'Article creation found.');
  72. // Check for the form options.
  73. foreach ($domains as $domain) {
  74. $this->assertNoText($domain->label(), 'Domain form item not found.');
  75. }
  76. $this->assertNoText($label, 'All affiliates field not found.');
  77. // Attempt saving the node.
  78. // The domain/domain affiliates fields are not accessible to this user.
  79. // The save will fail with an EntityStorageException until
  80. // https://www.drupal.org/node/2609252 is fixed.
  81. $edit = [];
  82. $edit['title[0][value]'] = $this->randomMachineName(8);
  83. $edit['body[0][value]'] = $this->randomMachineName(16);
  84. $this->drupalPostForm('node/add/article', $edit, t('Save'));
  85. // Check that the node exists in the database.
  86. $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
  87. $this->assertTrue($node, 'Node found in database.');
  88. // Test a user who can assign users to domains.
  89. $user4 = $this->drupalCreateUser(['administer users', 'assign editors to any domain']);
  90. $this->drupalLogin($user4);
  91. // Visit the account creation page.
  92. $this->drupalGet('admin/people/create');
  93. $this->assertResponse(200, 'User creation found.');
  94. // Check for the form options.
  95. foreach ($domains as $domain) {
  96. $this->assertText($domain->label(), 'Domain form item found.');
  97. }
  98. // Test a user who can assign users to some domains.
  99. $user5 = $this->drupalCreateUser(['administer users', 'assign domain editors']);
  100. $active_domain = array_rand($domains, 1);
  101. $this->addDomainsToEntity('user', $user5->id(), $active_domain, DOMAIN_ACCESS_FIELD);
  102. $this->drupalLogin($user5);
  103. // Visit the account creation page.
  104. $this->drupalGet('admin/people/create');
  105. $this->assertResponse(200, 'User creation found.');
  106. // Check for the form options.
  107. foreach ($domains as $domain) {
  108. if ($domain->id() == $active_domain) {
  109. $this->assertRaw('>' . $domain->label() . '</label>', 'Domain form item found.');
  110. }
  111. else {
  112. $this->assertNoRaw('>' . $domain->label() . '</label>', 'Domain form item not found.');
  113. }
  114. }
  115. // Test a user who can access no domain settings.
  116. $user6 = $this->drupalCreateUser(['administer users']);
  117. $this->drupalLogin($user6);
  118. // Visit the account creation page.
  119. $this->drupalGet('admin/people/create');
  120. $this->assertResponse(200, 'User creation found.');
  121. // Check for the form options.
  122. foreach ($domains as $domain) {
  123. $this->assertNoText($domain->label(), 'Domain form item not found.');
  124. }
  125. // Test a user who can access all domain settings.
  126. $user7 = $this->drupalCreateUser(['bypass node access', 'publish to any domain']);
  127. $this->drupalLogin($user7);
  128. // Create a new content type and test that the fields are created.
  129. // Create a content type programmatically.
  130. $type = $this->drupalCreateContentType();
  131. $type_exists = (bool) NodeType::load($type->id());
  132. $this->assertTrue($type_exists, 'The new content type has been created in the database.');
  133. // The test is not passing to domain_access_node_type_insert() properly.
  134. domain_access_confirm_fields('node', $type->id());
  135. // Visit the article creation page.
  136. $this->drupalGet('node/add/' . $type->id());
  137. $this->assertResponse(200, $type->id() . ' creation found.');
  138. // Check for the form options.
  139. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  140. foreach ($domains as $domain) {
  141. $this->assertText($domain->label(), 'Domain form item found.');
  142. }
  143. $this->assertText($label, 'All affiliates field found.');
  144. // Test user without access to affiliates field editing their user page.
  145. $user8 = $this->drupalCreateUser(['change own username']);
  146. $this->drupalLogin($user8);
  147. $user_edit_page = 'user/' . $user8->id() . '/edit';
  148. $this->drupalGet($user_edit_page);
  149. // Check for the form options.
  150. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  151. foreach ($domains as $domain) {
  152. $this->assertNoText($domain->label(), 'Domain form item not found.');
  153. }
  154. $this->assertNoText($label, 'All affiliates field not found.');
  155. // Change own username.
  156. $edit = [];
  157. $edit['name'] = $this->randomMachineName();
  158. $this->drupalPostForm($user_edit_page, $edit, t('Save'));
  159. }
  160. }