3 namespace Drupal\Tests\node\Functional;
5 use Drupal\node\NodeInterface;
8 * Create a node with revisions and test viewing, saving, reverting, and
9 * deleting revisions for user with access to all.
13 class NodeRevisionsAllTest extends NodeTestBase {
16 * A list of nodes created to be used as starting point of different tests.
18 * @var Drupal\node\NodeInterface[]
23 * Revision logs of nodes created by the setup method.
27 protected $revisionLogs;
30 * An arbitrary user for revision authoring.
32 * @var \Drupal\user\UserInterface
34 protected $revisionUser;
39 protected function setUp() {
42 // Create and log in user.
43 $web_user = $this->drupalCreateUser(
45 'view page revisions',
46 'revert page revisions',
47 'delete page revisions',
48 'edit any page content',
49 'delete any page content',
52 $this->drupalLogin($web_user);
54 // Create an initial node.
55 $node = $this->drupalCreateNode();
57 // Create a user for revision authoring.
58 // This must be different from user performing revert.
59 $this->revisionUser = $this->drupalCreateUser();
61 $settings = get_object_vars($node);
62 $settings['revision'] = 1;
67 // Get the original node.
68 $nodes[] = clone $node;
70 // Create three revisions.
72 for ($i = 0; $i < $revision_count; $i++) {
73 $logs[] = $node->revision_log = $this->randomMachineName(32);
75 $node = $this->createNodeRevision($node);
76 $nodes[] = clone $node;
79 $this->nodes = $nodes;
80 $this->revisionLogs = $logs;
84 * Creates a new revision for a given node.
86 * @param \Drupal\node\NodeInterface $node
89 * @return \Drupal\node\NodeInterface
90 * A node object with up to date revision information.
92 protected function createNodeRevision(NodeInterface $node) {
93 // Create revision with a random title and body and update variables.
94 $node->title = $this->randomMachineName();
96 'value' => $this->randomMachineName(32),
97 'format' => filter_default_format(),
99 $node->setNewRevision();
100 // Ensure the revision author is a different user.
101 $node->setRevisionUserId($this->revisionUser->id());
108 * Checks node revision operations.
110 public function testRevisions() {
111 $node_storage = $this->container->get('entity.manager')->getStorage('node');
112 $nodes = $this->nodes;
113 $logs = $this->revisionLogs;
115 // Get last node for simple checks.
118 // Create and log in user.
119 $content_admin = $this->drupalCreateUser(
121 'view all revisions',
122 'revert all revisions',
123 'delete all revisions',
124 'edit any page content',
125 'delete any page content',
128 $this->drupalLogin($content_admin);
130 // Confirm the correct revision text appears on "view revisions" page.
131 $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/view");
132 $this->assertText($node->body->value, 'Correct text displays for version.');
134 // Confirm the correct revision log message appears on the "revisions
136 $this->drupalGet("node/" . $node->id() . "/revisions");
137 foreach ($logs as $revision_log) {
138 $this->assertText($revision_log, 'Revision log message found.');
141 // Confirm that this is the current revision.
142 $this->assertTrue($node->isDefaultRevision(), 'Third node revision is the current one.');
144 // Confirm that revisions revert properly.
145 $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/revert", [], t('Revert'));
146 $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.',
148 '@type' => 'Basic page',
149 '%title' => $nodes[1]->getTitle(),
150 '%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
152 'Revision reverted.');
153 $node_storage->resetCache([$node->id()]);
154 $reverted_node = $node_storage->load($node->id());
155 $this->assertTrue(($nodes[1]->body->value == $reverted_node->body->value), 'Node reverted correctly.');
157 // Confirm the revision author is the user performing the revert.
158 $this->assertTrue($reverted_node->getRevisionUserId() == $this->loggedInUser->id(), 'Node revision author is user performing revert.');
159 // And that its not the revision author.
160 $this->assertTrue($reverted_node->getRevisionUserId() != $this->revisionUser->id(), 'Node revision author is not original revision author.');
162 // Confirm that this is not the current version.
163 $node = node_revision_load($node->getRevisionId());
164 $this->assertFalse($node->isDefaultRevision(), 'Third node revision is not the current one.');
166 // Confirm that the node can still be updated.
167 $this->drupalPostForm("node/" . $reverted_node->id() . "/edit", ['body[0][value]' => 'We are Drupal.'], t('Save'));
168 $this->assertText(t('Basic page @title has been updated.', ['@title' => $reverted_node->getTitle()]), 'Node was successfully saved after reverting a revision.');
169 $this->assertText('We are Drupal.', 'Node was correctly updated after reverting a revision.');
171 // Confirm revisions delete properly.
172 $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete", [], t('Delete'));
173 $this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.',
175 '%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
176 '@type' => 'Basic page',
177 '%title' => $nodes[1]->getTitle(),
179 'Revision deleted.');
180 $this->assertTrue(db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid and vid = :vid',
181 [':nid' => $node->id(), ':vid' => $nodes[1]->getRevisionId()])->fetchField() == 0,
182 'Revision not found.');
184 // Set the revision timestamp to an older date to make sure that the
185 // confirmation message correctly displays the stored revision date.
186 $old_revision_date = REQUEST_TIME - 86400;
187 db_update('node_revision')
188 ->condition('vid', $nodes[2]->getRevisionId())
190 'revision_timestamp' => $old_revision_date,
193 $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[2]->getRevisionId() . "/revert", [], t('Revert'));
194 $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', [
195 '@type' => 'Basic page',
196 '%title' => $nodes[2]->getTitle(),
197 '%revision-date' => format_date($old_revision_date),
200 // Create 50 more revisions in order to trigger paging on the revisions
203 for ($i = 0; $i < 50; $i++) {
204 $logs[] = $node->revision_log = $this->randomMachineName(32);
206 $node = $this->createNodeRevision($node);
207 $nodes[] = clone $node;
210 $this->drupalGet('node/' . $node->id() . '/revisions');
212 // Check that the pager exists.
213 $this->assertRaw('page=1');
215 // Check that the last revision is displayed on the first page.
216 $this->assertText(end($logs));
218 // Go to the second page and check that one of the initial three revisions
220 $this->clickLink(t('Page 2'));
221 $this->assertText($logs[2]);