DomainEntityReferenceTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace Drupal\Tests\domain\Functional;
  3. /**
  4. * Tests the domain record entity reference field type.
  5. *
  6. * @group domain
  7. */
  8. class DomainEntityReferenceTest extends DomainTestBase {
  9. /**
  10. * Modules to enable.
  11. *
  12. * @var array
  13. */
  14. public static $modules = ['domain', 'field', 'field_ui'];
  15. /**
  16. * Create, edit and delete a domain field via the user interface.
  17. */
  18. public function testDomainField() {
  19. $this->admin_user = $this->drupalCreateUser([
  20. 'administer content types',
  21. 'administer node fields',
  22. 'administer node display',
  23. 'administer domains',
  24. ]);
  25. $this->drupalLogin($this->admin_user);
  26. // Visit the article field administration page.
  27. $this->drupalGet('admin/structure/types/manage/article/fields');
  28. $this->assertResponse(200, 'Manage fields page accessed.');
  29. // Check for a domain field.
  30. $this->assertNoText('Domain test field', 'Domain form field not found.');
  31. // Visit the article field display administration page.
  32. $this->drupalGet('admin/structure/types/manage/article/display');
  33. $this->assertResponse(200, 'Manage field display page accessed.');
  34. // Check for a domain field.
  35. $this->assertNoText('Domain test field', 'Domain form field not found.');
  36. // Create test domain field.
  37. $this->domainCreateTestField();
  38. // Visit the article field administration page.
  39. $this->drupalGet('admin/structure/types/manage/article/fields');
  40. // Check the new field.
  41. $this->assertText('Domain test field', 'Added a test field instance.');
  42. // Visit the article field display administration page.
  43. $this->drupalGet('admin/structure/types/manage/article/display');
  44. // Check the new field.
  45. $this->assertText('Domain test field', 'Added a test field display instance.');
  46. }
  47. /**
  48. * Create content for a domain field.
  49. */
  50. public function testDomainFieldStorage() {
  51. $this->admin_user = $this->drupalCreateUser([
  52. 'bypass node access',
  53. 'administer content types',
  54. 'administer node fields',
  55. 'administer node display',
  56. 'administer domains',
  57. ]);
  58. $this->drupalLogin($this->admin_user);
  59. // Create test domain field.
  60. $this->domainCreateTestField();
  61. // Create 5 domains.
  62. $this->domainCreateTestDomains(5);
  63. // Visit the article field display administration page.
  64. $this->drupalGet('node/add/article');
  65. $this->assertResponse(200);
  66. // Check the new field exists on the page.
  67. $this->assertText('Domain test field', 'Found the domain field instance.');
  68. // We expect to find 5 domain options.
  69. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  70. foreach ($domains as $domain) {
  71. $string = 'value="' . $domain->id() . '"';
  72. $this->assertRaw($string, 'Found the domain option');
  73. if (!isset($one)) {
  74. $one = $domain->id();
  75. continue;
  76. }
  77. if (!isset($two)) {
  78. $two = $domain->id();
  79. }
  80. }
  81. // Try to post a node, assigned to the first two domains.
  82. $edit['title[0][value]'] = 'Test node';
  83. $edit["field_domain[{$one}]"] = TRUE;
  84. $edit["field_domain[{$two}]"] = TRUE;
  85. $this->drupalPostForm('node/add/article', $edit, 'Save');
  86. $this->assertResponse(200);
  87. $node = \Drupal::entityTypeManager()->getStorage('node')->load(1);
  88. $values = $node->get('field_domain');
  89. // Get the expected value count.
  90. $this->assertTrue(count($values) == 2, 'Node saved with two domain records.');
  91. }
  92. /**
  93. * Creates a simple field for testing on the article content type.
  94. *
  95. * Note: This code is a model for auto-creation of fields.
  96. */
  97. public function domainCreateTestField() {
  98. $label = 'domain';
  99. $name = 'field_' . $label;
  100. $storage = [
  101. 'field_name' => $name,
  102. 'entity_type' => 'node',
  103. 'type' => 'entity_reference',
  104. 'cardinality' => -1,
  105. 'settings' => [
  106. 'target_type' => 'domain',
  107. ],
  108. ];
  109. $field_storage_config = \Drupal::entityTypeManager()->getStorage('field_storage_config')->create($storage);
  110. $field_storage_config->save();
  111. $field = [
  112. 'field_name' => $name,
  113. 'entity_type' => 'node',
  114. 'label' => 'Domain test field',
  115. 'bundle' => 'article',
  116. 'settings' => [
  117. 'handler_settings' => [
  118. 'sort' => ['field' => 'weight', 'direction' => 'ASC'],
  119. ],
  120. ],
  121. ];
  122. $field_config = \Drupal::entityTypeManager()->getStorage('field_config')->create($field);
  123. $field_config->save();
  124. // Tell the form system how to behave.
  125. if ($display = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load('node.article.default')) {
  126. $display->setComponent($name, ['type' => 'options_buttons'])->save();
  127. }
  128. }
  129. }