summaryrefslogtreecommitdiff
path: root/modules/locale/locale.test
blob: c5d1ee5339d97ae8d7eec3c047449414721a3ae5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?php
// $Id$

/**
 * @file
 * Tests for Locale module.
 * 
 * The test file includes:
 *  - a functional test for the translation functionalities;
 *  - a functional test for the PO files import feature;
 *  - a functional test for the language switching feature
 *  - a couple of functional tests for the uninstall process.
 */

/**
 * Functional test for string translation and validation.
 */
class LocaleTranslationFunctionalTest extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => t('String translate and validate'),
      'description' => t('Adds a new locale and translates its name. Checks the validation of translation strings.'),
      'group' => 'Locale',
    );
  }

  function setUp() {
    parent::setUp('locale');
  }

  /**
   * Adds a language and tests string translation by users with the appropriate permissions.
   */
  function testStringTranslation() {
    global $base_url;

    // User to add and remove language.
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
    // User to translate and delete string.
    $translate_user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
    // Code for the language.
    $langcode = str_replace('simpletest_', 'si-', $this->randomName(6));
    // The English name for the language. This will be translated.
    $name = $this->randomName(16);
    // The native name for the language.
    $native = $this->randomName(16);
    // The domain prefix. Not tested yet.
    $prefix = strtolower(str_replace('si-', '', $langcode));
    // This is the language indicator on the translation search screen for
    // untranslated strings. Copied straight from locale.inc.
    $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
    // This will be the translation of $name.
    $translation = $this->randomName(16);

    // Add language.
    $this->drupalLogin($admin_user);
    $edit = array(
      'langcode' => $langcode,
      'name' => $name,
      'native' => $native,
      'prefix' => $prefix,
      'direction' => '0',
    );
    $this->drupalPost('admin/settings/language/add', $edit, t('Add custom language'));
    // Add string.
    t($name, array(), $langcode);
    // Reset locale cache.
    locale(NULL, NULL, TRUE);
    $this->assertText($langcode, 'Language code found');
    $this->assertText($name, 'Name found');
    $this->assertText($native, 'Native found');
    // No t() here, we do not want to add this string to the database and it's
    // surely not translated yet.
    $this->assertText($native, 'Test language added');
    $this->drupalLogout();

    // Search for the name and translate it.
    $this->drupalLogin($translate_user);
    $search = array (
      'string' => $name,
      'language' => 'all',
      'translation' => 'all',
      'group' => 'all',
    );
    $this->drupalPost('admin/build/translate/translate', $search, t('Filter'));
    // assertText seems to remove the input field where $name always could be
    // found, so this is not a false assert. See how assertNoText succeeds
    // later.
    $this->assertText($name, 'Search found the name');
    $this->assertRaw($language_indicator, 'Name is untranslated');
    // It's presumed that this is the only result. Given the random name, it's
    // reasonable.
    $this->clickLink(t('edit'));
    // We save the lid from the path.
    $matches = array();
    preg_match('!admin/build/translate/edit/(\d)+!', $this->getUrl(), $matches);
    $lid = $matches[1];
    // No t() here, it's surely not translated yet.
    $this->assertText($name, 'name found on edit screen');
    $edit = array (
      "translations[$langcode]" => $translation,
    );
    $this->drupalPost(NULL, $edit, t('Save translations'));
    $this->assertText(t('The string has been saved.'), 'The string has been saved.');
    $this->assertTrue($name != $translation && t($name, array(), $langcode) == $translation, 't() works');
    $this->drupalPost('admin/build/translate/translate', $search, t('Filter'));
    // The indicator should not be here.
    $this->assertNoRaw($language_indicator, 'String is translated');
    $this->drupalLogout();

    // Delete the language.
    $this->drupalLogin($admin_user);
    $path = 'admin/settings/language/delete/' . $langcode;
    // This a confirm form, we do not need any fields changed.
    $this->drupalPost($path, array(), t('Delete'));
    // We need raw here because %locale will add HTML.
    $this->assertRaw(t('The language %locale has been removed.', array('%locale' => $name)), 'The test language has been removed.');
    // Reload to remove $name.
    $this->drupalGet($path);
    $this->assertNoText($langcode, 'Language code not found');
    $this->assertNoText($name, 'Name not found');
    $this->assertNoText($native, 'Native not found');
    $this->drupalLogout();

    // Delete the name string.
    $this->drupalLogin($translate_user);
    $this->drupalPost('admin/build/translate/delete/' . $lid, array(), t('Delete'));
    $this->assertText(t('The string has been removed.'), 'The string has been removed message.');
    $this->drupalPost('admin/build/translate/translate', $search, t('Filter'));
    $this->assertNoText($name, 'Search now can not find the name');
  }

  /**
   * Tests the validation of the translation input.
   */
  function testStringValidation() {
    global $base_url;

    // User to add  language and strings
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'translate interface'));
    $this->drupalLogin($admin_user);
    $langcode = str_replace('simpletest_', 'si-', $this->randomName(6));
    // The English name for the language. This will be translated.
    $name = $this->randomName(16);
    // The native name for the language.
    $native = $this->randomName(16);
    // The domain prefix. Not tested yet.
    $prefix = strtolower(str_replace('si-', '', $langcode));
    // This is the language indicator on the translation search screen for
    // untranslated strings. Copied straight from locale.inc.
    $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
    // These will be the invalid translations of $name.
    $key = $this->randomName(16);
    $bad_translations[$key] = "<script>alert('xss');</script>" . $key;
    $key = $this->randomName(16);
    $bad_translations[$key] = '<img SRC="javascript:alert(\'xss\');">' . $key;
    $key = $this->randomName(16);
    $bad_translations[$key] = '<<SCRIPT>alert("xss");//<</SCRIPT>' . $key;
    $key = $this->randomName(16);
    $bad_translations[$key] ="<BODY ONLOAD=alert('xss')>" . $key;

    // Add language.
    $edit = array (
      'langcode' => $langcode,
      'name' => $name,
      'native' => $native,
      'prefix' => $prefix,
      'direction' => '0',
    );
    $this->drupalPost('admin/settings/language/add', $edit, t('Add custom language'));
    // Add string.
    t($name, array(), $langcode);
    // Reset locale cache.
    $search = array (
      'string' => $name,
      'language' => 'all',
      'translation' => 'all',
      'group' => 'all',
    );
    $this->drupalPost('admin/build/translate/translate', $search, t('Filter'));
    // Find the edit path
    $content = $this->drupalGetContent();
    $this->assertTrue(preg_match('@(admin/build/translate/edit/[0-9]+)@', $content, $matches), t('Found the edit path'));
    $path = $matches[0];
    foreach ($bad_translations as $key => $translation) {
      $edit = array (
        "translations[$langcode]" => $translation,
      );
      $this->drupalPost($path, $edit, t('Save translations'));
      // Check for a form error on the textarea.
      $form_class = $this->xpath('//form[@id="locale-translate-edit-form"]//textarea/@class');
      $this->assertNotIdentical(FALSE, strpos($form_class[0], 'error'), t('The string was rejected as unsafe.'));
      $this->assertNoText(t('The string has been saved.'), t('The string was not saved.'));
    }
  }
}

