View source
<?php
namespace Drupal\Tests\domain_alias\Functional;
use Drupal\user\RoleInterface;
class DomainAliasNegotiatorTest extends DomainAliasTestBase {
public static $modules = [
'domain',
'domain_alias',
'user',
'block',
];
public function testDomainAliasNegotiator() {
$this
->domainTableIsEmpty();
$this
->domainCreateTestDomains(2);
$this
->drupalPlaceBlock('domain_server_block');
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'administer domains',
]);
$domain_storage = \Drupal::entityTypeManager()
->getStorage('domain');
$alias_storage = \Drupal::entityTypeManager()
->getStorage('domain_alias');
$prefixes = [
'two',
'three',
];
foreach ($domain_storage
->loadMultiple() as $domain) {
$alias_domains[] = $domain;
$this
->drupalGet($domain
->getPath());
$this
->assertRaw($domain
->label(), 'Loaded the proper domain.');
$this
->assertRaw('Exact match', 'Direct domain match.');
}
foreach ($alias_domains as $index => $alias_domain) {
$prefix = $prefixes[$index];
$pattern = $prefix . '.' . $this->baseHostname;
$this
->domainAliasCreateTestAlias($alias_domain, $pattern);
$alias = $alias_storage
->loadByPattern($pattern);
$alias_domain
->set('hostname', $pattern);
$alias_domain
->setPath();
$url = $alias_domain
->getPath();
$this
->drupalGet($url);
$this
->assertRaw($alias_domain
->label(), 'Loaded the proper domain.');
$this
->assertRaw('ALIAS:', 'No direct domain match.');
$this
->assertRaw($alias
->getPattern(), 'Alias match.');
$alias
->set('redirect', 301);
$alias
->save();
$this
->drupalGet($url);
$alias
->set('redirect', 302);
$alias
->save();
$this
->drupalGet($url);
}
$alias_domain = $domain_storage
->loadDefaultDomain();
$pattern = '*.' . $this->baseHostname;
$this
->domainAliasCreateTestAlias($alias_domain, $pattern);
$alias = $alias_storage
->loadByPattern($pattern);
$alias_domain
->set('hostname', 'four.' . $this->baseHostname);
$alias_domain
->setPath();
$url = $alias_domain
->getPath();
$this
->drupalGet($url);
$this
->assertRaw($alias_domain
->label(), 'Loaded the proper domain.');
$this
->assertRaw('ALIAS:', 'No direct domain match.');
$this
->assertRaw($alias
->getPattern(), 'Alias match.');
$alias
->set('redirect', 301);
$alias
->save();
$this
->drupalGet($url);
$alias
->set('redirect', 302);
$alias
->save();
$this
->drupalGet($url);
user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, [
'administer domains',
]);
}
}