function ConfigurableLanguageManagerTest::setUp

Same name in this branch
  1. 11.x core/modules/language/tests/src/Kernel/ConfigurableLanguageManagerTest.php \Drupal\Tests\language\Kernel\ConfigurableLanguageManagerTest::setUp()
Same name and namespace in other branches
  1. 9 core/modules/language/tests/src/Kernel/ConfigurableLanguageManagerTest.php \Drupal\Tests\language\Kernel\ConfigurableLanguageManagerTest::setUp()
  2. 9 core/modules/language/tests/src/Functional/ConfigurableLanguageManagerTest.php \Drupal\Tests\language\Functional\ConfigurableLanguageManagerTest::setUp()
  3. 8.9.x core/modules/language/tests/src/Kernel/ConfigurableLanguageManagerTest.php \Drupal\Tests\language\Kernel\ConfigurableLanguageManagerTest::setUp()
  4. 8.9.x core/modules/language/tests/src/Functional/ConfigurableLanguageManagerTest.php \Drupal\Tests\language\Functional\ConfigurableLanguageManagerTest::setUp()
  5. 10 core/modules/language/tests/src/Kernel/ConfigurableLanguageManagerTest.php \Drupal\Tests\language\Kernel\ConfigurableLanguageManagerTest::setUp()
  6. 10 core/modules/language/tests/src/Functional/ConfigurableLanguageManagerTest.php \Drupal\Tests\language\Functional\ConfigurableLanguageManagerTest::setUp()

Overrides BrowserTestBase::setUp

File

core/modules/language/tests/src/Functional/ConfigurableLanguageManagerTest.php, line 52

Class

ConfigurableLanguageManagerTest
Tests Language Negotiation.

Namespace

Drupal\Tests\language\Functional

Code

protected function setUp() : void {
    parent::setUp();
    // The \Drupal\locale\LocaleTranslation service clears caches after the
    // response is flushed to the client. We use WaitTerminateTestTrait to wait
    // for Drupal to perform its termination work before continuing.
    $this->setWaitForTerminate();
    
    /** @var \Drupal\user\UserInterface $user */
    $user = $this->createUser([], '', TRUE);
    $this->drupalLogin($user);
    ConfigurableLanguage::createFromLangcode('es')->save();
    // Create a page node type and make it translatable.
    NodeType::create([
        'type' => 'page',
        'name' => 'Page',
    ])->save();
    $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'page');
    $config->setDefaultLangcode('en')
        ->setLanguageAlterable(TRUE)
        ->save();
    // Create a Node with title 'English' and translate it to Spanish.
    $node = Node::create([
        'type' => 'page',
        'title' => 'English',
    ]);
    $node->save();
    $node->addTranslation('es', [
        'title' => 'Español',
    ]);
    $node->save();
    // Enable both language_interface and language_content language negotiation.
    \Drupal::getContainer()->get('language_negotiator')
        ->updateConfiguration([
        'language_interface',
        'language_content',
    ]);
    // Set the preferred language of the user for admin pages to English.
    $user->set('preferred_admin_langcode', 'en')
        ->save();
    // Make sure node edit pages are administration pages.
    $this->config('node.settings')
        ->set('use_admin_theme', '1')
        ->save();
    $this->container
        ->get('router.builder')
        ->rebuild();
    // Place a Block with a translatable string on the page.
    $this->placeBlock('system_powered_by_block', [
        'region' => 'content',
    ]);
    // Load the Spanish Node page once, to register the translatable string.
    $this->drupalGet('/es/node/1');
    // Translate the Powered by string.
    
    /** @var \Drupal\locale\StringStorageInterface $string_storage */
    $string_storage = \Drupal::getContainer()->get('locale.storage');
    $source = $string_storage->findString([
        'source' => 'Powered by <a href=":poweredby">Drupal</a>',
    ]);
    $string_storage->createTranslation([
        'lid' => $source->lid,
        'language' => 'es',
        'translation' => 'Funciona con ...',
    ])
        ->save();
    // Invalidate caches so that the new translation will be used.
    Cache::invalidateTags([
        'rendered',
        'locale',
    ]);
}

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