/**
 * Functional tests for the import of translation files.
 */
class LocaleImportFunctionalTest extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => t('Translation import'),
      'description' => t('Tests the importation of locale files.'),
      'group' => t('Locale'),
    );
  }

  /**
   * A user able to create languages and import translations.
   */
  protected $admin_user = NULL;

  function setUp() {
    parent::setUp('locale', 'locale_test');

    $this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
    $this->drupalLogin($this->admin_user);
  }

  /**
   * Test importation of standalone .po files.
   */
  function testStandalonePoFile() {
    // Try importing a .po file.
    $name = tempnam(file_directory_temp(), "po_");
    file_put_contents($name, $this->getPoFile());
    $this->drupalPost('admin/build/translate/import', array(
      'langcode' => 'fr',
      'files[file]' => $name,
    ), t('Import'));
    unlink($name);

    // The importation should automatically create the corresponding language.
    $this->assertRaw(t('The language %language has been created.', array('%language' => 'French')), t('The language has been automatically created'));

    // The importation should have create 7 strings.
    $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'));

    // Try importing a .po file with invalid tags in the default text group.
    $name = tempnam(file_directory_temp(), "po_");
    file_put_contents($name, $this->getBadPoFile());
    $this->drupalPost('admin/build/translate/import', array(
      'langcode' => 'fr',
      'files[file]' => $name,
    ), t('Import'));
    unlink($name);
    // The importation should have created 1 string and rejected 2.
    $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.'));
    $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.');
    $this->assertRaw($skip_message, t('Unsafe strings were skipped.'));

    // Try importing a .po file with invalid tags in a non default text group.
    $name = tempnam(file_directory_temp(), "po_");
    file_put_contents($name, $this->getBadPoFile());
    $this->drupalPost('admin/build/translate/import', array(
      'langcode' => 'fr',
      'files[file]' => $name,
      'group' => 'custom',
    ), t('Import'));
    unlink($name);
    // The importation should have created 3 strings.
    $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.'));
  }

  /**
   * Helper function that returns a proper .po file.
   */
  function getPoFile() {
    return <<< EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 6\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\\n"

msgid "Monday"
msgstr "lundi"

msgid "Tuesday"
msgstr "mardi"

msgid "Wednesday"
msgstr "mercredi"

msgid "Thursday"
msgstr "jeudi"

msgid "Friday"
msgstr "vendredi"

msgid "Saturday"
msgstr "samedi"

msgid "Sunday"
msgstr "dimanche"
EOF;
  }

  /**
   * Helper function that returns a proper .po file.
   */
  function getBadPoFile() {
    return <<< EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 6\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\\n"

msgid "Save configuration"
msgstr "Enregistrer la configuration"

msgid "edit"
msgstr "modifier<img SRC="javascript:alert(\'xss\');">"

msgid "delete"
msgstr "supprimer<script>alert('xss');</script>"

EOF;
  }
}

