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;
32 protected function setUp() {
35 // Create and log in user.
36 $web_user = $this->drupalCreateUser(
38 'view page revisions',
39 'revert page revisions',
40 'delete page revisions',
41 'edit any page content',
42 'delete any page content'
45 $this->drupalLogin($web_user);
47 // Create an initial node.
48 $node = $this->drupalCreateNode();
50 $settings = get_object_vars($node);
51 $settings['revision'] = 1;
56 // Get the original node.
57 $nodes[] = clone $node;
59 // Create three revisions.
61 for ($i = 0; $i < $revision_count; $i++) {
62 $logs[] = $node->revision_log = $this->randomMachineName(32);
64 $node = $this->createNodeRevision($node);
65 $nodes[] = clone $node;
68 $this->nodes = $nodes;
69 $this->revisionLogs = $logs;
73 * Creates a new revision for a given node.
75 * @param \Drupal\node\NodeInterface $node
78 * @return \Drupal\node\NodeInterface
79 * A node object with up to date revision information.
81 protected function createNodeRevision(NodeInterface $node) {
82 // Create revision with a random title and body and update variables.
83 $node->title = $this->randomMachineName();
85 'value' => $this->randomMachineName(32),
86 'format' => filter_default_format(),
88 $node->setNewRevision();
95 * Checks node revision operations.
97 public function testRevisions() {
98 $node_storage = $this->container->get('entity.manager')->getStorage('node');
99 $nodes = $this->nodes;
100 $logs = $this->revisionLogs;
102 // Get last node for simple checks.
105 // Create and log in user.
106 $content_admin = $this->drupalCreateUser(
108 'view all revisions',
109 'revert all revisions',
110 'delete all revisions',
111 'edit any page content',
112 'delete any page content'
115 $this->drupalLogin($content_admin);
117 // Confirm the correct revision text appears on "view revisions" page.
118 $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/view");
119 $this->assertText($node->body->value, 'Correct text displays for version.');
121 // Confirm the correct revision log message appears on the "revisions
123 $this->drupalGet("node/" . $node->id() . "/revisions");
124 foreach ($logs as $revision_log) {
125 $this->assertText($revision_log, 'Revision log message found.');
128 // Confirm that this is the current revision.
129 $this->assertTrue($node->isDefaultRevision(), 'Third node revision is the current one.');
131 // Confirm that revisions revert properly.
132 $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/revert", [], t('Revert'));
133 $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.',
135 '@type' => 'Basic page',
136 '%title' => $nodes[1]->getTitle(),
137 '%revision-date' => format_date($nodes[1]->getRevisionCreationTime())
139 'Revision reverted.');
140 $node_storage->resetCache([$node->id()]);
141 $reverted_node = $node_storage->load($node->id());
142 $this->assertTrue(($nodes[1]->body->value == $reverted_node->body->value), 'Node reverted correctly.');
144 // Confirm that this is not the current version.
145 $node = node_revision_load($node->getRevisionId());
146 $this->assertFalse($node->isDefaultRevision(), 'Third node revision is not the current one.');
148 // Confirm that the node can still be updated.
149 $this->drupalPostForm("node/" . $reverted_node->id() . "/edit", ['body[0][value]' => 'We are Drupal.'], t('Save'));
150 $this->assertText(t('Basic page @title has been updated.', ['@title' => $reverted_node->getTitle()]), 'Node was successfully saved after reverting a revision.');
151 $this->assertText('We are Drupal.', 'Node was correctly updated after reverting a revision.');
153 // Confirm revisions delete properly.
154 $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete", [], t('Delete'));
155 $this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.',
157 '%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
158 '@type' => 'Basic page',
159 '%title' => $nodes[1]->getTitle(),
161 'Revision deleted.');
162 $this->assertTrue(db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid and vid = :vid',
163 [':nid' => $node->id(), ':vid' => $nodes[1]->getRevisionId()])->fetchField() == 0,
164 'Revision not found.');
166 // Set the revision timestamp to an older date to make sure that the
167 // confirmation message correctly displays the stored revision date.
168 $old_revision_date = REQUEST_TIME - 86400;
169 db_update('node_revision')
170 ->condition('vid', $nodes[2]->getRevisionId())
172 'revision_timestamp' => $old_revision_date,
175 $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[2]->getRevisionId() . "/revert", [], t('Revert'));
176 $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', [
177 '@type' => 'Basic page',
178 '%title' => $nodes[2]->getTitle(),
179 '%revision-date' => format_date($old_revision_date),
182 // Create 50 more revisions in order to trigger paging on the revisions
185 for ($i = 0; $i < 50; $i++) {
186 $logs[] = $node->revision_log = $this->randomMachineName(32);
188 $node = $this->createNodeRevision($node);
189 $nodes[] = clone $node;
192 $this->drupalGet('node/' . $node->id() . '/revisions');
194 // Check that the pager exists.
195 $this->assertRaw('page=1');
197 // Check that the last revision is displayed on the first page.
198 $this->assertText(end($logs));
200 // Go to the second page and check that one of the initial three revisions
202 $this->clickLink(t('Page 2'));
203 $this->assertText($logs[2]);