DomainHookTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Drupal\Tests\domain\Kernel;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. use Drupal\KernelTests\KernelTestBase;
  5. use Drupal\Tests\domain\Traits\DomainTestTrait;
  6. /**
  7. * Tests domain hooks documented in domain.api.php.
  8. *
  9. * Note that the other hooks are covered by functional tests, since they involve
  10. * UI elements.
  11. *
  12. * @see DomainReferencesTest
  13. * @see DomainListBuilderTes
  14. * @see DomainAliasNegotiatorTest
  15. *
  16. * @group domain
  17. */
  18. class DomainHookTest extends KernelTestBase {
  19. use DomainTestTrait;
  20. /**
  21. * Modules to enable.
  22. *
  23. * @var array
  24. */
  25. public static $modules = ['domain', 'domain_test', 'user', 'node'];
  26. /**
  27. * Domain id key.
  28. *
  29. * @var string
  30. */
  31. public $key = 'example_com';
  32. /**
  33. * The Domain storage handler service.
  34. *
  35. * @var \Drupal\domain\DomainStorageInterface
  36. */
  37. public $domainStorage;
  38. /**
  39. * The current user service.
  40. *
  41. * @var \Drupal\Core\Session\AccountInterface
  42. */
  43. public $currentUser;
  44. /**
  45. * The module handler service.
  46. *
  47. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  48. */
  49. public $moduleHandler;
  50. /**
  51. * Test setup.
  52. */
  53. protected function setUp() {
  54. parent::setUp();
  55. // Create a domain.
  56. $this->domainCreateTestDomains();
  57. // Get the services.
  58. $this->domainStorage = \Drupal::entityTypeManager()->getStorage('domain');
  59. $this->currentUser = \Drupal::service('current_user');
  60. $this->moduleHandler = \Drupal::service('module_handler');
  61. }
  62. /**
  63. * Tests domain loading.
  64. */
  65. public function testHookDomainLoad() {
  66. // Check the created domain based on its known id value.
  67. $domain = $this->domainStorage->load($this->key);
  68. // Internal hooks.
  69. $path = $domain->getPath();
  70. $url = $domain->getUrl();
  71. $this->assertTrue(isset($path), new FormattableMarkup('The path property was set to %path by hook_entity_load.', ['%path' => $path]));
  72. $this->assertTrue(isset($url), new FormattableMarkup('The url property was set to %url by hook_entity_load.', ['%url' => $url]));
  73. // External hooks.
  74. $this->assertTrue($domain->foo == 'bar', 'The foo property was set to <em>bar</em> by hook_domain_load.');
  75. }
  76. /**
  77. * Tests domain validation.
  78. */
  79. public function testHookDomainValidate() {
  80. $validator = \Drupal::service('domain.validator');
  81. // Test a good domain.
  82. $errors = $validator->validate('one.example.com');
  83. $this->assertEmpty($errors, 'No errors returned for example.com');
  84. // Test our hook implementation, which denies fail.example.com explicitly.
  85. $errors = $validator->validate('fail.example.com');
  86. $this->assertNotEmpty($errors, 'Errors returned for fail.example.com');
  87. $this->assertTrue(current($errors) == 'Fail.example.com cannot be registered', 'Error message returned correctly.');
  88. }
  89. /**
  90. * Tests domain request alteration.
  91. */
  92. public function testHookDomainRequestAlter() {
  93. // Set the request.
  94. $negotiator = \Drupal::service('domain.negotiator');
  95. $negotiator->setRequestDomain($this->baseHostname);
  96. // Check that the property was added by our hook.
  97. $domain = $negotiator->getActiveDomain();
  98. $this->assertTrue($domain->foo1 == 'bar1', 'The foo1 property was set to <em>bar1</em> by hook_domain_request_alter');
  99. }
  100. /**
  101. * Tests domain operations hook.
  102. */
  103. public function testHookDomainOperations() {
  104. $domain = $this->domainStorage->load($this->key);
  105. // Set the request.
  106. $operations = $this->moduleHandler->invokeAll('domain_operations', [$domain, $this->currentUser]);
  107. // Test that our operations were added by the hook.
  108. $this->assertTrue(isset($operations['domain_test']), 'Domain test operation loaded.');
  109. }
  110. /**
  111. * Tests domain references alter hook.
  112. */
  113. public function testHookDomainReferencesAlter() {
  114. $domain = $this->domainStorage->load($this->key);
  115. // Set the request.
  116. $manager = \Drupal::service('entity.manager');
  117. $target_type = 'domain';
  118. // Build a node entity selection query.
  119. $query = $manager->getStorage($target_type)->getQuery();
  120. $context = [
  121. 'entity_type' => 'node',
  122. 'bundle' => 'article',
  123. 'field_type' => 'editor',
  124. ];
  125. // Run the alteration, which should add metadata to the query for nodes.
  126. $this->moduleHandler->alter('domain_references', $query, $this->currentUser, $context);
  127. $this->assertTrue($query->getMetaData('domain_test') == 'Test string', 'Domain test query altered.');
  128. // Build a user entity selection query.
  129. $query = $manager->getStorage($target_type)->getQuery();
  130. $context = [
  131. 'entity_type' => 'user',
  132. 'bundle' => 'user',
  133. 'field_type' => 'admin',
  134. ];
  135. // Run the alteration, which does not add metadata for user queries.
  136. $this->moduleHandler->alter('domain_references', $query, $this->currentUser, $context);
  137. $this->assertEmpty($query->getMetaData('domain_test'), 'Domain test query not altered.');
  138. }
  139. }