Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\node\Functional;
|
Chris@0
|
4
|
Chris@18
|
5 use Drupal\Core\Database\Database;
|
Chris@0
|
6 use Drupal\node\NodeInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Create a node with revisions and test viewing, saving, reverting, and
|
Chris@0
|
10 * deleting revisions for user with access to all.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @group node
|
Chris@0
|
13 */
|
Chris@0
|
14 class NodeRevisionsAllTest extends NodeTestBase {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * A list of nodes created to be used as starting point of different tests.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var Drupal\node\NodeInterface[]
|
Chris@0
|
20 */
|
Chris@0
|
21 protected $nodes;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Revision logs of nodes created by the setup method.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var string[]
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $revisionLogs;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@16
|
31 * An arbitrary user for revision authoring.
|
Chris@16
|
32 *
|
Chris@16
|
33 * @var \Drupal\user\UserInterface
|
Chris@16
|
34 */
|
Chris@16
|
35 protected $revisionUser;
|
Chris@16
|
36
|
Chris@16
|
37 /**
|
Chris@0
|
38 * {@inheritdoc}
|
Chris@0
|
39 */
|
Chris@0
|
40 protected function setUp() {
|
Chris@0
|
41 parent::setUp();
|
Chris@0
|
42
|
Chris@0
|
43 // Create and log in user.
|
Chris@0
|
44 $web_user = $this->drupalCreateUser(
|
Chris@0
|
45 [
|
Chris@0
|
46 'view page revisions',
|
Chris@0
|
47 'revert page revisions',
|
Chris@0
|
48 'delete page revisions',
|
Chris@0
|
49 'edit any page content',
|
Chris@17
|
50 'delete any page content',
|
Chris@0
|
51 ]
|
Chris@0
|
52 );
|
Chris@0
|
53 $this->drupalLogin($web_user);
|
Chris@0
|
54
|
Chris@0
|
55 // Create an initial node.
|
Chris@0
|
56 $node = $this->drupalCreateNode();
|
Chris@0
|
57
|
Chris@16
|
58 // Create a user for revision authoring.
|
Chris@16
|
59 // This must be different from user performing revert.
|
Chris@16
|
60 $this->revisionUser = $this->drupalCreateUser();
|
Chris@16
|
61
|
Chris@0
|
62 $settings = get_object_vars($node);
|
Chris@0
|
63 $settings['revision'] = 1;
|
Chris@0
|
64
|
Chris@0
|
65 $nodes = [];
|
Chris@0
|
66 $logs = [];
|
Chris@0
|
67
|
Chris@0
|
68 // Get the original node.
|
Chris@0
|
69 $nodes[] = clone $node;
|
Chris@0
|
70
|
Chris@0
|
71 // Create three revisions.
|
Chris@0
|
72 $revision_count = 3;
|
Chris@0
|
73 for ($i = 0; $i < $revision_count; $i++) {
|
Chris@0
|
74 $logs[] = $node->revision_log = $this->randomMachineName(32);
|
Chris@0
|
75
|
Chris@0
|
76 $node = $this->createNodeRevision($node);
|
Chris@0
|
77 $nodes[] = clone $node;
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 $this->nodes = $nodes;
|
Chris@0
|
81 $this->revisionLogs = $logs;
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * Creates a new revision for a given node.
|
Chris@0
|
86 *
|
Chris@0
|
87 * @param \Drupal\node\NodeInterface $node
|
Chris@0
|
88 * A node object.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @return \Drupal\node\NodeInterface
|
Chris@0
|
91 * A node object with up to date revision information.
|
Chris@0
|
92 */
|
Chris@0
|
93 protected function createNodeRevision(NodeInterface $node) {
|
Chris@0
|
94 // Create revision with a random title and body and update variables.
|
Chris@0
|
95 $node->title = $this->randomMachineName();
|
Chris@0
|
96 $node->body = [
|
Chris@0
|
97 'value' => $this->randomMachineName(32),
|
Chris@0
|
98 'format' => filter_default_format(),
|
Chris@0
|
99 ];
|
Chris@0
|
100 $node->setNewRevision();
|
Chris@16
|
101 // Ensure the revision author is a different user.
|
Chris@16
|
102 $node->setRevisionUserId($this->revisionUser->id());
|
Chris@0
|
103 $node->save();
|
Chris@0
|
104
|
Chris@0
|
105 return $node;
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * Checks node revision operations.
|
Chris@0
|
110 */
|
Chris@0
|
111 public function testRevisions() {
|
Chris@0
|
112 $node_storage = $this->container->get('entity.manager')->getStorage('node');
|
Chris@0
|
113 $nodes = $this->nodes;
|
Chris@0
|
114 $logs = $this->revisionLogs;
|
Chris@0
|
115
|
Chris@0
|
116 // Get last node for simple checks.
|
Chris@0
|
117 $node = $nodes[3];
|
Chris@0
|
118
|
Chris@0
|
119 // Create and log in user.
|
Chris@0
|
120 $content_admin = $this->drupalCreateUser(
|
Chris@0
|
121 [
|
Chris@0
|
122 'view all revisions',
|
Chris@0
|
123 'revert all revisions',
|
Chris@0
|
124 'delete all revisions',
|
Chris@0
|
125 'edit any page content',
|
Chris@17
|
126 'delete any page content',
|
Chris@0
|
127 ]
|
Chris@0
|
128 );
|
Chris@0
|
129 $this->drupalLogin($content_admin);
|
Chris@0
|
130
|
Chris@0
|
131 // Confirm the correct revision text appears on "view revisions" page.
|
Chris@0
|
132 $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/view");
|
Chris@0
|
133 $this->assertText($node->body->value, 'Correct text displays for version.');
|
Chris@0
|
134
|
Chris@0
|
135 // Confirm the correct revision log message appears on the "revisions
|
Chris@0
|
136 // overview" page.
|
Chris@0
|
137 $this->drupalGet("node/" . $node->id() . "/revisions");
|
Chris@0
|
138 foreach ($logs as $revision_log) {
|
Chris@0
|
139 $this->assertText($revision_log, 'Revision log message found.');
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 // Confirm that this is the current revision.
|
Chris@0
|
143 $this->assertTrue($node->isDefaultRevision(), 'Third node revision is the current one.');
|
Chris@0
|
144
|
Chris@0
|
145 // Confirm that revisions revert properly.
|
Chris@0
|
146 $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/revert", [], t('Revert'));
|
Chris@0
|
147 $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.',
|
Chris@0
|
148 [
|
Chris@0
|
149 '@type' => 'Basic page',
|
Chris@0
|
150 '%title' => $nodes[1]->getTitle(),
|
Chris@18
|
151 '%revision-date' => $this->container->get('date.formatter')->format($nodes[1]->getRevisionCreationTime()),
|
Chris@0
|
152 ]),
|
Chris@0
|
153 'Revision reverted.');
|
Chris@0
|
154 $node_storage->resetCache([$node->id()]);
|
Chris@0
|
155 $reverted_node = $node_storage->load($node->id());
|
Chris@0
|
156 $this->assertTrue(($nodes[1]->body->value == $reverted_node->body->value), 'Node reverted correctly.');
|
Chris@0
|
157
|
Chris@16
|
158 // Confirm the revision author is the user performing the revert.
|
Chris@16
|
159 $this->assertTrue($reverted_node->getRevisionUserId() == $this->loggedInUser->id(), 'Node revision author is user performing revert.');
|
Chris@16
|
160 // And that its not the revision author.
|
Chris@16
|
161 $this->assertTrue($reverted_node->getRevisionUserId() != $this->revisionUser->id(), 'Node revision author is not original revision author.');
|
Chris@16
|
162
|
Chris@0
|
163 // Confirm that this is not the current version.
|
Chris@0
|
164 $node = node_revision_load($node->getRevisionId());
|
Chris@0
|
165 $this->assertFalse($node->isDefaultRevision(), 'Third node revision is not the current one.');
|
Chris@0
|
166
|
Chris@0
|
167 // Confirm that the node can still be updated.
|
Chris@0
|
168 $this->drupalPostForm("node/" . $reverted_node->id() . "/edit", ['body[0][value]' => 'We are Drupal.'], t('Save'));
|
Chris@0
|
169 $this->assertText(t('Basic page @title has been updated.', ['@title' => $reverted_node->getTitle()]), 'Node was successfully saved after reverting a revision.');
|
Chris@0
|
170 $this->assertText('We are Drupal.', 'Node was correctly updated after reverting a revision.');
|
Chris@0
|
171
|
Chris@0
|
172 // Confirm revisions delete properly.
|
Chris@0
|
173 $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete", [], t('Delete'));
|
Chris@0
|
174 $this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.',
|
Chris@0
|
175 [
|
Chris@18
|
176 '%revision-date' => $this->container->get('date.formatter')->format($nodes[1]->getRevisionCreationTime()),
|
Chris@0
|
177 '@type' => 'Basic page',
|
Chris@0
|
178 '%title' => $nodes[1]->getTitle(),
|
Chris@0
|
179 ]),
|
Chris@0
|
180 'Revision deleted.');
|
Chris@18
|
181 $connection = Database::getConnection();
|
Chris@0
|
182 $this->assertTrue(db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid and vid = :vid',
|
Chris@0
|
183 [':nid' => $node->id(), ':vid' => $nodes[1]->getRevisionId()])->fetchField() == 0,
|
Chris@0
|
184 'Revision not found.');
|
Chris@0
|
185
|
Chris@0
|
186 // Set the revision timestamp to an older date to make sure that the
|
Chris@0
|
187 // confirmation message correctly displays the stored revision date.
|
Chris@0
|
188 $old_revision_date = REQUEST_TIME - 86400;
|
Chris@18
|
189 $connection->update('node_revision')
|
Chris@0
|
190 ->condition('vid', $nodes[2]->getRevisionId())
|
Chris@0
|
191 ->fields([
|
Chris@0
|
192 'revision_timestamp' => $old_revision_date,
|
Chris@0
|
193 ])
|
Chris@0
|
194 ->execute();
|
Chris@0
|
195 $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[2]->getRevisionId() . "/revert", [], t('Revert'));
|
Chris@0
|
196 $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', [
|
Chris@0
|
197 '@type' => 'Basic page',
|
Chris@0
|
198 '%title' => $nodes[2]->getTitle(),
|
Chris@18
|
199 '%revision-date' => $this->container->get('date.formatter')->format($old_revision_date),
|
Chris@0
|
200 ]));
|
Chris@0
|
201
|
Chris@0
|
202 // Create 50 more revisions in order to trigger paging on the revisions
|
Chris@0
|
203 // overview screen.
|
Chris@0
|
204 $node = $nodes[0];
|
Chris@0
|
205 for ($i = 0; $i < 50; $i++) {
|
Chris@0
|
206 $logs[] = $node->revision_log = $this->randomMachineName(32);
|
Chris@0
|
207
|
Chris@0
|
208 $node = $this->createNodeRevision($node);
|
Chris@0
|
209 $nodes[] = clone $node;
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 $this->drupalGet('node/' . $node->id() . '/revisions');
|
Chris@0
|
213
|
Chris@0
|
214 // Check that the pager exists.
|
Chris@0
|
215 $this->assertRaw('page=1');
|
Chris@0
|
216
|
Chris@0
|
217 // Check that the last revision is displayed on the first page.
|
Chris@0
|
218 $this->assertText(end($logs));
|
Chris@0
|
219
|
Chris@0
|
220 // Go to the second page and check that one of the initial three revisions
|
Chris@0
|
221 // is displayed.
|
Chris@0
|
222 $this->clickLink(t('Page 2'));
|
Chris@0
|
223 $this->assertText($logs[2]);
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 }
|