InstallerTranslationTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Drupal\FunctionalTests\Installer;
  3. use Drupal\Core\Database\Database;
  4. use Drupal\user\Entity\User;
  5. /**
  6. * Installs Drupal in German and checks resulting site.
  7. *
  8. * @group Installer
  9. */
  10. class InstallerTranslationTest extends InstallerTestBase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. protected $defaultTheme = 'classy';
  15. /**
  16. * Overrides the language code in which to install Drupal.
  17. *
  18. * @var string
  19. */
  20. protected $langcode = 'de';
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected function setUpLanguage() {
  25. // Place a custom local translation in the translations directory.
  26. mkdir($this->root . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE);
  27. file_put_contents($this->root . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', $this->getPo('de'));
  28. parent::setUpLanguage();
  29. // After selecting a different language than English, all following screens
  30. // should be translated already.
  31. $elements = $this->xpath('//input[@type="submit"]/@value');
  32. $this->assertEqual(current($elements)->getText(), 'Save and continue de');
  33. $this->translations['Save and continue'] = 'Save and continue de';
  34. // Check the language direction.
  35. $direction = current($this->xpath('/@dir'))->getText();
  36. $this->assertEqual($direction, 'ltr');
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function setUpSettings() {
  42. // We are creating a table here to force an error in the installer because
  43. // it will try and create the drupal_install_test table as this is part of
  44. // the standard database tests performed by the installer in
  45. // Drupal\Core\Database\Install\Tasks.
  46. Database::getConnection('default')->query('CREATE TABLE {drupal_install_test} (id int NOT NULL PRIMARY KEY)');
  47. parent::setUpSettings();
  48. // Ensure that the error message translation is working.
  49. $this->assertRaw('Beheben Sie alle Probleme unten, um die Installation fortzusetzen. Informationen zur Konfiguration der Datenbankserver finden Sie in der <a href="https://www.drupal.org/docs/8/install">Installationshandbuch</a>, oder kontaktieren Sie Ihren Hosting-Anbieter.');
  50. $this->assertRaw('<strong>CREATE</strong> ein Test-Tabelle auf Ihrem Datenbankserver mit dem Befehl <em class="placeholder">CREATE TABLE {drupal_install_test} (id int NOT NULL PRIMARY KEY)</em> fehlgeschlagen.');
  51. // Now do it successfully.
  52. Database::getConnection('default')->query('DROP TABLE {drupal_install_test}');
  53. parent::setUpSettings();
  54. }
  55. /**
  56. * Verifies the expected behaviors of the installation result.
  57. */
  58. public function testInstaller() {
  59. $this->assertUrl('user/1');
  60. $this->assertSession()->statusCodeEquals(200);
  61. // Verify German was configured but not English.
  62. $this->drupalGet('admin/config/regional/language');
  63. $this->assertText('German');
  64. $this->assertNoText('English');
  65. // The current container still has the english as current language, rebuild.
  66. $this->rebuildContainer();
  67. /** @var \Drupal\user\Entity\User $account */
  68. $account = User::load(0);
  69. $this->assertEqual($account->language()->getId(), 'en', 'Anonymous user is English.');
  70. $account = User::load(1);
  71. $this->assertEqual($account->language()->getId(), 'en', 'Administrator user is English.');
  72. $account = $this->drupalCreateUser();
  73. $this->assertEqual($account->language()->getId(), 'de', 'New user is German.');
  74. // Ensure that we can enable basic_auth on a non-english site.
  75. $this->drupalPostForm('admin/modules', ['modules[basic_auth][enable]' => TRUE], t('Install'));
  76. $this->assertSession()->statusCodeEquals(200);
  77. // Assert that the theme CSS was added to the page.
  78. $edit = ['preprocess_css' => FALSE];
  79. $this->drupalPostForm('admin/config/development/performance', $edit, t('Save configuration'));
  80. $this->drupalGet('<front>');
  81. $this->assertRaw('classy/css/components/action-links.css');
  82. // Verify the strings from the translation files were imported.
  83. $test_samples = ['Save and continue', 'Anonymous'];
  84. foreach ($test_samples as $sample) {
  85. $edit = [];
  86. $edit['langcode'] = 'de';
  87. $edit['translation'] = 'translated';
  88. $edit['string'] = $sample;
  89. $this->drupalPostForm('admin/config/regional/translate', $edit, t('Filter'));
  90. $this->assertText($sample . ' de');
  91. }
  92. /** @var \Drupal\language\ConfigurableLanguageManager $language_manager */
  93. $language_manager = \Drupal::languageManager();
  94. // Installed in German, configuration should be in German. No German or
  95. // English overrides should be present.
  96. $config = \Drupal::config('user.settings');
  97. $override_de = $language_manager->getLanguageConfigOverride('de', 'user.settings');
  98. $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
  99. $this->assertEqual($config->get('anonymous'), 'Anonymous de');
  100. $this->assertEqual($config->get('langcode'), 'de');
  101. $this->assertTrue($override_de->isNew());
  102. $this->assertTrue($override_en->isNew());
  103. // Assert that adding English makes the English override available.
  104. $edit = ['predefined_langcode' => 'en'];
  105. $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
  106. $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
  107. $this->assertFalse($override_en->isNew());
  108. $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
  109. }
  110. /**
  111. * Returns the string for the test .po file.
  112. *
  113. * @param string $langcode
  114. * The language code.
  115. *
  116. * @return string
  117. * Contents for the test .po file.
  118. */
  119. protected function getPo($langcode) {
  120. return <<<ENDPO
  121. msgid ""
  122. msgstr ""
  123. msgid "Save and continue"
  124. msgstr "Save and continue $langcode"
  125. msgid "Anonymous"
  126. msgstr "Anonymous $langcode"
  127. msgid "Resolve all issues below to continue the installation. For help configuring your database server, see the <a href="https://www.drupal.org/docs/8/install">installation handbook</a>, or contact your hosting provider."
  128. msgstr "Beheben Sie alle Probleme unten, um die Installation fortzusetzen. Informationen zur Konfiguration der Datenbankserver finden Sie in der <a href="https://www.drupal.org/docs/8/install">Installationshandbuch</a>, oder kontaktieren Sie Ihren Hosting-Anbieter."
  129. msgid "Failed to <strong>CREATE</strong> a test table on your database server with the command %query. The server reports the following message: %error.<p>Are you sure the configured username has the necessary permissions to create tables in the database?</p>"
  130. msgstr "<strong>CREATE</strong> ein Test-Tabelle auf Ihrem Datenbankserver mit dem Befehl %query fehlgeschlagen."
  131. ENDPO;
  132. }
  133. }