You are here

public function DomainSourceUrlTest::testDomainSourceUrls in Domain Access 8

Tests domain source URLs.

File

domain_source/tests/src/Functional/DomainSourceUrlTest.php, line 36

Class

DomainSourceUrlTest
Tests behavior for the rewriting links using core URL methods.

Namespace

Drupal\Tests\domain_source\Functional

Code

public function testDomainSourceUrls() {

  // Create a node, assigned to a source domain.
  $id = 'one_example_com';
  $nodes_values = [
    'type' => 'page',
    'title' => 'foo',
    DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD => $id,
  ];
  $node = $this
    ->createNode($nodes_values);

  // Variables for our tests.
  $path = 'node/1';
  $domains = \Drupal::entityTypeManager()
    ->getStorage('domain')
    ->loadMultiple();
  $source = $domains[$id];
  $expected = $source
    ->getPath() . $path;
  $route_name = 'entity.node.canonical';
  $route_parameters = [
    'node' => 1,
  ];
  $uri = 'entity:' . $path;
  $uri_path = '/' . $path;
  $options = [];

  // Get the link using Url::fromRoute().
  $url = Url::fromRoute($route_name, $route_parameters, $options)
    ->toString();
  $this
    ->assertTrue($url == $expected, 'fromRoute');

  // Get the link using Url::fromUserInput()
  $url = Url::fromUserInput($uri_path, $options)
    ->toString();
  $this
    ->assertTrue($url == $expected, 'fromUserInput');

  // Get the link using Url::fromUri()
  $url = Url::fromUri($uri, $options)
    ->toString();
  $this
    ->assertTrue($url == $expected, 'fromUri');
}