translation.test 21.5 KB
Newer Older
1
2
<?php

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

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

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('language', '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
32
33

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

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

40
41
    // Set "Basic page" content type to use multilingual support with
    // translation.
42
    $this->drupalGet('admin/structure/types/manage/page');
43
    $edit = array();
44
    $edit['node_type_language'] = TRANSLATION_ENABLED;
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')), t('Basic page content type has been updated.'));
47

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

53
    // Reset static caches in our local language environment.
54
55
    $this->resetCaches();

56
57
    $this->drupalLogin($this->translator);
  }
58

59
  /**
60
   * Creates, modifies, and updates a basic page with a translation.
61
62
   */
  function testContentTranslation() {
63
    // Create Basic page in English.
64
65
66
    $node_title = $this->randomName();
    $node_body =  $this->randomName();
    $node = $this->createPage($node_title, $node_body, 'en');
67
68
69
70
71
72
73
74
    // Unpublish the original node to check that this has no impact on the
    // translation overview page, publish it again afterwards.
    $this->drupalLogin($this->admin_user);
    $this->drupalPost('node/' . $node->nid . '/edit', array('status' => FALSE), t('Save'));
    $this->drupalGet('node/' . $node->nid . '/translate');
    $this->drupalPost('node/' . $node->nid . '/edit', array('status' => NODE_PUBLISHED), t('Save'));
    $this->drupalLogin($this->translator);

75
    // Check that the "add translation" link uses a localized path.
76
    $languages = language_list();
77
    $prefixes = locale_language_negotiation_url_prefixes();
78
    $this->drupalGet('node/' . $node->nid . '/translate');
79
    $this->assertLinkByHref($prefixes['es'] . '/node/add/' . str_replace('_', '-', $node->type), 0, t('The "add translation" link for %language points to the localized path of the target language.', array('%language' => $languages['es']->name)));
80

81
    // Submit translation in Spanish.
82
83
    $node_translation_title = $this->randomName();
    $node_translation_body = $this->randomName();
84
    $node_translation = $this->createTranslation($node, $node_translation_title, $node_translation_body, 'es');
85

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

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

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

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

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

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

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

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

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

151
  /**
152
   * Checks that the language switch links behave properly.
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
  function testLanguageSwitchLinks() {
    // Create a Basic page in English and its translations in Spanish and
    // Italian.
    $node = $this->createPage($this->randomName(), $this->randomName(), 'en');
    $translation_es = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'es');
    $translation_it = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'it');

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

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

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

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

192
  /**
193
   * Tests that the language switcher block alterations work as intended.
194
195
196
197
   */
  function testLanguageSwitcherBlockIntegration() {
    // Enable Italian to have three items in the language switcher block.
    $this->drupalLogin($this->admin_user);
198
    $edit = array('languages[it][enabled]' => TRUE);
199
    $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
200
    $this->resetCaches();
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    $this->drupalLogin($this->translator);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return $node;
  }

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

363
    $field_langcode = LANGUAGE_NOT_SPECIFIED;
364
    $body_key = "body[$field_langcode][0][value]";
365
    $this->assertFieldByXPath('//input[@id="edit-title"]', $node->title, "Original title value correctly populated.");
366
    $this->assertFieldByXPath("//textarea[@name='$body_key']", $node->body[LANGUAGE_NOT_SPECIFIED][0]['value'], "Original body value correctly populated.");
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381

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

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

    return $translation;
  }

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

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

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

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

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

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

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

    return $result;
  }

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

    $found = TRUE;
    if ($value && $elements) {
      $found = FALSE;
      foreach ($elements as $element) {
        if ((string) $element == $value) {
          $found = TRUE;
          break;
        }
      }
    }

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