View source
<?php
namespace Drupal\Tests\domain_alias\Functional;
class DomainAliasValidatorTest extends DomainAliasTestBase {
public function testDomainAliasValidator() {
$this
->domainTableIsEmpty();
$validator = \Drupal::service('domain_alias.validator');
$this
->domainCreateTestDomains(1, 'foo.com');
$key = 'foo.com';
$domain = \Drupal::entityTypeManager()
->getStorage('domain')
->loadByHostname($key);
$this
->assertNotEmpty($domain, 'Test domain created.');
$patterns = [
'localhost' => 1,
'example.com' => 1,
'www.example.com' => 1,
'*.example.com' => 1,
'one.example.com' => 1,
'example.com:8080' => 1,
'foobar' => 0,
'*.*.example.com' => 0,
'example.com::8080' => 0,
'example.com:abc' => 0,
'.example.com' => 0,
'example.com.' => 0,
'EXAMPLE.com' => 0,
'éxample.com' => 0,
'foo.com' => 0,
];
foreach ($patterns as $pattern => $valid) {
$alias = $this
->domainAliasCreateTestAlias($domain, $pattern, 0, 'default', FALSE);
$errors = $validator
->validate($alias);
if ($valid) {
$this
->assertEmpty($errors, 'Validation test success.');
}
else {
$this
->assertNotEmpty($errors, 'Validation test success.');
}
}
$config = $this
->config('domain.settings');
$config
->set('allow_non_ascii', TRUE)
->save();
$patterns = [
'éxample.com' => 1,
];
foreach ($patterns as $pattern => $valid) {
$alias = $this
->domainAliasCreateTestAlias($domain, $pattern, 0, 'default', FALSE);
$errors = $validator
->validate($alias);
if ($valid) {
$this
->assertEmpty($errors, 'Validation test success.');
}
else {
$this
->assertNotEmpty($errors, 'Validation test success.');
}
}
}
}