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