You are here

public function DomainAccessPermissionsTest::testDomainAccessPermissions in Domain Access 8

Runs basic tests for node_access function.

File

domain_access/tests/src/Functional/DomainAccessPermissionsTest.php, line 84

Class

DomainAccessPermissionsTest
Tests the domain access integration with node_access callbacks.

Namespace

Drupal\Tests\domain_access\Functional

Code

public function testDomainAccessPermissions() {

  // Note that these are hook_node_access() rules. Node Access system tests
  // are in DomainAccessRecordsTest.
  // We expect to find 5 domain options. Set two for later use.
  foreach ($this->domains as $domain) {
    if (!isset($one)) {
      $one = $domain
        ->id();
      continue;
    }
    if (!isset($two)) {
      $two = $domain
        ->id();
    }
  }

  // Assign our user to domain $two. Test on $one and $two.
  $domain_user1 = $this
    ->drupalCreateUser([
    'access content',
    'edit domain content',
    'delete domain content',
  ]);
  $this
    ->addDomainsToEntity('user', $domain_user1
    ->id(), $two, DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD);
  $domain_user1 = $this->userStorage
    ->load($domain_user1
    ->id());
  $assigned = $this->manager
    ->getAccessValues($domain_user1);
  $this
    ->assertCount(1, $assigned, 'User assigned to one domain.');
  $this
    ->assertArrayHasKey($two, $assigned, 'User assigned to proper test domain.');

  // Assign one node to default domain, and one to our test domain.
  $domain_node1 = $this
    ->drupalCreateNode([
    'type' => 'page',
    DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD => [
      $one,
    ],
  ]);
  $domain_node2 = $this
    ->drupalCreateNode([
    'type' => 'page',
    DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD => [
      $two,
    ],
  ]);
  $assigned = $this->manager
    ->getAccessValues($domain_node1);
  $this
    ->assertArrayHasKey($one, $assigned, 'Node1 assigned to proper test domain.');
  $assigned = $this->manager
    ->getAccessValues($domain_node2);
  $this
    ->assertArrayHasKey($two, $assigned, 'Node2 assigned to proper test domain.');

  // Tests 'edit domain content' to edit content assigned to their domains.
  $this
    ->assertNodeAccess([
    'view' => TRUE,
    'update' => FALSE,
    'delete' => FALSE,
  ], $domain_node1, $domain_user1);
  $this
    ->assertNodeAccess([
    'view' => TRUE,
    'update' => TRUE,
    'delete' => TRUE,
  ], $domain_node2, $domain_user1);

  // Tests 'edit domain TYPE content'.
  // Assign our user to domain $two. Test on $one and $two.
  $domain_user3 = $this
    ->drupalCreateUser([
    'access content',
    'update page content on assigned domains',
    'delete page content on assigned domains',
  ]);
  $this
    ->addDomainsToEntity('user', $domain_user3
    ->id(), $two, DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD);
  $domain_user3 = $this->userStorage
    ->load($domain_user3
    ->id());
  $assigned = $this->manager
    ->getAccessValues($domain_user3);
  $this
    ->assertCount(1, $assigned, 'User assigned to one domain.');
  $this
    ->assertArrayHasKey($two, $assigned, 'User assigned to proper test domain.');

  // Assign two different node types to our test domain.
  $domain_node3 = $this
    ->drupalCreateNode([
    'type' => 'article',
    DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD => [
      $two,
    ],
  ]);
  $domain_node4 = $this
    ->drupalCreateNode([
    'type' => 'page',
    DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD => [
      $two,
    ],
  ]);
  $assigned = $this->manager
    ->getAccessValues($domain_node3);
  $this
    ->assertArrayHasKey($two, $assigned, 'Node3 assigned to proper test domain.');
  $assigned = $this->manager
    ->getAccessValues($domain_node4);
  $this
    ->assertArrayHasKey($two, $assigned, 'Node4 assigned to proper test domain.');

  // Tests 'edit TYPE content on assigned domains'.
  $this
    ->assertNodeAccess([
    'view' => TRUE,
    'update' => FALSE,
    'delete' => FALSE,
  ], $domain_node3, $domain_user3);
  $this
    ->assertNodeAccess([
    'view' => TRUE,
    'update' => TRUE,
    'delete' => TRUE,
  ], $domain_node4, $domain_user3);

  // @TODO: Test edit and delete for user with 'all affiliates' permission.
  // Tests 'edit domain TYPE content'.
  // Assign our user to domain $two. Test on $one and $two.
  $domain_user4 = $this
    ->drupalCreateUser([
    'access content',
    'update page content on assigned domains',
    'delete page content on assigned domains',
  ]);
  $this
    ->addDomainsToEntity('user', $domain_user4
    ->id(), $two, DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD);
  $this
    ->addDomainsToEntity('user', $domain_user4
    ->id(), 1, DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD);
  $domain_user4 = $this->userStorage
    ->load($domain_user4
    ->id());
  $assigned = $this->manager
    ->getAccessValues($domain_user4);
  $this
    ->assertCount(1, $assigned, 'User assigned to one domain.');
  $this
    ->assertArrayHasKey($two, $assigned, 'User assigned to proper test domain.');
  $this
    ->assertNotEmpty($domain_user4
    ->get(DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD)->value, 'User assign to all affiliates.');

  // Assign two different node types to our test domain.
  $domain_node5 = $this
    ->drupalCreateNode([
    'type' => 'article',
    DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD => [
      $one,
    ],
  ]);
  $domain_node6 = $this
    ->drupalCreateNode([
    'type' => 'page',
    DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD => [
      $one,
    ],
  ]);
  $assigned = $this->manager
    ->getAccessValues($domain_node5);
  $this
    ->assertArrayHasKey($one, $assigned, 'Node5 assigned to proper test domain.');
  $assigned = $this->manager
    ->getAccessValues($domain_node6);
  $this
    ->assertArrayHasKey($one, $assigned, 'Node6 assigned to proper test domain.');

  // Tests 'edit TYPE content on assigned domains'.
  $this
    ->assertNodeAccess([
    'view' => TRUE,
    'update' => FALSE,
    'delete' => FALSE,
  ], $domain_node5, $domain_user4);
  $this
    ->assertNodeAccess([
    'view' => TRUE,
    'update' => TRUE,
    'delete' => TRUE,
  ], $domain_node6, $domain_user4);
}