You are here

public function DomainSourceLanguageTest::testDomainSourceLanguage in Domain Access 8

Tests domain source language.

File

domain_source/tests/src/Functional/DomainSourceLanguageTest.php, line 53

Class

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

Namespace

Drupal\Tests\domain_source\Functional

Code

public function testDomainSourceLanguage() {

  // Create a node, assigned to a source domain.
  $id = 'one_example_com';

  // Create one node with no language.
  $node = $this
    ->drupalCreateNode([
    'body' => [
      [],
    ],
    'status' => 1,
    DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD => $id,
  ]);

  // Programmatically create a translation.
  $storage = \Drupal::entityTypeManager()
    ->getStorage('node');

  // Reload the node.
  $node = $storage
    ->load(1);

  // Create an Afrikaans translation assigned to domain 2.
  $id2 = 'two_example_com';
  $translation = $node
    ->addTranslation('af');
  $translation->title->value = $this
    ->randomString();
  $translation->{DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD} = $id2;
  $translation->status = 1;
  $node
    ->save();

  // 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');

  // Now test the same for the Arfrikaans translation.
  $path = 'node/1';
  $source = $domains[$id2];
  $expected = $source
    ->getPath() . 'af/' . $path;
  $route_name = 'entity.node.canonical';
  $route_parameters = [
    'node' => 1,
  ];
  $uri = 'entity:' . $path;
  $uri_path = '/' . $path;
  $language = \Drupal::entityTypeManager()
    ->getStorage('configurable_language')
    ->load('af');
  $options = [
    'language' => $language,
  ];
  $translation = $node
    ->getTranslation('af');
  $this
    ->assertTrue(domain_source_get($translation) == $id2, domain_source_get($translation));

  // Because of path cache, we have to flush here.
  drupal_flush_all_caches();

  // 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');
}