function LanguageSwitchingTest::doTestHomePageLinks

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

The home page link should be "/" or "/{language_prefix}".

Parameters

string $block_label: The label of the language switching block.

See also

self::testLanguageBlock()

1 call to LanguageSwitchingTest::doTestHomePageLinks()
LanguageSwitchingTest::testLanguageBlock in core/modules/language/tests/src/Functional/LanguageSwitchingTest.php
Functional tests for the language switcher block.

File

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

Class

LanguageSwitchingTest
Functional tests for the language switching feature.

Namespace

Drupal\Tests\language\Functional

Code

protected function doTestHomePageLinks($block_label) {
  // Create a node and set as home page.
  $this->createHomePage();
  // Go to home page.
  $this->DrupalGet('<front>');
  // The language switcher block should display.
  $this->assertSession()
    ->pageTextContains($block_label);
  // Assert that each list item and anchor element has the appropriate data-
  // attributes.
  $language_switchers = $this->xpath('//div[@id=:id]/ul/li', [
    ':id' => 'block-test-language-block',
  ]);
  $list_items = [];
  $anchors = [];
  $labels = [];
  foreach ($language_switchers as $list_item) {
    $list_items[] = [
      'hreflang' => $list_item->getAttribute('hreflang'),
      'data-drupal-link-system-path' => $list_item->getAttribute('data-drupal-link-system-path'),
    ];
    $link = $list_item->find('xpath', 'a');
    $anchors[] = [
      'hreflang' => $link->getAttribute('hreflang'),
      'data-drupal-link-system-path' => $link->getAttribute('data-drupal-link-system-path'),
      'href' => $link->getAttribute('href'),
    ];
    $labels[] = $link->getText();
  }
  $expected_list_items = [
    0 => [
      'hreflang' => 'en',
      'data-drupal-link-system-path' => '<front>',
    ],
    1 => [
      'hreflang' => 'fr',
      'data-drupal-link-system-path' => '<front>',
    ],
  ];
  $this->assertSame($expected_list_items, $list_items, 'The list items have the correct attributes that will contain the correct home page links.');
  $expected_anchors = [
    0 => [
      'hreflang' => 'en',
      'data-drupal-link-system-path' => '<front>',
      'href' => Url::fromRoute('<front>')->toString(),
    ],
    1 => [
      'hreflang' => 'fr',
      'data-drupal-link-system-path' => '<front>',
      'href' => Url::fromRoute('<front>')->toString() . 'fr',
    ],
  ];
  $this->assertSame($expected_anchors, $anchors, 'The anchors have the correct attributes that will link to the correct home page in that language.');
  $this->assertSame([
    'English',
    'français',
  ], $labels, 'The language links labels are in their own language on the language switcher block.');
}

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