Project

General

Profile

Paste
Download (21.6 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / translation / translation.test @ 175ce36f

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for the Translation module.
6
 */
7

    
8
/**
9
 * Functional tests for the Translation module.
10
 */
11
class TranslationTestCase extends DrupalWebTestCase {
12
  protected $book;
13

    
14
  public static function getInfo() {
15
    return array(
16
      'name' => 'Translation functionality',
17
      'description' => 'Create a basic page with translation, modify the page outdating translation, and update translation.',
18
      'group' => 'Translation'
19
    );
20
  }
21

    
22
  function setUp() {
23
    parent::setUp('locale', 'translation', 'translation_test');
24

    
25
    // Setup users.
26
    $this->admin_user = $this->drupalCreateUser(array('bypass node access', 'administer nodes', 'administer languages', 'administer content types', 'administer blocks', 'access administration pages', 'translate content'));
27
    $this->translator = $this->drupalCreateUser(array('create page content', 'edit own page content', 'translate content'));
28

    
29
    $this->drupalLogin($this->admin_user);
30

    
31
    // Add languages.
32
    $this->addLanguage('en');
33
    $this->addLanguage('es');
34
    $this->addLanguage('it');
35

    
36
    // Disable Italian to test the translation behavior with disabled languages.
37
    $edit = array('enabled[it]' => FALSE);
38
    $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
39

    
40
    // Set "Basic page" content type to use multilingual support with
41
    // translation.
42
    $this->drupalGet('admin/structure/types/manage/page');
43
    $edit = array();
44
    $edit['language_content_type'] = 2;
45
    $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
46
    $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), 'Basic page content type has been updated.');
47

    
48
    // Enable the language switcher block.
49
    $language_type = LANGUAGE_TYPE_INTERFACE;
50
    $edit = array("blocks[locale_$language_type][region]" => 'sidebar_first');
51
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
52

    
53
    // Enable URL language detection and selection to make the language switcher
54
    // block appear.
55
    $edit = array('language[enabled][locale-url]' => TRUE);
56
    $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
57
    $this->assertRaw(t('Language negotiation configuration saved.'), 'URL language detection enabled.');
58
    $this->resetCaches();
59

    
60
    $this->drupalLogin($this->translator);
61
  }
62

    
63
  /**
64
   * Creates, modifies, and updates a basic page with a translation.
65
   */
