function LanguageSwitchingTest::doTestLanguageBlockAnonymous

Same name in other branches
  1. 9 core/modules/language/tests/src/Functional/LanguageSwitchingTest.php \Drupal\Tests\language\Functional\LanguageSwitchingTest::doTestLanguageBlockAnonymous()
  2. 8.9.x core/modules/language/tests/src/Functional/LanguageSwitchingTest.php \Drupal\Tests\language\Functional\LanguageSwitchingTest::doTestLanguageBlockAnonymous()
  3. 11.x 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()

1 call to LanguageSwitchingTest::doTestLanguageBlockAnonymous()
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 222

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) {
        $langcode = $list_item->getAttribute('hreflang');
        if ($list_item->hasClass('is-active')) {
            $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.');
}

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