Projet

Général

Profil

Paste
Télécharger (16,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n.test @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * Base class for Internationalization tests
6
 */
7
class Drupali18nTestCase extends DrupalWebTestCase {
8
  protected $current_user;
9
  protected $default_language;
10
  protected $secondary_language;
11

    
12
  function setUpLanguages($admin_permissions = array()) {
13
    // Setup admin user.
14
    $this->admin_user = $this->drupalCreateUser(array_merge(array('bypass node access', 'administer nodes', 'administer languages', 'administer content types', 'administer blocks', 'access administration pages', 'translate interface'), $admin_permissions));
15

    
16
    $this->drupalLogin($this->admin_user);
17

    
18
    // Add languages.
19
    $this->default_language = 'en';
20
    $this->secondary_language = 'es';
21
    $this->addLanguage($this->default_language);
22
    $this->addLanguage($this->secondary_language);
23

    
24
    // Enable URL language detection and selection to make the language switcher
25
    // block appear.
26
    $edit = array('language[enabled][locale-url]' => TRUE);
27
    $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
28
    $this->assertRaw(t('Language negotiation configuration saved.'), t('URL language detection enabled.'));
29
    $this->drupalGet('admin/config/regional/language/configure');
30
    $this->resetCaches();
31
  }
32

    
33
  /**
34
   * Set up content-type (with translation).
35
   */
36
  function setUpContentType($settings = array()) {
37
    $settings += array(
38
      'type' => 'page',
39
      'mode' => TRANSLATION_ENABLED,
40
      'status' => 1,
41
      'promote' => 0,
42
    );
43

    
44
    $type = node_type_get_type($settings['type']);
45
    // Create content editor with translation permissions.
46
    $this->content_editor = $this->drupalCreateUser(array(
47
      'create ' . $type->type . ' content',
48
      'edit own ' . $type->type . ' content',
49
      'translate content',
50
      'translate interface',
51
    ));
52

    
53
    $this->drupalLogin($this->admin_user);
54
    // Set content type to use multilingual support with translation.
55
    $this->drupalGet('admin/structure/types/manage/' . $type->type);
56
    $edit = array();
57
    $edit['language_content_type'] = $settings['mode'];
58
    // Mark status and promoted
59
    $edit['node_options[status]'] = $settings['status'];
60
    $edit['node_options[promote]'] = $settings['promote'];
61
    $this->drupalPost('admin/structure/types/manage/' . $type->type, $edit, t('Save content type'));
62
    $this->assertRaw(t('The content type %type has been updated.', array('%type' => $type->name)), t('%type content type has been updated.', array('%type' => $type->name)));
63
    $this->drupalGet('admin/structure/types/manage/' . $type->type);
64
    $this->enableLanguageBlock();
65
  }
66

    
67
  /**
68
   * Enable the language switcher block.
69
   */
70
  function enableLanguageBlock() {
71
    // Enable the language switcher block.
72
    $language_type = LANGUAGE_TYPE_INTERFACE;
73
    $edit = array("blocks[locale_$language_type][region]" => 'sidebar_first');
74
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
75
  }
76

    
77
  /**
78
   * Set up translation for content type (default: page).
79
   */
80
  function setUpContentTranslation($settings = array()) {
81
    $settings += array(
82
      'mode' => TRANSLATION_ENABLED,
83
    );
84
    $this->setUpContentType($settings);
85
  }
86

    
87
  /**
88
   * Install a the specified language if it has not been already. Otherwise make sure that
89
   * the language is enabled.
90
   *
91
   * @param $language_code
92
   *   The language code the check.
93
   */
94
  function addLanguage($language_code) {
95
    // Check to make sure that language has not already been installed.
96
    $this->drupalGet('admin/config/regional/language');
97

    
98
    if (strpos($this->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
99
      // Doesn't have language installed so add it.
100
      $edit = array();
101
      $edit['langcode'] = $language_code;
102
      $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
103

    
104
      // Make sure we are not using a stale list.
105
      drupal_static_reset('language_list');
106
      $languages = language_list('language');
107
      $this->assertTrue(array_key_exists($language_code, $languages), t('Language was installed successfully.'));
108

    
109
      if (array_key_exists($language_code, $languages)) {
110
        $this->assertRaw(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => $languages[$language_code]->name, '@locale-help' => url('admin/help/locale'))), t('Language has been created.'));
111
      }
112
    }
113
    elseif ($this->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(':name' => 'enabled[' . $language_code . ']'))) {
114
      // It's installed and enabled. No need to do anything.
115
      $this->assertTrue(true, 'Language [' . $language_code . '] already installed and enabled.');
116
    }
117
    else {
118
      // It's installed but not enabled. Enable it.
119
      $this->assertTrue(true, 'Language [' . $language_code . '] already installed.');
120
      $this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));
