You are here

protected function LanguageSwitchingTest::doTestLanguageBlockAnonymous in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/language/tests/src/Functional/LanguageSwitchingTest.php \Drupal\Tests\language\Functional\LanguageSwitchingTest::doTestLanguageBlockAnonymous()
  2. 9 core/modules/language/tests/src/Functional/LanguageSwitchingTest.php \Drupal\Tests\language\Functional\LanguageSwitchingTest::doTestLanguageBlockAnonymous()

For anonymous users, the "active" class is set by PHP.

Parameters

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

See also

self::testLanguageBlock()

File

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

Class

LanguageSwitchingTest
Functional tests for the language switching feature.

Namespace

Drupal\Tests\language\Functional

Code

protected function doTestLanguageBlockAnonymous($block_label) {
  $this
    ->drupalLogout();

  // Assert that the language switching block is displayed on the frontpage
  // and ensure that the active class is added when query params are present.
  $this
    ->drupalGet('', [
    'query' => [
      'foo' => 'bar',
    ],
  ]);
  $this
    ->assertSession()
    ->pageTextContains($block_label);

  // Assert that only the current language is marked as active.
  $language_switchers = $this
    ->xpath('//div[@id=:id]/ul/li', [
    ':id' => 'block-test-language-block',
  ]);
  $links = [
    'active' => [],
    'inactive' => [],
  ];
  $anchors = [
    'active' => [],
    'inactive' => [],
  ];
  $labels = [];
  foreach ($language_switchers as $list_item) {
    $classes = explode(" ", $list_item
      ->getAttribute('class'));
    [
      $langcode,
    ] = array_intersect($classes, [
      'en',
      'fr',
    ]);
    if (in_array('is-active', $classes)) {
      $links['active'][] = $langcode;
    }
    else {
      $links['inactive'][] = $langcode;
    }
    $link = $list_item
      ->find('xpath', 'a');
    $anchor_classes = explode(" ", $link
      ->getAttribute('class'));
    if (in_array('is-active', $anchor_classes)) {
      $anchors['active'][] = $langcode;
    }
    else {
      $anchors['inactive'][] = $langcode;
    }
    $labels[] = $link
      ->getText();
  }
  $this
    ->assertSame([
    'active' => [
      'en',
    ],
    'inactive' => [
      'fr',
    ],
  ], $links, 'Only the current language list item is marked as active on the language switcher block.');
  $this
    ->assertSame([
    'active' => [
      'en',
    ],
    'inactive' => [
      'fr',
    ],
  ], $anchors, 'Only the current language anchor is marked as active on the language switcher block.');
  $this
    ->assertSame([
    'English',
    'français',
  ], $labels, 'The language links labels are in their own language on the language switcher block.');
}