You are here

protected function LanguageSwitchingTest::doTestLanguageBlockAuthenticated 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::doTestLanguageBlockAuthenticated()

For authenticated users, the "active" class is set by JavaScript.

Parameters

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

See also

self::testLanguageBlock()

1 call to LanguageSwitchingTest::doTestLanguageBlockAuthenticated()
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 87

Class

LanguageSwitchingTest
Functional tests for the language switching feature.

Namespace

Drupal\Tests\language\Functional

Code

protected function doTestLanguageBlockAuthenticated($block_label) {

  // Assert that the language switching block is displayed on the frontpage.
  $this
    ->drupalGet('');
  $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) {
    $classes = explode(" ", $list_item
      ->getAttribute('class'));
    list($langcode) = array_intersect($classes, [
      'en',
      'fr',
    ]);
    $list_items[] = [
      'langcode_class' => $langcode,
      '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'),
    ];
    $labels[] = $link
      ->getText();
  }
  $expected_list_items = [
    0 => [
      'langcode_class' => 'en',
      'data-drupal-link-system-path' => 'user/2',
    ],
    1 => [
      'langcode_class' => 'fr',
      'data-drupal-link-system-path' => 'user/2',
    ],
  ];
  $this
    ->assertSame($expected_list_items, $list_items, 'The list items have the correct attributes that will allow the drupal.active-link library to mark them as active.');
  $expected_anchors = [
    0 => [
      'hreflang' => 'en',
      'data-drupal-link-system-path' => 'user/2',
    ],
    1 => [
      'hreflang' => 'fr',
      'data-drupal-link-system-path' => 'user/2',
    ],
  ];
  $this
    ->assertSame($expected_anchors, $anchors, 'The anchors have the correct attributes that will allow the drupal.active-link library to mark them as active.');
  $settings = $this
    ->getDrupalSettings();
  $this
    ->assertSame('user/2', $settings['path']['currentPath'], 'drupalSettings.path.currentPath is set correctly to allow drupal.active-link to mark the correct links as active.');
  $this
    ->assertFalse($settings['path']['isFront'], 'drupalSettings.path.isFront is set correctly to allow drupal.active-link to mark the correct links as active.');
  $this
    ->assertSame('en', $settings['path']['currentLanguage'], 'drupalSettings.path.currentLanguage is set correctly to allow drupal.active-link to mark the correct links as active.');
  $this
    ->assertSame([
    'English',
    'français',
  ], $labels, 'The language links labels are in their own language on the language switcher block.');
}