66
  function testContentTranslation() {
67
    // Create Basic page in English.
68
    $node_title = $this->randomName();
69
    $node_body =  $this->randomName();
70
    $node = $this->createPage($node_title, $node_body, 'en');
71

    
72
    // Unpublish the original node to check that this has no impact on the
73
    // translation overview page, publish it again afterwards.
74
    $this->drupalLogin($this->admin_user);
75
    $this->drupalPost('node/' . $node->nid . '/edit', array('status' => FALSE), t('Save'));
76
    $this->drupalGet('node/' . $node->nid . '/translate');
77
    $this->drupalPost('node/' . $node->nid . '/edit', array('status' => NODE_PUBLISHED), t('Save'));
78
    $this->drupalLogin($this->translator);
79

    
80
    // Check that the "add translation" link uses a localized path.
81
    $languages = language_list();
82
    $this->drupalGet('node/' . $node->nid . '/translate');
83
    $this->assertLinkByHref($languages['es']->prefix . '/node/add/' . str_replace('_', '-', $node->type), 0, format_string('The "add translation" link for %language points to the localized path of the target language.', array('%language' => $languages['es']->name)));
84

    
85
    // Submit translation in Spanish.
86
    $node_translation_title = $this->randomName();
87
    $node_translation_body = $this->randomName();
88
    $node_translation = $this->createTranslation($node, $node_translation_title, $node_translation_body, 'es');
89

    
90
    // Check that the "edit translation" and "view node" links use localized
91
    // paths.
92
    $this->drupalGet('node/' . $node->nid . '/translate');
93
    $this->assertLinkByHref($languages['es']->prefix . '/node/' . $node_translation->nid . '/edit', 0, format_string('The "edit" link for the translation in %language points to the localized path of the translation language.', array('%language' => $languages['es']->name)));
94
    $this->assertLinkByHref($languages['es']->prefix . '/node/' . $node_translation->nid, 0, format_string('The "view" link for the translation in %language points to the localized path of the translation language.', array('%language' => $languages['es']->name)));
95

    
96
    // Attempt to submit a duplicate translation by visiting the node/add page
97
    // with identical query string.
98
    $this->drupalGet('node/add/page', array('query' => array('translation' => $node->nid, 'target' => 'es')));
99
    $this->assertRaw(t('A translation of %title in %language already exists', array('%title' => $node_title, '%language' => $languages['es']->name)), 'Message regarding attempted duplicate translation is displayed.');
100

    
101
    // Attempt a resubmission of the form - this emulates using the back button
102
    // to return to the page then resubmitting the form without a refresh.
103
    $edit = array();
104
    $langcode = LANGUAGE_NONE;
105
    $edit["title"] = $this->randomName();
106
    $edit["body[$langcode][0][value]"] = $this->randomName();
107
    $this->drupalPost('node/add/page', $edit, t('Save'), array('query' => array('translation' => $node->nid, 'language' => 'es')));
108
    $duplicate = $this->drupalGetNodeByTitle($edit["title"]);
109
    $this->assertEqual($duplicate->tnid, 0, 'The node does not have a tnid.');
110

    
111
    // Update original and mark translation as outdated.
112
    $node_body = $this->randomName();
113
    $node->body[LANGUAGE_NONE][0]['value'] = $node_body;
114
    $edit = array();
115
    $edit["body[$langcode][0][value]"] = $node_body;
116
    $edit['translation[retranslate]'] = TRUE;
117
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
118
    $this->assertRaw(t('Basic page %title has been updated.', array('%title' => $node_title)), 'Original node updated.');
119

    
120
    // Check to make sure that interface shows translation as outdated.
121
    $this->drupalGet('node/' . $node->nid . '/translate');
122
    $this->assertRaw('<span class="marker">' . t('outdated') . '</span>', 'Translation marked as outdated.');
123

    
124
    // Update translation and mark as updated.
125
    $edit = array();
126
    $edit["body[$langcode][0][value]"] = $this->randomName();
127
    $edit['translation[status]'] = FALSE;
128
    $this->drupalPost('node/' . $node_translation->nid . '/edit', $edit, t('Save'));
129
    $this->assertRaw(t('Basic page %title has been updated.', array('%title' => $node_translation_title)), 'Translated node updated.');
130

    
131
    // Confirm that disabled languages are an option for translators when
132
    // creating nodes.
133
    $this->drupalGet('node/add/page');
134
    $this->assertFieldByXPath('//select[@name="language"]//option', 'it', 'Italian (disabled) is available in language selection.');
135
    $translation_it = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'it');
136
    $this->assertRaw($translation_it->body[LANGUAGE_NONE][0]['value'], 'Content created in Italian (disabled).');
137

    
138
    // Confirm that language neutral is an option for translators when there are
139
    // disabled languages.
140
    $this->drupalGet('node/add/page');
141
    $this->assertFieldByXPath('//select[@name="language"]//option', LANGUAGE_NONE, 'Language neutral is available in language selection with disabled languages.');
142
    $node2 = $this->createPage($this->randomName(), $this->randomName(), LANGUAGE_NONE);
143
    $this->assertRaw($node2->body[LANGUAGE_NONE][0]['value'], 'Language neutral content created with disabled languages available.');
144

    
145
    // Leave just one language enabled and check that the translation overview
146
    // page is still accessible.
147
    $this->drupalLogin($this->admin_user);
148
    $edit = array('enabled[es]' => FALSE);
149
    $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
150
    $this->drupalLogin($this->translator);
151
    $this->drupalGet('node/' . $node->nid . '/translate');
152
    $this->assertRaw(t('Translations of %title', array('%title' => $node->title)), 'Translation overview page available with only one language enabled.');
153
  }
154

    
155
  /**
156
   * Checks that the language switch links behave properly.
157
   */
158
  function testLanguageSwitchLinks() {
159
    // Create a Basic page in English and its translations in Spanish and
160
    // Italian.
161
    $node = $this->createPage($this->randomName(), $this->randomName(), 'en');
162
    $translation_es = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'es');
163
    $translation_it = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'it');
164

    
165
    // Check that language switch links are correctly shown only for enabled
166
    // languages.
167
    $this->assertLanguageSwitchLinks($node, $translation_es);
168
    $this->assertLanguageSwitchLinks($translation_es, $node);
169
    $this->assertLanguageSwitchLinks($node, $translation_it, FALSE);
