NodeRevisionsAllTestCase.php 5.24 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\node\Tests\NodeRevisionsAllTestCase.
 */

namespace Drupal\node\Tests;

10 11
use Drupal\Core\Language\Language;

12 13 14 15 16
/**
 * Tests actions against revisions for user with access to all revisions.
 */
class NodeRevisionsAllTestCase extends NodeTestBase {
  protected $nodes;
17
  protected $revisionLogs;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  protected $profile = "standard";

  public static function getInfo() {
    return array(
      'name' => 'Node revisions all',
      'description' => 'Create a node with revisions and test viewing, saving, reverting, and deleting revisions for user with access to all.',
      'group' => 'Node',
    );
  }

  function setUp() {
    parent::setUp();

    // Create and log in user.
    $web_user = $this->drupalCreateUser(
      array(
        'view page revisions',
        'revert page revisions',
        'delete page revisions',
        'edit any page content',
        'delete any page content'
      )
    );
    $this->drupalLogin($web_user);

    // Create an initial node.
    $node = $this->drupalCreateNode();

    $settings = get_object_vars($node);
    $settings['revision'] = 1;

    $nodes = array();
    $logs = array();

    // Get the original node.
53
    $nodes[] = clone $node;
54 55 56 57

    // Create three revisions.
    $revision_count = 3;
    for ($i = 0; $i < $revision_count; $i++) {
58
      $logs[] = $node->revision_log = $this->randomName(32);
59 60

      // Create revision with a random title and body and update variables.
61
      $node->title = $this->randomName();
62
      $node->body = array(
63 64 65 66 67 68
        'value' => $this->randomName(32),
        'format' => filter_default_format(),
      );
      $node->setNewRevision();
      $node->save();

69
      $node = node_load($node->id(), TRUE); // Make sure we get revision information.
70
      $nodes[] = clone $node;
71 72 73
    }

    $this->nodes = $nodes;
74
    $this->revisionLogs = $logs;
75 76 77 78 79 80 81
  }

  /**
   * Checks node revision operations.
   */
  function testRevisions() {
    $nodes = $this->nodes;
82
    $logs = $this->revisionLogs;
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

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

    // Create and login user.
    $content_admin = $this->drupalCreateUser(
      array(
        'view all revisions',
        'revert all revisions',
        'delete all revisions',
        'edit any page content',
        'delete any page content'
      )
    );
    $this->drupalLogin($content_admin);

    // Confirm the correct revision text appears on "view revisions" page.
100
    $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/view");
101
    $this->assertText($node->body->value, 'Correct text displays for version.');
102

103 104
    // Confirm the correct revision log message appears on the "revisions
    // overview" page.
105
    $this->drupalGet("node/" . $node->id() . "/revisions");
106 107
    foreach ($logs as $revision_log) {
      $this->assertText($revision_log, 'Revision log message found.');
108 109 110 111 112 113
    }

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

    // Confirm that revisions revert properly.
114
    $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/revert", array(), t('Revert'));
115 116 117
    $this->assertRaw(t('@type %title has been reverted back to the revision from %revision-date.',
      array(
        '@type' => 'Basic page',
118 119
        '%title' => $nodes[1]->getTitle(),
        '%revision-date' => format_date($nodes[1]->getRevisionCreationTime())
120 121
      )),
      'Revision reverted.');
122
    $reverted_node = node_load($node->id(), TRUE);
123
    $this->assertTrue(($nodes[1]->body->value == $reverted_node->body->value), 'Node reverted correctly.');
124 125

    // Confirm that this is not the current version.
126
    $node = node_revision_load($node->getRevisionId());
127 128 129
    $this->assertFalse($node->isDefaultRevision(), 'Third node revision is not the current one.');

    // Confirm revisions delete properly.
130
    $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete", array(), t('Delete'));
131 132
    $this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.',
      array(
133
        '%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
134
        '@type' => 'Basic page',
135
        '%title' => $nodes[1]->getTitle(),
136 137
      )),
      'Revision deleted.');
138
    $this->assertTrue(db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid and vid = :vid',
139
      array(':nid' => $node->id(), ':vid' => $nodes[1]->getRevisionId()))->fetchField() == 0,
140 141 142 143 144
      '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;
145
    db_update('node_revision')
146
      ->condition('vid', $nodes[2]->getRevisionId())
147
      ->fields(array(
148
        'revision_timestamp' => $old_revision_date,
149 150
      ))
      ->execute();
151
    $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[2]->getRevisionId() . "/revert", array(), t('Revert'));
152 153
    $this->assertRaw(t('@type %title has been reverted back to the revision from %revision-date.', array(
      '@type' => 'Basic page',
154
      '%title' => $nodes[2]->getTitle(),
155 156 157 158
      '%revision-date' => format_date($old_revision_date),
    )));
  }
}