function LanguageUILanguageNegotiationTest::testLanguageDomain

Same name in other branches
  1. 9 core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php \Drupal\Tests\language\Functional\LanguageUILanguageNegotiationTest::testLanguageDomain()
  2. 10 core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php \Drupal\Tests\language\Functional\LanguageUILanguageNegotiationTest::testLanguageDomain()
  3. 11.x core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php \Drupal\Tests\language\Functional\LanguageUILanguageNegotiationTest::testLanguageDomain()

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

File

core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php, line 478

Class

LanguageUILanguageNegotiationTest
Tests the language UI for language switching.

Namespace

Drupal\Tests\language\Functional

Code

public 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 = [
        '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 = [
        '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 = [
        '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, new FormattableMarkup('The right URL (@url) in accordance with the chosen language', [
        '@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, new FormattableMarkup('The right HTTPS URL (via options) (@url) in accordance with the chosen language', [
        '@url' => $italian_url,
    ]));
    // Test HTTPS via current URL scheme.
    $request = Request::create('', 'GET', [], [], [], [
        '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, new FormattableMarkup('The right URL (via current URL scheme) (@url) in accordance with the chosen language', [
        '@url' => $italian_url,
    ]));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.