170

    
171
    // Check that links to the displayed translation appear only in the language
172
    // switcher block.
173
    $this->assertLanguageSwitchLinks($node, $node, FALSE, 'node');
174
    $this->assertLanguageSwitchLinks($node, $node, TRUE, 'block-locale');
175

    
176
    // Unpublish the Spanish translation to check that the related language
177
    // switch link is not shown.
178
    $this->drupalLogin($this->admin_user);
179
    $edit = array('status' => FALSE);
180
    $this->drupalPost("node/$translation_es->nid/edit", $edit, t('Save'));
181
    $this->drupalLogin($this->translator);
182
    $this->assertLanguageSwitchLinks($node, $translation_es, FALSE);
183

    
184
    // Check that content translation links are shown even when no language
185
    // negotiation is configured.
186
    $this->drupalLogin($this->admin_user);
187
    $edit = array('language[enabled][locale-url]' => FALSE);
188
    $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
189
    $this->resetCaches();
190
    $edit = array('status' => TRUE);
191
    $this->drupalPost("node/$translation_es->nid/edit", $edit, t('Save'));
192
    $this->drupalLogin($this->translator);
193
    $this->assertLanguageSwitchLinks($node, $translation_es, TRUE, 'node');
194
  }
195

    
196
  /**
197
   * Tests that the language switcher block alterations work as intended.
198
   */
199
  function testLanguageSwitcherBlockIntegration() {
200
    // Enable Italian to have three items in the language switcher block.
201
    $this->drupalLogin($this->admin_user);
202
    $edit = array('enabled[it]' => TRUE);
203
    $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
204
    $this->drupalLogin($this->translator);
205

    
206
    // Create a Basic page in English.
207
    $type = 'block-locale';
208
    $node = $this->createPage($this->randomName(), $this->randomName(), 'en');
209
    $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
210
    $this->assertLanguageSwitchLinks($node, $this->emptyNode('es'), TRUE, $type);
211
    $this->assertLanguageSwitchLinks($node, $this->emptyNode('it'), TRUE, $type);
212

    
213
    // Create the Spanish translation.
214
    $translation_es = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'es');
215
    $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
216
    $this->assertLanguageSwitchLinks($node, $translation_es, TRUE, $type);
217
    $this->assertLanguageSwitchLinks($node, $this->emptyNode('it'), TRUE, $type);
218

    
219
    // Create the Italian translation.
220
    $translation_it = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'it');
221
    $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
222
    $this->assertLanguageSwitchLinks($node, $translation_es, TRUE, $type);
223
    $this->assertLanguageSwitchLinks($node, $translation_it, TRUE, $type);
224

    
225
    // Create a language neutral node and check that the language switcher is
226
    // left untouched.
227
    $node2 = $this->createPage($this->randomName(), $this->randomName(), LANGUAGE_NONE);
228
    $node2_en = (object) array('nid' => $node2->nid, 'language' => 'en');
229
    $node2_es = (object) array('nid' => $node2->nid, 'language' => 'es');
230
    $node2_it = (object) array('nid' => $node2->nid, 'language' => 'it');
231
    $this->assertLanguageSwitchLinks($node2_en, $node2_en, TRUE, $type);
232
    $this->assertLanguageSwitchLinks($node2_en, $node2_es, TRUE, $type);
233
    $this->assertLanguageSwitchLinks($node2_en, $node2_it, TRUE, $type);
234

    
235
    // Disable translation support to check that the language switcher is left
236
    // untouched only for new nodes.
237
    $this->drupalLogin($this->admin_user);
238
    $edit = array('language_content_type' => 0);
239
    $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
240
    $this->drupalLogin($this->translator);
241

    
242
    // Existing translations trigger alterations even if translation support is
243
    // disabled.
244
    $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
245
    $this->assertLanguageSwitchLinks($node, $translation_es, TRUE, $type);
246
    $this->assertLanguageSwitchLinks($node, $translation_it, TRUE, $type);
247

    
248
    // Check that new nodes with a language assigned do not trigger language
249
    // switcher alterations when translation support is disabled.
250
    $node = $this->createPage($this->randomName(), $this->randomName());
251
    $node_es = (object) array('nid' => $node->nid, 'language' => 'es');
252
    $node_it = (object) array('nid' => $node->nid, 'language' => 'it');
253
    $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
