You are here

function ContentEntitySourceContentModerationTest::testModeratedContentTranslations in Translation Management Tool 8

Test the content moderation workflow with translatable nodes.

File

sources/content/tests/src/Functional/ContentEntitySourceContentModerationTest.php, line 200

Class

ContentEntitySourceContentModerationTest
Tests content entity source integration with content moderation.

Namespace

Drupal\Tests\tmgmt_content\Functional

Code

function testModeratedContentTranslations() {
  $this
    ->loginAsTranslator([
    'administer tmgmt',
    'translate any entity',
    'create content translations',
    'access content',
    'view own unpublished content',
    'edit own article content',
    'access content overview',
    'view all revisions',
    'view latest version',
    'use ' . $this->workflow
      ->id() . ' transition create_new_draft',
    'use ' . $this->workflow
      ->id() . ' transition publish',
  ]);

  // Create a node in English.
  $title = 'Moderated node';
  $node = $this
    ->createNode([
    'title' => $title,
    'type' => 'article',
    'langcode' => 'en',
    'moderation_state' => 'published',
    'uid' => $this->translator_user
      ->id(),
  ]);

  // Create a draft of the published node.
  $draft_title = '[Draft] ' . $title;
  $edit = [
    'title[0][value]' => $draft_title,
    'moderation_state[0][state]' => 'draft',
  ];
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, 'Save');

  // Go to content overview and translate a draft.
  $this
    ->drupalGet('admin/tmgmt/sources');
  $this
    ->assertLink($draft_title);
  $edit = [
    'items[' . $node
      ->id() . ']' => $node
      ->id(),
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Request translation');
  $this
    ->assertText('One job needs to be checked out.');
  $this
    ->assertText($draft_title . ' (English to ?, Unprocessed)');
  $edit = [
    'target_language' => 'de',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Submit to provider');
  $this
    ->assertText(t('The translation of @title to German is finished and can now be reviewed.', [
    '@title' => $draft_title,
  ]));

  // Assert a draft label on the jobs overview page.
  $this
    ->drupalGet('admin/tmgmt/jobs');
  $this
    ->assertText($draft_title);
  $this
    ->clickLink('Manage');
  $this
    ->assertText($draft_title . ' (English to German, Active)');
  $this
    ->clickLink('Review');
  $this
    ->assertText('Job item ' . $draft_title);

  // Assert there is no content moderation form element.
  $this
    ->assertNoFieldByName('moderation_state|0|value[source]');

  // Assert custom content moderation form element.
  $this
    ->assertText('Current source state');
  $this
    ->assertText('Draft');
  $this
    ->assertOptionSelected('edit-moderation-state-new-state', 'draft');

  // Change the moderation state of the translation to published.
  $translation_title = 'de(de-ch): [Published] ' . $title;
  $edit = [
    'title|0|value[translation]' => $translation_title,
    'moderation_state[new_state]' => 'published',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertText(t('The translation for @title has been saved successfully.', [
    '@title' => $draft_title,
  ]));
  $this
    ->clickLink('Review');

  // The published state is preselected now.
  $this
    ->assertOptionSelected('edit-moderation-state-new-state', 'published');
  $review_url = $this
    ->getUrl();

  // Assert the preview mode.
  $this
    ->clickLink('Preview');
  $this
    ->assertText(t('Preview of @title for German', [
    '@title' => $draft_title,
  ]));
  $this
    ->assertText($translation_title);

  // Save the translation as completed and assert the published translation.
  $this
    ->drupalPostForm($review_url, [], 'Save as completed');
  $this
    ->assertText('Validation completed successfully.');
  $this
    ->assertText(t('The translation for @title has been accepted as @translation_title.', [
    '@title' => $draft_title,
    '@translation_title' => $translation_title,
  ]));
  $this
    ->clickLink($translation_title);
  $this
    ->assertUrl('de/node/' . $node
    ->id());
  $this
    ->assertText($translation_title);
  $this
    ->clickLink('Revisions');
  $this
    ->assertText("Created by translation job {$draft_title}.");

  // There is one translation revision.
  $this
    ->assertNodeTranslationsRevisionsCount($node
    ->id(), 'de', 1);

  // Assert the source entity has not changed.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertText($title);
  $this
    ->drupalGet('node/' . $node
    ->id() . '/latest');
  $this
    ->assertText($draft_title);

  // Translate the node to Spanish and save it as a draft.
  $edit = [
    'target_language' => 'es',
    'items[' . $node
      ->id() . ']' => $node
      ->id(),
  ];
  $this
    ->drupalPostForm('admin/tmgmt/sources', $edit, 'Request translation');
  $this
    ->drupalPostForm(NULL, [], 'Submit to provider');
  $this
    ->assertText(t('The translation of @title to Spanish is finished and can now be reviewed.', [
    '@title' => $draft_title,
  ]));
  $this
    ->clickLink('reviewed');

  // Spanish translation is saved as a draft.
  $this
    ->drupalPostForm(NULL, [], 'Save as completed');
  $this
    ->assertText(t('The translation for @title has been accepted.', [
    '@title' => $draft_title,
  ]));
  $this
    ->drupalGet('es/node/' . $node
    ->id());

  // The spanish translation is not published. English content is displayed.
  $this
    ->assertText($title);
  $this
    ->assertNoText('es: ' . $title);
  $this
    ->clickLink('Latest version');
  $this
    ->assertText('es: ' . $draft_title);

  // There is one more revision in German created when Spanish translation was
  // saved.
  $this
    ->assertNodeTranslationsRevisionsCount($node
    ->id(), 'de', 2);
  $this
    ->assertNodeTranslationsRevisionsCount($node
    ->id(), 'es', 1);

  // Create a new revision in the source language.
  $second_draft_title = "{$draft_title} (2)";
  $edit = [
    'title[0][value]' => $second_draft_title,
    'moderation_state[0][state]' => 'draft',
  ];
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, 'Save (this translation)');

  // The update to the source language creates a new German revision.
  $this
    ->assertNodeTranslationsRevisionsCount($node
    ->id(), 'de', 3);

  // Create a draft revision in italian using core translation.
  $edit = [
    'title[0][value]' => "it: {$second_draft_title}",
    'moderation_state[0][state]' => 'draft',
  ];
  $this
    ->drupalPostForm('it/node/' . $node
    ->id() . '/translations/add/en/it', $edit, 'Save (this translation)');

  // New German revision has been created when Italian translation was added.
  $this
    ->assertNodeTranslationsRevisionsCount($node
    ->id(), 'de', 4);

  // Assert the source overview behavior.
  $this
    ->drupalGet('admin/tmgmt/sources');
  $this
    ->assertEqual(count($this
    ->xpath('//tbody/tr')), 1);

  // English is original language and it is linked.
  $this
    ->assertTextByXpath('//tbody/tr[1]/td[4]/@class', 'langstatus-en');
  $this
    ->assertTextByXpath('//tbody/tr[1]/td[4]/a/img/@title', 'Original language');

  // There is no french translation.
  $this
    ->assertTextByXpath('//tbody/tr[1]/td[5]/@class', 'langstatus-fr');
  $this
    ->assertTextByXpath('//tbody/tr[1]/td[5]/img/@title', 'Not translated');

  // There is a german translation (published).
  $this
    ->assertTextByXpath('//tbody/tr[1]/td[6]/@class', 'langstatus-de');
  $this
    ->assertTextByXpath('//tbody/tr[1]/td[6]/a/img/@title', 'Translation up to date');

  // There is an italian translation (saved as a draft).
  $this
    ->assertTextByXpath('//tbody/tr[1]/td[7]/@class', 'langstatus-it');
  $this
    ->assertTextByXpath('//tbody/tr[1]/td[7]/a/img/@title', 'Translation up to date');

  // There is a spanish translation (saved as a draft).
  $this
    ->assertTextByXpath('//tbody/tr[1]/td[8]/@class', 'langstatus-es');
  $this
    ->assertTextByXpath('//tbody/tr[1]/td[8]/a/img/@title', 'Translation up to date');

  // Assert the content overview filters.
  $edit = [
    'search[target_language]' => 'de',
    'search[target_status]' => 'untranslated',
  ];
  $this
    ->drupalPostForm('admin/tmgmt/sources/content/node', $edit, 'Search');

  // The german translation was published.
  $this
    ->assertText('No source items matching given criteria have been found.');
  $this
    ->assertNoLink($second_draft_title);

  // The italian translation was saved as a draft.
  $edit = [
    'search[target_language]' => 'it',
    'search[target_status]' => 'untranslated',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Search');
  $this
    ->assertText('No source items matching given criteria have been found.');
  $this
    ->assertNoLink($second_draft_title);

  // The french translation does not exist.
  $edit = [
    'search[target_language]' => 'fr',
    'search[target_status]' => 'untranslated',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Search');
  $this
    ->assertLink($second_draft_title);

  // Translate a new revision to German.
  $edit = [
    'target_language' => 'de',
    'items[' . $node
      ->id() . ']' => $node
      ->id(),
  ];
  $this
    ->drupalPostForm('admin/tmgmt/sources', $edit, 'Request translation');
  $this
    ->drupalPostForm(NULL, [], 'Submit to provider');
  $this
    ->assertText(t('The translation of @title to German is finished and can now be reviewed.', [
    '@title' => $second_draft_title,
  ]));
  $this
    ->clickLink('reviewed');
  $this
    ->assertOptionSelected('edit-moderation-state-new-state', 'draft');
  $this
    ->drupalPostForm(NULL, [], 'Save as completed');

  // Submitting another German translation creates a new revision.
  $this
    ->assertNodeTranslationsRevisionsCount($node
    ->id(), 'de', 5);

  // German language still shows the latest published translation.
  $this
    ->drupalGet('de/node/' . $node
    ->id());
  $this
    ->assertText($translation_title);
  $this
    ->drupalGet('de/node/' . $node
    ->id() . '/latest');
  $this
    ->assertText('de(de-ch): [Draft] Moderated node (2)');
  $this
    ->clickLink('Revisions');
  $this
    ->assertText('Created by translation job [Draft] Moderated node (2).');
  $this
    ->assertText('Created by translation job [Draft] Moderated node.');

  // Italian translation was not published yet.
  $this
    ->drupalGet('it/node/' . $node
    ->id());
  $this
    ->assertNoText('it: ' . $second_draft_title);
  $this
    ->clickLink('Latest version');
  $this
    ->assertText('it: ' . $second_draft_title);

  // Spanish translation was not published either.
  $this
    ->drupalGet('es/node/' . $node
    ->id());
  $this
    ->assertNoText('es: ' . $draft_title);
  $this
    ->clickLink('Latest version');
  $this
    ->assertText('es: ' . $draft_title);

  // Create a published node.
  $title = 'Published article';
  $node = $this
    ->createNode([
    'title' => $title,
    'type' => 'article',
    'langcode' => 'en',
    'moderation_state' => 'published',
    'uid' => $this->translator_user
      ->id(),
  ]);

  // Create a draft.
  $draft_title = 'Draft article';
  $edit = [
    'title[0][value]' => $draft_title,
    'moderation_state[0][state]' => 'draft',
  ];
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, 'Save');

  // Publish a translation in German.
  $edit = [
    'title[0][value]' => "de: {$draft_title}",
    'moderation_state[0][state]' => 'published',
  ];
  $this
    ->drupalPostForm('de/node/' . $node
    ->id() . '/translations/add/en/de', $edit, 'Save (this translation)');

  // Provide another translation in German using TMGMT.
  $edit = [
    'items[' . $node
      ->id() . ']' => $node
      ->id(),
    'target_language' => 'de',
  ];
  $this
    ->drupalPostForm('admin/tmgmt/sources', $edit, 'Request translation');
  $this
    ->drupalPostForm(NULL, [], 'Submit to provider');
  $this
    ->assertText("The translation of {$draft_title} to German is finished and can now be reviewed.");
  $this
    ->clickLink('reviewed');
  $this
    ->assertOptionSelected('edit-moderation-state-new-state', 'draft');
  $edit = [
    'moderation_state[new_state]' => 'published',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save as completed');
  $this
    ->assertText("The translation for {$draft_title} has been accepted as de(de-ch): {$draft_title}.");

  // Update the default moderation state.
  \Drupal::configFactory()
    ->getEditable('tmgmt_content.settings')
    ->set('default_moderation_states', [
    $this->workflow
      ->id() => 'published',
  ])
    ->save();

  // Provide translation in Spanish as well.
  $edit = [
    'items[' . $node
      ->id() . ']' => $node
      ->id(),
    'target_language' => 'es',
  ];
  $this
    ->drupalPostForm('admin/tmgmt/sources', $edit, 'Request translation');
  $this
    ->drupalPostForm(NULL, [], 'Submit to provider');
  $this
    ->assertText("The translation of {$draft_title} to Spanish is finished and can now be reviewed.");
  $this
    ->clickLink('reviewed');

  // Assert that the default moderation state is being set.
  $this
    ->assertOptionSelected('edit-moderation-state-new-state', 'published');
  $this
    ->drupalPostForm(NULL, [], 'Save as completed');
  $this
    ->assertText("The translation for {$draft_title} has been accepted as es: {$draft_title}.");

  // The latest revision contains all the translations.
  $node = Node::load($node
    ->id());
  $this
    ->assertEqual(array_keys($node
    ->getTranslationLanguages()), [
    'en',
    'de',
    'es',
  ]);

  // Create a node in German language.
  $node = $this
    ->createNode([
    'title' => 'Moderated node (de)',
    'type' => 'article',
    'langcode' => 'de',
    'moderation_state' => 'published',
    'uid' => $this->translator_user
      ->id(),
  ]);

  // Create a draft of the published node.
  $edit = [
    'title[0][value]' => 'Draft node (de)',
    'moderation_state[0][state]' => 'draft',
  ];
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, 'Save');

  // Go to the overview and and assert there is a draft in german language.
  $this
    ->drupalGet('admin/tmgmt/sources');
  $this
    ->assertText('Draft node (de)');
  $this
    ->assertNoText('Moderated node (de)');
}