function NodeRevisionsTest::testRevisions

Same name and namespace in other branches
  1. 11.x core/modules/node/tests/src/Functional/NodeRevisionsTest.php \Drupal\Tests\node\Functional\NodeRevisionsTest::testRevisions()
  2. 10 core/modules/node/tests/src/Functional/NodeRevisionsTest.php \Drupal\Tests\node\Functional\NodeRevisionsTest::testRevisions()
  3. 9 core/modules/node/tests/src/Functional/NodeRevisionsTest.php \Drupal\Tests\node\Functional\NodeRevisionsTest::testRevisions()

Checks node revision related operations.

File

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

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 testRevisions() {
  $node_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $nodes = $this->nodes;
  $logs = $this->revisionLogs;
  // Get last node for simple checks.
  $node = $nodes[3];
  // Confirm the correct revision text appears on "view revisions" page.
  $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/view");
  $this->assertText($node->body->value, 'Correct text displays for version.');
  // Confirm the correct log message appears on "revisions overview" page.
  $this->drupalGet("node/" . $node->id() . "/revisions");
  foreach ($logs as $revision_log) {
    $this->assertText($revision_log, 'Revision log message found.');
  }
  // Original author, and editor names should appear on revisions overview.
  $web_user = $nodes[0]->revision_uid->entity;
  $this->assertText(t('by @name', [
    '@name' => $web_user->getAccountName(),
  ]));
  $editor = $nodes[2]->revision_uid->entity;
  $this->assertText(t('by @name', [
    '@name' => $editor->getAccountName(),
  ]));
  // Confirm that this is the default revision.
  $this->assertTrue($node->isDefaultRevision(), 'Third node revision is the default one.');
  // Confirm that revisions revert properly.
  $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionid() . "/revert", [], t('Revert'));
  $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', [
    '@type' => 'Basic page',
    '%title' => $nodes[1]->label(),
    '%revision-date' => $this->container
      ->get('date.formatter')
      ->format($nodes[1]->getRevisionCreationTime()),
  ]), 'Revision reverted.');
  $node_storage->resetCache([
    $node->id(),
  ]);
  $reverted_node = $node_storage->load($node->id());
  $this->assertTrue($nodes[1]->body->value == $reverted_node->body->value, 'Node reverted correctly.');
  // Confirm the revision author is the user performing the revert.
  $this->assertTrue($reverted_node->getRevisionUserId() == $this->loggedInUser
    ->id(), 'Node revision author is user performing revert.');
  // And that its not the revision author.
  $this->assertTrue($reverted_node->getRevisionUserId() != $nodes[1]->getRevisionUserId(), 'Node revision author is not original revision author.');
  // Confirm that this is not the default version.
  $node = node_revision_load($node->getRevisionId());
  $this->assertFalse($node->isDefaultRevision(), 'Third node revision is not the default one.');
  // Confirm revisions delete properly.
  $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete", [], t('Delete'));
  $this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.', [
    '%revision-date' => $this->container
      ->get('date.formatter')
      ->format($nodes[1]->getRevisionCreationTime()),
    '@type' => 'Basic page',
    '%title' => $nodes[1]->label(),
  ]), 'Revision deleted.');
  $connection = Database::getConnection();
  $nids = \Drupal::entityQuery('node')->accessCheck(FALSE)
    ->allRevisions()
    ->condition('nid', $node->id())
    ->condition('vid', $nodes[1]->getRevisionId())
    ->execute();
  $this->assertCount(0, $nids);
  // Set the revision timestamp to an older date to make sure that the
  // confirmation message correctly displays the stored revision date.
  $old_revision_date = REQUEST_TIME - 86400;
  $connection->update('node_revision')
    ->condition('vid', $nodes[2]->getRevisionId())
    ->fields([
    'revision_timestamp' => $old_revision_date,
  ])
    ->execute();
  $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[2]->getRevisionId() . "/revert", [], t('Revert'));
  $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', [
    '@type' => 'Basic page',
    '%title' => $nodes[2]->label(),
    '%revision-date' => $this->container
      ->get('date.formatter')
      ->format($old_revision_date),
  ]));
  // Make a new revision and set it to not be default.
  // This will create a new revision that is not "front facing".
  $new_node_revision = clone $node;
  $new_body = $this->randomMachineName();
  $new_node_revision->body->value = $new_body;
  // Save this as a non-default revision.
  $new_node_revision->setNewRevision();
  $new_node_revision->isDefaultRevision = FALSE;
  $new_node_revision->save();
  $this->drupalGet('node/' . $node->id());
  $this->assertNoText($new_body, 'Revision body text is not present on default version of node.');
  // Verify that the new body text is present on the revision.
  $this->drupalGet("node/" . $node->id() . "/revisions/" . $new_node_revision->getRevisionId() . "/view");
  $this->assertText($new_body, 'Revision body text is present when loading specific revision.');
  // Verify that the non-default revision vid is greater than the default
  // revision vid.
  $default_revision = $connection->select('node', 'n')
    ->fields('n', [
    'vid',
  ])
    ->condition('nid', $node->id())
    ->execute()
    ->fetchCol();
  $default_revision_vid = $default_revision[0];
  $this->assertTrue($new_node_revision->getRevisionId() > $default_revision_vid, 'Revision vid is greater than default revision vid.');
  // Create an 'EN' node with a revision log message.
  $node = $this->drupalCreateNode();
  $node->title = 'Node title in EN';
  $node->revision_log = 'Simple revision message (EN)';
  $node->save();
  $this->drupalGet("node/" . $node->id() . "/revisions");
  // Verify revisions is accessible since the type has revisions enabled.
  $this->assertSession()
    ->statusCodeEquals(200);
  // Check initial revision is shown on the node revisions overview page.
  $this->assertText('Simple revision message (EN)');
  // Verify that delete operation is inaccessible for the default revision.
  $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/delete");
  $this->assertSession()
    ->statusCodeEquals(403);
  // Verify that revert operation is inaccessible for the default revision.
  $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/revert");
  $this->assertSession()
    ->statusCodeEquals(403);
  // Create a new revision and new log message.
  $node = Node::load($node->id());
  $node->body->value = 'New text (EN)';
  $node->revision_log = 'New revision message (EN)';
  $node->setNewRevision();
  $node->save();
  // Check both revisions are shown on the node revisions overview page.
  $this->drupalGet("node/" . $node->id() . "/revisions");
  $this->assertText('Simple revision message (EN)');
  $this->assertText('New revision message (EN)');
  // Create an 'EN' node with a revision log message.
  $node = $this->drupalCreateNode();
  $node->langcode = 'en';
  $node->title = 'Node title in EN';
  $node->revision_log = 'Simple revision message (EN)';
  $node->save();
  $this->drupalGet("node/" . $node->id() . "/revisions");
  // Verify revisions is accessible since the type has revisions enabled.
  $this->assertSession()
    ->statusCodeEquals(200);
  // Check initial revision is shown on the node revisions overview page.
  $this->assertText('Simple revision message (EN)');
  // Add a translation in 'DE' and create a new revision and new log message.
  $translation = $node->addTranslation('de');
  $translation->title->value = 'Node title in DE';
  $translation->body->value = 'New text (DE)';
  $translation->revision_log = 'New revision message (DE)';
  $translation->setNewRevision();
  $translation->save();
  // View the revision UI in 'IT', only the original node revision is shown.
  $this->drupalGet("it/node/" . $node->id() . "/revisions");
  $this->assertText('Simple revision message (EN)');
  $this->assertNoText('New revision message (DE)');
  // View the revision UI in 'DE', only the translated node revision is shown.
  $this->drupalGet("de/node/" . $node->id() . "/revisions");
  $this->assertNoText('Simple revision message (EN)');
  $this->assertText('New revision message (DE)');
  // View the revision UI in 'EN', only the original node revision is shown.
  $this->drupalGet("node/" . $node->id() . "/revisions");
  $this->assertText('Simple revision message (EN)');
  $this->assertNoText('New revision message (DE)');
}

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