annotate core/modules/locale/tests/src/Functional/LocaleUpdateTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\locale\Functional;
Chris@0 4
Chris@0 5 use Drupal\Core\Language\LanguageInterface;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Tests for updating the interface translations of projects.
Chris@0 9 *
Chris@0 10 * @group locale
Chris@0 11 */
Chris@0 12 class LocaleUpdateTest extends LocaleUpdateBase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * {@inheritdoc}
Chris@0 16 */
Chris@0 17 protected function setUp() {
Chris@0 18 parent::setUp();
Chris@0 19 module_load_include('compare.inc', 'locale');
Chris@0 20 module_load_include('fetch.inc', 'locale');
Chris@0 21 $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'translate interface']);
Chris@0 22 $this->drupalLogin($admin_user);
Chris@0 23 // We use German as test language. This language must match the translation
Chris@0 24 // file that come with the locale_test module (test.de.po) and can therefore
Chris@0 25 // not be chosen randomly.
Chris@0 26 $this->addLanguage('de');
Chris@0 27 }
Chris@0 28
Chris@0 29 /**
Chris@0 30 * Checks if local or remote translation sources are detected.
Chris@0 31 *
Chris@0 32 * The translation status process by default checks the status of the
Chris@0 33 * installed projects. For testing purpose a predefined set of modules with
Chris@0 34 * fixed file names and release versions is used. This custom project
Chris@0 35 * definition is applied using a hook_locale_translation_projects_alter
Chris@0 36 * implementation in the locale_test module.
Chris@0 37 *
Chris@0 38 * This test generates a set of local and remote translation files in their
Chris@0 39 * respective local and remote translation directory. The test checks whether
Chris@0 40 * the most recent files are selected in the different check scenarios: check
Chris@0 41 * for local files only, check for both local and remote files.
Chris@0 42 */
Chris@0 43 public function testUpdateCheckStatus() {
Chris@0 44 // Case when contributed modules are absent.
Chris@0 45 $this->drupalGet('admin/reports/translations');
Chris@0 46 $this->assertText(t('Missing translations for one project'));
Chris@0 47
Chris@0 48 $config = $this->config('locale.settings');
Chris@0 49 // Set a flag to let the locale_test module replace the project data with a
Chris@0 50 // set of test projects.
Chris@0 51 \Drupal::state()->set('locale.test_projects_alter', TRUE);
Chris@0 52
Chris@0 53 // Create local and remote translations files.
Chris@0 54 $this->setTranslationFiles();
Chris@0 55 $config->set('translation.default_filename', '%project-%version.%language._po')->save();
Chris@0 56
Chris@0 57 // Set the test conditions.
Chris@0 58 $edit = [
Chris@0 59 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_LOCAL,
Chris@0 60 ];
Chris@0 61 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
Chris@0 62
Chris@0 63 // Get status of translation sources at local file system.
Chris@0 64 $this->drupalGet('admin/reports/translations/check');
Chris@0 65 $result = locale_translation_get_status();
Chris@0 66 $this->assertEqual($result['contrib_module_one']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of contrib_module_one found');
Chris@0 67 $this->assertEqual($result['contrib_module_one']['de']->timestamp, $this->timestampOld, 'Translation timestamp found');
Chris@0 68 $this->assertEqual($result['contrib_module_two']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of contrib_module_two found');
Chris@0 69 $this->assertEqual($result['contrib_module_two']['de']->timestamp, $this->timestampNew, 'Translation timestamp found');
Chris@0 70 $this->assertEqual($result['locale_test']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of locale_test found');
Chris@0 71 $this->assertEqual($result['custom_module_one']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of custom_module_one found');
Chris@0 72
Chris@0 73 // Set the test conditions.
Chris@0 74 $edit = [
Chris@0 75 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
Chris@0 76 ];
Chris@0 77 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
Chris@0 78
Chris@0 79 // Get status of translation sources at both local and remote locations.
Chris@0 80 $this->drupalGet('admin/reports/translations/check');
Chris@0 81 $result = locale_translation_get_status();
Chris@0 82 $this->assertEqual($result['contrib_module_one']['de']->type, LOCALE_TRANSLATION_REMOTE, 'Translation of contrib_module_one found');
Chris@0 83 $this->assertEqual($result['contrib_module_one']['de']->timestamp, $this->timestampNew, 'Translation timestamp found');
Chris@0 84 $this->assertEqual($result['contrib_module_two']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of contrib_module_two found');
Chris@0 85 $this->assertEqual($result['contrib_module_two']['de']->timestamp, $this->timestampNew, 'Translation timestamp found');
Chris@0 86 $this->assertEqual($result['contrib_module_three']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of contrib_module_three found');
Chris@0 87 $this->assertEqual($result['contrib_module_three']['de']->timestamp, $this->timestampOld, 'Translation timestamp found');
Chris@0 88 $this->assertEqual($result['locale_test']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of locale_test found');
Chris@0 89 $this->assertEqual($result['custom_module_one']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of custom_module_one found');
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * Tests translation import from remote sources.
Chris@0 94 *
Chris@0 95 * Test conditions:
Chris@0 96 * - Source: remote and local files
Chris@0 97 * - Import overwrite: all existing translations
Chris@0 98 */
Chris@0 99 public function testUpdateImportSourceRemote() {
Chris@0 100 $config = $this->config('locale.settings');
Chris@0 101
Chris@0 102 // Build the test environment.
Chris@0 103 $this->setTranslationFiles();
Chris@0 104 $this->setCurrentTranslations();
Chris@0 105 $config->set('translation.default_filename', '%project-%version.%language._po');
Chris@0 106
Chris@0 107 // Set the update conditions for this test.
Chris@0 108 $edit = [
Chris@0 109 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
Chris@0 110 'overwrite' => LOCALE_TRANSLATION_OVERWRITE_ALL,
Chris@0 111 ];
Chris@0 112 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
Chris@0 113
Chris@0 114 // Get the translation status.
Chris@0 115 $this->drupalGet('admin/reports/translations/check');
Chris@0 116
Chris@0 117 // Check the status on the Available translation status page.
Chris@0 118 $this->assertRaw('<label for="edit-langcodes-de" class="visually-hidden">Update German</label>', 'German language found');
Chris@0 119 $this->assertText('Updates for: Contributed module one, Contributed module two, Custom module one, Locale test', 'Updates found');
Chris@18 120 /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
Chris@18 121 $date_formatter = $this->container->get('date.formatter');
Chris@18 122 $this->assertText('Contributed module one (' . $date_formatter->format($this->timestampNew, 'html_date') . ')', 'Updates for Contrib module one');
Chris@18 123 $this->assertText('Contributed module two (' . $date_formatter->format($this->timestampNew, 'html_date') . ')', 'Updates for Contrib module two');
Chris@0 124
Chris@0 125 // Execute the translation update.
Chris@0 126 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
Chris@0 127
Chris@0 128 // Check if the translation has been updated, using the status cache.
Chris@0 129 $status = locale_translation_get_status();
Chris@0 130 $this->assertEqual($status['contrib_module_one']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_one found');
Chris@0 131 $this->assertEqual($status['contrib_module_two']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_two found');
Chris@0 132 $this->assertEqual($status['contrib_module_three']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_three found');
Chris@0 133
Chris@0 134 // Check the new translation status.
Chris@0 135 // The static cache needs to be flushed first to get the most recent data
Chris@0 136 // from the database. The function was called earlier during this test.
Chris@0 137 drupal_static_reset('locale_translation_get_file_history');
Chris@0 138 $history = locale_translation_get_file_history();
Chris@0 139 $this->assertTrue($history['contrib_module_one']['de']->timestamp >= $this->timestampNow, 'Translation of contrib_module_one is imported');
Chris@0 140 $this->assertTrue($history['contrib_module_one']['de']->last_checked >= $this->timestampNow, 'Translation of contrib_module_one is updated');
Chris@0 141 $this->assertEqual($history['contrib_module_two']['de']->timestamp, $this->timestampNew, 'Translation of contrib_module_two is imported');
Chris@0 142 $this->assertTrue($history['contrib_module_two']['de']->last_checked >= $this->timestampNow, 'Translation of contrib_module_two is updated');
Chris@0 143 $this->assertEqual($history['contrib_module_three']['de']->timestamp, $this->timestampMedium, 'Translation of contrib_module_three is not imported');
Chris@0 144 $this->assertEqual($history['contrib_module_three']['de']->last_checked, $this->timestampMedium, 'Translation of contrib_module_three is not updated');
Chris@0 145
Chris@0 146 // Check whether existing translations have (not) been overwritten.
Chris@0 147 $this->assertEqual(t('January', [], ['langcode' => 'de']), 'Januar_1', 'Translation of January');
Chris@0 148 $this->assertEqual(t('February', [], ['langcode' => 'de']), 'Februar_2', 'Translation of February');
Chris@0 149 $this->assertEqual(t('March', [], ['langcode' => 'de']), 'Marz_2', 'Translation of March');
Chris@0 150 $this->assertEqual(t('April', [], ['langcode' => 'de']), 'April_2', 'Translation of April');
Chris@0 151 $this->assertEqual(t('May', [], ['langcode' => 'de']), 'Mai_customized', 'Translation of May');
Chris@0 152 $this->assertEqual(t('June', [], ['langcode' => 'de']), 'Juni', 'Translation of June');
Chris@0 153 $this->assertEqual(t('Monday', [], ['langcode' => 'de']), 'Montag', 'Translation of Monday');
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Tests translation import from local sources.
Chris@0 158 *
Chris@0 159 * Test conditions:
Chris@0 160 * - Source: local files only
Chris@0 161 * - Import overwrite: all existing translations
Chris@0 162 */
Chris@0 163 public function testUpdateImportSourceLocal() {
Chris@0 164 $config = $this->config('locale.settings');
Chris@0 165
Chris@0 166 // Build the test environment.
Chris@0 167 $this->setTranslationFiles();
Chris@0 168 $this->setCurrentTranslations();
Chris@0 169 $config->set('translation.default_filename', '%project-%version.%language._po');
Chris@0 170
Chris@0 171 // Set the update conditions for this test.
Chris@0 172 $edit = [
Chris@0 173 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_LOCAL,
Chris@0 174 'overwrite' => LOCALE_TRANSLATION_OVERWRITE_ALL,
Chris@0 175 ];
Chris@0 176 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
Chris@0 177
Chris@0 178 // Execute the translation update.
Chris@0 179 $this->drupalGet('admin/reports/translations/check');
Chris@0 180 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
Chris@0 181
Chris@0 182 // Check if the translation has been updated, using the status cache.
Chris@0 183 $status = locale_translation_get_status();
Chris@0 184 $this->assertEqual($status['contrib_module_one']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_one found');
Chris@0 185 $this->assertEqual($status['contrib_module_two']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_two found');
Chris@0 186 $this->assertEqual($status['contrib_module_three']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_three found');
Chris@0 187
Chris@0 188 // Check the new translation status.
Chris@0 189 // The static cache needs to be flushed first to get the most recent data
Chris@0 190 // from the database. The function was called earlier during this test.
Chris@0 191 drupal_static_reset('locale_translation_get_file_history');
Chris@0 192 $history = locale_translation_get_file_history();
Chris@0 193 $this->assertTrue($history['contrib_module_one']['de']->timestamp >= $this->timestampMedium, 'Translation of contrib_module_one is imported');
Chris@0 194 $this->assertEqual($history['contrib_module_one']['de']->last_checked, $this->timestampMedium, 'Translation of contrib_module_one is updated');
Chris@0 195 $this->assertEqual($history['contrib_module_two']['de']->timestamp, $this->timestampNew, 'Translation of contrib_module_two is imported');
Chris@0 196 $this->assertTrue($history['contrib_module_two']['de']->last_checked >= $this->timestampNow, 'Translation of contrib_module_two is updated');
Chris@0 197 $this->assertEqual($history['contrib_module_three']['de']->timestamp, $this->timestampMedium, 'Translation of contrib_module_three is not imported');
Chris@0 198 $this->assertEqual($history['contrib_module_three']['de']->last_checked, $this->timestampMedium, 'Translation of contrib_module_three is not updated');
Chris@0 199
Chris@0 200 // Check whether existing translations have (not) been overwritten.
Chris@0 201 $this->assertEqual(t('January', [], ['langcode' => 'de']), 'Januar_customized', 'Translation of January');
Chris@0 202 $this->assertEqual(t('February', [], ['langcode' => 'de']), 'Februar_2', 'Translation of February');
Chris@0 203 $this->assertEqual(t('March', [], ['langcode' => 'de']), 'Marz_2', 'Translation of March');
Chris@0 204 $this->assertEqual(t('April', [], ['langcode' => 'de']), 'April_2', 'Translation of April');
Chris@0 205 $this->assertEqual(t('May', [], ['langcode' => 'de']), 'Mai_customized', 'Translation of May');
Chris@0 206 $this->assertEqual(t('June', [], ['langcode' => 'de']), 'Juni', 'Translation of June');
Chris@0 207 $this->assertEqual(t('Monday', [], ['langcode' => 'de']), 'Montag', 'Translation of Monday');
Chris@0 208 }
Chris@0 209
Chris@0 210 /**
Chris@0 211 * Tests translation import and only overwrite non-customized translations.
Chris@0 212 *
Chris@0 213 * Test conditions:
Chris@0 214 * - Source: remote and local files
Chris@0 215 * - Import overwrite: only overwrite non-customized translations
Chris@0 216 */
Chris@0 217 public function testUpdateImportModeNonCustomized() {
Chris@0 218 $config = $this->config('locale.settings');
Chris@0 219
Chris@0 220 // Build the test environment.
Chris@0 221 $this->setTranslationFiles();
Chris@0 222 $this->setCurrentTranslations();
Chris@0 223 $config->set('translation.default_filename', '%project-%version.%language._po');
Chris@0 224
Chris@0 225 // Set the test conditions.
Chris@0 226 $edit = [
Chris@0 227 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
Chris@0 228 'overwrite' => LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED,
Chris@0 229 ];
Chris@0 230 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
Chris@0 231
Chris@0 232 // Execute translation update.
Chris@0 233 $this->drupalGet('admin/reports/translations/check');
Chris@0 234 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
Chris@0 235
Chris@0 236 // Check whether existing translations have (not) been overwritten.
Chris@0 237 $this->assertEqual(t('January', [], ['langcode' => 'de']), 'Januar_customized', 'Translation of January');
Chris@0 238 $this->assertEqual(t('February', [], ['langcode' => 'de']), 'Februar_customized', 'Translation of February');
Chris@0 239 $this->assertEqual(t('March', [], ['langcode' => 'de']), 'Marz_2', 'Translation of March');
Chris@0 240 $this->assertEqual(t('April', [], ['langcode' => 'de']), 'April_2', 'Translation of April');
Chris@0 241 $this->assertEqual(t('May', [], ['langcode' => 'de']), 'Mai_customized', 'Translation of May');
Chris@0 242 $this->assertEqual(t('June', [], ['langcode' => 'de']), 'Juni', 'Translation of June');
Chris@0 243 $this->assertEqual(t('Monday', [], ['langcode' => 'de']), 'Montag', 'Translation of Monday');
Chris@0 244 }
Chris@0 245
Chris@0 246 /**
Chris@0 247 * Tests translation import and don't overwrite any translation.
Chris@0 248 *
Chris@0 249 * Test conditions:
Chris@0 250 * - Source: remote and local files
Chris@0 251 * - Import overwrite: don't overwrite any existing translation
Chris@0 252 */
Chris@0 253 public function testUpdateImportModeNone() {
Chris@0 254 $config = $this->config('locale.settings');
Chris@0 255
Chris@0 256 // Build the test environment.
Chris@0 257 $this->setTranslationFiles();
Chris@0 258 $this->setCurrentTranslations();
Chris@0 259 $config->set('translation.default_filename', '%project-%version.%language._po');
Chris@0 260
Chris@0 261 // Set the test conditions.
Chris@0 262 $edit = [
Chris@0 263 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
Chris@0 264 'overwrite' => LOCALE_TRANSLATION_OVERWRITE_NONE,
Chris@0 265 ];
Chris@0 266 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
Chris@0 267
Chris@0 268 // Execute translation update.
Chris@0 269 $this->drupalGet('admin/reports/translations/check');
Chris@0 270 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
Chris@0 271
Chris@0 272 // Check whether existing translations have (not) been overwritten.
Chris@0 273 $this->assertTranslation('January', 'Januar_customized', 'de');
Chris@0 274 $this->assertTranslation('February', 'Februar_customized', 'de');
Chris@0 275 $this->assertTranslation('March', 'Marz', 'de');
Chris@0 276 $this->assertTranslation('April', 'April_2', 'de');
Chris@0 277 $this->assertTranslation('May', 'Mai_customized', 'de');
Chris@0 278 $this->assertTranslation('June', 'Juni', 'de');
Chris@0 279 $this->assertTranslation('Monday', 'Montag', 'de');
Chris@0 280 }
Chris@0 281
Chris@0 282 /**
Chris@0 283 * Tests automatic translation import when a module is enabled.
Chris@0 284 */
Chris@0 285 public function testEnableUninstallModule() {
Chris@0 286 // Make the hidden test modules look like a normal custom module.
Chris@0 287 \Drupal::state()->set('locale.test_system_info_alter', TRUE);
Chris@0 288
Chris@0 289 // Check if there is no translation yet.
Chris@0 290 $this->assertTranslation('Tuesday', '', 'de');
Chris@0 291
Chris@0 292 // Enable a module.
Chris@0 293 $edit = [
Chris@0 294 'modules[locale_test_translate][enable]' => 'locale_test_translate',
Chris@0 295 ];
Chris@0 296 $this->drupalPostForm('admin/modules', $edit, t('Install'));
Chris@0 297
Chris@0 298 // Check if translations have been imported.
Chris@0 299 $this->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.',
Chris@0 300 ['%number' => 7, '%update' => 0, '%delete' => 0]), 'One translation file imported.');
Chris@0 301 $this->assertTranslation('Tuesday', 'Dienstag', 'de');
Chris@0 302
Chris@0 303 $edit = [
Chris@0 304 'uninstall[locale_test_translate]' => 1,
Chris@0 305 ];
Chris@0 306 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
Chris@0 307 $this->drupalPostForm(NULL, [], t('Uninstall'));
Chris@0 308
Chris@0 309 // Check if the file data is removed from the database.
Chris@0 310 $history = locale_translation_get_file_history();
Chris@0 311 $this->assertFalse(isset($history['locale_test_translate']), 'Project removed from the file history');
Chris@0 312 $projects = locale_translation_get_projects();
Chris@0 313 $this->assertFalse(isset($projects['locale_test_translate']), 'Project removed from the project list');
Chris@0 314 }
Chris@0 315
Chris@0 316 /**
Chris@0 317 * Tests automatic translation import when a language is added.
Chris@0 318 *
Chris@0 319 * When a language is added, the system will check for translations files of
Chris@0 320 * enabled modules and will import them. When a language is removed the system
Chris@0 321 * will remove all translations of that language from the database.
Chris@0 322 */
Chris@0 323 public function testEnableLanguage() {
Chris@0 324 // Make the hidden test modules look like a normal custom module.
Chris@0 325 \Drupal::state()->set('locale.test_system_info_alter', TRUE);
Chris@0 326
Chris@0 327 // Enable a module.
Chris@0 328 $edit = [
Chris@0 329 'modules[locale_test_translate][enable]' => 'locale_test_translate',
Chris@0 330 ];
Chris@0 331 $this->drupalPostForm('admin/modules', $edit, t('Install'));
Chris@0 332
Chris@0 333 // Check if there is no Dutch translation yet.
Chris@0 334 $this->assertTranslation('Extraday', '', 'nl');
Chris@0 335 $this->assertTranslation('Tuesday', 'Dienstag', 'de');
Chris@0 336
Chris@0 337 // Add a language.
Chris@0 338 $edit = [
Chris@0 339 'predefined_langcode' => 'nl',
Chris@0 340 ];
Chris@0 341 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
Chris@0 342
Chris@0 343 // Check if the right number of translations are added.
Chris@0 344 $this->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.',
Chris@0 345 ['%number' => 8, '%update' => 0, '%delete' => 0]), 'One language added.');
Chris@0 346 $this->assertTranslation('Extraday', 'extra dag', 'nl');
Chris@0 347
Chris@0 348 // Check if the language data is added to the database.
Chris@0 349 $result = db_query("SELECT project FROM {locale_file} WHERE langcode='nl'")->fetchField();
Chris@0 350 $this->assertTrue($result, 'Files added to file history');
Chris@0 351
Chris@0 352 // Remove a language.
Chris@0 353 $this->drupalPostForm('admin/config/regional/language/delete/nl', [], t('Delete'));
Chris@0 354
Chris@0 355 // Check if the language data is removed from the database.
Chris@0 356 $result = db_query("SELECT project FROM {locale_file} WHERE langcode='nl'")->fetchField();
Chris@0 357 $this->assertFalse($result, 'Files removed from file history');
Chris@0 358
Chris@0 359 // Check that the Dutch translation is gone.
Chris@0 360 $this->assertTranslation('Extraday', '', 'nl');
Chris@0 361 $this->assertTranslation('Tuesday', 'Dienstag', 'de');
Chris@0 362 }
Chris@0 363
Chris@0 364 /**
Chris@0 365 * Tests automatic translation import when a custom language is added.
Chris@0 366 */
Chris@0 367 public function testEnableCustomLanguage() {
Chris@0 368 // Make the hidden test modules look like a normal custom module.
Chris@0 369 \Drupal::state()->set('locale.test_system_info_alter', TRUE);
Chris@0 370
Chris@0 371 // Enable a module.
Chris@0 372 $edit = [
Chris@0 373 'modules[locale_test_translate][enable]' => 'locale_test_translate',
Chris@0 374 ];
Chris@0 375 $this->drupalPostForm('admin/modules', $edit, t('Install'));
Chris@0 376
Chris@0 377 // Create a custom language with language code 'xx' and a random
Chris@0 378 // name.
Chris@0 379 $langcode = 'xx';
Chris@0 380 $name = $this->randomMachineName(16);
Chris@0 381 $edit = [
Chris@0 382 'predefined_langcode' => 'custom',
Chris@0 383 'langcode' => $langcode,
Chris@0 384 'label' => $name,
Chris@0 385 'direction' => LanguageInterface::DIRECTION_LTR,
Chris@0 386 ];
Chris@0 387 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
Chris@0 388
Chris@0 389 // Ensure the translation file is automatically imported when the language
Chris@0 390 // was added.
Chris@0 391 $this->assertText(t('One translation file imported.'), 'Language file automatically imported.');
Chris@0 392 $this->assertText(t('One translation string was skipped because of disallowed or malformed HTML'), 'Language file automatically imported.');
Chris@0 393
Chris@0 394 // Ensure the strings were successfully imported.
Chris@0 395 $search = [
Chris@0 396 'string' => 'lundi',
Chris@0 397 'langcode' => $langcode,
Chris@0 398 'translation' => 'translated',
Chris@0 399 ];
Chris@0 400 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 401 $this->assertNoText(t('No strings available.'), 'String successfully imported.');
Chris@0 402
Chris@0 403 // Ensure the multiline string was imported.
Chris@0 404 $search = [
Chris@0 405 'string' => 'Source string for multiline translation',
Chris@0 406 'langcode' => $langcode,
Chris@0 407 'translation' => 'all',
Chris@0 408 ];
Chris@0 409 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 410 $this->assertText('Multiline translation string to make sure that import works with it.', 'String successfully imported.');
Chris@0 411
Chris@0 412 // Ensure 'Allowed HTML source string' was imported but the translation for
Chris@0 413 // 'Another allowed HTML source string' was not because it contains invalid
Chris@0 414 // HTML.
Chris@0 415 $search = [
Chris@0 416 'string' => 'HTML source string',
Chris@0 417 'langcode' => $langcode,
Chris@0 418 'translation' => 'all',
Chris@0 419 ];
Chris@0 420 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 421 $this->assertText('Allowed HTML source string', 'String successfully imported.');
Chris@0 422 $this->assertNoText('Another allowed HTML source string', 'String with disallowed translation not imported.');
Chris@0 423 }
Chris@0 424
Chris@0 425 }