254
    $this->assertLanguageSwitchLinks($node, $node_es, TRUE, $type);
255
    $this->assertLanguageSwitchLinks($node, $node_it, TRUE, $type);
256
  }
257

    
258
  /**
259
   * Resets static caches to make the test code match the client-side behavior.
260
   */
261
  function resetCaches() {
262
    drupal_static_reset('locale_url_outbound_alter');
263
  }
264

    
265
  /**
266
   * Returns an empty node data structure.
267
   *
268
   * @param $langcode
269
   *   The language code.
270
   *
271
   * @return
272
   *   An empty node data structure.
273
   */
274
  function emptyNode($langcode) {
275
    return (object) array('nid' => NULL, 'language' => $langcode);
276
  }
277

    
278
  /**
279
   * Installs the specified language, or enables it if it is already installed.
280
   *
281
   * @param $language_code
282
   *   The language code to check.
283
   */
284
  function addLanguage($language_code) {
285
    // Check to make sure that language has not already been installed.
286
    $this->drupalGet('admin/config/regional/language');
287

    
288
    if (strpos($this->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
289
      // Doesn't have language installed so add it.
290
      $edit = array();
291
      $edit['langcode'] = $language_code;
292
      $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
293

    
294
      // Make sure we are not using a stale list.
295
      drupal_static_reset('language_list');
296
      $languages = language_list('language');
297
      $this->assertTrue(array_key_exists($language_code, $languages), 'Language was installed successfully.');
298

    
299
      if (array_key_exists($language_code, $languages)) {
300
        $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'))), 'Language has been created.');
301
      }
302
    }
303
    elseif ($this->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(':name' => 'enabled[' . $language_code . ']'))) {
304
      // It's installed and enabled. No need to do anything.
305
      $this->assertTrue(true, 'Language [' . $language_code . '] already installed and enabled.');
306
    }
307
    else {
308
      // It's installed but not enabled. Enable it.
309
      $this->assertTrue(true, 'Language [' . $language_code . '] already installed.');
310
      $this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));
311
      $this->assertRaw(t('Configuration saved.'), 'Language successfully enabled.');
312
    }
313
  }
314

    
315
  /**
316
   * Creates a "Basic page" in the specified language.
317
   *
318
   * @param $title
319
   *   The title of a basic page in the specified language.
320
   * @param $body
321
   *   The body of a basic page in the specified language.
322
   * @param $language
323
   *   (optional) Language code.
324
   *
325
   * @return
326
   *   A node object.
327
   */
328
  function createPage($title, $body, $language = NULL) {
329
    $edit = array();
330
    $langcode = LANGUAGE_NONE;
331
    $edit["title"] = $title;
332
    $edit["body[$langcode][0][value]"] = $body;
333
    if (!empty($language)) {
334
      $edit['language'] = $language;
335
    }
336
    $this->drupalPost('node/add/page', $edit, t('Save'));
337
    $this->assertRaw(t('Basic page %title has been created.', array('%title' => $title)), 'Basic page created.');
338

    
339
    // Check to make sure the node was created.
340
    $node = $this->drupalGetNodeByTitle($title);
341
    $this->assertTrue($node, 'Node found in database.');
342

    
343
    return $node;
344
  }
345

    
346
  /**
347
   * Creates a translation for a basic page in the specified language.
348
   *
349
   * @param $node
350
   *   The basic page to create the translation for.
351
   * @param $title
352
   *   The title of a basic page in the specified language.
353
   * @param $body
354
   *   The body of a basic page in the specified language.
355
   * @param $language
356
   *   Language code.
357
   *
358
   * @return
359
   *   Translation object.
360
   */
361
  function createTranslation($node, $title, $body, $language) {
362
    $this->drupalGet('node/add/page', array('query' => array('translation' => $node->nid, 'target' => $language)));
363

    
364
    $langcode = LANGUAGE_NONE;
365
    $body_key = "body[$langcode][0][value]";
366
    $this->assertFieldByXPath('//input[@id="edit-title"]', $node->title, "Original title value correctly populated.");
367
    $this->assertFieldByXPath("//textarea[@name='$body_key']", $node->body[LANGUAGE_NONE][0]['value'], "Original body value correctly populated.");
368

    
369
    $edit = array();
370
    $edit["title"] = $title;
371
    $edit[$body_key] = $body;
372
    $this->drupalPost(NULL, $edit, t('Save'));
373
    $this->assertRaw(t('Basic page %title has been created.', array('%title' => $title)), 'Translation created.');
374

    
375
    // Check to make sure that translation was successful.
376
    $translation = $this->drupalGetNodeByTitle($title);
377
    $this->assertTrue($translation, 'Node found in database.');
378
    $this->assertTrue($translation->tnid == $node->nid, 'Translation set id correctly stored.');
379

    
380
    return $translation;
381
  }
