function LanguageUILanguageNegotiationTest::testLanguageDomain

Tests url() when separate domains are used for multiple languages.

File

drupal/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php, line 433
Definition of Drupal\language\Tests\LanguageUILanguageNegotiationTest.

Class

LanguageUILanguageNegotiationTest
Test UI language negotiation

Namespace

Drupal\language\Tests

Code

function testLanguageDomain() {

  // Add the Italian language.
  $langcode = 'it';
  $language = new Language(array(
    'langcode' => $langcode,
  ));
  language_save($language);
  $languages = language_list();

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

  // Change the domain for the Italian language.
  $edit = array(
    'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN,
    'domain[it]' => 'it.example.com',
  );
  $this
    ->drupalPost('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
  $this
    ->rebuildContainer();

  // 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('admin', array(
    'language' => $languages['it'],
    'script' => '',
  ));
  $url_scheme = $this->request
    ->isSecure() ? 'https://' : 'http://';
  $correct_link = $url_scheme . $link;
  $this
    ->assertTrue($italian_url == $correct_link, format_string('The url() function returns the right URL (@url) in accordance with the chosen language', array(
    '@url' => $italian_url,
  )));

  // Test HTTPS via options.
  $this
    ->settingsSet('mixed_mode_sessions', TRUE);
  $this
    ->rebuildContainer();
  $italian_url = url('admin', array(
    'https' => TRUE,
    'language' => $languages['it'],
    'script' => '',
  ));
  $correct_link = 'https://' . $link;
  $this
    ->assertTrue($italian_url == $correct_link, format_string('The url() function returns the right HTTPS URL (via options) (@url) in accordance with the chosen language', array(
    '@url' => $italian_url,
  )));
  $this
    ->settingsSet('mixed_mode_sessions', FALSE);

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