InstallerTranslationMultipleLanguageTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Drupal\FunctionalTests\Installer;
  3. /**
  4. * Tests translation files for multiple languages get imported during install.
  5. *
  6. * @group Installer
  7. */
  8. class InstallerTranslationMultipleLanguageTest extends InstallerTestBase {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. protected $defaultTheme = 'stark';
  13. /**
  14. * Switch to the multilingual testing profile.
  15. *
  16. * @var string
  17. */
  18. protected $profile = 'testing_multilingual';
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setUpLanguage() {
  23. // Place custom local translations in the translations directory.
  24. mkdir(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE);
  25. file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', $this->getPo('de'));
  26. file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.es.po', $this->getPo('es'));
  27. parent::setUpLanguage();
  28. }
  29. /**
  30. * Returns the string for the test .po file.
  31. *
  32. * @param string $langcode
  33. * The language code.
  34. *
  35. * @return string
  36. * Contents for the test .po file.
  37. */
  38. protected function getPo($langcode) {
  39. return <<<ENDPO
  40. msgid ""
  41. msgstr ""
  42. msgid "Save and continue"
  43. msgstr "Save and continue $langcode"
  44. msgid "Anonymous"
  45. msgstr "Anonymous $langcode"
  46. msgid "Language"
  47. msgstr "Language $langcode"
  48. #: Testing site name configuration during the installer.
  49. msgid "Drupal"
  50. msgstr "Drupal"
  51. ENDPO;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. protected function installParameters() {
  57. $params = parent::installParameters();
  58. $params['forms']['install_configure_form']['site_name'] = 'SITE_NAME_' . $this->langcode;
  59. return $params;
  60. }
  61. /**
  62. * Tests that translations ended up at the expected places.
  63. */
  64. public function testTranslationsLoaded() {
  65. // Ensure the title is correct.
  66. $this->assertEqual('SITE_NAME_' . $this->langcode, \Drupal::config('system.site')->get('name'));
  67. // Verify German and Spanish were configured.
  68. $this->drupalGet('admin/config/regional/language');
  69. $this->assertText('German');
  70. $this->assertText('Spanish');
  71. // If the installer was English or we used a profile that keeps English, we
  72. // expect that configured also. Otherwise English should not be configured
  73. // on the site.
  74. if ($this->langcode == 'en' || $this->profile == 'testing_multilingual_with_english') {
  75. $this->assertText('English');
  76. }
  77. else {
  78. $this->assertNoText('English');
  79. }
  80. // Verify the strings from the translation files were imported.
  81. $this->verifyImportedStringsTranslated();
  82. /** @var \Drupal\language\ConfigurableLanguageManager $language_manager */
  83. $language_manager = \Drupal::languageManager();
  84. // If the site was installed in a foreign language (only tested with German
  85. // in subclasses), then the active configuration should be updated and no
  86. // override should exist in German. Otherwise the German translation should
  87. // end up in overrides the same way as Spanish (which is not used as a site
  88. // installation language). English should be available based on profile
  89. // information and should be possible to add if not yet added, making
  90. // English overrides available.
  91. $config = \Drupal::config('user.settings');
  92. $override_de = $language_manager->getLanguageConfigOverride('de', 'user.settings');
  93. $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
  94. $override_es = $language_manager->getLanguageConfigOverride('es', 'user.settings');
  95. if ($this->langcode == 'de') {
  96. // Active configuration should be in German and no German override should
  97. // exist.
  98. $this->assertEqual($config->get('anonymous'), 'Anonymous de');
  99. $this->assertEqual($config->get('langcode'), 'de');
  100. $this->assertTrue($override_de->isNew());
  101. if ($this->profile == 'testing_multilingual_with_english') {
  102. // English is already added in this profile. Should make the override
  103. // available.
  104. $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
  105. }
  106. else {
  107. // English is not yet available.
  108. $this->assertTrue($override_en->isNew());
  109. // Adding English should make the English override available.
  110. $edit = ['predefined_langcode' => 'en'];
  111. $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
  112. $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
  113. $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
  114. }
  115. // Activate a module, to make sure that config is not overridden by module
  116. // installation.
  117. $edit = [
  118. 'modules[views][enable]' => TRUE,
  119. 'modules[filter][enable]' => TRUE,
  120. ];
  121. $this->drupalPostForm('admin/modules', $edit, t('Install'));
  122. // Verify the strings from the translation are still as expected.
  123. $this->verifyImportedStringsTranslated();
  124. }
  125. else {
  126. // Active configuration should be English.
  127. $this->assertEqual($config->get('anonymous'), 'Anonymous');
  128. $this->assertEqual($config->get('langcode'), 'en');
  129. // There should not be an English override.
  130. $this->assertTrue($override_en->isNew());
  131. // German should be an override.
  132. $this->assertEqual($override_de->get('anonymous'), 'Anonymous de');
  133. }
  134. // Spanish is always an override (never used as installation language).
  135. $this->assertEqual($override_es->get('anonymous'), 'Anonymous es');
  136. }
  137. /**
  138. * Helper function to verify that the expected strings are translated.
  139. */
  140. protected function verifyImportedStringsTranslated() {
  141. $test_samples = ['Save and continue', 'Anonymous', 'Language'];
  142. $langcodes = ['de', 'es'];
  143. foreach ($test_samples as $sample) {
  144. foreach ($langcodes as $langcode) {
  145. $edit = [];
  146. $edit['langcode'] = $langcode;
  147. $edit['translation'] = 'translated';
  148. $edit['string'] = $sample;
  149. $this->drupalPostForm('admin/config/regional/translate', $edit, t('Filter'));
  150. $this->assertText($sample . ' ' . $langcode);
  151. }
  152. }
  153. }
  154. }