/**
 * Locale uninstall with English UI functional test.
 */
class LocaleUninstallFunctionalTest extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => t('Locale uninstall (EN)'),
      'description' => t('Tests the uninstall process using the built-in UI language.'),
      'group' => t('Locale'),
    );
  }

  /**
   * The default language set for the UI before uninstall.
   */
  protected $ui_language;
  
  function setUp() {
    parent::setUp('locale');
    $this->ui_language = 'en';
  }
  
  /**
   * Check if the values of the Locale variables are correct after uninstall.
   */
  function testUninstallProcess() {
    $locale_module = array('locale');
    
    // Add a new language and optionally set it as default.
    require_once DRUPAL_ROOT . '/includes/locale.inc';
    locale_add_language('fr', 'French', 'Français', LANGUAGE_LTR, '', '', TRUE, $this->ui_language == 'fr');
    
    // Check the UI language.
    drupal_init_language();
    global $language;
    $this->assertEqual($language->language, $this->ui_language, t('Current language: %lang', array('%lang' => $language->language)));

    // Change language negotiation options.
    variable_set('language_negotiation', LANGUAGE_NEGOTIATION_PATH_DEFAULT);
    
    // Enable multilingual workflow option for articles.
    variable_set('language_content_type_article', 1);
    
    // Change JavaScript translations directory.
    variable_set('locale_js_directory', 'js_translations');
    
    // Build the JavaScript translation file for French.
    $user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
    $this->drupalLogin($user);
    $this->drupalGet('admin/build/translate/translate');
    $string = db_fetch_object(db_query('SELECT min(lid) AS lid FROM {locales_source} WHERE location LIKE \'%.js%\' AND textgroup = \'default\''));
    $edit = array('translations[fr]' => 'french translation');
    $this->drupalPost('admin/build/translate/edit/'. $string->lid, $edit, t('Save translations'));
    _locale_rebuild_js('fr');
    $file = db_fetch_object(db_query('SELECT javascript FROM {languages} WHERE language = \'fr\''));
    $js_file = file_create_path(variable_get('locale_js_directory', 'languages')) .'/fr_'. $file->javascript .'.js';
    $this->assertTrue($result = file_exists($js_file), t('JavaScript file created: %file', array('%file' => $result ? $js_file : t('none'))));
    
    // Disable string caching.
    variable_set('locale_cache_strings', 0);
    
    // Uninstall Locale.
    module_disable($locale_module);
    drupal_uninstall_modules($locale_module);
    
    // Visit the front page.
    $this->drupalGet('');

    // Check the init language logic.
    drupal_init_language();
    $this->assertEqual($language->language, 'en', t('Language after uninstall: %lang', array('%lang' => $language->language)));
    
    // Check JavaScript files deletion.
    $this->assertTrue($result = !file_exists($js_file), t('JavaScript file deleted: %file', array('%file' => $result ? $js_file : t('found'))));
    
    // Check language count.
    $language_count = variable_get('language_count', 1);
    $this->assertEqual($language_count, 1, t('Language count: %count', array('%count' => $language_count)));

    // Check language negotiation.
    $language_negotiation = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE) == LANGUAGE_NEGOTIATION_NONE;
    $this->assertTrue($language_negotiation, t('Language negotiation: %setting', array('%setting' => t($language_negotiation ? 'none' : 'set'))));

    // Check JavaScript parsed.
    $javascript_parsed_count = count(variable_get('javascript_parsed', array()));
    $this->assertEqual($javascript_parsed_count, 0, t('JavaScript parsed count: %count', array('%count' => $javascript_parsed_count)));

    // Check multilingual workflow option for articles.
    $multilingual = variable_get('language_content_type_article', 0);
    $this->assertEqual($multilingual, 0, t('Multilingual workflow option: %status', array('%status' => t($multilingual ? 'enabled': 'disabled'))));
    
    // Check JavaScript translations directory.
    $locale_js_directory = variable_get('locale_js_directory', 'languages');
    $this->assertEqual($locale_js_directory, 'languages', t('JavaScript translations directory: %dir', array('%dir' => $locale_js_directory)));
    
    // Check string caching.
    $locale_cache_strings = variable_get('locale_cache_strings', 1);
    $this->assertEqual($locale_cache_strings, 1, t('String caching: %status', array('%status' => t($locale_cache_strings ? 'enabled': 'disabled')))); 
  }
}

