1 translation.test TranslationTestCase::testContentTranslation()

Creates, modifies, and updates a page with a translation.

File

core/modules/translation/tests/translation.test, line 64
Tests for the Translation module.

Class

TranslationTestCase
Functional tests for the Translation module.

Code

function testContentTranslation() {
  // Create Page in English.
  $node_title = $this->randomName();
  $node_body = $this->randomName();
  $node = $this->createPage($node_title, $node_body, 'en');
  // Unpublish the original node to check that this has no impact on the
  // translation overview page, publish it again afterwards.
  $this->backdropLogin($this->admin_user);
  $this->backdropPost('node/' . $node->nid . '/edit', array('status' => 0), t('Save'));
  $this->backdropGet('node/' . $node->nid . '/translate');
  $this->backdropPost('node/' . $node->nid . '/edit', array('status' => 1), t('Save'));
  $this->backdropLogin($this->translator);

  // Check that the "Add translation" link uses a localized path.
  $languages = language_list();
  $prefixes = locale_language_negotiation_url_prefixes();
  $this->backdropGet('node/' . $node->nid . '/translate');
  $this->assertLinkByHref($prefixes['es'] . '/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)));

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

  // Check that the "Edit translation" and "View node" links use localized
  // paths.
  $this->backdropGet('node/' . $node->nid . '/translate');
  $this->assertLinkByHref($prefixes['es'] . '/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)));
  $this->assertLinkByHref($prefixes['es'] . '/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)));

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

  // 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();
  $langcode = LANGUAGE_NONE;
  $edit["title"] = $this->randomName();
  $edit["body[$langcode][0][value]"] = $this->randomName();
  $this->backdropPost('node/add/page', $edit, t('Save'), array('query' => array('translation' => $node->nid, 'language' => 'es')));
  $duplicate = $this->backdropGetNodeByTitle($edit["title"]);
  $this->assertEqual($duplicate->tnid, 0, 'The node does not have a tnid.');

  // Update original and mark translation as outdated.
  $node_body = $this->randomName();
  $node->body[LANGUAGE_NONE][0]['value'] = $node_body;
  $edit = array();
  $edit["body[$langcode][0][value]"] = $node_body;
  $edit['translation[retranslate]'] = TRUE;
  $this->backdropPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $this->assertRaw(t('Page %title has been updated.', array('%title' => $node_title)), 'Original node updated.');

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

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

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

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

  // Leave just one language enabled and check that the translation overview
  // page is still accessible.
  $this->backdropLogin($this->admin_user);
  $edit = array('languages[es][enabled]' => FALSE);
  $this->backdropPost('admin/config/regional/language', $edit, t('Save configuration'));
  $this->backdropLogin($this->translator);
  $this->backdropGet('node/' . $node->nid . '/translate');
  $this->assertRaw(t('Translations of %title', array('%title' => $node->title)), 'Translation overview page available with only one language enabled.');
}