function NodeRevisionsTest::testRevisionTranslationRevert

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

Class

NodeRevisionsTest
Tests per-content-type node CRUD operation permissions.

Namespace

Drupal\Tests\node\Functional

Code

public function testRevisionTranslationRevert() : void {
    // 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'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.