DomainTestTrait.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Drupal\Tests\domain\Traits;
  3. /**
  4. * Contains helper classes for tests to set up various configuration.
  5. */
  6. trait DomainTestTrait {
  7. /**
  8. * Generates a list of domains for testing.
  9. *
  10. * In my environment, I use the example.com hostname as a base. Then I name
  11. * hostnames one.* two.* up to ten. Note that we always use *_example_com
  12. * for the machine_name (entity id) value, though the hostname can vary
  13. * based on the system. This naming allows us to load test schema files.
  14. *
  15. * The script may also add test1, test2, test3 up to any number to test a
  16. * large number of domains.
  17. *
  18. * @param int $count
  19. * The number of domains to create.
  20. * @param string|null $base_hostname
  21. * The root domain to use for domain creation (e.g. example.com).
  22. * @param array $list
  23. * An optional list of subdomains to apply instead of the default set.
  24. */
  25. public function domainCreateTestDomains($count = 1, $base_hostname = NULL, array $list = []) {
  26. $original_domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple(NULL, TRUE);
  27. if (empty($base_hostname)) {
  28. $base_hostname = $this->baseHostname;
  29. }
  30. // Note: these domains are rigged to work on my test server.
  31. // For proper testing, yours should be set up similarly, but you can pass a
  32. // $list array to change the default.
  33. if (empty($list)) {
  34. $list = [
  35. '',
  36. 'one',
  37. 'two',
  38. 'three',
  39. 'four',
  40. 'five',
  41. 'six',
  42. 'seven',
  43. 'eight',
  44. 'nine',
  45. 'ten',
  46. ];
  47. }
  48. for ($i = 0; $i < $count; $i++) {
  49. if ($i === 0) {
  50. $hostname = $base_hostname;
  51. $machine_name = 'example.com';
  52. $name = 'Example';
  53. }
  54. elseif (!empty($list[$i])) {
  55. $hostname = $list[$i] . '.' . $base_hostname;
  56. $machine_name = $list[$i] . '.example.com';
  57. $name = 'Test ' . ucfirst($list[$i]);
  58. }
  59. // These domains are not setup and are just for UX testing.
  60. else {
  61. $hostname = 'test' . $i . '.' . $base_hostname;
  62. $machine_name = 'test' . $i . '.example.com';
  63. $name = 'Test ' . $i;
  64. }
  65. // Create a new domain programmatically.
  66. $values = [
  67. 'hostname' => $hostname,
  68. 'name' => $name,
  69. 'id' => \Drupal::entityTypeManager()->getStorage('domain')->createMachineName($machine_name),
  70. ];
  71. $domain = \Drupal::entityTypeManager()->getStorage('domain')->create($values);
  72. $domain->save();
  73. }
  74. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple(NULL, TRUE);
  75. }
  76. /**
  77. * Adds a test domain to an entity.
  78. *
  79. * @param string $entity_type
  80. * The entity type being acted upon.
  81. * @param int $entity_id
  82. * The entity id.
  83. * @param array|string $ids
  84. * An id or array of ids to add.
  85. * @param string $field
  86. * The name of the domain field used to attach to the entity.
  87. */
  88. public function addDomainsToEntity($entity_type, $entity_id, $ids, $field) {
  89. if ($entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity_id)) {
  90. $entity->set($field, $ids);
  91. $entity->save();
  92. }
  93. }
  94. /**
  95. * Returns an uncached list of all domains.
  96. *
  97. * @return array
  98. * An array of domain entities.
  99. */
  100. public function getDomains() {
  101. \Drupal::entityTypeManager()->getStorage('domain')->resetCache();
  102. return \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  103. }
  104. /**
  105. * Returns an uncached list of all domains, sorted by weight.
  106. *
  107. * @return array
  108. * An array of domain entities.
  109. */
  110. public function getDomainsSorted() {
  111. \Drupal::entityTypeManager()->getStorage('domain')->resetCache();
  112. return \Drupal::entityTypeManager()->getStorage('domain')->loadMultipleSorted();
  113. }
  114. /**
  115. * Converts a domain hostname to a trusted host pattern.
  116. *
  117. * @param string $hostname
  118. * A hostname string.
  119. *
  120. * @return string
  121. * A regex-safe hostname, without delimiters.
  122. */
  123. public function prepareTrustedHostname($hostname) {
  124. $hostname = mb_strtolower(preg_replace('/:\d+$/', '', trim($hostname)));
  125. return preg_quote($hostname);
  126. }
  127. /**
  128. * Set the base hostname for this test.
  129. */
  130. public function setBaseHostname() {
  131. $this->baseHostname = \Drupal::entityTypeManager()->getStorage('domain')->createHostname();
  132. }
  133. /**
  134. * Reusable test function for checking initial / empty table status.
  135. */
  136. public function domainTableIsEmpty() {
  137. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple(NULL, TRUE);
  138. $this->assertTrue(empty($domains), 'No domains have been created.');
  139. $default_id = \Drupal::entityTypeManager()->getStorage('domain')->loadDefaultId();
  140. $this->assertTrue(empty($default_id), 'No default domain has been set.');
  141. }
  142. /**
  143. * Creates domain record for use with POST request tests.
  144. */
  145. public function domainPostValues() {
  146. $edit = [];
  147. $domain = \Drupal::entityTypeManager()->getStorage('domain')->create();
  148. $required = \Drupal::service('domain.validator')->getRequiredFields();
  149. foreach ($required as $key) {
  150. $edit[$key] = $domain->get($key);
  151. }
  152. // URL validation has issues on Travis, so only do it when requested.
  153. $edit['validate_url'] = 0;
  154. $edit['id'] = \Drupal::entityTypeManager()->getStorage('domain')->createMachineName($edit['hostname']);
  155. return $edit;
  156. }
  157. }