annotate core/modules/locale/tests/src/Functional/LocaleUpdateTest.php @ 0:4c8ae668cc8c

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