You are here

public function NodeRevisionsTest::testRevisionTranslationRevert in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeRevisionsTest.php \Drupal\Tests\node\Functional\NodeRevisionsTest::testRevisionTranslationRevert()

Tests the revision translations are correctly reverted.

File

core/modules/node/tests/src/Functional/NodeRevisionsTest.php, line 391

Class

NodeRevisionsTest
Create a node with revisions and test viewing, saving, reverting, and deleting revisions for users with access for this content type.

Namespace

Drupal\Tests\node\Functional

Code

public function testRevisionTranslationRevert() {

  // Create a node and a few revisions.
  $node = $this
    ->drupalCreateNode([
    'langcode' => 'en',
  ]);
  $initial_revision_id = $node
    ->getRevisionId();
  $initial_title = $node
    ->label();
  $this
    ->createRevisions($node, 2);

  // Translate the node and create a few translation revisions.
  $translation = $node
    ->addTranslation('it');
  $this
    ->createRevisions($translation, 3);
  $revert_id = $node
    ->getRevisionId();
  $translated_title = $translation
    ->label();
  $untranslatable_string = $node->untranslatable_string_field->value;

  // Create a new revision for the default translation in-between a series of
  // translation revisions.
  $this
    ->createRevisions($node, 1);
  $default_translation_title = $node
    ->label();

  // And create a few more translation revisions.
  $this
    ->createRevisions($translation, 2);
  $translation_revision_id = $translation
    ->getRevisionId();

  // Now revert the a translation revision preceding the last default
  // translation revision, and check that the desired value was reverted but
  // the default translation value was preserved.
  $revert_translation_url = Url::fromRoute('node.revision_revert_translation_confirm', [
    'node' => $node
      ->id(),
    'node_revision' => $revert_id,
    'langcode' => 'it',
  ]);
  $this
    ->drupalPostForm($revert_translation_url, [], t('Revert'));

  /** @var \Drupal\node\NodeStorage $node_storage */
  $node_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $node_storage
    ->resetCache();

  /** @var \Drupal\node\NodeInterface $node */
  $node = $node_storage
    ->load($node
    ->id());
  $this
    ->assertTrue($node
    ->getRevisionId() > $translation_revision_id);
  $this
    ->assertEqual($node
    ->label(), $default_translation_title);
  $this
    ->assertEqual($node
    ->getTranslation('it')
    ->label(), $translated_title);
  $this
    ->assertNotEqual($node->untranslatable_string_field->value, $untranslatable_string);
  $latest_revision_id = $translation
    ->getRevisionId();

  // Now revert the a translation revision preceding the last default
  // translation revision again, and check that the desired value was reverted
  // but the default translation value was preserved. But in addition the
  // untranslated field will be reverted as well.
  $this
    ->drupalPostForm($revert_translation_url, [
    'revert_untranslated_fields' => TRUE,
  ], t('Revert'));
  $node_storage
    ->resetCache();

  /** @var \Drupal\node\NodeInterface $node */
  $node = $node_storage
    ->load($node
    ->id());
  $this
    ->assertTrue($node
    ->getRevisionId() > $latest_revision_id);
  $this
    ->assertEqual($node
    ->label(), $default_translation_title);
  $this
    ->assertEqual($node
    ->getTranslation('it')
    ->label(), $translated_title);
  $this
    ->assertEqual($node->untranslatable_string_field->value, $untranslatable_string);
  $latest_revision_id = $translation
    ->getRevisionId();

  // Now revert the entity revision to the initial one where the translation
  // didn't exist.
  $revert_url = Url::fromRoute('node.revision_revert_confirm', [
    'node' => $node
      ->id(),
    'node_revision' => $initial_revision_id,
  ]);
  $this
    ->drupalPostForm($revert_url, [], t('Revert'));
  $node_storage
    ->resetCache();

  /** @var \Drupal\node\NodeInterface $node */
  $node = $node_storage
    ->load($node
    ->id());
  $this
    ->assertTrue($node
    ->getRevisionId() > $latest_revision_id);
  $this
    ->assertEqual($node
    ->label(), $initial_title);
  $this
    ->assertFalse($node
    ->hasTranslation('it'));
}