View source
<?php
namespace Drupal\Tests\domain_source\Functional;
use Drupal\Tests\domain\Functional\DomainTestBase;
use Drupal\domain_access\DomainAccessManagerInterface;
use Drupal\domain_source\DomainSourceElementManagerInterface;
class DomainSourceTokenTest extends DomainTestBase {
public static $modules = [
'domain',
'domain_access',
'domain_source',
'field',
'node',
'user',
];
protected function setUp() {
parent::setUp();
$this
->domainCreateTestDomains(4, 'example.com');
}
public function testDomainSourceTokens() {
$token_handler = \Drupal::token();
$domains = \Drupal::entityTypeManager()
->getStorage('domain')
->loadMultiple();
$nodes_values = [
'type' => 'page',
'title' => 'foo',
DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD => [
'example_com',
'one_example_com',
'two_example_com',
],
DomainAccessManagerInterface::DOMAIN_ACCESS_ALL_FIELD => 0,
DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD => 'one_example_com',
];
$node = $this
->createNode($nodes_values);
$this
->assertEqual($token_handler
->replace('[node:canonical-source-domain-url]', [
'node' => $node,
]), $domains['one_example_com']
->getPath() . 'node/1');
$this
->assertEqual($node
->toUrl('canonical')
->setAbsolute()
->toString(), $domains['one_example_com']
->getPath() . 'node/1');
$node
->set(DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD, 'two_example_com');
$this
->assertEqual($token_handler
->replace('[node:canonical-source-domain-url]', [
'node' => $node,
]), $domains['two_example_com']
->getPath() . 'node/1');
$this
->assertEqual($node
->toUrl('canonical')
->setAbsolute()
->toString(), $domains['two_example_com']
->getPath() . 'node/1');
$config = $this
->config('domain_source.settings');
$config
->set('exclude_routes', [
'canonical' => 'canonical',
])
->save();
drupal_flush_all_caches();
$node
->set(DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD, 'one_example_com');
$one_example_com_absolute_url = $node
->toUrl('canonical')
->setAbsolute()
->toString();
$this
->assertEqual($token_handler
->replace('[node:canonical-source-domain-url]', [
'node' => $node,
]), $domains['one_example_com']
->getPath() . 'node/1');
$node
->set(DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD, 'two_example_com');
$two_example_com_absolute_url = $node
->toUrl('canonical')
->setAbsolute()
->toString();
$this
->assertEqual($token_handler
->replace('[node:canonical-source-domain-url]', [
'node' => $node,
]), $domains['two_example_com']
->getPath() . 'node/1');
$this
->assertEqual($one_example_com_absolute_url, $two_example_com_absolute_url, 'Canonical url rewrite is not used, domain source change did not affect url.');
}
}