121
      $this->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
122
    }
123
  }
124

    
125
  /**
126
   * Create translation set from a node
127
   *
128
   * @param $source
129
   *   Source node
130
   * @param $languages
131
   *   Optional list of language codes
132
   */
133
  function createNodeTranslationSet(&$source, $languages = NULL) {
134
    if (empty($source->tnid)) {
135
      $source->tnid = $source->nid;
136
    }
137
    $translations[$source->language] = $source;
138
    foreach (language_list() as $language) {
139
      if ($language->language != $source->language) {
140
        $translations[$language->language] = $this->createNodeTranslation($source, $language);
141
      }
142
    }
143
    return $translations;
144
  }
145

    
146
  /**
147
   * Create a node of the specified type in the specified language.
148
   
149
   * @param $type
150
   *   The node type.
151
   * @param $title
152
   *   Title of node in specified language.
153
   * @param $body
154
   *   Body of node in specified language.
155
   * @param $langcode
156
   *   Language code.
157
   */
158
  function createNode($type, $title, $body, $langcode, $edit = array()) {
159
    $lang = LANGUAGE_NONE;
160
    $edit["title"] = $title;
161
    $edit["body[$lang][0][value]"] = $body;
162
    $edit['language'] = $langcode;
163
    $this->drupalPost('node/add/' . $type, $edit, t('Save'));
164
    $info = node_type_load($type);
165
    $message = t('@name %title has been created.', array('@name' => $info->name, '%title' => $title));
166
    $this->assertRaw($message);
167

    
168
    // Check to make sure the node was created.
169
    $node = $this->drupalGetNodeByTitle($title);
170
    $this->assertTrue($node, t('Node found in database.'));
171

    
172
    return $node;
173
  }
174

    
175
  /**
176
   * Create a translation for the specified node in the specified language.
177
   *
178
   * @param $node
179
   *   The basic page to create translation for.
180
   * @param $title
181
   *   Title of node in specified language.
182
   * @param $body
183
   *   Body of node in specified language.
184
   * @param $language
185
   *   Language code.
186
   */
187
  function createNodeTranslation($node, $language, $title = NULL, $body = NULL) {
188
    $body = $body ? $body : $this->randomName();
189
    $title = $title ? $title : $this->randomName();
190
    $this->drupalGet('node/add/' . $node->type, array('query' => array('translation' => $node->nid, 'target' => $language->language)));
191

    
192

    
193
    $this->assertFieldByXPath('//input[@id="edit-title"]', $node->title, "Original title value correctly populated.");
194
    $field_lang = field_language('node', $node, 'body');
195
    $body_key = "body[und][0][value]";
196
    $this->assertFieldByXPath("//textarea[@name='$body_key']", $node->body[$field_lang][0]['value'], "Original body value correctly populated.");
197

    
198
    $edit = array();
199
    $edit["title"] = $title;
200
    $edit[$body_key] = $body;
201
    $this->drupalPost(NULL, $edit, t('Save'));
202
    $info = node_type_load($node->type);
203
    $message = t('@name %title has been created.', array('@name' => $info->name, '%title' => $title));
204
    $this->assertRaw($message);
205
    // Check to make sure that translation was successful.
206
    $translation = $this->drupalGetNodeByTitle($title);
207
    $this->assertTrue($translation, t('Node found in database.'));
208
    $this->assertTrue($translation->tnid == $node->nid, t('Translation set id correctly stored.'));
209

    
210
    return $translation;
211
  }
212

    
213
  /**
214
   * Retrieves a Drupal path or an absolute path with language
215
   *
216
   * @param $language
217
   *   Language code or language object
218
   */
219
  protected function i18nGet($language, $path = '', array $options = array(), array $headers = array()) {
220
    $options['language'] = $this->getLanguage($language);
221
    return $this->drupalGet($path, $options, $headers);
222
  }
