You are here

public function LanguageSwitchingTest::testLanguageBlockWithDomain in Drupal 9

Same name and namespace in other branches
  1. 8 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()

Tests language switcher links for domain based negotiation.

File

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

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
    ->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()
    ->pageTextContains('The domain may not be left blank for English');

  // 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()
    ->pageTextContains('The configuration options have been 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
  $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);
}