You are here

protected function LanguageSwitchingTest::doTestLanguageLinkActiveClassAuthenticated in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/language/src/Tests/LanguageSwitchingTest.php \Drupal\language\Tests\LanguageSwitchingTest::doTestLanguageLinkActiveClassAuthenticated()

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

See also

testLanguageLinkActiveClass()

1 call to LanguageSwitchingTest::doTestLanguageLinkActiveClassAuthenticated()
LanguageSwitchingTest::testLanguageLinkActiveClass in core/modules/language/src/Tests/LanguageSwitchingTest.php
Test active class on links when switching languages.

File

core/modules/language/src/Tests/LanguageSwitchingTest.php, line 292
Contains \Drupal\language\Tests\LanguageSwitchingTest.

Class

LanguageSwitchingTest
Functional tests for the language switching feature.

Namespace

Drupal\language\Tests

Code

protected function doTestLanguageLinkActiveClassAuthenticated() {
  $function_name = '#type link';
  $path = 'language_test/type-link-active-class';

  // Test links generated by the link generator on an English page.
  $current_language = 'English';
  $this
    ->drupalGet($path);

  // Language code 'none' link should be active.
  $langcode = 'none';
  $links = $this
    ->xpath('//a[@id = :id and @data-drupal-link-system-path = :path]', array(
    ':id' => 'no_lang_link',
    ':path' => $path,
  ));
  $this
    ->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.', array(
    ':function' => $function_name,
    ':language' => $current_language,
    ':langcode' => $langcode,
  )));

  // Language code 'en' link should be active.
  $langcode = 'en';
  $links = $this
    ->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', array(
    ':id' => 'en_link',
    ':lang' => 'en',
    ':path' => $path,
  ));
  $this
    ->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.', array(
    ':function' => $function_name,
    ':language' => $current_language,
    ':langcode' => $langcode,
  )));

  // Language code 'fr' link should not be active.
  $langcode = 'fr';
  $links = $this
    ->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', array(
    ':id' => 'fr_link',
    ':lang' => 'fr',
    ':path' => $path,
  ));
  $this
    ->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to NOT mark it as active.', array(
    ':function' => $function_name,
    ':language' => $current_language,
    ':langcode' => $langcode,
  )));

  // Verify that drupalSettings contains the correct values.
  $settings = $this
    ->getDrupalSettings();
  $this
    ->assertIdentical($settings['path']['currentPath'], $path, '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.');

  // Test links generated by the link generator on a French page.
  $current_language = 'French';
  $this
    ->drupalGet('fr/language_test/type-link-active-class');

  // Language code 'none' link should be active.
  $langcode = 'none';
  $links = $this
    ->xpath('//a[@id = :id and @data-drupal-link-system-path = :path]', array(
    ':id' => 'no_lang_link',
    ':path' => $path,
  ));
  $this
    ->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.', array(
    ':function' => $function_name,
    ':language' => $current_language,
    ':langcode' => $langcode,
  )));

  // Language code 'en' link should not be active.
  $langcode = 'en';
  $links = $this
    ->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', array(
    ':id' => 'en_link',
    ':lang' => 'en',
    ':path' => $path,
  ));
  $this
    ->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to NOT mark it as active.', array(
    ':function' => $function_name,
    ':language' => $current_language,
    ':langcode' => $langcode,
  )));

  // Language code 'fr' link should be active.
  $langcode = 'fr';
  $links = $this
    ->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', array(
    ':id' => 'fr_link',
    ':lang' => 'fr',
    ':path' => $path,
  ));
  $this
    ->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.', array(
    ':function' => $function_name,
    ':language' => $current_language,
    ':langcode' => $langcode,
  )));

  // Verify that drupalSettings contains the correct values.
  $settings = $this
    ->getDrupalSettings();
  $this
    ->assertIdentical($settings['path']['currentPath'], $path, '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'], 'fr', 'drupalSettings.path.currentLanguage is set correctly to allow drupal.active-link to mark the correct links as active.');
}