223
  /**
224
   * Check strings for different languages
225
   */
226
  function i18nAssertTranslations($translations, $path = '', $message = 'Translation found for language.') {
227
    foreach ($translations as $langcode => $text) {
228
      $language = $this->getLanguage($langcode);
229
      if ($language->enabled) {
230
        $this->i18nGet($language, $path);
231
        $this->assertRaw($text, $message . ' ' . $language->name . ': ' . check_plain($text));
232
      }
233
    }
234
  }
235
  /**
236
   * Create node with language
237
   */
238
  protected function i18nCreateNode($language, $settings = array()) {
239
    $language = $this->getLanguage($language);
240
    $settings += array('language'  => $language->language, 'body' => array());
241
    $settings['body'] += array($language->language => array(array()));
242
    return $this->drupalCreateNode($settings);
243
  }
244
  /**
245
   * Move block to region, from block.test
246
   */
247
  function moveBlockToRegion($block, $region = 'sidebar_first') {
248
    $this->drupalLogin($this->admin_user);
249
    // Set the created block to a specific region.
250
    $edit = array();
251
    $edit['blocks[' . $block['module'] . '_' . $block['delta'] . '][region]'] = $region;
252
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
253

    
254
    // Confirm that the block was moved to the proper region.
255
    $this->assertText(t('The block settings have been updated.'), t('Block successfully moved to %region_name region.', array( '%region_name' => $region)));
256

    
257
    // Confirm that the block is being displayed.
258
    $this->drupalGet('node');
259
    $this->assertText(check_plain($block['title']), t('Block successfully being displayed on the page.'));
260

    
261
    // Confirm that the custom block was found at the proper region.
262
    $xpath = $this->buildXPathQuery('//div[@class=:region-class]//div[@id=:block-id]/*', array(
263
      ':region-class' => 'region region-' . str_replace('_', '-', $region),
264
      ':block-id' => 'block-' . $block['module'] . '-' . $block['delta'],
265
    ));
266
    $this->assertFieldByXPath($xpath, NULL, t('Custom block found in %region_name region.', array('%region_name' => $region)));
267
  }
268
  /**
269
   * Get language object for langcode
270
   */
271
  public function getLanguage($langcode) {
272
    if (is_object($langcode)) {
273
      return $langcode;
274
    }
275
    else {
276
      $language_list = language_list();
277
      return $language_list[$langcode];
278
    }
279
  }
280
  /**
281
   * Switch global language
282
   */
283
  public function switchLanguage($newlang = NULL) {
284
    $newlang = $newlang ? $newlang : $this->install_locale;
285
    $GLOBALS[LANGUAGE_TYPE_INTERFACE] = $this->getLanguage($newlang);
286
  }
287

    
288
  /**
289
   * Get all languages that are not default
290
   */
291
  public function getOtherLanguages() {
292
    $languages = language_list();
293
    unset($languages[language_default('language')]);
294
    return $languages;
295
  }
296
  /**
297
   * Get enabled languages
298
   */
299
  public function getEnabledLanguages() {
300
    $list = array();
301
    foreach (language_list() as $langcode => $language) {
302
      if (!empty($language->enabled)) {
303
        $list[$langcode] = $language;
304
      }
305
    }
306
    return $list;
307
  }
308
  /**
309
   * Create translation for string in textgroup
310
   *
311
   * @param $translations
312
   *   Optional array of langcode => translation. If not present, it will be generated.
313
   */
