function LanguageSwitchingTest::doTestLanguageBlockAnonymous

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

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->assertText($block_label, 'Language switcher block found.');
    // 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'));
        list($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->assertIdentical($links, [
        'active' => [
            'en',
        ],
        'inactive' => [
            'fr',
        ],
    ], 'Only the current language list item is marked as active on the language switcher block.');
    $this->assertIdentical($anchors, [
        'active' => [
            'en',
        ],
        'inactive' => [
            'fr',
        ],
    ], 'Only the current language anchor is marked as active on the language switcher block.');
    $this->assertIdentical($labels, [
        'English',
        'français',
    ], '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.