View source  
  <?php
namespace Drupal\Tests\node\Functional;
use Drupal\Core\Database\Database;
use Drupal\node\NodeInterface;
class NodeRevisionsAllTest extends NodeTestBase {
  
  protected $defaultTheme = 'stark';
  
  protected $nodes;
  
  protected $revisionLogs;
  
  protected $revisionUser;
  
  protected function setUp() {
    parent::setUp();
    
    $web_user = $this
      ->drupalCreateUser([
      'view page revisions',
      'revert page revisions',
      'delete page revisions',
      'edit any page content',
      'delete any page content',
    ]);
    $this
      ->drupalLogin($web_user);
    
    $node = $this
      ->drupalCreateNode();
    
    $this->revisionUser = $this
      ->drupalCreateUser();
    $settings = get_object_vars($node);
    $settings['revision'] = 1;
    $nodes = [];
    $logs = [];
    
    $nodes[] = clone $node;
    
    $revision_count = 3;
    for ($i = 0; $i < $revision_count; $i++) {
      $logs[] = $node->revision_log = $this
        ->randomMachineName(32);
      $node = $this
        ->createNodeRevision($node);
      $nodes[] = clone $node;
    }
    $this->nodes = $nodes;
    $this->revisionLogs = $logs;
  }
  
  protected function createNodeRevision(NodeInterface $node) {
    
    $node->title = $this
      ->randomMachineName();
    $node->body = [
      'value' => $this
        ->randomMachineName(32),
      'format' => filter_default_format(),
    ];
    $node
      ->setNewRevision();
    
    $node
      ->setRevisionUserId($this->revisionUser
      ->id());
    $node
      ->save();
    return $node;
  }
  
  public function testRevisions() {
    $node_storage = $this->container
      ->get('entity_type.manager')
      ->getStorage('node');
    $nodes = $this->nodes;
    $logs = $this->revisionLogs;
    
    $node = $nodes[3];
    
    $content_admin = $this
      ->drupalCreateUser([
      'view all revisions',
      'revert all revisions',
      'delete all revisions',
      'edit any page content',
      'delete any page content',
    ]);
    $this
      ->drupalLogin($content_admin);
    
    $this
      ->drupalGet("node/" . $node
      ->id() . "/revisions/" . $node
      ->getRevisionId() . "/view");
    $this
      ->assertText($node->body->value, 'Correct text displays for version.');
    
    $this
      ->drupalGet("node/" . $node
      ->id() . "/revisions");
    foreach ($logs as $revision_log) {
      $this
        ->assertText($revision_log, 'Revision log message found.');
    }
    
    $this
      ->assertTrue($node
      ->isDefaultRevision(), 'Third node revision is the current one.');
    
    $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]
        ->getTitle(),
      '%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.');
    
    $this
      ->assertTrue($reverted_node
      ->getRevisionUserId() == $this->loggedInUser
      ->id(), 'Node revision author is user performing revert.');
    
    $this
      ->assertTrue($reverted_node
      ->getRevisionUserId() != $this->revisionUser
      ->id(), 'Node revision author is not original revision author.');
    
    $node = node_revision_load($node
      ->getRevisionId());
    $this
      ->assertFalse($node
      ->isDefaultRevision(), 'Third node revision is not the current one.');
    
    $this
      ->drupalPostForm("node/" . $reverted_node
      ->id() . "/edit", [
      'body[0][value]' => 'We are Drupal.',
    ], t('Save'));
    $this
      ->assertText(t('Basic page @title has been updated.', [
      '@title' => $reverted_node
        ->getTitle(),
    ]), 'Node was successfully saved after reverting a revision.');
    $this
      ->assertText('We are Drupal.', 'Node was correctly updated after reverting a revision.');
    
    $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]
        ->getTitle(),
    ]), 'Revision deleted.');
    $connection = Database::getConnection();
    $this
      ->assertTrue($connection
      ->query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid and vid = :vid', [
      ':nid' => $node
        ->id(),
      ':vid' => $nodes[1]
        ->getRevisionId(),
    ])
      ->fetchField() == 0, 'Revision not found.');
    
    $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]
        ->getTitle(),
      '%revision-date' => $this->container
        ->get('date.formatter')
        ->format($old_revision_date),
    ]));
    
    $node = $nodes[0];
    for ($i = 0; $i < 50; $i++) {
      $logs[] = $node->revision_log = $this
        ->randomMachineName(32);
      $node = $this
        ->createNodeRevision($node);
      $nodes[] = clone $node;
    }
    $this
      ->drupalGet('node/' . $node
      ->id() . '/revisions');
    
    $this
      ->assertRaw('page=1');
    
    $this
      ->assertText(end($logs));
    
    $this
      ->clickLink(t('Page 2'));
    $this
      ->assertText($logs[2]);
  }
}