annotate core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.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@18 5 use Drupal\Core\Url;
Chris@18 6 use Drupal\Core\Database\Database;
Chris@0 7 use Drupal\language\Entity\ConfigurableLanguage;
Chris@0 8 use Drupal\Tests\BrowserTestBase;
Chris@0 9 use Drupal\Core\Language\LanguageInterface;
Chris@17 10 use Drupal\Component\Render\FormattableMarkup;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Adds a new locale and translates its name. Checks the validation of
Chris@0 14 * translation strings and search results.
Chris@0 15 *
Chris@0 16 * @group locale
Chris@0 17 */
Chris@0 18 class LocaleTranslationUiTest extends BrowserTestBase {
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Modules to enable.
Chris@0 22 *
Chris@0 23 * @var array
Chris@0 24 */
Chris@0 25 public static $modules = ['locale'];
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Enable interface translation to English.
Chris@0 29 */
Chris@0 30 public function testEnglishTranslation() {
Chris@0 31 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
Chris@0 32 $this->drupalLogin($admin_user);
Chris@0 33
Chris@0 34 $this->drupalPostForm('admin/config/regional/language/edit/en', ['locale_translate_english' => TRUE], t('Save language'));
Chris@0 35 $this->assertLinkByHref('/admin/config/regional/translate?langcode=en', 0, 'Enabled interface translation to English.');
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Adds a language and tests string translation by users with the appropriate permissions.
Chris@0 40 */
Chris@0 41 public function testStringTranslation() {
Chris@0 42 // User to add and remove language.
Chris@0 43 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
Chris@0 44 // User to translate and delete string.
Chris@0 45 $translate_user = $this->drupalCreateUser(['translate interface', 'access administration pages']);
Chris@0 46 // Code for the language.
Chris@0 47 $langcode = 'xx';
Chris@0 48 // The English name for the language. This will be translated.
Chris@0 49 $name = 'cucurbitaceae';
Chris@0 50 // This will be the translation of $name.
Chris@0 51 $translation = $this->randomMachineName(16);
Chris@0 52 $translation_to_en = $this->randomMachineName(16);
Chris@0 53
Chris@0 54 // Add custom language.
Chris@0 55 $this->drupalLogin($admin_user);
Chris@0 56 $edit = [
Chris@0 57 'predefined_langcode' => 'custom',
Chris@0 58 'langcode' => $langcode,
Chris@0 59 'label' => $name,
Chris@0 60 'direction' => LanguageInterface::DIRECTION_LTR,
Chris@0 61 ];
Chris@0 62 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
Chris@0 63 // Add string.
Chris@0 64 t($name, [], ['langcode' => $langcode])->render();
Chris@0 65 // Reset locale cache.
Chris@0 66 $this->container->get('string_translation')->reset();
Chris@0 67 $this->assertRaw('"edit-languages-' . $langcode . '-weight"', 'Language code found.');
Chris@0 68 $this->assertText(t($name), 'Test language added.');
Chris@0 69 $this->drupalLogout();
Chris@0 70
Chris@0 71 // Search for the name and translate it.
Chris@0 72 $this->drupalLogin($translate_user);
Chris@0 73 $search = [
Chris@0 74 'string' => $name,
Chris@0 75 'langcode' => $langcode,
Chris@0 76 'translation' => 'untranslated',
Chris@0 77 ];
Chris@0 78 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 79 $this->assertText($name, 'Search found the string as untranslated.');
Chris@0 80
Chris@0 81 // No t() here, it's surely not translated yet.
Chris@0 82 $this->assertText($name, 'name found on edit screen.');
Chris@0 83 $this->assertNoOption('edit-langcode', 'en', 'No way to translate the string to English.');
Chris@0 84 $this->drupalLogout();
Chris@0 85 $this->drupalLogin($admin_user);
Chris@0 86 $this->drupalPostForm('admin/config/regional/language/edit/en', ['locale_translate_english' => TRUE], t('Save language'));
Chris@0 87 $this->drupalLogout();
Chris@0 88 $this->drupalLogin($translate_user);
Chris@0 89 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 90 $this->assertText($name, 'Search found the string as untranslated.');
Chris@0 91
Chris@0 92 // Assume this is the only result, given the random name.
Chris@0 93 $textarea = current($this->xpath('//textarea'));
Chris@0 94 $lid = $textarea->getAttribute('name');
Chris@0 95 $edit = [
Chris@0 96 $lid => $translation,
Chris@0 97 ];
Chris@0 98 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 99 $this->assertText(t('The strings have been saved.'), 'The strings have been saved.');
Chris@0 100 $url_bits = explode('?', $this->getUrl());
Chris@18 101 $this->assertEqual($url_bits[0], Url::fromRoute('locale.translate_page', [], ['absolute' => TRUE])->toString(), 'Correct page redirection.');
Chris@0 102 $search = [
Chris@0 103 'string' => $name,
Chris@0 104 'langcode' => $langcode,
Chris@0 105 'translation' => 'translated',
Chris@0 106 ];
Chris@0 107 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 108 $this->assertRaw($translation, 'Non-English translation properly saved.');
Chris@0 109
Chris@0 110 $search = [
Chris@0 111 'string' => $name,
Chris@0 112 'langcode' => 'en',
Chris@0 113 'translation' => 'untranslated',
Chris@0 114 ];
Chris@0 115 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 116 $textarea = current($this->xpath('//textarea'));
Chris@0 117 $lid = $textarea->getAttribute('name');
Chris@0 118 $edit = [
Chris@0 119 $lid => $translation_to_en,
Chris@0 120 ];
Chris@0 121 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 122 $search = [
Chris@0 123 'string' => $name,
Chris@0 124 'langcode' => 'en',
Chris@0 125 'translation' => 'translated',
Chris@0 126 ];
Chris@0 127 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 128 $this->assertRaw($translation_to_en, 'English translation properly saved.');
Chris@0 129
Chris@0 130 $this->assertTrue($name != $translation && t($name, [], ['langcode' => $langcode]) == $translation, 't() works for non-English.');
Chris@0 131 // Refresh the locale() cache to get fresh data from t() below. We are in
Chris@0 132 // the same HTTP request and therefore t() is not refreshed by saving the
Chris@0 133 // translation above.
Chris@0 134 $this->container->get('string_translation')->reset();
Chris@0 135 // Now we should get the proper fresh translation from t().
Chris@0 136 $this->assertTrue($name != $translation_to_en && t($name, [], ['langcode' => 'en']) == $translation_to_en, 't() works for English.');
Chris@0 137 $this->assertTrue(t($name, [], ['langcode' => LanguageInterface::LANGCODE_SYSTEM]) == $name, 't() works for LanguageInterface::LANGCODE_SYSTEM.');
Chris@0 138
Chris@0 139 $search = [
Chris@0 140 'string' => $name,
Chris@0 141 'langcode' => 'en',
Chris@0 142 'translation' => 'untranslated',
Chris@0 143 ];
Chris@0 144 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 145 $this->assertText(t('No strings available.'), 'String is translated.');
Chris@0 146
Chris@0 147 // Test invalidation of 'rendered' cache tag after string translation.
Chris@0 148 $this->drupalLogout();
Chris@0 149 $this->drupalGet('xx/user/login');
Chris@0 150 $this->assertText('Enter the password that accompanies your username.');
Chris@0 151
Chris@0 152 $this->drupalLogin($translate_user);
Chris@0 153 $search = [
Chris@0 154 'string' => 'accompanies your username',
Chris@0 155 'langcode' => $langcode,
Chris@0 156 'translation' => 'untranslated',
Chris@0 157 ];
Chris@0 158 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 159 $textarea = current($this->xpath('//textarea'));
Chris@0 160 $lid = $textarea->getAttribute('name');
Chris@0 161 $edit = [
Chris@0 162 $lid => 'Please enter your Llama username.',
Chris@0 163 ];
Chris@0 164 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 165
Chris@0 166 $this->drupalLogout();
Chris@0 167 $this->drupalGet('xx/user/login');
Chris@0 168 $this->assertText('Please enter your Llama username.');
Chris@0 169
Chris@0 170 // Delete the language.
Chris@0 171 $this->drupalLogin($admin_user);
Chris@0 172 $path = 'admin/config/regional/language/delete/' . $langcode;
Chris@0 173 // This a confirm form, we do not need any fields changed.
Chris@0 174 $this->drupalPostForm($path, [], t('Delete'));
Chris@0 175 // We need raw here because %language and %langcode will add HTML.
Chris@0 176 $t_args = ['%language' => $name, '%langcode' => $langcode];
Chris@0 177 $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), 'The test language has been removed.');
Chris@0 178 // Reload to remove $name.
Chris@0 179 $this->drupalGet($path);
Chris@0 180 // Verify that language is no longer found.
Chris@0 181 $this->assertResponse(404, 'Language no longer found.');
Chris@0 182 $this->drupalLogout();
Chris@0 183
Chris@0 184 // Delete the string.
Chris@0 185 $this->drupalLogin($translate_user);
Chris@0 186 $search = [
Chris@0 187 'string' => $name,
Chris@0 188 'langcode' => 'en',
Chris@0 189 'translation' => 'translated',
Chris@0 190 ];
Chris@0 191 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 192 // Assume this is the only result, given the random name.
Chris@0 193 $textarea = current($this->xpath('//textarea'));
Chris@0 194 $lid = $textarea->getAttribute('name');
Chris@0 195 $edit = [
Chris@0 196 $lid => '',
Chris@0 197 ];
Chris@0 198 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 199 $this->assertRaw($name, 'The strings have been saved.');
Chris@0 200 $this->drupalLogin($translate_user);
Chris@0 201 $search = [
Chris@0 202 'string' => $name,
Chris@0 203 'langcode' => 'en',
Chris@0 204 'translation' => 'untranslated',
Chris@0 205 ];
Chris@0 206 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 207 $this->assertNoText(t('No strings available.'), 'The translation has been removed');
Chris@0 208 }
Chris@0 209
Chris@0 210 /**
Chris@0 211 * Adds a language and checks that the JavaScript translation files are
Chris@0 212 * properly created and rebuilt on deletion.
Chris@0 213 */
Chris@0 214 public function testJavaScriptTranslation() {
Chris@0 215 $user = $this->drupalCreateUser(['translate interface', 'administer languages', 'access administration pages']);
Chris@0 216 $this->drupalLogin($user);
Chris@0 217 $config = $this->config('locale.settings');
Chris@0 218
Chris@0 219 $langcode = 'xx';
Chris@0 220 // The English name for the language. This will be translated.
Chris@0 221 $name = $this->randomMachineName(16);
Chris@0 222
Chris@0 223 // Add custom language.
Chris@0 224 $edit = [
Chris@0 225 'predefined_langcode' => 'custom',
Chris@0 226 'langcode' => $langcode,
Chris@0 227 'label' => $name,
Chris@0 228 'direction' => LanguageInterface::DIRECTION_LTR,
Chris@0 229 ];
Chris@0 230 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
Chris@0 231 $this->container->get('language_manager')->reset();
Chris@0 232
Chris@0 233 // Build the JavaScript translation file.
Chris@0 234
Chris@0 235 // Retrieve the source string of the first string available in the
Chris@0 236 // {locales_source} table and translate it.
Chris@18 237 $query = Database::getConnection()->select('locales_source', 's');
Chris@0 238 $query->addJoin('INNER', 'locales_location', 'l', 's.lid = l.lid');
Chris@0 239 $source = $query->fields('s', ['source'])
Chris@0 240 ->condition('l.type', 'javascript')
Chris@0 241 ->range(0, 1)
Chris@0 242 ->execute()
Chris@0 243 ->fetchField();
Chris@0 244
Chris@0 245 $search = [
Chris@0 246 'string' => $source,
Chris@0 247 'langcode' => $langcode,
Chris@0 248 'translation' => 'all',
Chris@0 249 ];
Chris@0 250 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 251
Chris@0 252 $textarea = current($this->xpath('//textarea'));
Chris@0 253 $lid = $textarea->getAttribute('name');
Chris@0 254 $edit = [
Chris@0 255 $lid => $this->randomMachineName(),
Chris@0 256 ];
Chris@0 257 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 258
Chris@0 259 // Trigger JavaScript translation parsing and building.
Chris@0 260 _locale_rebuild_js($langcode);
Chris@0 261
Chris@0 262 $locale_javascripts = \Drupal::state()->get('locale.translation.javascript') ?: [];
Chris@0 263 $js_file = 'public://' . $config->get('javascript.directory') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js';
Chris@17 264 $this->assertTrue($result = file_exists($js_file), new FormattableMarkup('JavaScript file created: %file', ['%file' => $result ? $js_file : 'not found']));
Chris@0 265
Chris@0 266 // Test JavaScript translation rebuilding.
Chris@18 267 \Drupal::service('file_system')->delete($js_file);
Chris@17 268 $this->assertTrue($result = !file_exists($js_file), new FormattableMarkup('JavaScript file deleted: %file', ['%file' => $result ? $js_file : 'found']));
Chris@0 269 _locale_rebuild_js($langcode);
Chris@17 270 $this->assertTrue($result = file_exists($js_file), new FormattableMarkup('JavaScript file rebuilt: %file', ['%file' => $result ? $js_file : 'not found']));
Chris@0 271 }
Chris@0 272
Chris@0 273 /**
Chris@0 274 * Tests the validation of the translation input.
Chris@0 275 */
Chris@0 276 public function testStringValidation() {
Chris@0 277 // User to add language and strings.
Chris@0 278 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'translate interface']);
Chris@0 279 $this->drupalLogin($admin_user);
Chris@0 280 $langcode = 'xx';
Chris@0 281 // The English name for the language. This will be translated.
Chris@0 282 $name = $this->randomMachineName(16);
Chris@0 283
Chris@0 284 // These will be the invalid translations of $name.
Chris@0 285 $key = $this->randomMachineName(16);
Chris@0 286 $bad_translations[$key] = "<script>alert('xss');</script>" . $key;
Chris@0 287 $key = $this->randomMachineName(16);
Chris@0 288 $bad_translations[$key] = '<img SRC="javascript:alert(\'xss\');">' . $key;
Chris@0 289 $key = $this->randomMachineName(16);
Chris@0 290 $bad_translations[$key] = '<<SCRIPT>alert("xss");//<</SCRIPT>' . $key;
Chris@0 291 $key = $this->randomMachineName(16);
Chris@0 292 $bad_translations[$key] = "<BODY ONLOAD=alert('xss')>" . $key;
Chris@0 293
Chris@0 294 // Add custom language.
Chris@0 295 $edit = [
Chris@0 296 'predefined_langcode' => 'custom',
Chris@0 297 'langcode' => $langcode,
Chris@0 298 'label' => $name,
Chris@0 299 'direction' => LanguageInterface::DIRECTION_LTR,
Chris@0 300 ];
Chris@0 301 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
Chris@0 302 // Add string.
Chris@0 303 t($name, [], ['langcode' => $langcode])->render();
Chris@0 304 // Reset locale cache.
Chris@0 305 $search = [
Chris@0 306 'string' => $name,
Chris@0 307 'langcode' => $langcode,
Chris@0 308 'translation' => 'all',
Chris@0 309 ];
Chris@0 310 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 311 // Find the edit path.
Chris@0 312
Chris@0 313 $textarea = current($this->xpath('//textarea'));
Chris@0 314 $lid = $textarea->getAttribute('name');
Chris@0 315 foreach ($bad_translations as $translation) {
Chris@0 316 $edit = [
Chris@0 317 $lid => $translation,
Chris@0 318 ];
Chris@0 319 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 320 // Check for a form error on the textarea.
Chris@0 321 $form_class = $this->xpath('//form[@id="locale-translate-edit-form"]//textarea/@class');
Chris@0 322 $this->assertContains('error', $form_class[0]->getText(), 'The string was rejected as unsafe.');
Chris@0 323 $this->assertNoText(t('The string has been saved.'), 'The string was not saved.');
Chris@0 324 }
Chris@0 325 }
Chris@0 326
Chris@0 327 /**
Chris@0 328 * Tests translation search form.
Chris@0 329 */
Chris@0 330 public function testStringSearch() {
Chris@0 331 // User to add and remove language.
Chris@0 332 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
Chris@0 333 // User to translate and delete string.
Chris@0 334 $translate_user = $this->drupalCreateUser(['translate interface', 'access administration pages']);
Chris@0 335
Chris@0 336 // Code for the language.
Chris@0 337 $langcode = 'xx';
Chris@0 338 // The English name for the language. This will be translated.
Chris@0 339 $name = $this->randomMachineName(16);
Chris@0 340 // This will be the translation of $name.
Chris@0 341 $translation = $this->randomMachineName(16);
Chris@0 342
Chris@0 343 // Add custom language.
Chris@0 344 $this->drupalLogin($admin_user);
Chris@0 345 $edit = [
Chris@0 346 'predefined_langcode' => 'custom',
Chris@0 347 'langcode' => $langcode,
Chris@0 348 'label' => $name,
Chris@0 349 'direction' => LanguageInterface::DIRECTION_LTR,
Chris@0 350 ];
Chris@0 351 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
Chris@0 352
Chris@0 353 $edit = [
Chris@0 354 'predefined_langcode' => 'custom',
Chris@0 355 'langcode' => 'yy',
Chris@0 356 'label' => $this->randomMachineName(16),
Chris@0 357 'direction' => LanguageInterface::DIRECTION_LTR,
Chris@0 358 ];
Chris@0 359 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
Chris@0 360
Chris@0 361 // Add string.
Chris@0 362 t($name, [], ['langcode' => $langcode])->render();
Chris@0 363 // Reset locale cache.
Chris@0 364 $this->container->get('string_translation')->reset();
Chris@0 365 $this->drupalLogout();
Chris@0 366
Chris@0 367 // Search for the name.
Chris@0 368 $this->drupalLogin($translate_user);
Chris@0 369 $search = [
Chris@0 370 'string' => $name,
Chris@0 371 'langcode' => $langcode,
Chris@0 372 'translation' => 'all',
Chris@0 373 ];
Chris@0 374 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 375 // assertText() seems to remove the input field where $name always could be
Chris@0 376 // found, so this is not a false assert. See how assertNoText succeeds
Chris@0 377 // later.
Chris@0 378 $this->assertText($name, 'Search found the string.');
Chris@0 379
Chris@0 380 // Ensure untranslated string doesn't appear if searching on 'only
Chris@0 381 // translated strings'.
Chris@0 382 $search = [
Chris@0 383 'string' => $name,
Chris@0 384 'langcode' => $langcode,
Chris@0 385 'translation' => 'translated',
Chris@0 386 ];
Chris@0 387 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 388 $this->assertText(t('No strings available.'), "Search didn't find the string.");
Chris@0 389
Chris@0 390 // Ensure untranslated string appears if searching on 'only untranslated
Chris@0 391 // strings'.
Chris@0 392 $search = [
Chris@0 393 'string' => $name,
Chris@0 394 'langcode' => $langcode,
Chris@0 395 'translation' => 'untranslated',
Chris@0 396 ];
Chris@0 397 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 398 $this->assertNoText(t('No strings available.'), 'Search found the string.');
Chris@0 399
Chris@0 400 // Add translation.
Chris@0 401 // Assume this is the only result, given the random name.
Chris@0 402 // We save the lid from the path.
Chris@0 403 $textarea = current($this->xpath('//textarea'));
Chris@0 404 $lid = $textarea->getAttribute('name');
Chris@0 405 $edit = [
Chris@0 406 $lid => $translation,
Chris@0 407 ];
Chris@0 408 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 409
Chris@0 410 // Ensure translated string does appear if searching on 'only
Chris@0 411 // translated strings'.
Chris@0 412 $search = [
Chris@0 413 'string' => $translation,
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.'), 'Search found the translation.');
Chris@0 419
Chris@0 420 // Ensure translated source string doesn't appear if searching on 'only
Chris@0 421 // untranslated strings'.
Chris@0 422 $search = [
Chris@0 423 'string' => $name,
Chris@0 424 'langcode' => $langcode,
Chris@0 425 'translation' => 'untranslated',
Chris@0 426 ];
Chris@0 427 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 428 $this->assertText(t('No strings available.'), "Search didn't find the source string.");
Chris@0 429
Chris@0 430 // Ensure translated string doesn't appear if searching on 'only
Chris@0 431 // untranslated strings'.
Chris@0 432 $search = [
Chris@0 433 'string' => $translation,
Chris@0 434 'langcode' => $langcode,
Chris@0 435 'translation' => 'untranslated',
Chris@0 436 ];
Chris@0 437 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 438 $this->assertText(t('No strings available.'), "Search didn't find the translation.");
Chris@0 439
Chris@0 440 // Ensure translated string does appear if searching on the custom language.
Chris@0 441 $search = [
Chris@0 442 'string' => $translation,
Chris@0 443 'langcode' => $langcode,
Chris@0 444 'translation' => 'all',
Chris@0 445 ];
Chris@0 446 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 447 $this->assertNoText(t('No strings available.'), 'Search found the translation.');
Chris@0 448
Chris@0 449 // Ensure translated string doesn't appear if searching in System (English).
Chris@0 450 $search = [
Chris@0 451 'string' => $translation,
Chris@0 452 'langcode' => 'yy',
Chris@0 453 'translation' => 'all',
Chris@0 454 ];
Chris@0 455 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 456 $this->assertText(t('No strings available.'), "Search didn't find the translation.");
Chris@0 457
Chris@0 458 // Search for a string that isn't in the system.
Chris@0 459 $unavailable_string = $this->randomMachineName(16);
Chris@0 460 $search = [
Chris@0 461 'string' => $unavailable_string,
Chris@0 462 'langcode' => $langcode,
Chris@0 463 'translation' => 'all',
Chris@0 464 ];
Chris@0 465 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 466 $this->assertText(t('No strings available.'), "Search didn't find the invalid string.");
Chris@0 467 }
Chris@0 468
Chris@0 469 /**
Chris@0 470 * Tests that only changed strings are saved customized when edited.
Chris@0 471 */
Chris@0 472 public function testUICustomizedStrings() {
Chris@0 473 $user = $this->drupalCreateUser(['translate interface', 'administer languages', 'access administration pages']);
Chris@0 474 $this->drupalLogin($user);
Chris@0 475 ConfigurableLanguage::createFromLangcode('de')->save();
Chris@0 476
Chris@0 477 // Create test source string.
Chris@0 478 $string = $this->container->get('locale.storage')->createString([
Chris@0 479 'source' => $this->randomMachineName(100),
Chris@0 480 'context' => $this->randomMachineName(20),
Chris@0 481 ])->save();
Chris@0 482
Chris@0 483 // Create translation for new string and save it as non-customized.
Chris@0 484 $translation = $this->container->get('locale.storage')->createTranslation([
Chris@0 485 'lid' => $string->lid,
Chris@0 486 'language' => 'de',
Chris@0 487 'translation' => $this->randomMachineName(100),
Chris@0 488 'customized' => 0,
Chris@0 489 ])->save();
Chris@0 490
Chris@0 491 // Reset locale cache.
Chris@0 492 $this->container->get('string_translation')->reset();
Chris@0 493
Chris@0 494 // Ensure non-customized translation string does appear if searching
Chris@0 495 // non-customized translation.
Chris@0 496 $search = [
Chris@0 497 'string' => $string->getString(),
Chris@0 498 'langcode' => 'de',
Chris@0 499 'translation' => 'translated',
Chris@0 500 'customized' => '0',
Chris@0 501 ];
Chris@0 502 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 503
Chris@0 504 $this->assertText($translation->getString(), 'Translation is found in search result.');
Chris@0 505
Chris@0 506 // Submit the translations without changing the translation.
Chris@0 507 $textarea = current($this->xpath('//textarea'));
Chris@0 508 $lid = $textarea->getAttribute('name');
Chris@0 509 $edit = [
Chris@0 510 $lid => $translation->getString(),
Chris@0 511 ];
Chris@0 512 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 513
Chris@0 514 // Ensure unchanged translation string does appear if searching
Chris@0 515 // non-customized translation.
Chris@0 516 $search = [
Chris@0 517 'string' => $string->getString(),
Chris@0 518 'langcode' => 'de',
Chris@0 519 'translation' => 'translated',
Chris@0 520 'customized' => '0',
Chris@0 521 ];
Chris@0 522 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 523 $this->assertText($string->getString(), 'Translation is not marked as customized.');
Chris@0 524
Chris@0 525 // Submit the translations with a new translation.
Chris@0 526 $textarea = current($this->xpath('//textarea'));
Chris@0 527 $lid = $textarea->getAttribute('name');
Chris@0 528 $edit = [
Chris@0 529 $lid => $this->randomMachineName(100),
Chris@0 530 ];
Chris@0 531 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 532
Chris@0 533 // Ensure changed translation string does appear if searching customized
Chris@0 534 // translation.
Chris@0 535 $search = [
Chris@0 536 'string' => $string->getString(),
Chris@0 537 'langcode' => 'de',
Chris@0 538 'translation' => 'translated',
Chris@0 539 'customized' => '1',
Chris@0 540 ];
Chris@0 541 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 542 $this->assertText($string->getString(), "Translation is marked as customized.");
Chris@0 543 }
Chris@0 544
Chris@0 545 }