function BookMultilingualTest::setCurrentLanguage

Same name in other branches
  1. 9 core/modules/book/tests/src/Kernel/BookMultilingualTest.php \Drupal\Tests\book\Kernel\BookMultilingualTest::setCurrentLanguage()
  2. 11.x core/modules/book/tests/src/Kernel/BookMultilingualTest.php \Drupal\Tests\book\Kernel\BookMultilingualTest::setCurrentLanguage()

Sets the current language.

Parameters

string $langcode: The langcode. The content language will be set to this using the appropriate domain while the user language will be set to something else so subsequent tests catch errors where the interface language is used instead of the content language.

3 calls to BookMultilingualTest::setCurrentLanguage()
BookMultilingualTest::testMultilingualBookBreadcrumbBuilder in core/modules/book/tests/src/Kernel/BookMultilingualTest.php
Tests various book breadcrumb builder methods return correct translations.
BookMultilingualTest::testMultilingualBookExport in core/modules/book/tests/src/Kernel/BookMultilingualTest.php
Tests the book export returns correct translations.
BookMultilingualTest::testMultilingualBookManager in core/modules/book/tests/src/Kernel/BookMultilingualTest.php
Tests various book manager methods return correct translations.

File

core/modules/book/tests/src/Kernel/BookMultilingualTest.php, line 278

Class

BookMultilingualTest
Tests multilingual books.

Namespace

Drupal\Tests\book\Kernel

Code

protected function setCurrentLanguage(string $langcode) : void {
    $request = Request::create("http://{$langcode}.book.test.domain/");
    $request->setSession(new Session(new MockArraySessionStorage()));
    \Drupal::requestStack()->push($request);
    $language_manager = $this->container
        ->get('language_manager');
    $language_manager->reset();
    $current_user = \Drupal::currentUser();
    $languages = $language_manager->getLanguages();
    unset($languages[$langcode]);
    $current_user->getAccount()
        ->set('preferred_langcode', reset($languages)->getId());
    $this->assertNotSame($current_user->getPreferredLangcode(), $langcode);
}

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