382

    
383
  /**
384
   * Asserts an element identified by the given XPath has the given content.
385
   *
386
   * @param $xpath
387
   *   The XPath used to find the element.
388
   * @param array $arguments
389
   *   An array of arguments with keys in the form ':name' matching the
390
   *   placeholders in the query. The values may be either strings or numeric
391
   *   values.
392
   * @param $value
393
   *   The text content of the matched element to assert.
394
   * @param $message
395
   *   The message to display.
396
   * @param $group
397
   *   The group this message belongs to.
398
   *
399
   * @return
400
   *   TRUE on pass, FALSE on fail.
401
   */
402
  function assertContentByXPath($xpath, array $arguments = array(), $value = NULL, $message = '', $group = 'Other') {
403
    $found = $this->findContentByXPath($xpath, $arguments, $value);
404
    return $this->assertTrue($found, $message, $group);
405
  }
406

    
407
  /**
408
   * Tests whether the specified language switch links are found.
409
   *
410
   * @param $node
411
   *   The node to display.
412
   * @param $translation
413
   *   The translation whose link has to be checked.
414
   * @param $find
415
   *   TRUE if the link must be present in the node page.
416
   * @param $types
417
   *   The page areas to be checked.
418
   *
419
   * @return
420
   *   TRUE if the language switch links are found, FALSE if not.
421
   */
422
  function assertLanguageSwitchLinks($node, $translation, $find = TRUE, $types = NULL) {
423
    if (empty($types)) {
424
      $types = array('node', 'block-locale');
425
    }
426
    elseif (is_string($types)) {
427
      $types = array($types);
428
    }
429

    
430
    $result = TRUE;
431
    $languages = language_list();
432
    $page_language = $languages[entity_language('node', $node)];
433
    $translation_language = $languages[$translation->language];
434
    $url = url("node/$translation->nid", array('language' => $translation_language));
435

    
436
    $this->drupalGet("node/$node->nid", array('language' => $page_language));
437

    
438
    foreach ($types as $type) {
439
      $args = array('%translation_language' => $translation_language->native, '%page_language' => $page_language->native, '%type' => $type);
440
      if ($find) {
441
        $message = format_string('[%page_language] Language switch item found for %translation_language language in the %type page area.', $args);
442
      }
443
      else {
444
        $message = format_string('[%page_language] Language switch item not found for %translation_language language in the %type page area.', $args);
445
      }
446

    
447
      if (!empty($translation->nid)) {
448
        $xpath = '//div[contains(@class, :type)]//a[@href=:url]';
449
      }
450
      else {
451
        $xpath = '//div[contains(@class, :type)]//span[contains(@class, "locale-untranslated")]';
452
      }
453

    
454
      $found = $this->findContentByXPath($xpath, array(':type' => $type, ':url' => $url), $translation_language->native);
455
      $result = $this->assertTrue($found == $find, $message) && $result;
456
    }
457

    
458
    return $result;
459
  }
460

    
461
  /**
462
   * Searches for elements matching the given xpath and value.
463
   *
464
   * @param $xpath
465
   *   The XPath used to find the element.
466
   * @param array $arguments
467
   *   An array of arguments with keys in the form ':name' matching the
468
   *   placeholders in the query. The values may be either strings or numeric
469
   *   values.
470
   * @param $value
471
   *   The text content of the matched element to assert.
472
   *
473
   * @return
474
   *   TRUE if found, otherwise FALSE.
475
   */
476
  function findContentByXPath($xpath, array $arguments = array(), $value = NULL) {
477
    $elements = $this->xpath($xpath, $arguments);
478

    
479
    $found = TRUE;
480
    if ($value && $elements) {
481
      $found = FALSE;
482
      foreach ($elements as $element) {
483
        if ((string) $element == $value) {
484
          $found = TRUE;
485
          break;
486
        }
487
      }
488
    }
489

    
490
    return $elements && $found;
491
  }
492
}