function TranslationTest::testContentTranslation

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

File

drupal/core/modules/translation/lib/Drupal/translation/Tests/TranslationTest.php, line 71
Definition of Drupal\translation\Tests\TranslationTest.

Class

TranslationTest
Functional tests for the Translation module.

Namespace

Drupal\translation\Tests

Code

function testContentTranslation() {

  // Create Basic 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
    ->drupalLogin($this->admin_user);
  $this
    ->drupalPost('node/' . $node->nid . '/edit', array(), t('Save and unpublish'));
  $this
    ->drupalGet('node/' . $node->nid . '/translate');
  $this
    ->drupalPost('node/' . $node->nid . '/edit', array(), t('Save and publish'));
  $this
    ->drupalLogin($this->translator);

  // Check that the "add translation" link uses a localized path.
  $languages = language_list();
  $prefixes = language_negotiation_url_prefixes();
  $this
    ->drupalGet('node/' . $node->nid . '/translate');
  $this
    ->assertLinkByHref($prefixes['es'] . '/node/add/' . $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
    ->drupalGet('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
    ->drupalGet('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']->name,
  )), '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::LANGCODE_NOT_SPECIFIED;
  $edit["title"] = $this
    ->randomName();
  $edit["body[{$langcode}][0][value]"] = $this
    ->randomName();
  $this
    ->drupalPost('node/add/page', $edit, t('Save'), array(
    'query' => array(
      'translation' => $node->nid,
      'language' => 'es',
    ),
  ));
  $duplicate = $this
    ->drupalGetNodeByTitle($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::LANGCODE_NOT_SPECIFIED][0]['value'] = $node_body;
  $edit = array();
  $edit["body[{$langcode}][0][value]"] = $node_body;
  $edit['translation[retranslate]'] = TRUE;
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $this
    ->assertRaw(t('Basic page %title has been updated.', array(
    '%title' => $node_title,
  )), 'Original node updated.');

  // Check to make sure that interface shows translation as outdated.
  $this
    ->drupalGet('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
    ->drupalPost('node/' . $node_translation->nid . '/edit', $edit, t('Save'));
  $this
    ->assertRaw(t('Basic page %title has been updated.', array(
    '%title' => $node_translation_title,
  )), 'Translated node updated.');

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

  // Leave just one language installed and check that the translation overview
  // page is still accessible.
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->drupalPost('admin/config/regional/language/delete/es', array(), t('Delete'));
  $this
    ->drupalLogin($this->translator);
  $this
    ->drupalGet('node/' . $node->nid . '/translate');
  $this
    ->assertRaw(t('Translations of %title', array(
    '%title' => $node
      ->label(),
  )), 'Translation overview page available with only one language enabled.');
}