Chris@16: siteDirectory . '/files/translations', 0777, TRUE); Chris@16: file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', $this->getPo('de')); Chris@16: file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.es.po', $this->getPo('es')); Chris@16: Chris@16: parent::setUpLanguage(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns the string for the test .po file. Chris@16: * Chris@16: * @param string $langcode Chris@16: * The language code. Chris@16: * @return string Chris@16: * Contents for the test .po file. Chris@16: */ Chris@16: protected function getPo($langcode) { Chris@16: return <<langcode; Chris@16: return $params; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests that translations ended up at the expected places. Chris@16: */ Chris@16: public function testTranslationsLoaded() { Chris@16: // Ensure the title is correct. Chris@16: $this->assertEqual('SITE_NAME_' . $this->langcode, \Drupal::config('system.site')->get('name')); Chris@16: Chris@16: // Verify German and Spanish were configured. Chris@16: $this->drupalGet('admin/config/regional/language'); Chris@16: $this->assertText('German'); Chris@16: $this->assertText('Spanish'); Chris@16: // If the installer was English or we used a profile that keeps English, we Chris@16: // expect that configured also. Otherwise English should not be configured Chris@16: // on the site. Chris@16: if ($this->langcode == 'en' || $this->profile == 'testing_multilingual_with_english') { Chris@16: $this->assertText('English'); Chris@16: } Chris@16: else { Chris@16: $this->assertNoText('English'); Chris@16: } Chris@16: Chris@16: // Verify the strings from the translation files were imported. Chris@16: $this->verifyImportedStringsTranslated(); Chris@16: Chris@16: /** @var \Drupal\language\ConfigurableLanguageManager $language_manager */ Chris@16: $language_manager = \Drupal::languageManager(); Chris@16: Chris@16: // If the site was installed in a foreign language (only tested with German Chris@16: // in subclasses), then the active configuration should be updated and no Chris@16: // override should exist in German. Otherwise the German translation should Chris@16: // end up in overrides the same way as Spanish (which is not used as a site Chris@16: // installation language). English should be available based on profile Chris@16: // information and should be possible to add if not yet added, making Chris@16: // English overrides available. Chris@16: Chris@16: $config = \Drupal::config('user.settings'); Chris@16: $override_de = $language_manager->getLanguageConfigOverride('de', 'user.settings'); Chris@16: $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings'); Chris@16: $override_es = $language_manager->getLanguageConfigOverride('es', 'user.settings'); Chris@16: Chris@16: if ($this->langcode == 'de') { Chris@16: // Active configuration should be in German and no German override should Chris@16: // exist. Chris@16: $this->assertEqual($config->get('anonymous'), 'Anonymous de'); Chris@16: $this->assertEqual($config->get('langcode'), 'de'); Chris@16: $this->assertTrue($override_de->isNew()); Chris@16: Chris@16: if ($this->profile == 'testing_multilingual_with_english') { Chris@16: // English is already added in this profile. Should make the override Chris@16: // available. Chris@16: $this->assertEqual($override_en->get('anonymous'), 'Anonymous'); Chris@16: } Chris@16: else { Chris@16: // English is not yet available. Chris@16: $this->assertTrue($override_en->isNew()); Chris@16: Chris@16: // Adding English should make the English override available. Chris@16: $edit = ['predefined_langcode' => 'en']; Chris@16: $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); Chris@16: $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings'); Chris@16: $this->assertEqual($override_en->get('anonymous'), 'Anonymous'); Chris@16: } Chris@16: Chris@16: // Activate a module, to make sure that config is not overridden by module Chris@16: // installation. Chris@16: $edit = [ Chris@16: 'modules[views][enable]' => TRUE, Chris@16: 'modules[filter][enable]' => TRUE, Chris@16: ]; Chris@16: $this->drupalPostForm('admin/modules', $edit, t('Install')); Chris@16: Chris@16: // Verify the strings from the translation are still as expected. Chris@16: $this->verifyImportedStringsTranslated(); Chris@16: } Chris@16: else { Chris@16: // Active configuration should be English. Chris@16: $this->assertEqual($config->get('anonymous'), 'Anonymous'); Chris@16: $this->assertEqual($config->get('langcode'), 'en'); Chris@16: // There should not be an English override. Chris@16: $this->assertTrue($override_en->isNew()); Chris@16: // German should be an override. Chris@16: $this->assertEqual($override_de->get('anonymous'), 'Anonymous de'); Chris@16: } Chris@16: Chris@16: // Spanish is always an override (never used as installation language). Chris@16: $this->assertEqual($override_es->get('anonymous'), 'Anonymous es'); Chris@16: Chris@16: } Chris@16: Chris@16: /** Chris@16: * Helper function to verify that the expected strings are translated. Chris@16: */ Chris@16: protected function verifyImportedStringsTranslated() { Chris@16: $test_samples = ['Save and continue', 'Anonymous', 'Language']; Chris@16: $langcodes = ['de', 'es']; Chris@16: Chris@16: foreach ($test_samples as $sample) { Chris@16: foreach ($langcodes as $langcode) { Chris@16: $edit = []; Chris@16: $edit['langcode'] = $langcode; Chris@16: $edit['translation'] = 'translated'; Chris@16: $edit['string'] = $sample; Chris@16: $this->drupalPostForm('admin/config/regional/translate', $edit, t('Filter')); Chris@16: $this->assertText($sample . ' ' . $langcode); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: }