314
  function createStringTranslation($textgroup, $name, $translations = NULL) {
315
    // Generate translations if not found, they will be the same length as source string
316
    if (!$translations) {
317
      $length = strlen($name);
318
      foreach ($this->getOtherLanguages() as $language) {
319
        $translations[$language->language] = $this->randomName($length);
320
      }
321
    }
322
    $this->drupalLogin($this->translator);
323
    // This is the language indicator on the translation search screen for
324
    // untranslated strings. Copied straight from locale.inc.
325
    $language_indicator = "<em class=\"locale-untranslated\">";
326
    // Search for the name and translate it.
327
    $search = array(
328
      'string' => $name,
329
      'language' => 'all',
330
      'translation' => 'all',
331
      'group' => $textgroup,
332
    );
333
    $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
334
    // assertText() seems to remove the input field where $name always could be
335
    // found, so this is not a false assert. See how assertNoText succeeds
336
    // later.
337
    $this->assertText(check_plain($name), t('Search found the name.'));
338
    $this->assertRaw($language_indicator, t('Name is untranslated.'));
339
    // Assume this is the only result, given the random name.
340
    $this->clickLink(t('edit'));
341
    // We save the lid from the path.
342
    $matches = array();
343
    preg_match('!admin/config/regional/translate/edit/(\d+)!', $this->getUrl(), $matches);
344
    $lid = $matches[1];
345
    // No t() here, it's surely not translated yet.
346
    $this->assertText(check_plain($name), t('name found on edit screen.'));
347
    foreach ($translations as $langcode => $translation) {
348
      $edit["translations[$langcode]"] = $translation;
349
    }
350
    $this->drupalPost(NULL, $edit, t('Save translations'));
351
    $this->assertText(t('The string has been saved.'), t('The string has been saved.'));
352
    $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
353
    $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
354
    // The indicator should not be here.
355
    $this->assertNoRaw($language_indicator, t('String is translated.'));
356
    return $translations;
357
  }
358

    
359
  /**
360
   * Reset static caches to make the test code match the client site behavior.
361
   */
362
  function resetCaches() {
363
    drupal_static_reset('locale_url_outbound_alter');
364
    drupal_static_reset('language_list');
365
    drupal_language_initialize();
366
  }
367

    
368
  /**
369
   * Print out a variable for debugging
370
   */
371
  function printDebug($data, $title = 'Debug') {
372
    $output = '<h2>' . $title . '<h2 />';
373
    $output .= '<pre>';
374
    $output .= is_array($data) || is_object($data) ? print_r($data, TRUE) : $data;
375
    $output .= '<pre>';
376
    //$this->assertTrue(TRUE, $output);
377
    $this->verbose($output);
378
  }
379
  /**
380
   * Debug dump object with some formatting
381
   */
382
  function printObject($object, $title = 'Object') {
383
    $output = $this->formatTable($object);
384
    $this->printDebug($output, $title);
385
  }
386

    
387
  /**
388
   * Print out current HTML page
389
   */
390
  function printPage() {
391
    $this->printDebug($this->drupalGetContent());
392
  }
393

    
394
  /**
395
   * Dump table contents
396
   *
397
   * @params $table1, $table2..
398
   *   One or more table names
399
   */
400
  function dumpTable() {
401
    $output = '';
402
    foreach (func_get_args() as $table) {
403
      $header = $rows = array();
404
      $result = db_query('SELECT * FROM {' . $table . '}');
405
      $output .= '<h2>Table dump <i>' . $table . '</i>:</h2>';
406
      while ($row = $result->fetchAssoc()) {
407
        $rows[] = $row;
408
        if (empty($header)) {
409
          $header = array_keys($row);
410
        }
411
      }
412
      if (!empty($rows)) {
413
        $output .= theme('table', array('header' => $header, 'rows' => $rows));
414
      }
415
      else {
416
        $output .= ' No rows';
417
      }
418
      $output .= '<br />';
419
    }
420
    $this->verbose($output);
421
  }
422

    
423
  /**
424
   * Format object as table, recursive
425
   */
426
  function formatTable($object) {
427
    foreach ($object as $key => $value) {
428
      $rows[] = array(
429
        $key,
430
        is_array($value) || is_object($value) ? $this->formatTable($value) : $value,
431
      );
432
    }
433
    if (!empty($rows)) {
434
      return theme('table', array('rows' => $rows));
435
    }
436
    else {
437
      return 'No properties';
438
    }
439
  }
440
}
441

    
442
class Drupali18nConfigTestCase extends Drupali18nTestCase {
443
  public static function getInfo() {
444
    return array(
445
      'name' => 'Multilingual configuration',
446
      'group' => 'Internationalization',
447
      'description' => 'Basic configuration for the i18n module',
448
    );
449
  }
450

    
451
  function setUp() {
452
    parent::setUp('translation', 'i18n_node');
453
    parent::setUpLanguages();
454
  }
455

    
456
  function testEnableLanguages() {
457
    // A language with two letter code may help too
458
    $this->addLanguage('pt-br');
459

    
460
    // Disable Italian to test the translation behavior with disabled languages.
461
    $this->addLanguage('it');
462
    $edit = array('enabled[it]' => FALSE);
463
    $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
464
  }
465
}