You are here

protected function LanguageSwitchingTest::doTestLanguageBlockAuthenticated in Drupal 8

Same name and namespace in other branches
  1. 9 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 85

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
    ->assertText($block_label, 'Language switcher block found.');

  // 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
    ->assertIdentical($list_items, $expected_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
    ->assertIdentical($anchors, $expected_anchors, 'The anchors have the correct attributes that will allow the drupal.active-link library to mark them as active.');
  $settings = $this
    ->getDrupalSettings();
  $this
    ->assertIdentical($settings['path']['currentPath'], 'user/2', 'drupalSettings.path.currentPath is set correctly to allow drupal.active-link to mark the correct links as active.');
  $this
    ->assertIdentical($settings['path']['isFront'], FALSE, 'drupalSettings.path.isFront is set correctly to allow drupal.active-link to mark the correct links as active.');
  $this
    ->assertIdentical($settings['path']['currentLanguage'], 'en', 'drupalSettings.path.currentLanguage is set correctly to allow drupal.active-link to mark the correct links as active.');
  $this
    ->assertIdentical($labels, [
    'English',
    'français',
  ], 'The language links labels are in their own language on the language switcher block.');
}