DomainAccessElementTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Drupal\Tests\domain_access\Functional;
  3. use Drupal\Tests\domain\Functional\DomainTestBase;
  4. /**
  5. * Tests behavior for the domain access field element.
  6. *
  7. * @group domain_access
  8. */
  9. class DomainAccessElementTest extends DomainTestBase {
  10. /**
  11. * Modules to enable.
  12. *
  13. * @var array
  14. */
  15. public static $modules = [
  16. 'domain',
  17. 'domain_access',
  18. 'field',
  19. 'field_ui',
  20. 'user',
  21. ];
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function setUp() {
  26. parent::setUp();
  27. // Create 5 domains.
  28. $this->domainCreateTestDomains(5);
  29. }
  30. /**
  31. * Test runner.
  32. */
  33. public function testDomainAccessElement() {
  34. $this->runInstalledTest('article');
  35. $node_type = $this->createContentType(['type' => 'test']);
  36. $this->runInstalledTest('test');
  37. }
  38. /**
  39. * Basic test setup.
  40. */
  41. public function runInstalledTest($node_type) {
  42. $admin = $this->drupalCreateUser([
  43. 'bypass node access',
  44. 'administer content types',
  45. 'administer node fields',
  46. 'administer node display',
  47. 'administer domains',
  48. 'publish to any domain',
  49. ]);
  50. $this->drupalLogin($admin);
  51. $this->drupalGet('node/add/' . $node_type);
  52. $this->assertSession()->statusCodeEquals(200);
  53. // Set the title, so the node can be saved.
  54. $this->fillField('title[0][value]', 'Test node');
  55. // We expect to find 5 domain options. We set two as selected.
  56. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  57. $count = 0;
  58. $ids = ['example_com', 'one_example_com', 'two_example_com'];
  59. foreach ($domains as $domain) {
  60. $locator = DOMAIN_ACCESS_FIELD . '[' . $domain->id() . ']';
  61. $this->findField($locator);
  62. if (in_array($domain->id(), $ids)) {
  63. $this->checkField($locator);
  64. }
  65. }
  66. // Find the all affiliates field.
  67. $locator = DOMAIN_ACCESS_ALL_FIELD . '[value]';
  68. $this->findField($locator);
  69. // Set all affiliates to TRUE.
  70. $this->checkField($locator);
  71. // Save the form.
  72. $this->pressButton('edit-submit');
  73. $this->assertSession()->statusCodeEquals(200);
  74. // Get node data. Note that we create one new node for each test case.
  75. $storage = \Drupal::entityTypeManager()->getStorage('node');
  76. $nid = $node_type == 'article' ? 1 : 2;
  77. $node = $storage->load($nid);
  78. // Check that two values are set.
  79. $manager = \Drupal::service('domain_access.manager');
  80. $values = $manager->getAccessValues($node);
  81. $this->assert(count($values) == 3, 'Node saved with three domain records.');
  82. $value = $manager->getAllValue($node);
  83. $this->assert($value == 1, 'Node saved to all affiliates.');
  84. // Now login as a user with limited rights.
  85. $account = $this->drupalCreateUser([
  86. 'create ' . $node_type . ' content',
  87. 'edit any ' . $node_type . ' content',
  88. 'publish to any assigned domain',
  89. ]);
  90. $ids = ['example_com', 'one_example_com'];
  91. $this->addDomainsToEntity('user', $account->id(), $ids, DOMAIN_ACCESS_FIELD);
  92. $user_storage = \Drupal::entityTypeManager()->getStorage('user');
  93. $user = $user_storage->load($account->id());
  94. $values = $manager->getAccessValues($user);
  95. $this->assert(count($values) == 2, 'User saved with two domain records.');
  96. $value = $manager->getAllValue($user);
  97. $this->assert($value == 0, 'User not saved to all affiliates.');
  98. $this->drupalLogin($account);
  99. $this->drupalGet('node/' . $node->id() . '/edit');
  100. $this->assertSession()->statusCodeEquals(200);
  101. foreach ($domains as $domain) {
  102. $locator = DOMAIN_ACCESS_FIELD . '[' . $domain->id() . ']';
  103. $this->findField($locator);
  104. if ($domain->id() == 'example_com') {
  105. $this->checkField($locator);
  106. }
  107. elseif ($domain->id() == 'one_example_com') {
  108. $this->uncheckField($locator);
  109. }
  110. else {
  111. $this->assertSession()->fieldNotExists($locator);
  112. }
  113. }
  114. $locator = DOMAIN_ACCESS_ALL_FIELD . '[value]';
  115. $this->assertSession()->fieldNotExists($locator);
  116. // Save the form.
  117. $this->pressButton('edit-submit');
  118. $this->assertSession()->statusCodeEquals(200);
  119. // Now, check the node.
  120. $storage->resetCache([$node->id()]);
  121. $node = $storage->load($node->id());
  122. // Check that two values are set.
  123. $values = $manager->getAccessValues($node);
  124. $this->assert(count($values) == 2, 'Node saved with two domain records.');
  125. $value = $manager->getAllValue($node);
  126. $this->assert($value == 1, 'Node saved to all affiliates.');
  127. }
  128. }