1 node.test NodeRevisionsTestCase::testRevisions()

Checks node revision related operations.

File

core/modules/node/tests/node.test, line 217
Tests for node.module.

Class

NodeRevisionsTestCase
Tests the node revision functionality.

Code

function testRevisions() {
  $nodes = $this->nodes;
  $logs = $this->logs;

  // Get last node for simple checks.
  $node = $nodes[3];

  // Confirm the correct revision text appears on "view revisions" page.
  $this->backdropGet("node/$node->nid/revisions/$node->vid/view");
  $this->assertText($node->body[LANGUAGE_NONE][0]['value'], 'Correct text displays for version.');

  // Confirm the correct log message appears on "revisions overview" page.
  $this->backdropGet("node/$node->nid/revisions");
  foreach ($logs as $log) {
    $this->assertText($log, t('Log message found.'));
  }

  // Confirm that this is the active revision.
  $this->assertTrue($node->isActiveRevision(), 'Third node revision is the current one.');

  // Confirm that revisions revert properly.
  $this->backdropPost("node/$node->nid/revisions/{$nodes[1]->vid}/revert", array(), t('Revert'));
  $this->assertRaw(t('@type %title has been reverted back to the revision from %revision-date.', array(
    '@type' => 'Page',
    '%title' => $nodes[1]->title,
    '%revision-date' => format_date($nodes[1]->revision_timestamp),
  )), 'Revision reverted.');
  $reverted_node = node_load($node->nid);
  $this->assertTrue(($nodes[1]->body[LANGUAGE_NONE][0]['value'] == $reverted_node->body[LANGUAGE_NONE][0]['value']), 'Node reverted correctly.');

  // Confirm that this is not the current version.
  $node = node_load($node->nid, $node->vid);
  $this->assertFalse($node->isActiveRevision(), 'Third node revision is not the current one.');

  // Confirm revisions delete properly.
  $this->backdropPost("node/$node->nid/revisions/{$nodes[1]->vid}/delete", array(), t('Delete'));
  $this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.', array(
    '%revision-date' => format_date($nodes[1]->revision_timestamp),
    '@type' => 'Page',
    '%title' => $nodes[1]->title,
  )), 'Revision deleted.');
  $this->assertTrue(db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid and vid = :vid', array(':nid' => $node->nid, ':vid' => $nodes[1]->vid))->fetchField() == 0, 'Revision not found.');

  // 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;
  db_update('node_revision')
    ->condition('vid', $nodes[2]->vid)
    ->fields(array(
      'timestamp' => $old_revision_date,
    ))
    ->execute();
  $this->backdropPost("node/$node->nid/revisions/{$nodes[2]->vid}/revert", array(), t('Revert'));
  $this->assertRaw(t('@type %title has been reverted back to the revision from %revision-date.', array(
    '@type' => 'Page',
    '%title' => $nodes[2]->title,
    '%revision-date' => format_date($old_revision_date),
  )));

  // Make a new revision and set it to not be active.
  // This will create a new revision that is not "front facing".
  $new_node_revision = clone $node;
  $new_body = $this->randomName();
  $new_node_revision->body[LANGUAGE_NONE][0]['value'] = $new_body;
  // Save this as a non-active revision.
  $new_node_revision->revision = TRUE;
  $new_node_revision->is_active_revision = FALSE;
  node_save($new_node_revision);

  $this->backdropGet("node/$node->nid");
  $this->assertNoText($new_body, t('Revision body text is not present on active version of node.'));

  // Verify that the new body text is present on the revision.
  $this->backdropGet("node/$node->nid/revisions/" . $new_node_revision->vid . "/view");
  $this->assertText($new_body, t('Revision body text is present when loading specific revision.'));

  // Verify that the non-active revision vid is greater than the active
  // revision vid.
  $active_revision = db_select('node', 'n')
    ->fields('n', array('vid'))
    ->condition('nid', $node->nid)
    ->execute()
    ->fetchCol();
  $active_revision_vid = $active_revision[0];
  $this->assertTrue($new_node_revision->vid > $active_revision_vid, 'Revision vid is greater than active revision vid.');
}