You are here

public function DomainCreateTest::testDomainCreate in Domain Access 8

Tests initial domain creation.

File

domain/tests/src/Functional/DomainCreateTest.php, line 15

Class

DomainCreateTest
Tests the domain record creation API.

Namespace

Drupal\Tests\domain\Functional

Code

public function testDomainCreate() {

  // No domains should exist.
  $this
    ->domainTableIsEmpty();

  // Create a new domain programmatically.
  $storage = \Drupal::entityTypeManager()
    ->getStorage('domain');
  $domain = $storage
    ->create();
  $domain
    ->set('id', $storage
    ->createMachineName($domain
    ->getHostname()));
  $keys = [
    'id',
    'name',
    'hostname',
    'scheme',
    'status',
    'weight',
    'is_default',
  ];
  foreach ($keys as $key) {
    $property = $domain
      ->get($key);
    $this
      ->assertNotNull($property, 'Property loaded');
  }
  $domain
    ->save();

  // Did it save correctly?
  $default_id = $storage
    ->loadDefaultId();
  $this
    ->assertNotEmpty($default_id, 'Default domain has been set.');

  // Does it load correctly?
  $new_domain = $storage
    ->load($default_id);
  $this
    ->assertEquals($new_domain
    ->id(), $domain
    ->id(), 'Domain loaded properly.');

  // Has domain id been set?
  $this
    ->assertNotNull($new_domain
    ->getDomainId(), 'Domain id set properly.');

  // Has a UUID been set?
  $this
    ->assertNotNull($new_domain
    ->uuid(), 'Entity UUID set properly.');

  // Delete the domain.
  $domain
    ->delete();
  $domain = $storage
    ->load($default_id, TRUE);
  $this
    ->assertEmpty($domain, 'Domain record deleted.');

  // No domains should exist.
  $this
    ->domainTableIsEmpty();
}