function InstallerTranslationMultipleLanguageTest::testTranslationsLoaded

Same name in other branches
  1. 8.9.x core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationMultipleLanguageTest.php \Drupal\FunctionalTests\Installer\InstallerTranslationMultipleLanguageTest::testTranslationsLoaded()
  2. 10 core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationMultipleLanguageTest.php \Drupal\FunctionalTests\Installer\InstallerTranslationMultipleLanguageTest::testTranslationsLoaded()
  3. 11.x core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationMultipleLanguageTest.php \Drupal\FunctionalTests\Installer\InstallerTranslationMultipleLanguageTest::testTranslationsLoaded()

Tests that translations ended up at the expected places.

File

core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationMultipleLanguageTest.php, line 77

Class

InstallerTranslationMultipleLanguageTest
Tests translation files for multiple languages get imported during install.

Namespace

Drupal\FunctionalTests\Installer

Code

public function testTranslationsLoaded() {
    // Ensure the title is correct.
    $this->assertEquals('SITE_NAME_' . $this->langcode, \Drupal::config('system.site')->get('name'));
    // Verify German and Spanish were configured.
    $this->drupalGet('admin/config/regional/language');
    $this->assertSession()
        ->pageTextContains('German');
    $this->assertSession()
        ->pageTextContains('Spanish');
    // If the installer was English or we used a profile that keeps English, we
    // expect that configured also. Otherwise English should not be configured
    // on the site.
    if ($this->langcode == 'en' || $this->profile == 'testing_multilingual_with_english') {
        $this->assertSession()
            ->pageTextContains('English');
    }
    else {
        $this->assertSession()
            ->pageTextNotContains('English');
    }
    // Verify the strings from the translation files were imported.
    $this->verifyImportedStringsTranslated();
    
    /** @var \Drupal\language\ConfigurableLanguageManager $language_manager */
    $language_manager = \Drupal::languageManager();
    // If the site was installed in a foreign language (only tested with German
    // in subclasses), then the active configuration should be updated and no
    // override should exist in German. Otherwise the German translation should
    // end up in overrides the same way as Spanish (which is not used as a site
    // installation language). English should be available based on profile
    // information and should be possible to add if not yet added, making
    // English overrides available.
    $config = \Drupal::config('user.settings');
    $override_de = $language_manager->getLanguageConfigOverride('de', 'user.settings');
    $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
    $override_es = $language_manager->getLanguageConfigOverride('es', 'user.settings');
    if ($this->langcode == 'de') {
        // Active configuration should be in German and no German override should
        // exist.
        $this->assertEquals('Anonymous de', $config->get('anonymous'));
        $this->assertEquals('de', $config->get('langcode'));
        $this->assertTrue($override_de->isNew());
        if ($this->profile == 'testing_multilingual_with_english') {
            // English is already added in this profile. Should make the override
            // available.
            $this->assertEquals('Anonymous', $override_en->get('anonymous'));
        }
        else {
            // English is not yet available.
            $this->assertTrue($override_en->isNew());
            // Adding English should make the English override available.
            $edit = [
                'predefined_langcode' => 'en',
            ];
            $this->drupalGet('admin/config/regional/language/add');
            $this->submitForm($edit, 'Add language');
            $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
            $this->assertEquals('Anonymous', $override_en->get('anonymous'));
        }
        // Activate a module, to make sure that config is not overridden by module
        // installation.
        $edit = [
            'modules[views][enable]' => TRUE,
            'modules[filter][enable]' => TRUE,
        ];
        $this->drupalGet('admin/modules');
        $this->submitForm($edit, 'Install');
        // Verify the strings from the translation are still as expected.
        $this->verifyImportedStringsTranslated();
    }
    else {
        // Active configuration should be English.
        $this->assertEquals('Anonymous', $config->get('anonymous'));
        $this->assertEquals('en', $config->get('langcode'));
        // There should not be an English override.
        $this->assertTrue($override_en->isNew());
        // German should be an override.
        $this->assertEquals('Anonymous de', $override_de->get('anonymous'));
    }
    // Spanish is always an override (never used as installation language).
    $this->assertEquals('Anonymous es', $override_es->get('anonymous'));
}

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