function LanguageSwitchingTest::testLanguageBlockWithDomain

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

Test language switcher links for domain based negotiation.

File

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

Class

LanguageSwitchingTest
Functional tests for the language switching feature.

Namespace

Drupal\Tests\language\Functional

Code

public function testLanguageBlockWithDomain() {
    // 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->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(t('The domain may not be left blank for English'), 'The form does not allow blank domains.');
    // 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->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
    $this->assertText(t('The configuration options have been saved'), 'Domain configuration is saved.');
    // 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
    list($english_link) = $this->xpath('//div[@id=:id]/ul/li/a[@hreflang=:hreflang]', [
        ':id' => 'block-test-language-block',
        ':hreflang' => 'en',
    ]);
    $english_url = $generator->generateFromRoute('entity.user.canonical', [
        'user' => 2,
    ], [
        'language' => $languages['en'],
    ]);
    $this->assertEqual($english_url, $english_link->getAttribute('href'));
    // Verify the Italian URL is correct
    list($italian_link) = $this->xpath('//div[@id=:id]/ul/li/a[@hreflang=:hreflang]', [
        ':id' => 'block-test-language-block',
        ':hreflang' => 'it',
    ]);
    $italian_url = $generator->generateFromRoute('entity.user.canonical', [
        'user' => 2,
    ], [
        'language' => $languages['it'],
    ]);
    $this->assertEqual($italian_url, $italian_link->getAttribute('href'));
}

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