DomainValidatorTest.php in Domain Access 8
File
domain/tests/src/Functional/DomainValidatorTest.php
View source
<?php
namespace Drupal\Tests\domain\Functional;
use Drupal\Core\Config\ConfigValueException;
class DomainValidatorTest extends DomainTestBase {
public function testDomainValidator() {
$this
->domainTableIsEmpty();
$validator = \Drupal::service('domain.validator');
$storage = \Drupal::entityTypeManager()
->getStorage('domain');
$this
->domainCreateTestDomains(1, 'foo.com');
$key = 'foo.com';
$domain = $storage
->loadByHostname($key);
$this
->assertNotEmpty($domain, 'Test domain created.');
$hostnames = [
'localhost' => 1,
'example.com' => 1,
'www.example.com' => 1,
'one.example.com' => 1,
'example.com:8080' => 1,
'example.com::8080' => 0,
'example.com:abc' => 0,
'.example.com' => 0,
'example.com.' => 0,
'EXAMPLE.com' => 0,
'éxample.com' => 0,
];
foreach ($hostnames as $hostname => $valid) {
$errors = $validator
->validate($hostname);
if ($valid) {
$this
->assertEmpty($errors, 'Validation correct with no errors.');
}
else {
$this
->assertNotEmpty($errors, 'Validation correct with proper errors.');
}
}
$test_hostname = 'foo.com';
$test_domain = $storage
->create([
'hostname' => $test_hostname,
'name' => 'Test domain',
'id' => 'test_domain',
]);
try {
$test_domain
->save();
$this
->fail('Duplicate hostname validation');
} catch (ConfigValueException $e) {
$expected_message = "The hostname ({$test_hostname}) is already registered.";
$this
->assertEqual($expected_message, $e
->getMessage());
}
$config = $this
->config('domain.settings');
$config
->set('www_prefix', TRUE);
$config
->set('allow_non_ascii', TRUE);
$config
->save();
$hostnames = [
'www.example.com' => 0,
'éxample.com' => 1,
];
foreach ($hostnames as $hostname => $valid) {
$errors = $validator
->validate($hostname);
if ($valid) {
$this
->assertEmpty($errors, 'Validation test correct with no errors.');
}
else {
$this
->assertNotEmpty($errors, 'Validation test correct with errors.');
}
}
}
}