DomainAccessDefaultValueTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Drupal\Tests\domain_access\Functional;
  3. use Drupal\Tests\domain\Functional\DomainTestBase;
  4. /**
  5. * Tests the domain access handling of default field values.
  6. *
  7. * @see https://www.drupal.org/node/2779133
  8. *
  9. * @group domain_access
  10. */
  11. class DomainAccessDefaultValueTest extends DomainTestBase {
  12. /**
  13. * Modules to enable.
  14. *
  15. * @var array
  16. */
  17. public static $modules = ['domain', 'domain_access', 'field', 'field_ui'];
  18. /**
  19. * Test the usage of DomainAccessManager::getDefaultValue().
  20. */
  21. public function testDomainAccessDefaultValue() {
  22. $this->admin_user = $this->drupalCreateUser([
  23. 'bypass node access',
  24. 'administer content types',
  25. 'administer node fields',
  26. 'administer node display',
  27. 'administer domains',
  28. 'publish to any domain',
  29. ]);
  30. $this->drupalLogin($this->admin_user);
  31. // Create 5 domains.
  32. $this->domainCreateTestDomains(5);
  33. // Visit the article field display administration page.
  34. $this->drupalGet('node/add/article');
  35. $this->assertResponse(200);
  36. // Check the new field exists on the page.
  37. $this->assertText('Domain Access', 'Found the domain field instance.');
  38. $this->assertRaw('name="field_domain_access[example_com]" value="example_com" checked="checked"', 'Default domain selected.');
  39. // Check the all affiliates field.
  40. $this->assertText('Send to all affiliates', 'Found the all affiliates field instance.');
  41. $this->assertNoRaw('name="field_domain_all_affiliates[value]" value="1" checked="checked"', 'All affiliates not selected.');
  42. // Now save the node with the values set.
  43. $edit = [
  44. 'title[0][value]' => 'Test node',
  45. 'field_domain_access[example_com]' => 'example_com',
  46. ];
  47. $this->drupalPostForm('node/add/article', $edit, 'Save');
  48. // Load the node.
  49. $node = \Drupal::entityTypeManager()->getStorage('node')->load(1);
  50. $this->assertTrue($node, 'Article node created.');
  51. // Check that the values are set.
  52. $values = \Drupal::service('domain_access.manager')->getAccessValues($node);
  53. $this->assertTrue(count($values) == 1, 'Node saved with one domain record.');
  54. $allValue = \Drupal::service('domain_access.manager')->getAllValue($node);
  55. $this->assertTrue(empty($allValue), 'Not sent to all affiliates.');
  56. // Logout the admin user.
  57. $this->drupalLogout();
  58. // Create a limited value user.
  59. $this->test_user = $this->drupalCreateUser([
  60. 'create article content',
  61. 'edit any article content',
  62. ]);
  63. // Login and try to edit the created node.
  64. $this->drupalLogin($this->test_user);
  65. $this->drupalGet('node/1/edit');
  66. $this->assertResponse(200);
  67. // Now save the node with the values set.
  68. $edit = [
  69. 'title[0][value]' => 'Test node update',
  70. ];
  71. $this->drupalPostForm('node/1/edit', $edit, 'Save');
  72. // Load the node.
  73. $node = \Drupal::entityTypeManager()->getStorage('node')->load(1);
  74. $this->assertTrue($node, 'Article node created.');
  75. // Check that the values are set.
  76. $values = \Drupal::service('domain_access.manager')->getAccessValues($node);
  77. $this->assertTrue(count($values) == 1, 'Node saved with one domain record.');
  78. $allValue = \Drupal::service('domain_access.manager')->getAllValue($node);
  79. $this->assertTrue(empty($allValue), 'Not sent to all affiliates.');
  80. }
  81. }