You are here

public function DomainHookTest::testHookDomainReferencesAlter in Domain Access 8

Tests domain references alter hook.

File

domain/tests/src/Kernel/DomainHookTest.php, line 146

Class

DomainHookTest
Tests domain hooks documented in domain.api.php.

Namespace

Drupal\Tests\domain\Kernel

Code

public function testHookDomainReferencesAlter() {
  $domain = $this->domainStorage
    ->load($this->key);

  // Set the request.
  $manager = \Drupal::service('entity_type.manager');
  $target_type = 'domain';

  // Build a node entity selection query.
  $query = $manager
    ->getStorage($target_type)
    ->getQuery();
  $context = [
    'entity_type' => 'node',
    'bundle' => 'article',
    'field_type' => 'editor',
  ];

  // Run the alteration, which should add metadata to the query for nodes.
  $this->moduleHandler
    ->alter('domain_references', $query, $this->currentUser, $context);
  $this
    ->assertTrue($query
    ->getMetaData('domain_test') == 'Test string', 'Domain test query altered.');

  // Build a user entity selection query.
  $query = $manager
    ->getStorage($target_type)
    ->getQuery();
  $context = [
    'entity_type' => 'user',
    'bundle' => 'user',
    'field_type' => 'admin',
  ];

  // Run the alteration, which does not add metadata for user queries.
  $this->moduleHandler
    ->alter('domain_references', $query, $this->currentUser, $context);
  $this
    ->assertEmpty($query
    ->getMetaData('domain_test'), 'Domain test query not altered.');
}