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