DomainSourceElementTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Drupal\Tests\domain_source\Functional;
  3. use Drupal\Tests\domain\Functional\DomainTestBase;
  4. /**
  5. * Tests behavior for the domain source field element.
  6. *
  7. * @group domain_source
  8. */
  9. class DomainSourceElementTest extends DomainTestBase {
  10. /**
  11. * Modules to enable.
  12. *
  13. * @var array
  14. */
  15. public static $modules = [
  16. 'domain',
  17. 'domain_access',
  18. 'domain_source',
  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. * Test runner.
  33. */
  34. public function testDomainSourceElement() {
  35. $this->runInstalledTest('article');
  36. $node_type = $this->createContentType(['type' => 'test']);
  37. $this->runInstalledTest('test');
  38. }
  39. /**
  40. * Basic test setup.
  41. */
  42. public function runInstalledTest($node_type) {
  43. $admin = $this->drupalCreateUser([
  44. 'bypass node access',
  45. 'administer content types',
  46. 'administer node fields',
  47. 'administer node display',
  48. 'administer domains',
  49. 'publish to any domain',
  50. ]);
  51. $this->drupalLogin($admin);
  52. $this->drupalGet('node/add/article');
  53. $this->assertSession()->statusCodeEquals(200);
  54. $nid = $node_type == 'article' ? 1 : 2;
  55. // Set the title, so the node can be saved.
  56. $this->fillField('title[0][value]', 'Test node');
  57. // We expect to find 5 domain options. We set two as selected.
  58. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  59. $count = 0;
  60. $ids = ['example_com', 'one_example_com', 'two_example_com'];
  61. foreach ($domains as $domain) {
  62. $locator = DOMAIN_ACCESS_FIELD . '[' . $domain->id() . ']';
  63. $this->findField($locator);
  64. if (in_array($domain->id(), $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. // Set all affiliates to TRUE.
  72. $this->checkField($locator);
  73. // Find the Domain Source field.
  74. $locator = DOMAIN_SOURCE_FIELD;
  75. $this->findField($locator);
  76. // Set it to one_example_com.
  77. $this->selectFieldOption($locator, 'one_example_com');
  78. // Save the form.
  79. $this->pressButton('edit-submit');
  80. $this->assertSession()->statusCodeEquals(200);
  81. // Check the URL.
  82. $url = $this->geturl();
  83. $this->assert(strpos($url, 'node/' . $nid . '/edit') === FALSE, 'Form submitted.');
  84. // Edit the node.
  85. $this->drupalGet('node/' . $nid . '/edit');
  86. $this->assertSession()->statusCodeEquals(200);
  87. // Set the domain source field to an unselected domain.
  88. $this->selectFieldOption($locator, 'three_example_com');
  89. // Save the form.
  90. $this->pressButton('edit-submit');
  91. $this->assertSession()->assertEscaped('The source domain must be selected as a publishing option.');
  92. // Check the URL.
  93. $url = $this->geturl();
  94. $this->assert(strpos($url, 'node/' . $nid . '/edit') > 0, 'Form not submitted.');
  95. // Set the field properly and save again.
  96. $this->selectFieldOption($locator, 'one_example_com');
  97. // Save the form.
  98. $this->pressButton('edit-submit');
  99. $this->assertSession()->statusCodeEquals(200);
  100. // Check the URL.
  101. $url = $this->geturl();
  102. $this->assert(strpos($url, 'node/' . $nid . '/edit') === FALSE, 'Form submitted.');
  103. // Save with no source.
  104. // Edit the node.
  105. $this->drupalGet('node/1/edit');
  106. $this->assertSession()->statusCodeEquals(200);
  107. // Set the domain source field to an unselected domain.
  108. $this->selectFieldOption($locator, '_none');
  109. // Save the form.
  110. $this->pressButton('edit-submit');
  111. $this->assertSession()->statusCodeEquals(200);
  112. // Check the URL.
  113. $url = $this->geturl();
  114. $this->assert(strpos($url, 'node/' . $nid . '/edit') === FALSE, 'Form submitted.');
  115. }
  116. /**
  117. * Test for https://www.drupal.org/project/domain/issues/3010256.
  118. */
  119. public function testAnonForm() {
  120. // Editor with no domain permissions should not see the element.
  121. $editor = $this->drupalCreateUser([
  122. 'create article content',
  123. ]);
  124. $this->drupalLogin($editor);
  125. $this->drupalGet('node/add/article');
  126. $this->assertSession()->statusCodeEquals(200);
  127. $locator = DOMAIN_SOURCE_FIELD;
  128. $this->assertSession()->fieldNotExists($locator);
  129. // Editor with domain permissions should see the element once they
  130. // are assigned to domains.
  131. $editor2 = $this->drupalCreateUser([
  132. 'create article content',
  133. 'publish to any assigned domain',
  134. ]);
  135. $this->drupalLogin($editor2);
  136. $this->drupalGet('node/add/article');
  137. $this->assertSession()->statusCodeEquals(200);
  138. $locator = DOMAIN_SOURCE_FIELD;
  139. $this->assertSession()->fieldNotExists($locator);
  140. // Domain assignment.
  141. $ids = ['example_com', 'one_example_com'];
  142. $this->addDomainsToEntity('user', $editor2->id(), $ids, DOMAIN_ACCESS_FIELD);
  143. $this->drupalGet('node/add/article');
  144. $this->assertSession()->statusCodeEquals(200);
  145. $this->assertSession()->fieldExists($locator);
  146. }
  147. }