function LanguageSwitchingTest::testLanguageBlockWithDomain

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

Tests language switcher links for domain based negotiation.

File

core/modules/language/tests/src/Functional/LanguageSwitchingTest.php, line 314

Class

LanguageSwitchingTest
Functional tests for the language switching feature.

Namespace

Drupal\Tests\language\Functional

Code

public function testLanguageBlockWithDomain() : void {
    // Add the Italian language.
    ConfigurableLanguage::createFromLangcode('it')->save();
    // Rebuild the container so that the new language is picked up by services
    // that hold a list of languages.
    $this->rebuildContainer();
    $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');
    // Change the domain for the Italian language.
    $edit = [
        'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
        'domain[en]' => \Drupal::request()->getHost(),
        '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');
    // Enable the language switcher block.
    $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, [
        'id' => 'test_language_block',
    ]);
    $this->drupalGet('');
    
    /** @var \Drupal\Core\Routing\UrlGenerator $generator */
    $generator = $this->container
        ->get('url_generator');
    // Verify the English URL is correct
    $english_url = $generator->generateFromRoute('entity.user.canonical', [
        'user' => 2,
    ], [
        'language' => $languages['en'],
    ]);
    $this->assertSession()
        ->elementAttributeContains('xpath', '//div[@id="block-test-language-block"]/ul/li/a[@hreflang="en"]', 'href', $english_url);
    // Verify the Italian URL is correct
    $italian_url = $generator->generateFromRoute('entity.user.canonical', [
        'user' => 2,
    ], [
        'language' => $languages['it'],
    ]);
    $this->assertSession()
        ->elementAttributeContains('xpath', '//div[@id="block-test-language-block"]/ul/li/a[@hreflang="it"]', 'href', $italian_url);
}

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