InstallerTranslationTest.php 6.64 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\system\Tests\Installer\InstallerTranslationTest.
 */

namespace Drupal\system\Tests\Installer;

10
use Drupal\Core\Database\Database;
11
use Drupal\simpletest\InstallerTestBase;
12
use Drupal\user\Entity\User;
13 14

/**
15
 * Installs Drupal in German and checks resulting site.
16 17
 *
 * @group Installer
18
 */
19
class InstallerTranslationTest extends InstallerTestBase {
20

21
  /**
22
   * Overrides the language code in which to install Drupal.
23
   *
24
   * @var string
25
   */
26
  protected $langcode = 'de';
27

28 29 30 31
  /**
   * Overrides InstallerTest::setUpLanguage().
   */
  protected function setUpLanguage() {
32
    // Place a custom local translation in the translations directory.
33
    mkdir(\Drupal::root() . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE);
34
    file_put_contents(\Drupal::root() . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', $this->getPo('de'));
35

36
    parent::setUpLanguage();
37

38 39 40
    // After selecting a different language than English, all following screens
    // should be translated already.
    $elements = $this->xpath('//input[@type="submit"]/@value');
41 42
    $this->assertEqual((string) current($elements), 'Save and continue de');
    $this->translations['Save and continue'] = 'Save and continue de';
43 44 45 46

    // Check the language direction.
    $direction = (string) current($this->xpath('/html/@dir'));
    $this->assertEqual($direction, 'ltr');
47
  }
48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  /**
   * @{inheritdoc}
   */
  protected function setUpSettings() {
    // We are creating a table here to force an error in the installer because
    // it will try and create the drupal_install_test table as this is part of
    // the standard database tests performed by the installer in
    // Drupal\Core\Database\Install\Tasks.
    Database::getConnection('default')->query('CREATE TABLE {drupal_install_test} (id int NULL)');
    parent::setUpSettings();

    // Ensure that the error message translation is working.
    $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/getting-started/install">Installationshandbuch</a>, oder kontaktieren Sie Ihren Hosting-Anbieter.');
    $this->assertRaw('<strong>CREATE</strong> ein Test-Tabelle auf Ihrem Datenbankserver mit dem Befehl <em class="placeholder">CREATE TABLE {drupal_install_test} (id int NULL)</em> fehlgeschlagen.');

    // Now do it successfully.
    Database::getConnection('default')->query('DROP TABLE {drupal_install_test}');
    parent::setUpSettings();
  }

69
  /**
70
   * Verifies the expected behaviors of the installation result.
71 72 73 74
   */
  public function testInstaller() {
    $this->assertUrl('user/1');
    $this->assertResponse(200);
75

76 77 78 79 80
    // Verify German was configured but not English.
    $this->drupalGet('admin/config/regional/language');
    $this->assertText('German');
    $this->assertNoText('English');

81 82
    // The current container still has the english as current language, rebuild.
    $this->rebuildContainer();
83
    /** @var \Drupal\user\Entity\User $account */
84
    $account = User::load(0);
85
    $this->assertEqual($account->language()->getId(), 'en', 'Anonymous user is English.');
86
    $account = User::load(1);
87 88 89
    $this->assertEqual($account->language()->getId(), 'en', 'Administrator user is English.');
    $account = $this->drupalCreateUser();
    $this->assertEqual($account->language()->getId(), 'de', 'New user is German.');
90

91
    // Ensure that we can enable basic_auth on a non-english site.
92
    $this->drupalPostForm('admin/modules', array('modules[Web services][basic_auth][enable]' => TRUE), t('Install'));
93
    $this->assertResponse(200);
94 95 96 97 98

    // Assert that the theme CSS was added to the page.
    $edit = array('preprocess_css' => FALSE);
    $this->drupalPostForm('admin/config/development/performance', $edit, t('Save configuration'));
    $this->drupalGet('<front>');
alexpott's avatar
Issue  
alexpott committed
99
    $this->assertRaw('classy/css/components/action-links.css');
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

    // Verify the strings from the translation files were imported.
    $test_samples = ['Save and continue', 'Anonymous'];
    foreach($test_samples as $sample) {
      $edit = array();
      $edit['langcode'] = 'de';
      $edit['translation'] = 'translated';
      $edit['string'] = $sample;
      $this->drupalPostForm('admin/config/regional/translate', $edit, t('Filter'));
      $this->assertText($sample . ' de');
    }

    /** @var \Drupal\language\ConfigurableLanguageManager $language_manager */
    $language_manager = \Drupal::languageManager();

    // Installed in German, configuration should be in German. No German or
    // English overrides should be present.
    $config = \Drupal::config('user.settings');
    $override_de = $language_manager->getLanguageConfigOverride('de', 'user.settings');
    $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
    $this->assertEqual($config->get('anonymous'), 'Anonymous de');
    $this->assertEqual($config->get('langcode'), 'de');
    $this->assertTrue($override_de->isNew());
    $this->assertTrue($override_en->isNew());

    // Assert that adding English makes the English override available.
    $edit = ['predefined_langcode' => 'en'];
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
    $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
    $this->assertFalse($override_en->isNew());
    $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
  }

  /**
   * Returns the string for the test .po file.
   *
   * @param string $langcode
   *   The language code.
   * @return string
   *   Contents for the test .po file.
   */
  protected function getPo($langcode) {
    return <<<ENDPO
msgid ""
msgstr ""

msgid "Save and continue"
msgstr "Save and continue $langcode"

msgid "Anonymous"
msgstr "Anonymous $langcode"
151 152 153 154 155 156

msgid "Resolve all issues below to continue the installation. For help configuring your database server, see the <a href="https://www.drupal.org/getting-started/install">installation handbook</a>, or contact your hosting provider."
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/getting-started/install">Installationshandbuch</a>, oder kontaktieren Sie Ihren Hosting-Anbieter."

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>"
msgstr "<strong>CREATE</strong> ein Test-Tabelle auf Ihrem Datenbankserver mit dem Befehl %query fehlgeschlagen."
157
ENDPO;
158 159
  }

160
}