You are here

public function DomainAccessAllAffiliatesTest::testDomainAccessAllFieldStorage in Domain Access 8

Tests the storage of the domain access field.

File

domain_access/tests/src/Functional/DomainAccessAllAffiliatesTest.php, line 53

Class

DomainAccessAllAffiliatesTest
Tests the domain access entity reference field type.

Namespace

Drupal\Tests\domain_access\Functional

Code

public function testDomainAccessAllFieldStorage() {
  $label = 'Send to all affiliates';
  $this->admin_user = $this
    ->drupalCreateUser([
    'bypass node access',
    'administer content types',
    'administer node fields',
    'administer node display',
    'administer domains',
    'publish to any domain',
  ]);
  $this
    ->drupalLogin($this->admin_user);

  // Create 5 domains.
  $this
    ->domainCreateTestDomains(5);

  // Visit the article field display administration page.
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertResponse(200);

  // Check the new field exists on the page.
  $this
    ->assertText($label, 'Found the domain field instance.');

  // We expect to find 5 domain options.
  $domains = \Drupal::entityTypeManager()
    ->getStorage('domain')
    ->loadMultiple();
  foreach ($domains as $domain) {
    $string = 'value="' . $domain
      ->id() . '"';
    $this
      ->assertRaw($string, 'Found the domain option.');
    if (!isset($one)) {
      $one = $domain
        ->id();
      continue;
    }
    if (!isset($two)) {
      $two = $domain
        ->id();
    }
  }

  // Try to post a node, assigned to the first two domains.
  $edit['title[0][value]'] = 'Test node';
  $edit["field_domain_access[{$one}]"] = TRUE;
  $edit["field_domain_access[{$two}]"] = TRUE;
  $edit["field_domain_all_affiliates[value]"] = 1;
  $this
    ->drupalGet('node/add/article');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertResponse(200);
  $node = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->load(1);

  // Check that two values are set.
  $values = \Drupal::service('domain_access.manager')
    ->getAccessValues($node);
  $this
    ->assertCount(2, $values, 'Node saved with two domain records.');

  // Check that all affiliates is set.
  $this
    ->assertNotEmpty($node
    ->get(DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD)->value, 'Node assigned to all affiliates.');
}