You are here

protected function ConfigurableLanguageManagerTest::setUp in Drupal 9

Same name in this branch
  1. 9 core/modules/language/tests/src/Functional/ConfigurableLanguageManagerTest.php \Drupal\Tests\language\Functional\ConfigurableLanguageManagerTest::setUp()
  2. 9 core/modules/language/tests/src/Kernel/ConfigurableLanguageManagerTest.php \Drupal\Tests\language\Kernel\ConfigurableLanguageManagerTest::setUp()
Same name and namespace in other branches
  1. 8 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 45

Class

ConfigurableLanguageManagerTest
Tests Language Negotiation.

Namespace

Drupal\Tests\language\Functional

Code

protected function setUp() : void {
  parent::setUp();

  /** @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' => t('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',
  ]);
}