DomainTokenTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Drupal\Tests\domain\Functional;
  3. use Drupal\Core\Session\AccountInterface;
  4. /**
  5. * Tests the domain token handler.
  6. *
  7. * @group domain
  8. */
  9. class DomainTokenTest extends DomainTestBase {
  10. /**
  11. * Modules to enable.
  12. *
  13. * @var array
  14. */
  15. public static $modules = ['domain', 'block'];
  16. /**
  17. * Tests the handling of an inbound request.
  18. */
  19. public function testDomainTokens() {
  20. // No domains should exist.
  21. $this->domainTableIsEmpty();
  22. // Create four new domains programmatically.
  23. $this->domainCreateTestDomains(4);
  24. // Since we cannot read the service request, we place a block
  25. // which shows the current domain token information.
  26. $this->drupalPlaceBlock('domain_token_block');
  27. // To get around block access, let the anon user view the block.
  28. user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, ['view domain information']);
  29. // Test the response of the default home page.
  30. foreach (\Drupal::entityTypeManager()->getStorage('domain')->loadMultiple() as $domain) {
  31. $this->drupalGet($domain->getPath());
  32. $this->assertRaw($domain->label(), 'Loaded the proper domain.');
  33. $this->assertRaw('<th>Token</th>', 'Token values printed.');
  34. foreach ($this->tokenList() as $token => $callback) {
  35. $this->assertRaw("<td>$token</td>", "$token found correctly.");
  36. // The URL token is sensitive to the path, which is /user, but that
  37. // does not come across when making the callback outside of a request
  38. // context.
  39. $value = $domain->{$callback}();
  40. if ($token == '[domain:url]') {
  41. $value = str_replace('user', '', $value);
  42. if (substr($value, -1) != '/') {
  43. $value .= '/';
  44. }
  45. }
  46. $this->assertRaw('<td>' . $value . '</td>', 'Value set correctly to ' . $value);
  47. }
  48. }
  49. }
  50. /**
  51. * Gets the list of tokens and value callbacks used by the test.
  52. *
  53. * @return array
  54. * An array keyed by token string, with value of expected domain value.
  55. */
  56. private function tokenList() {
  57. $tokens = [];
  58. foreach (\Drupal::service('domain.token')->getCallbacks() as $key => $callback) {
  59. $name = "[domain:$key]";
  60. $tokens[$name] = $callback;
  61. }
  62. return $tokens;
  63. }
  64. }