DomainValidatorTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Drupal\Tests\domain\Functional;
  3. use Drupal\Core\Config\ConfigValueException;
  4. /**
  5. * Tests domain record validation.
  6. *
  7. * @group domain
  8. */
  9. class DomainValidatorTest extends DomainTestBase {
  10. /**
  11. * Tests that a domain hostname validates.
  12. */
  13. public function testDomainValidator() {
  14. // No domains should exist.
  15. $this->domainTableIsEmpty();
  16. $validator = \Drupal::service('domain.validator');
  17. $storage = \Drupal::entityTypeManager()->getStorage('domain');
  18. // Create a domain.
  19. $this->domainCreateTestDomains(1, 'foo.com');
  20. // Check the created domain based on its known id value.
  21. $key = 'foo.com';
  22. /** @var \Drupal\domain\Entity\Domain $domain */
  23. $domain = $storage->loadByHostname($key);
  24. $this->assertTrue(!empty($domain), 'Test domain created.');
  25. // Valid hostnames to test. Valid is the boolean value.
  26. $hostnames = [
  27. 'localhost' => 1,
  28. 'example.com' => 1,
  29. // See www-prefix test, below.
  30. 'www.example.com' => 1,
  31. 'one.example.com' => 1,
  32. 'example.com:8080' => 1,
  33. // Only one colon.
  34. 'example.com::8080' => 0,
  35. // No letters after a colon.
  36. 'example.com:abc' => 0,
  37. // Cannot begin with a dot.
  38. '.example.com' => 0,
  39. // Cannot end with a dot.
  40. 'example.com.' => 0,
  41. // Lowercase only.
  42. 'EXAMPLE.com' => 0,
  43. // ascii-only.
  44. 'éxample.com' => 0,
  45. ];
  46. foreach ($hostnames as $hostname => $valid) {
  47. $errors = $validator->validate($hostname);
  48. if ($valid) {
  49. $this->assertTrue(empty($errors), 'Validation correct with no errors.');
  50. }
  51. else {
  52. $this->assertTrue(!empty($errors), 'Validation correct with proper errors.');
  53. }
  54. }
  55. // Test duplicate hostname creation.
  56. $test_hostname = 'foo.com';
  57. $test_domain = $storage->create([
  58. 'hostname' => $test_hostname,
  59. 'name' => 'Test domain',
  60. 'id' => 'test_domain',
  61. ]);
  62. try {
  63. $test_domain->save();
  64. $this->fail('Duplicate hostname validation');
  65. }
  66. catch (ConfigValueException $e) {
  67. $expected_message = "The hostname ($test_hostname) is already registered.";
  68. $this->assertEqual($expected_message, $e->getMessage());
  69. }
  70. // Test the two configurable options.
  71. $config = $this->config('domain.settings');
  72. $config->set('www_prefix', TRUE);
  73. $config->set('allow_non_ascii', TRUE);
  74. $config->save();
  75. // Valid hostnames to test. Valid is the boolean value.
  76. $hostnames = [
  77. // No www-prefix allowed.
  78. 'www.example.com' => 0,
  79. // ascii-only allowed.
  80. 'éxample.com' => 1,
  81. ];
  82. foreach ($hostnames as $hostname => $valid) {
  83. $errors = $validator->validate($hostname);
  84. if ($valid) {
  85. $this->assertTrue(empty($errors), 'Validation test correct with no errors.');
  86. }
  87. else {
  88. $this->assertTrue(!empty($errors), 'Validation test correct with errors.');
  89. }
  90. }
  91. }
  92. }