You are here

function LanguageUILanguageNegotiationTest::testLanguageDomain in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/language/src/Tests/LanguageUILanguageNegotiationTest.php \Drupal\language\Tests\LanguageUILanguageNegotiationTest::testLanguageDomain()

Tests URL handling when separate domains are used for multiple languages.

File

core/modules/language/src/Tests/LanguageUILanguageNegotiationTest.php, line 424
Contains \Drupal\language\Tests\LanguageUILanguageNegotiationTest.

Class

LanguageUILanguageNegotiationTest
Tests the language UI for language switching.

Namespace

Drupal\language\Tests

Code

function testLanguageDomain() {
  global $base_url;

  // Get the current host URI we're running on.
  $base_url_host = parse_url($base_url, PHP_URL_HOST);

  // Add the Italian language.
  ConfigurableLanguage::createFromLangcode('it')
    ->save();
  $languages = $this->container
    ->get('language_manager')
    ->getLanguages();

  // Enable browser and URL language detection.
  $edit = array(
    'language_interface[enabled][language-url]' => TRUE,
    'language_interface[weight][language-url]' => -10,
  );
  $this
    ->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));

  // Do not allow blank domain.
  $edit = array(
    'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
    'domain[en]' => '',
  );
  $this
    ->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
  $this
    ->assertText('The domain may not be left blank for English', 'The form does not allow blank domains.');
  $this
    ->rebuildContainer();

  // Change the domain for the Italian language.
  $edit = array(
    'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
    'domain[en]' => $base_url_host,
    'domain[it]' => 'it.example.com',
  );
  $this
    ->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
  $this
    ->assertText('The configuration options have been saved', 'Domain configuration is saved.');
  $this
    ->rebuildContainer();

  // Try to use an invalid domain.
  $edit = [
    'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
    'domain[en]' => $base_url_host,
    'domain[it]' => 'it.example.com/',
  ];
  $this
    ->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
  $this
    ->assertRaw(t('The domain for %language may only contain the domain name, not a trailing slash, protocol and/or port.', [
    '%language' => 'Italian',
  ]));

  // Build the link we're going to test.
  $link = 'it.example.com' . rtrim(base_path(), '/') . '/admin';

  // Test URL in another language: http://it.example.com/admin.
  // Base path gives problems on the testbot, so $correct_link is hard-coded.
  // @see UrlAlterFunctionalTest::assertUrlOutboundAlter (path.test).
  $italian_url = Url::fromRoute('system.admin', [], [
    'language' => $languages['it'],
  ])
    ->toString();
  $url_scheme = \Drupal::request()
    ->isSecure() ? 'https://' : 'http://';
  $correct_link = $url_scheme . $link;
  $this
    ->assertEqual($italian_url, $correct_link, format_string('The right URL (@url) in accordance with the chosen language', array(
    '@url' => $italian_url,
  )));

  // Test HTTPS via options.
  $italian_url = Url::fromRoute('system.admin', [], [
    'https' => TRUE,
    'language' => $languages['it'],
  ])
    ->toString();
  $correct_link = 'https://' . $link;
  $this
    ->assertTrue($italian_url == $correct_link, format_string('The right HTTPS URL (via options) (@url) in accordance with the chosen language', array(
    '@url' => $italian_url,
  )));

  // Test HTTPS via current URL scheme.
  $request = Request::create('', 'GET', array(), array(), array(), array(
    'HTTPS' => 'on',
  ));
  $this->container
    ->get('request_stack')
    ->push($request);
  $italian_url = Url::fromRoute('system.admin', [], [
    'language' => $languages['it'],
  ])
    ->toString();
  $correct_link = 'https://' . $link;
  $this
    ->assertTrue($italian_url == $correct_link, format_string('The right URL (via current URL scheme) (@url) in accordance with the chosen language', array(
    '@url' => $italian_url,
  )));
}