InstallerTranslationMultipleLanguageTest.php 6.0 KB

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