You are here

public function NodeRevisionsTest::testRevisionTranslationRevert in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/Functional/NodeRevisionsTest.php \Drupal\Tests\node\Functional\NodeRevisionsTest::testRevisionTranslationRevert()
  2. 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 388

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
    ->drupalGet($revert_translation_url);
  $this
    ->submitForm([], '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
    ->assertGreaterThan($translation_revision_id, $node
    ->getRevisionId());
  $this
    ->assertEquals($default_translation_title, $node
    ->label());
  $this
    ->assertEquals($translated_title, $node
    ->getTranslation('it')
    ->label());
  $this
    ->assertNotEquals($untranslatable_string, $node->untranslatable_string_field->value);
  $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
    ->drupalGet($revert_translation_url);
  $this
    ->submitForm([
    'revert_untranslated_fields' => TRUE,
  ], 'Revert');
  $node_storage
    ->resetCache();

  /** @var \Drupal\node\NodeInterface $node */
  $node = $node_storage
    ->load($node
    ->id());
  $this
    ->assertGreaterThan($latest_revision_id, $node
    ->getRevisionId());
  $this
    ->assertEquals($default_translation_title, $node
    ->label());
  $this
    ->assertEquals($translated_title, $node
    ->getTranslation('it')
    ->label());
  $this
    ->assertEquals($untranslatable_string, $node->untranslatable_string_field->value);
  $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
    ->drupalGet($revert_url);
  $this
    ->submitForm([], 'Revert');
  $node_storage
    ->resetCache();

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