/**
 * Locale uninstall with French UI functional test.
 * 
 * Because this class extends LocaleUninstallFunctionalTest, it doesn't require a new
 * test of its own. Rather, it switches the default UI language in setUp and then
 * runs the testUninstallProcess (which it inherits from LocaleUninstallFunctionalTest)
 * to test with this new language.
 */
class LocaleUninstallFrenchFunctionalTest extends LocaleUninstallFunctionalTest {
  function getInfo() {
    return array(
      'name' => t('Locale uninstall (FR)'),
      'description' => t('Tests the uninstall process using French as UI language.'),
      'group' => t('Locale'),
    );
  }
  
  function setUp() {
    parent::setUp();
    $this->ui_language = 'fr';
  }
}


/**
 * Functional tests for the language switching feature.
 */
class LanguageSwitchingFunctionalTest extends DrupalWebTestCase {

  function getInfo() {
    return array(
      'name' => t('Language switching'),
      'description' => t('Tests for the language switching feature.'),
      'group' => t('Locale'),
    );
  }

  function setUp() {
    parent::setUp('locale');

    // Create and login user
    $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer languages', 'translate interface', 'access administration pages'));
    $this->drupalLogin($admin_user);
  }

  function testLanguageBlock() {
    // Enable the language switching block.
    $edit = array(
      'locale_language-switcher[region]' => 'left',
    );
    $this->drupalPost('admin/build/block', $edit, t('Save blocks'));

    // Add language.
    $edit = array(
      'langcode' => 'fr',
    );
    $this->drupalPost('admin/settings/language/add', $edit, t('Add language'));

    // Set language negotiation.
    $edit = array(
      'language_negotiation' => LANGUAGE_NEGOTIATION_PATH_DEFAULT,
    );
    $this->drupalPost('admin/settings/language/configure', $edit, t('Save settings'));

    // Assert that the language switching block is displayed on the frontpage.
    $this->drupalGet('');
    $this->assertText(t('Languages'));

    // Assert that only the current language is marked as active.
    list($language_switcher) = $this->xpath('//div[@id="block-locale-language-switcher"]');
    $links = array(
      'active' => array(),
      'inactive' => array(),
    );
    $anchors = array(
      'active' => array(),
      'inactive' => array(),
    );
    foreach ($language_switcher->div->ul->li as $link) {
      $classes = explode(" ", (string) $link['class']);
      list($language) = array_intersect($classes, array('en', 'fr'));
      if (in_array('active', $classes)) {
        $links['active'][] = $language;
      }
      else {
        $links['inactive'][] = $language;
      }
      $anchor_classes = explode(" ", (string) $link->a['class']);
      if (in_array('active', $anchor_classes)) {
        $anchors['active'][] = $language;
      }
      else {
        $anchors['inactive'][] = $language;
      }
    }
    $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'));
    $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'));
  }
}