You are here

public function DomainAccessGrantsTest::testDomainAccessGrants in Domain Access 8

Creates a node and tests the creation of node access rules.

File

domain_access/tests/src/Functional/DomainAccessGrantsTest.php, line 43

Class

DomainAccessGrantsTest
Tests the application of domain access grants.

Namespace

Drupal\Tests\domain_access\Functional

Code

public function testDomainAccessGrants() {
  $node_storage = \Drupal::entityTypeManager()
    ->getStorage('node');

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

  // Assign a node to a random domain.
  $domains = \Drupal::entityTypeManager()
    ->getStorage('domain')
    ->loadMultiple();
  $active_domain = array_rand($domains, 1);
  $domain = $domains[$active_domain];

  // Create an article node.
  $node1 = $this
    ->drupalCreateNode([
    'type' => 'article',
    DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD => [
      $domain
        ->id(),
    ],
  ]);
  $this
    ->assertNotNull($node_storage
    ->load($node1
    ->id()), 'Article node created.');

  // Test the response of the node on each site. Should allow access only to
  // the selected site.
  foreach ($domains as $domain) {
    $path = $domain
      ->getPath() . 'node/' . $node1
      ->id();
    $this
      ->drupalGet($path);
    if ($domain
      ->id() == $active_domain) {
      $this
        ->assertResponse(200);
      $this
        ->assertRaw($node1
        ->getTitle(), 'Article found on domain.');
    }
    else {
      $this
        ->assertResponse(403);
    }
  }

  // Create an article node.
  $node2 = $this
    ->drupalCreateNode([
    'type' => 'article',
    DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD => [
      $domain
        ->id(),
    ],
    DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD => 1,
  ]);
  $this
    ->assertNotNull($node_storage
    ->load($node2
    ->id()), 'Article node created.');

  // Test the response of the node on each site. Should allow access on all.
  foreach ($domains as $domain) {
    $path = $domain
      ->getPath() . 'node/' . $node2
      ->id();
    $this
      ->drupalGet($path);
    $this
      ->assertResponse(200);
    $this
      ->assertRaw($node2
      ->getTitle(), 'Article found on domain.');
  }
}