function LanguageUILanguageNegotiationTest::testLanguageDomain

Same name in other branches
  1. 8.9.x 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 490

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->drupalGet('admin/config/regional/language/detection');
    $this->submitForm($edit, 'Save settings');
    // Do not allow blank domain.
    $edit = [
        'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
        'domain[en]' => '',
    ];
    $this->drupalGet('admin/config/regional/language/detection/url');
    $this->submitForm($edit, 'Save configuration');
    $this->assertSession()
        ->statusMessageContains('The domain may not be left blank for English', 'error');
    $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->drupalGet('admin/config/regional/language/detection/url');
    $this->submitForm($edit, 'Save configuration');
    $this->assertSession()
        ->statusMessageContains('The configuration options have been saved', 'status');
    $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->drupalGet('admin/config/regional/language/detection/url');
    $this->submitForm($edit, 'Save configuration');
    $this->assertSession()
        ->statusMessageContains("The domain for Italian may only contain the domain name, not a trailing slash, protocol and/or port.", 'error');
    // 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->assertEquals($correct_link, $italian_url, 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->assertSame($correct_link, $italian_url, 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->assertSame($correct_link, $italian_url, 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.