function NodeRevisionsTest::testRevisions

Same name and namespace in other branches
  1. 8.9.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. 11.x 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 146

Class

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

Namespace

Drupal\Tests\node\Functional

Code

public function testRevisions() {
    // Access to the revision page for a node with 1 revision is allowed.
    $node = $this->drupalCreateNode();
    $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/view");
    $this->assertSession()
        ->statusCodeEquals(200);
    $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->assertSession()
        ->pageTextContains($node->body->value);
    // Confirm the correct log message appears on "revisions overview" page.
    $this->drupalGet("node/" . $node->id() . "/revisions");
    foreach ($logs as $revision_log) {
        $this->assertSession()
            ->pageTextContains($revision_log);
    }
    // Original author, and editor names should appear on revisions overview.
    $web_user = $nodes[0]->revision_uid->entity;
    $this->assertSession()
        ->pageTextContains('by ' . $web_user->getAccountName());
    $editor = $nodes[2]->revision_uid->entity;
    $this->assertSession()
        ->pageTextContains('by ' . $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->drupalGet("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionid() . "/revert");
    $this->submitForm([], 'Revert');
    $this->assertSession()
        ->pageTextContains("Basic page {$nodes[1]->label()} has been reverted to the revision from {$this->container->get('date.formatter')->format($nodes[1]->getRevisionCreationTime())}.");
    $node_storage->resetCache([
        $node->id(),
    ]);
    $reverted_node = $node_storage->load($node->id());
    $this->assertSame($nodes[1]->body->value, $reverted_node->body->value, 'Node reverted correctly.');
    // Confirm the revision author is the user performing the revert.
    $this->assertSame($this->loggedInUser
        ->id(), $reverted_node->getRevisionUserId(), 'Node revision author is user performing revert.');
    // And that its not the revision author.
    $this->assertNotSame($nodes[1]->getRevisionUserId(), $reverted_node->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->drupalGet("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete");
    $this->submitForm([], 'Delete');
    $this->assertSession()
        ->pageTextContains("Revision from {$this->container->get('date.formatter')->format($nodes[1]->getRevisionCreationTime())} of Basic page {$nodes[1]->label()} has been 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->drupalGet("node/" . $node->id() . "/revisions/" . $nodes[2]->getRevisionId() . "/revert");
    $this->submitForm([], 'Revert');
    $this->assertSession()
        ->pageTextContains("Basic page {$nodes[2]->label()} has been reverted to the revision from {$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();
    // Verify that revision body text is not present on default version of node.
    $this->drupalGet('node/' . $node->id());
    $this->assertSession()
        ->pageTextNotContains($new_body);
    // Verify that the new body text is present on the revision.
    $this->drupalGet("node/" . $node->id() . "/revisions/" . $new_node_revision->getRevisionId() . "/view");
    $this->assertSession()
        ->pageTextContains($new_body);
    // 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->assertGreaterThan($default_revision_vid, $new_node_revision->getRevisionId());
    // 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->assertSession()
        ->pageTextContains('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->assertSession()
        ->pageTextContains('Simple revision message (EN)');
    $this->assertSession()
        ->pageTextContains('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->assertSession()
        ->pageTextContains('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->assertSession()
        ->pageTextContains('Simple revision message (EN)');
    $this->assertSession()
        ->pageTextNotContains('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->assertSession()
        ->pageTextNotContains('Simple revision message (EN)');
    $this->assertSession()
        ->pageTextContains('New revision message (DE)');
    // View the revision UI in 'EN', only the original node revision is shown.
    $this->drupalGet("node/" . $node->id() . "/revisions");
    $this->assertSession()
        ->pageTextContains('Simple revision message (EN)');
    $this->assertSession()
        ->pageTextNotContains('New revision message (DE)');
}

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