View source
<?php
namespace Drupal\Tests\domain\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\domain\Traits\DomainTestTrait;
class DomainHookTest extends KernelTestBase {
use DomainTestTrait;
public static $modules = [
'domain',
'domain_test',
'user',
'node',
];
public $key = 'example_com';
public $domainStorage;
public $currentUser;
public $moduleHandler;
public $baseHostname;
protected function setUp() {
parent::setUp();
$this
->domainCreateTestDomains();
$this->domainStorage = \Drupal::entityTypeManager()
->getStorage('domain');
$this->currentUser = \Drupal::service('current_user');
$this->moduleHandler = \Drupal::service('module_handler');
}
public function testHookDomainLoad() {
$domain = $this->domainStorage
->load($this->key);
$path = $domain
->getPath();
$url = $domain
->getUrl();
$this
->assertTrue(isset($path), new FormattableMarkup('The path property was set to %path by hook_entity_load.', [
'%path' => $path,
]));
$this
->assertTrue(isset($url), new FormattableMarkup('The url property was set to %url by hook_entity_load.', [
'%url' => $url,
]));
$this
->assertTrue($domain->foo == 'bar', 'The foo property was set to <em>bar</em> by hook_domain_load.');
}
public function testHookDomainValidate() {
$validator = \Drupal::service('domain.validator');
$errors = $validator
->validate('one.example.com');
$this
->assertEmpty($errors, 'No errors returned for example.com');
$errors = $validator
->validate('fail.example.com');
$this
->assertNotEmpty($errors, 'Errors returned for fail.example.com');
$this
->assertTrue(current($errors) == 'Fail.example.com cannot be registered', 'Error message returned correctly.');
}
public function testHookDomainRequestAlter() {
$negotiator = \Drupal::service('domain.negotiator');
$negotiator
->setRequestDomain($this->baseHostname);
$domain = $negotiator
->getActiveDomain();
$this
->assertTrue($domain->foo1 == 'bar1', 'The foo1 property was set to <em>bar1</em> by hook_domain_request_alter');
}
public function testHookDomainOperations() {
$domain = $this->domainStorage
->load($this->key);
$operations = $this->moduleHandler
->invokeAll('domain_operations', [
$domain,
$this->currentUser,
]);
$this
->assertArrayHasKey('domain_test', $operations, 'Domain test operation loaded.');
}
public function testHookDomainReferencesAlter() {
$domain = $this->domainStorage
->load($this->key);
$manager = \Drupal::service('entity_type.manager');
$target_type = 'domain';
$query = $manager
->getStorage($target_type)
->getQuery();
$context = [
'entity_type' => 'node',
'bundle' => 'article',
'field_type' => 'editor',
];
$this->moduleHandler
->alter('domain_references', $query, $this->currentUser, $context);
$this
->assertTrue($query
->getMetaData('domain_test') == 'Test string', 'Domain test query altered.');
$query = $manager
->getStorage($target_type)
->getQuery();
$context = [
'entity_type' => 'user',
'bundle' => 'user',
'field_type' => 'admin',
];
$this->moduleHandler
->alter('domain_references', $query, $this->currentUser, $context);
$this
->assertEmpty($query
->getMetaData('domain_test'), 'Domain test query not altered.');
}
}