/[drupal]/drupal/modules/locale/locale.test
ViewVC logotype

Contents of /drupal/modules/locale/locale.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.50 - (show annotations) (download) (as text)
Mon Nov 2 05:57:05 2009 UTC (3 weeks, 5 days ago) by webchick
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10
Changes since 1.49: +43 -7 lines
File MIME type: text/x-php
#608424 by peximo and plach: Fixed notice when multilingual body display set to hidden (with tests).
1 <?php
2 // $Id: locale.test,v 1.49 2009/10/27 04:12:39 webchick Exp $
3
4 /**
5 * @file
6 * Tests for Locale module.
7 *
8 * The test file includes:
9 * - a functional test for the language configuration forms;
10 * - functional tests for the translation functionalities, including searching;
11 * - a functional test for the PO files import feature, including validation;
12 * - functional tests for translations and templates export feature;
13 * - functional tests for the uninstall process;
14 * - a functional test for the language switching feature;
15 * - a functional test for a user's ability to change their default language;
16 * - a functional test for configuring a different path alias per language;
17 * - a functional test for configuring a different path alias per language;
18 * - a functional test for multilingual support by content type and on nodes.
19 * - a functional test for multilingual fields.
20 */
21
22
23 /**
24 * Functional tests for the language configuration forms.
25 */
26 class LocaleConfigurationTest extends DrupalWebTestCase {
27 public static function getInfo() {
28 return array(
29 'name' => 'Language configuration',
30 'description' => 'Adds a new locale and tests changing its status and the default language.',
31 'group' => 'Locale',
32 );
33 }
34
35 function setUp() {
36 parent::setUp('locale');
37 }
38
39 /**
40 * Functional tests for adding, editing and deleting languages.
41 */
42 function testLanguageConfiguration() {
43 global $base_url;
44
45 // User to add and remove language.
46 $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
47 $this->drupalLogin($admin_user);
48
49 // Add predefined language.
50 $edit = array(
51 'langcode' => 'fr',
52 );
53 $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
54 $this->assertText('fr', t('Language added successfully.'));
55 $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
56
57 // Add custom language.
58 // Code for the language.
59 $langcode = 'xx';
60 // The English name for the language.
61 $name = $this->randomName(16);
62 // The native name for the language.
63 $native = $this->randomName(16);
64 // The domain prefix.
65 $prefix = $langcode;
66 $edit = array(
67 'langcode' => $langcode,
68 'name' => $name,
69 'native' => $native,
70 'prefix' => $prefix,
71 'direction' => '0',
72 );
73 $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
74 $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
75 $this->assertText($langcode, t('Language code found.'));
76 $this->assertText($name, t('Name found.'));
77 $this->assertText($native, t('Native found.'));
78 $this->assertText($native, t('Test language added.'));
79
80 // Check if we can change the default language.
81 $path = 'admin/config/regional/language';
82 $this->drupalGet($path);
83 $this->assertFieldChecked('edit-site-default-en', t('English is the default language.'));
84 // Change the default language.
85 $edit = array(
86 'site_default' => $langcode,
87 );
88 $this->drupalPost($path, $edit, t('Save configuration'));
89 $this->assertNoFieldChecked('edit-site-default-en', t('Default language updated.'));
90 $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
91
92 // Ensure we can't delete the default language.
93 $path = 'admin/config/regional/language/delete/' . $langcode;
94 $this->drupalGet($path);
95 $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
96 $this->assertText(t('The default language cannot be deleted.'), t('Failed to delete the default language.'));
97
98 // Check if we can disable a language.
99 $edit = array(
100 'enabled[en]' => FALSE,
101 );
102 $this->drupalPost($path, $edit, t('Save configuration'));
103 $this->assertNoFieldChecked('edit-enabled-en', t('Language disabled.'));
104
105 // Set disabled language to be the default and ensure it is re-enabled.
106 $edit = array(
107 'site_default' => 'en',
108 );
109 $this->drupalPost($path, $edit, t('Save configuration'));
110 $this->assertFieldChecked('edit-enabled-en', t('Default language re-enabled.'));
111
112 // Ensure 'edit' link works.
113 $this->clickLink(t('edit'));
114 $this->assertTitle(t('Edit language | Drupal'), t('Page title is "Edit language".'));
115 // Edit a language.
116 $path = 'admin/config/regional/language/edit/' . $langcode;
117 $name = $this->randomName(16);
118 $edit = array(
119 'name' => $name,
120 );
121 $this->drupalPost($path, $edit, t('Save language'));
122 $this->assertRaw($name, t('The language has been updated.'));
123 $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
124
125 // Ensure 'delete' link works.
126 $path = 'admin/config/regional/language';
127 $this->drupalGet($path);
128 $this->clickLink(t('delete'));
129 $this->assertText(t('Are you sure you want to delete the language'), t('"delete" link is correct.'));
130 // Delete the language.
131 $path = 'admin/config/regional/language/delete/' . $langcode;
132 $this->drupalGet($path);
133 // First test the 'cancel' link.
134 $this->clickLink(t('Cancel'));
135 $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
136 $this->assertRaw($name, t('The language was not deleted.'));
137 // Delete the language for real. This a confirm form, we do not need any
138 // fields changed.
139 $this->drupalPost($path, array(), t('Delete'));
140 // We need raw here because %locale will add HTML.
141 $this->assertRaw(t('The language %locale has been removed.', array('%locale' => $name)), t('The test language has been removed.'));
142 $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
143 // Reload to remove $name.
144 $this->drupalGet($path);
145 $this->assertNoText($langcode, t('Language code not found.'));
146 $this->assertNoText($name, t('Name not found.'));
147 $this->assertNoText($native, t('Native not found.'));
148
149 // Ensure we can't delete the English language.
150 $path = 'admin/config/regional/language/delete/en';
151 $this->drupalGet($path);
152 $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), t('Correct page redirection.'));
153 $this->assertText(t('The English language cannot be deleted.'), t('Failed to delete English language.'));
154
155 $this->drupalLogout();
156 }
157
158 }
159
160 /**
161 * Functional test for string translation and validation.
162 */
163 class LocaleTranslationFunctionalTest extends DrupalWebTestCase {
164 public static function getInfo() {
165 return array(
166 'name' => 'String translate, search and validate',
167 'description' => 'Adds a new locale and translates its name. Checks the validation of translation strings and search results.',
168 'group' => 'Locale',
169 );
170 }
171
172 function setUp() {
173 parent::setUp('locale');
174 }
175
176 /**
177 * Adds a language and tests string translation by users with the appropriate permissions.
178 */
179 function testStringTranslation() {
180 global $base_url;
181
182 // User to add and remove language.
183 $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
184 // User to translate and delete string.
185 $translate_user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
186 // Code for the language.
187 $langcode = 'xx';
188 // The English name for the language. This will be translated.
189 $name = $this->randomName(16);
190 // The native name for the language.
191 $native = $this->randomName(16);
192 // The domain prefix.
193 $prefix = $langcode;
194 // This is the language indicator on the translation search screen for
195 // untranslated strings. Copied straight from locale.inc.
196 $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
197 // This will be the translation of $name.
198 $translation = $this->randomName(16);
199
200 // Add custom language.
201 $this->drupalLogin($admin_user);
202 $edit = array(
203 'langcode' => $langcode,
204 'name' => $name,
205 'native' => $native,
206 'prefix' => $prefix,
207 'direction' => '0',
208 );
209 $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
210 // Add string.
211 t($name, array(), array('langcode' => $langcode));
212 // Reset locale cache.
213 locale(NULL, NULL, NULL, TRUE);
214 $this->assertText($langcode, t('Language code found.'));
215 $this->assertText($name, t('Name found.'));
216 $this->assertText($native, t('Native found.'));
217 // No t() here, we do not want to add this string to the database and it's
218 // surely not translated yet.
219 $this->assertText($native, t('Test language added.'));
220 $this->drupalLogout();
221
222 // Search for the name and translate it.
223 $this->drupalLogin($translate_user);
224 $search = array(
225 'string' => $name,
226 'language' => 'all',
227 'translation' => 'all',
228 'group' => 'all',
229 );
230 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
231 // assertText() seems to remove the input field where $name always could be
232 // found, so this is not a false assert. See how assertNoText succeeds
233 // later.
234 $this->assertText($name, t('Search found the name.'));
235 $this->assertRaw($language_indicator, t('Name is untranslated.'));
236 // Assume this is the only result, given the random name.
237 $this->clickLink(t('edit'));
238 // We save the lid from the path.
239 $matches = array();
240 preg_match('!admin/config/regional/translate/edit/(\d+)!', $this->getUrl(), $matches);
241 $lid = $matches[1];
242 // No t() here, it's surely not translated yet.
243 $this->assertText($name, t('name found on edit screen.'));
244 $edit = array(
245 "translations[$langcode]" => $translation,
246 );
247 $this->drupalPost(NULL, $edit, t('Save translations'));
248 $this->assertText(t('The string has been saved.'), t('The string has been saved.'));
249 $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
250 $this->assertTrue($name != $translation && t($name, array(), array('langcode' => $langcode)) == $translation, t('t() works.'));
251 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
252 // The indicator should not be here.
253 $this->assertNoRaw($language_indicator, t('String is translated.'));
254
255 // Try to edit a non-existent string and ensure we're redirected correctly.
256 // Assuming we don't have 999,999 strings already.
257 $random_lid = 999999;
258 $this->drupalGet('admin/config/regional/translate/edit/' . $random_lid);
259 $this->assertText(t('String not found'), t('String not found.'));
260 $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
261 $this->drupalLogout();
262
263 // Delete the language.
264 $this->drupalLogin($admin_user);
265 $path = 'admin/config/regional/language/delete/' . $langcode;
266 // This a confirm form, we do not need any fields changed.
267 $this->drupalPost($path, array(), t('Delete'));
268 // We need raw here because %locale will add HTML.
269 $this->assertRaw(t('The language %locale has been removed.', array('%locale' => $name)), t('The test language has been removed.'));
270 // Reload to remove $name.
271 $this->drupalGet($path);
272 $this->assertNoText($langcode, t('Language code not found.'));
273 $this->assertNoText($name, t('Name not found.'));
274 $this->assertNoText($native, t('Native not found.'));
275 $this->drupalLogout();
276
277 // Delete the string.
278 $this->drupalLogin($translate_user);
279 $search = array(
280 'string' => $name,
281 'language' => 'all',
282 'translation' => 'all',
283 'group' => 'all',
284 );
285 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
286 // Assume this is the only result, given the random name.
287 $this->clickLink(t('delete'));
288 $this->assertText(t('Are you sure you want to delete the string'), t('"delete" link is correct.'));
289 // Delete the string.
290 $path = 'admin/config/regional/translate/delete/' . $lid;
291 $this->drupalGet($path);
292 // First test the 'cancel' link.
293 $this->clickLink(t('Cancel'));
294 $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
295 $this->assertRaw($name, t('The string was not deleted.'));
296 // Delete the name string.
297 $this->drupalPost('admin/config/regional/translate/delete/' . $lid, array(), t('Delete'));
298 $this->assertText(t('The string has been removed.'), t('The string has been removed message.'));
299 $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
300 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
301 $this->assertNoText($name, t('Search now can not find the name.'));
302 }
303
304 /**
305 * Tests the validation of the translation input.
306 */
307 function testStringValidation() {
308 global $base_url;
309
310 // User to add language and strings.
311 $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'translate interface'));
312 $this->drupalLogin($admin_user);
313 $langcode = 'xx';
314 // The English name for the language. This will be translated.
315 $name = $this->randomName(16);
316 // The native name for the language.
317 $native = $this->randomName(16);
318 // The domain prefix.
319 $prefix = $langcode;
320 // This is the language indicator on the translation search screen for
321 // untranslated strings. Copied straight from locale.inc.
322 $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
323 // These will be the invalid translations of $name.
324 $key = $this->randomName(16);
325 $bad_translations[$key] = "<script>alert('xss');</script>" . $key;
326 $key = $this->randomName(16);
327 $bad_translations[$key] = '<img SRC="javascript:alert(\'xss\');">' . $key;
328 $key = $this->randomName(16);
329 $bad_translations[$key] = '<<SCRIPT>alert("xss");//<</SCRIPT>' . $key;
330 $key = $this->randomName(16);
331 $bad_translations[$key] ="<BODY ONLOAD=alert('xss')>" . $key;
332
333 // Add custom language.
334 $edit = array(
335 'langcode' => $langcode,
336 'name' => $name,
337 'native' => $native,
338 'prefix' => $prefix,
339 'direction' => '0',
340 );
341 $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
342 // Add string.
343 t($name, array(), array('langcode' => $langcode));
344 // Reset locale cache.
345 $search = array(
346 'string' => $name,
347 'language' => 'all',
348 'translation' => 'all',
349 'group' => 'all',
350 );
351 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
352 // Find the edit path.
353 $content = $this->drupalGetContent();
354 $this->assertTrue(preg_match('@(admin/config/regional/translate/edit/[0-9]+)@', $content, $matches), t('Found the edit path.'));
355 $path = $matches[0];
356 foreach ($bad_translations as $key => $translation) {
357 $edit = array(
358 "translations[$langcode]" => $translation,
359 );
360 $this->drupalPost($path, $edit, t('Save translations'));
361 // Check for a form error on the textarea.
362 $form_class = $this->xpath('//form[@id="locale-translate-edit-form"]//textarea/@class');
363 $this->assertNotIdentical(FALSE, strpos($form_class[0], 'error'), t('The string was rejected as unsafe.'));
364 $this->assertNoText(t('The string has been saved.'), t('The string was not saved.'));
365 }
366 }
367
368 /**
369 * Tests translation search form.
370 */
371 function testStringSearch() {
372 global $base_url;
373
374 // User to add and remove language.
375 $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
376 // User to translate and delete string.
377 $translate_user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
378
379 // Code for the language.
380 $langcode = 'xx';
381 // The English name for the language. This will be translated.
382 $name = $this->randomName(16);
383 // The native name for the language.
384 $native = $this->randomName(16);
385 // The domain prefix.
386 $prefix = $langcode;
387 // This is the language indicator on the translation search screen for
388 // untranslated strings. Copied straight from locale.inc.
389 $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
390 // This will be the translation of $name.
391 $translation = $this->randomName(16);
392
393 // Add custom language.
394 $this->drupalLogin($admin_user);
395 $edit = array(
396 'langcode' => $langcode,
397 'name' => $name,
398 'native' => $native,
399 'prefix' => $prefix,
400 'direction' => '0',
401 );
402 $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
403 // Add string.
404 t($name, array(), array('langcode' => $langcode));
405 // Reset locale cache.
406 locale(NULL, NULL, NULL, TRUE);
407 $this->drupalLogout();
408
409 // Search for the name.
410 $this->drupalLogin($translate_user);
411 $search = array(
412 'string' => $name,
413 'language' => 'all',
414 'translation' => 'all',
415 'group' => 'all',
416 );
417 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
418 // assertText() seems to remove the input field where $name always could be
419 // found, so this is not a false assert. See how assertNoText succeeds
420 // later.
421 $this->assertText($name, t('Search found the string.'));
422
423 // Ensure untranslated string doesn't appear if searching on 'only
424 // translated strings'.
425 $search = array(
426 'string' => $name,
427 'language' => 'all',
428 'translation' => 'translated',
429 'group' => 'all',
430 );
431 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
432 $this->assertText(t('No strings found for your search.'), t("Search didn't find the string."));
433
434 // Ensure untranslated string appears if searching on 'only untranslated
435 // strings'.
436 $search = array(
437 'string' => $name,
438 'language' => 'all',
439 'translation' => 'untranslated',
440 'group' => 'all',
441 );
442 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
443 $this->assertNoText(t('No strings found for your search.'), t('Search found the string.'));
444
445 // Add translation.
446 // Assume this is the only result, given the random name.
447 $this->clickLink(t('edit'));
448 // We save the lid from the path.
449 $matches = array();
450 preg_match('!admin/config/regional/translate/edit/(\d)+!', $this->getUrl(), $matches);
451 $lid = $matches[1];
452 $edit = array(
453 "translations[$langcode]" => $translation,
454 );
455 $this->drupalPost(NULL, $edit, t('Save translations'));
456
457 // Ensure translated string does appear if searching on 'only
458 // translated strings'.
459 $search = array(
460 'string' => $translation,
461 'language' => 'all',
462 'translation' => 'translated',
463 'group' => 'all',
464 );
465 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
466 $this->assertNoText(t('No strings found for your search.'), t('Search found the translation.'));
467
468 // Ensure translated source string doesn't appear if searching on 'only
469 // untranslated strings'.
470 $search = array(
471 'string' => $name,
472 'language' => 'all',
473 'translation' => 'untranslated',
474 'group' => 'all',
475 );
476 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
477 $this->assertText(t('No strings found for your search.'), t("Search didn't find the source string."));
478
479 // Ensure translated string doesn't appear if searching on 'only
480 // untranslated strings'.
481 $search = array(
482 'string' => $translation,
483 'language' => 'all',
484 'translation' => 'untranslated',
485 'group' => 'all',
486 );
487 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
488 $this->assertText(t('No strings found for your search.'), t("Search didn't find the translation."));
489
490 // Ensure translated string does appear if searching on the custom language.
491 $search = array(
492 'string' => $translation,
493 'language' => $langcode,
494 'translation' => 'all',
495 'group' => 'all',
496 );
497 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
498 $this->assertNoText(t('No strings found for your search.'), t('Search found the translation.'));
499
500 // Ensure translated string doesn't appear if searching on English.
501 $search = array(
502 'string' => $translation,
503 'language' => 'en',
504 'translation' => 'all',
505 'group' => 'all',
506 );
507 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
508 $this->assertText(t('No strings found for your search.'), t("Search didn't find the translation."));
509
510 // Search for a string that isn't in the system.
511 $unavailable_string = $this->randomName(16);
512 $search = array(
513 'string' => $unavailable_string,
514 'language' => 'all',
515 'translation' => 'all',
516 'group' => 'all',
517 );
518 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
519 $this->assertText(t('No strings found for your search.'), t("Search didn't find the invalid string."));
520 }
521 }
522
523 /**
524 * Functional tests for the import of translation files.
525 */
526 class LocaleImportFunctionalTest extends DrupalWebTestCase {
527 public static function getInfo() {
528 return array(
529 'name' => 'Translation import',
530 'description' => 'Tests the importation of locale files.',
531 'group' => 'Locale',
532 );
533 }
534
535 /**
536 * A user able to create languages and import translations.
537 */
538 protected $admin_user = NULL;
539
540 function setUp() {
541 parent::setUp('locale', 'locale_test');
542
543 $this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
544 $this->drupalLogin($this->admin_user);
545 }
546
547 /**
548 * Test importation of standalone .po files.
549 */
550 function testStandalonePoFile() {
551 // Try importing a .po file.
552 $this->importPoFile($this->getPoFile(), array(
553 'langcode' => 'fr',
554 ));
555
556 // The import should automatically create the corresponding language.
557 $this->assertRaw(t('The language %language has been created.', array('%language' => 'French')), t('The language has been automatically created.'));
558
559 // The import should have created 7 strings.
560 $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 7, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported.'));
561
562 // Ensure we were redirected correctly.
563 $this->assertEqual($this->getUrl(), url('admin/config/regional/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
564
565
566 // Try importing a .po file with invalid tags in the default text group.
567 $this->importPoFile($this->getBadPoFile(), array(
568 'langcode' => 'fr',
569 ));
570
571 // The import should have created 1 string and rejected 2.
572 $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 1, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported.'));
573 $skip_message = format_plural(2, 'One translation string was skipped because it contains disallowed HTML.', '@count translation strings were skipped because they contain disallowed HTML.');
574 $this->assertRaw($skip_message, t('Unsafe strings were skipped.'));
575
576
577 // Try importing a .po file with invalid tags in a non default text group.
578 $this->importPoFile($this->getBadPoFile(), array(
579 'langcode' => 'fr',
580 'group' => 'custom',
581 ));
582
583 // The import should have created 3 strings.
584 $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 3, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported.'));
585
586
587 // Try importing a .po file which doesn't exist.
588 $name = $this->randomName(16);
589 $this->drupalPost('admin/config/regional/translate/import', array(
590 'langcode' => 'fr',
591 'files[file]' => $name,
592 'group' => 'custom',
593 ), t('Import'));
594 $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/import', array('absolute' => TRUE)), t('Correct page redirection.'));
595 $this->assertText(t('File to import not found.'), t('File to import not found message.'));
596
597
598 // Try importing a .po file with overriding strings, and ensure existing
599 // strings are kept.
600 $this->importPoFile($this->getOverwritePoFile(), array(
601 'langcode' => 'fr',
602 'mode' => 1, // Existing strings are kept, only new strings are added.
603 ));
604
605 // The import should have created 1 string.
606 $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 1, '%update' => 0, '%delete' => 0)), t('The translation file was successfully imported.'));
607 // Ensure string wasn't overwritten.
608 $search = array(
609 'string' => 'Montag',
610 'language' => 'fr',
611 'translation' => 'translated',
612 'group' => 'all',
613 );
614 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
615 $this->assertText(t('No strings found for your search.'), t('String not overwritten by imported string.'));
616
617
618 // Try importing a .po file with overriding strings, and ensure existing
619 // strings are overwritten.
620 $this->importPoFile($this->getOverwritePoFile(), array(
621 'langcode' => 'fr',
622 'mode' => 0, // Strings in the uploaded file replace existing ones, new ones are added.
623 ));
624
625 // The import should have updated 2 strings.
626 $this->assertRaw(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => 0, '%update' => 2, '%delete' => 0)), t('The translation file was successfully imported.'));
627 // Ensure string was overwritten.
628 $search = array(
629 'string' => 'Montag',
630 'language' => 'fr',
631 'translation' => 'translated',
632 'group' => 'all',
633 );
634 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
635 $this->assertNoText(t('No strings found for your search.'), t('String overwritten by imported string.'));
636 }
637
638 /**
639 * Test automatic importation of a module's translation files when a language
640 * is enabled.
641 */
642 function testAutomaticModuleTranslationImportLanguageEnable() {
643 // Code for the language - manually set to match the test translation file.
644 $langcode = 'xx';
645 // The English name for the language.
646 $name = $this->randomName(16);
647 // The native name for the language.
648 $native = $this->randomName(16);
649 // The domain prefix.
650 $prefix = $langcode;
651
652 // Create a custom language.
653 $edit = array(
654 'langcode' => $langcode,
655 'name' => $name,
656 'native' => $native,
657 'prefix' => $prefix,
658 'direction' => '0',
659 );
660 $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
661
662 // Ensure the translation file was automatically imported when language was
663 // added.
664 $this->assertText(t('One translation file imported for the enabled modules.'), t('Language file automatically imported.'));
665
666 // Ensure strings were successfully imported.
667 $search = array(
668 'string' => 'lundi',
669 'language' => $langcode,
670 'translation' => 'translated',
671 'group' => 'all',
672 );
673 $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
674 $this->assertNoText(t('No strings found for your search.'), t('String successfully imported.'));
675 }
676
677 /**
678 * Test automatic importation of a module's translation files when a language
679 * is enabled.
680 */
681 function testLanguageContext() {
682 // Try importing a .po file.
683 $this->importPoFile($this->getPoFileWithContext(), array(
684 'langcode' => 'hr',
685 ));
686
687 $this->assertIdentical(t('May', array(), array('langcode' => 'hr', 'context' => 'Long month name')), 'Svibanj', t('Long month name context is working.'));
688 $this->assertIdentical(t('May', array(), array('langcode' => 'hr')), 'Svi.', t('Default context is working.'));
689 }
690
691 /**
692 * Helper function: import a standalone .po file in a given language.
693 *
694 * @param $contents
695 * Contents of the .po file to import.
696 * @param $options
697 * Additional options to pass to the translation import form.
698 */
699 function importPoFile($contents, array $options = array()) {
700 $name = tempnam(file_directory_path('temporary'), "po_");
701 file_put_contents($name, $contents);
702 $options['files[file]'] = $name;
703 $this->drupalPost('admin/config/regional/translate/import', $options, t('Import'));
704 unlink($name);
705 }
706
707 /**
708 * Helper function that returns a proper .po file.
709 */
710 function getPoFile() {
711 return <<< EOF
712 msgid ""
713 msgstr ""
714 "Project-Id-Version: Drupal 6\\n"
715 "MIME-Version: 1.0\\n"
716 "Content-Type: text/plain; charset=UTF-8\\n"
717 "Content-Transfer-Encoding: 8bit\\n"
718 "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
719
720 msgid "Monday"
721 msgstr "lundi"
722
723 msgid "Tuesday"
724 msgstr "mardi"
725
726 msgid "Wednesday"
727 msgstr "mercredi"
728
729 msgid "Thursday"
730 msgstr "jeudi"
731
732 msgid "Friday"
733 msgstr "vendredi"
734
735 msgid "Saturday"
736 msgstr "samedi"
737
738 msgid "Sunday"
739 msgstr "dimanche"
740 EOF;
741 }
742
743 /**
744 * Helper function that returns a bad .po file.
745 */
746 function getBadPoFile() {
747 return <<< EOF
748 msgid ""
749 msgstr ""
750 "Project-Id-Version: Drupal 6\\n"
751 "MIME-Version: 1.0\\n"
752 "Content-Type: text/plain; charset=UTF-8\\n"
753 "Content-Transfer-Encoding: 8bit\\n"
754 "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
755
756 msgid "Save configuration"
757 msgstr "Enregistrer la configuration"
758
759 msgid "edit"
760 msgstr "modifier<img SRC="javascript:alert(\'xss\');">"
761
762 msgid "delete"
763 msgstr "supprimer<script>alert('xss');</script>"
764
765 EOF;
766 }
767
768 /**
769 * Helper function that returns a proper .po file, for testing overwriting
770 * existing translations.
771 */
772 function getOverwritePoFile() {
773 return <<< EOF
774 msgid ""
775 msgstr ""
776 "Project-Id-Version: Drupal 6\\n"
777 "MIME-Version: 1.0\\n"
778 "Content-Type: text/plain; charset=UTF-8\\n"
779 "Content-Transfer-Encoding: 8bit\\n"
780 "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
781
782 msgid "Monday"
783 msgstr "Montag"
784
785 msgid "Day"
786 msgstr "Jour"
787 EOF;
788 }
789
790 /**
791 * Helper function that returns a .po file with context.
792 */
793 function getPoFileWithContext() {
794 // Croatian (code hr) is one the the languages that have a different
795 // form for the full name and the abbreviated name for the month May.
796 return <<< EOF
797 msgid ""
798 msgstr ""
799 "Project-Id-Version: Drupal 6\\n"
800 "MIME-Version: 1.0\\n"
801 "Content-Type: text/plain; charset=UTF-8\\n"
802 "Content-Transfer-Encoding: 8bit\\n"
803 "Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\\n"
804
805 msgctxt "Long month name"
806 msgid "May"
807 msgstr "Svibanj"
808
809 msgid "May"
810 msgstr "Svi."
811 EOF;
812 }
813
814
815 }
816
817 /**
818 * Functional tests for the export of translation files.
819 */
820 class LocaleExportFunctionalTest extends DrupalWebTestCase {
821 public static function getInfo() {
822 return array(
823 'name' => 'Translation export',
824 'description' => 'Tests the exportation of locale files.',
825 'group' => 'Locale',
826 );
827 }
828
829 /**
830 * A user able to create languages and export translations.
831 */
832 protected $admin_user = NULL;
833
834 function setUp() {
835 parent::setUp('locale', 'locale_test');
836
837 $this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
838 $this->drupalLogin($this->admin_user);
839 }
840
841 /**
842 * Test exportation of translations.
843 */
844 function testExportTranslation() {
845 // First import some known translations.
846 // This will also automatically enable the 'fr' language.
847 $name = tempnam(file_directory_path('temporary'), "po_");
848 file_put_contents($name, $this->getPoFile());
849 $this->drupalPost('admin/config/regional/translate/import', array(
850 'langcode' => 'fr',
851 'files[file]' => $name,
852 ), t('Import'));
853 unlink($name);
854
855 // Get the French translations.
856 $this->drupalPost('admin/config/regional/translate/export', array(
857 'langcode' => 'fr',
858 ), t('Export'));
859
860 // Ensure we have a translation file.
861 $this->assertRaw('# French translation of Drupal', t('Exported French translation file.'));
862 // Ensure our imported translations exist in the file.
863 $this->assertRaw('msgstr "lundi"', t('French translations present in exported file.'));
864 }
865
866 /**
867 * Test exportation of translation template file.
868 */
869 function testExportTranslationTemplateFile() {
870 // Get the translation template file.
871 // There are two 'Export' buttons on this page, but it somehow works. It'd
872 // be better if we could use the submit button id like documented but that
873 // doesn't work.
874 $this->drupalPost('admin/config/regional/translate/export', array(), t('Export'));
875 // Ensure we have a translation file.
876 $this->assertRaw('# LANGUAGE translation of PROJECT', t('Exported translation template file.'));
877 }
878
879 /**
880 * Helper function that returns a proper .po file.
881 */
882 function getPoFile() {
883 return <<< EOF
884 msgid ""
885 msgstr ""
886 "Project-Id-Version: Drupal 6\\n"
887 "MIME-Version: 1.0\\n"
888 "Content-Type: text/plain; charset=UTF-8\\n"
889 "Content-Transfer-Encoding: 8bit\\n"
890 "Plural-Forms: nplurals=2; plural=(n > 1);\\n"
891
892 msgid "Monday"
893 msgstr "lundi"
894 EOF;
895 }
896
897 }
898
899 /**
900 * Locale uninstall with English UI functional test.
901 */
902 class LocaleUninstallFunctionalTest extends DrupalWebTestCase {
903 public static function getInfo() {
904 return array(
905 'name' => 'Locale uninstall (EN)',
906 'description' => 'Tests the uninstall process using the built-in UI language.',
907 'group' => 'Locale',
908 );
909 }
910
911 /**
912 * The default language set for the UI before uninstall.
913 */
914 protected $language_interface;
915
916 function setUp() {
917 parent::setUp('locale');
918 $this->language_interface = 'en';
919 }
920
921 /**
922 * Check if the values of the Locale variables are correct after uninstall.
923 */
924 function testUninstallProcess() {
925 $locale_module = array('locale');
926
927 // Add a new language and optionally set it as default.
928 require_once DRUPAL_ROOT . '/includes/locale.inc';
929 locale_add_language('fr', 'French', 'Français', LANGUAGE_LTR, '', '', TRUE, $this->language_interface == 'fr');
930
931 // Check the UI language.
932 drupal_language_initialize();
933 global $language_interface;
934 $this->assertEqual($language_interface->language, $this->language_interface, t('Current language: %lang', array('%lang' => $language_interface->language)));
935
936 // Enable multilingual workflow option for articles.
937 variable_set('language_content_type_article', 1);
938
939 // Change JavaScript translations directory.
940 variable_set('locale_js_directory', 'js_translations');
941
942 // Build the JavaScript translation file for French.
943 $user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
944 $this->drupalLogin($user);
945 $this->drupalGet('admin/config/regional/translate/translate');
946 $string = db_query('SELECT min(lid) AS lid FROM {locales_source} WHERE location LIKE :location AND textgroup = :textgroup', array(
947 ':location' => '%.js%',
948 ':textgroup' => 'default',
949 ))->fetchObject();
950 $edit = array('translations[fr]' => 'french translation');
951 $this->drupalPost('admin/config/regional/translate/edit/' . $string->lid, $edit, t('Save translations'));
952 _locale_rebuild_js('fr');
953 $file = db_query('SELECT javascript FROM {languages} WHERE language = :language', array(':language' => 'fr'))->fetchObject();
954 $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/fr_' . $file->javascript . '.js';
955 $this->assertTrue($result = file_exists($js_file), t('JavaScript file created: %file', array('%file' => $result ? $js_file : t('none'))));
956
957 // Disable string caching.
958 variable_set('locale_cache_strings', 0);
959
960 // Change language negotiation options.
961 drupal_load('module', 'locale');
962 variable_set('language_types', drupal_language_types() + array('language_custom' => TRUE));
963 variable_set('language_negotiation_' . LANGUAGE_TYPE_INTERFACE, locale_language_negotiation_info());
964 variable_set('language_negotiation_' . LANGUAGE_TYPE_CONTENT, locale_language_negotiation_info());
965 variable_set('language_negotiation_' . LANGUAGE_TYPE_URL, locale_language_negotiation_info());
966
967 // Change language providers settings.
968 variable_set('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX);
969 variable_set('locale_language_negotiation_session_param', TRUE);
970
971 // Uninstall Locale.
972 module_disable($locale_module);
973 drupal_uninstall_modules($locale_module);
974
975 // Visit the front page.
976 $this->drupalGet('');
977
978 // Check the init language logic.
979 drupal_language_initialize();
980 $this->assertEqual($language_interface->language, 'en', t('Language after uninstall: %lang', array('%lang' => $language_interface->language)));
981
982 // Check JavaScript files deletion.
983 $this->assertTrue($result = !file_exists($js_file), t('JavaScript file deleted: %file', array('%file' => $result ? $js_file : t('found'))));
984
985 // Check language count.
986 $language_count = variable_get('language_count', 1);
987 $this->assertEqual($language_count, 1, t('Language count: %count', array('%count' => $language_count)));
988
989 // Check language negotiation.
990 require_once DRUPAL_ROOT . '/includes/language.inc';
991 $this->assertTrue(count(language_types()) == count(drupal_language_types()), t('Language types reset'));
992 $language_negotiation = language_negotiation_get(LANGUAGE_TYPE_INTERFACE) == LANGUAGE_NEGOTIATION_DEFAULT;
993 $this->assertTrue($language_negotiation, t('Interface language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set'))));
994 $language_negotiation = language_negotiation_get(LANGUAGE_TYPE_CONTENT) == LANGUAGE_NEGOTIATION_DEFAULT;
995 $this->assertTrue($language_negotiation, t('Content language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set'))));
996 $language_negotiation = language_negotiation_get(LANGUAGE_TYPE_URL) == LANGUAGE_NEGOTIATION_DEFAULT;
997 $this->assertTrue($language_negotiation, t('URL language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set'))));
998
999 // Check language providers settings.
1000 $this->assertFalse(variable_get('locale_language_negotiation_url_part', FALSE), t('URL language provider indicator settings cleared.'));
1001 $this->assertFalse(variable_get('locale_language_negotiation_session_param', FALSE), t('Visit language provider settings cleared.'));
1002
1003 // Check JavaScript parsed.
1004 $javascript_parsed_count = count(variable_get('javascript_parsed', array()));
1005 $this->assertEqual($javascript_parsed_count, 0, t('JavaScript parsed count: %count', array('%count' => $javascript_parsed_count)));
1006
1007 // Check multilingual workflow option for articles.
1008 $multilingual = variable_get('language_content_type_article', 0);
1009 $this->assertEqual($multilingual, 0, t('Multilingual workflow option: %status', array('%status' => t($multilingual ? 'enabled': 'disabled'))));
1010
1011 // Check JavaScript translations directory.
1012 $locale_js_directory = variable_get('locale_js_directory', 'languages');
1013 $this->assertEqual($locale_js_directory, 'languages', t('JavaScript translations directory: %dir', array('%dir' => $locale_js_directory)));
1014
1015 // Check string caching.
1016 $locale_cache_strings = variable_get('locale_cache_strings', 1);
1017 $this->assertEqual($locale_cache_strings, 1, t('String caching: %status', array('%status' => t($locale_cache_strings ? 'enabled': 'disabled'))));
1018 }
1019 }
1020
1021 /**
1022 * Locale uninstall with French UI functional test.
1023 *
1024 * Because this class extends LocaleUninstallFunctionalTest, it doesn't require a new
1025 * test of its own. Rather, it switches the default UI language in setUp and then
1026 * runs the testUninstallProcess (which it inherits from LocaleUninstallFunctionalTest)
1027 * to test with this new language.
1028 */
1029 class LocaleUninstallFrenchFunctionalTest extends LocaleUninstallFunctionalTest {
1030 public static function getInfo() {
1031 return array(
1032 'name' => 'Locale uninstall (FR)',
1033 'description' => 'Tests the uninstall process using French as interface language.',
1034 'group' => 'Locale',
1035 );
1036 }
1037
1038 function setUp() {
1039 parent::setUp();
1040 $this->language_interface = 'fr';
1041 }
1042 }
1043
1044
1045 /**
1046 * Functional tests for the language switching feature.
1047 */
1048 class LanguageSwitchingFunctionalTest extends DrupalWebTestCase {
1049
1050 public static function getInfo() {
1051 return array(
1052 'name' => 'Language switching',
1053 'description' => 'Tests for the language switching feature.',
1054 'group' => 'Locale',
1055 );
1056 }
1057
1058 function setUp() {
1059 parent::setUp('locale');
1060
1061 // Create and login user.
1062 $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer languages', 'translate interface', 'access administration pages'));
1063 $this->drupalLogin($admin_user);
1064 }
1065
1066 /**
1067 * Functional tests for the language switcher block.
1068 */
1069 function testLanguageBlock() {
1070 // Enable the language switching block.
1071 $edit = array(
1072 'locale_language[region]' => 'sidebar_first',
1073 );
1074 $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
1075
1076 // Add language.
1077 $edit = array(
1078 'langcode' => 'fr',
1079 );
1080 $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
1081
1082 // Set language negotiation.
1083 drupal_load('module', 'locale');
1084 include_once DRUPAL_ROOT . '/includes/language.inc';
1085 language_negotiation_set(LANGUAGE_TYPE_CONTENT, locale_language_negotiation_info());
1086
1087 // Assert that the language switching block is displayed on the frontpage.
1088 $this->drupalGet('');
1089 $this->assertText(t('Languages'), t('Language switcher block found.'));
1090
1091 // Assert that only the current language is marked as active.
1092 list($language_switcher) = $this->xpath('//div[@id="block-locale-language"]/div[@class="content"]');
1093 $links = array(
1094 'active' => array(),
1095 'inactive' => array(),
1096 );
1097 $anchors = array(
1098 'active' => array(),
1099 'inactive' => array(),
1100 );
1101 foreach ($language_switcher->ul->li as $link) {
1102 $classes = explode(" ", (string) $link['class']);
1103 list($language) = array_intersect($classes, array('en', 'fr'));
1104 if (in_array('active', $classes)) {
1105 $links['active'][] = $language;
1106 }
1107 else {
1108 $links['inactive'][] = $language;
1109 }
1110 $anchor_classes = explode(" ", (string) $link->a['class']);
1111 if (in_array('active', $anchor_classes)) {
1112 $anchors['active'][] = $language;
1113 }
1114 else {
1115 $anchors['inactive'][] = $language;
1116 }
1117 }
1118 $this->assertIdentical($links, array('active' => array('en'), 'inactive' => array('fr')), t('Only the current language list item is marked as active on the language switcher block.'));
1119 $this->assertIdentical($anchors, array('active' => array('en'), 'inactive' => array('fr')), t('Only the current language anchor is marked as active on the language switcher block.'));
1120 }
1121 }
1122
1123 /**
1124 * Functional tests for a user's ability to change their default language.
1125 */
1126 class LocaleUserLanguageFunctionalTest extends DrupalWebTestCase {
1127 public static function getInfo() {
1128 return array(
1129 'name' => 'User language settings',
1130 'description' => "Tests user's ability to change their default language.",
1131 'group' => 'Locale',
1132 );
1133 }
1134
1135 function setUp() {
1136 parent::setUp('locale');
1137 }
1138
1139 /**
1140 * Test if user can change their default language.
1141 */
1142 function testUserLanguageConfiguration() {
1143 global $base_url;
1144
1145 // User to add and remove language.
1146 $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
1147 // User to change their default language.
1148 $web_user = $this->drupalCreateUser();
1149
1150 // Add custom language.
1151 $this->drupalLogin($admin_user);
1152 // Code for the language.
1153 $langcode = 'xx';
1154 // The English name for the language.
1155 $name = $this->randomName(16);
1156 // The native name for the language.
1157 $native = $this->randomName(16);
1158 // The domain prefix.
1159 $prefix = 'xx';
1160 $edit = array(
1161 'langcode' => $langcode,
1162 'name' => $name,
1163 'native' => $native,
1164 'prefix' => $prefix,
1165 'direction' => '0',
1166 );
1167 $this->drupalPost('admin/config/regional/language/add'