You are here

public function DomainTestTrait::domainCreateTestDomains in Domain Access 8

Generates a list of domains for testing.

In my environment, I use the example.local hostname as a base. Then I name hostnames one.* two.* up to ten. Note that we always use *_example_com for the machine_name (entity id) value, though the hostname can vary based on the system. This naming allows us to load test schema files.

The script may also add test1, test2, test3 up to any number to test a large number of domains.

Parameters

int $count: The number of domains to create.

string|null $base_hostname: The root domain to use for domain creation (e.g. example.com). You should normally leave this blank to account for alternate test environments.

array $list: An optional list of subdomains to apply instead of the default set.

56 calls to DomainTestTrait::domainCreateTestDomains()
ActiveDomainDefaultArgumentTest::setUp in domain/tests/src/Functional/Views/ActiveDomainDefaultArgumentTest.php
DomainAccessAllAffiliatesTest::testDomainAccessAllFieldStorage in domain_access/tests/src/Functional/DomainAccessAllAffiliatesTest.php
Tests the storage of the domain access field.
DomainAccessCacheTest::testDomainResponse in domain_access/tests/src/Functional/DomainAccessCacheTest.php
Tests that a domain response is proper.
DomainAccessDefaultValueTest::testDomainAccessDefaultValue in domain_access/tests/src/Functional/DomainAccessDefaultValueTest.php
Test the usage of DomainAccessManager::getDefaultValue().
DomainAccessElementTest::setUp in domain_access/tests/src/Functional/DomainAccessElementTest.php

... See full list

File

domain/tests/src/Traits/DomainTestTrait.php, line 29

Class

DomainTestTrait
Contains helper classes for tests to set up various configuration.

Namespace

Drupal\Tests\domain\Traits

Code

public function domainCreateTestDomains($count = 1, $base_hostname = NULL, array $list = []) {
  $original_domains = \Drupal::entityTypeManager()
    ->getStorage('domain')
    ->loadMultiple(NULL, TRUE);
  if (empty($base_hostname)) {
    $base_hostname = $this->baseHostname;
  }

  // Note: these domains are rigged to work on my test server.
  // For proper testing, yours should be set up similarly, but you can pass a
  // $list array to change the default.
  if (empty($list)) {
    $list = [
      '',
      'one',
      'two',
      'three',
      'four',
      'five',
      'six',
      'seven',
      'eight',
      'nine',
      'ten',
    ];
  }
  for ($i = 0; $i < $count; $i++) {
    if ($i === 0) {
      $hostname = $base_hostname;
      $machine_name = 'example.com';
      $name = 'Example';
    }
    elseif (!empty($list[$i])) {
      $hostname = $list[$i] . '.' . $base_hostname;
      $machine_name = $list[$i] . '.example.com';
      $name = 'Test ' . ucfirst($list[$i]);
    }
    else {
      $hostname = 'test' . $i . '.' . $base_hostname;
      $machine_name = 'test' . $i . '.example.com';
      $name = 'Test ' . $i;
    }

    // Create a new domain programmatically.
    $values = [
      'hostname' => $hostname,
      'name' => $name,
      'id' => \Drupal::entityTypeManager()
        ->getStorage('domain')
        ->createMachineName($machine_name),
    ];
    $domain = \Drupal::entityTypeManager()
      ->getStorage('domain')
      ->create($values);
    $domain
      ->save();
  }
  $domains = \Drupal::entityTypeManager()
    ->getStorage('domain')
    ->loadMultiple(NULL, TRUE);
}