238c7317e2a23fedc891964a366540975c399a36
[yaffs-website] / web / core / modules / node / tests / src / Functional / NodeRevisionsAllTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 use Drupal\node\NodeInterface;
6
7 /**
8  * Create a node with revisions and test viewing, saving, reverting, and
9  * deleting revisions for user with access to all.
10  *
11  * @group node
12  */
13 class NodeRevisionsAllTest extends NodeTestBase {
14
15   /**
16    * A list of nodes created to be used as starting point of different tests.
17    *
18    * @var Drupal\node\NodeInterface[]
19    */
20   protected $nodes;
21
22   /**
23    * Revision logs of nodes created by the setup method.
24    *
25    * @var string[]
26    */
27   protected $revisionLogs;
28
29   /**
30    * An arbitrary user for revision authoring.
31    *
32    * @var \Drupal\user\UserInterface
33    */
34   protected $revisionUser;
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::setUp();
41
42     // Create and log in user.
43     $web_user = $this->drupalCreateUser(
44       [
45         'view page revisions',
46         'revert page revisions',
47         'delete page revisions',
48         'edit any page content',
49         'delete any page content',
50       ]
51     );
52     $this->drupalLogin($web_user);
53
54     // Create an initial node.
55     $node = $this->drupalCreateNode();
56
57     // Create a user for revision authoring.
58     // This must be different from user performing revert.
59     $this->revisionUser = $this->drupalCreateUser();
60
61     $settings = get_object_vars($node);
62     $settings['revision'] = 1;
63
64     $nodes = [];
65     $logs = [];
66
67     // Get the original node.
68     $nodes[] = clone $node;
69
70     // Create three revisions.
71     $revision_count = 3;
72     for ($i = 0; $i < $revision_count; $i++) {
73       $logs[] = $node->revision_log = $this->randomMachineName(32);
74
75       $node = $this->createNodeRevision($node);
76       $nodes[] = clone $node;
77     }
78
79     $this->nodes = $nodes;
80     $this->revisionLogs = $logs;
81   }
82
83   /**
84    * Creates a new revision for a given node.
85    *
86    * @param \Drupal\node\NodeInterface $node
87    *   A node object.
88    *
89    * @return \Drupal\node\NodeInterface
90    *   A node object with up to date revision information.
91    */
92   protected function createNodeRevision(NodeInterface $node) {
93     // Create revision with a random title and body and update variables.
94     $node->title = $this->randomMachineName();
95     $node->body = [
96       'value' => $this->randomMachineName(32),
97       'format' => filter_default_format(),
98     ];
99     $node->setNewRevision();
100     // Ensure the revision author is a different user.
101     $node->setRevisionUserId($this->revisionUser->id());
102     $node->save();
103
104     return $node;
105   }
106
107   /**
108    * Checks node revision operations.
109    */
110   public function testRevisions() {
111     $node_storage = $this->container->get('entity.manager')->getStorage('node');
112     $nodes = $this->nodes;
113     $logs = $this->revisionLogs;
114
115     // Get last node for simple checks.
116     $node = $nodes[3];
117
118     // Create and log in user.
119     $content_admin = $this->drupalCreateUser(
120       [
121         'view all revisions',
122         'revert all revisions',
123         'delete all revisions',
124         'edit any page content',
125         'delete any page content',
126       ]
127     );
128     $this->drupalLogin($content_admin);
129
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.');
133
134     // Confirm the correct revision log message appears on the "revisions
135     // overview" page.
136     $this->drupalGet("node/" . $node->id() . "/revisions");
137     foreach ($logs as $revision_log) {
138       $this->assertText($revision_log, 'Revision log message found.');
139     }
140
141     // Confirm that this is the current revision.
142     $this->assertTrue($node->isDefaultRevision(), 'Third node revision is the current one.');
143
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.',
147       [
148         '@type' => 'Basic page',
149         '%title' => $nodes[1]->getTitle(),
150         '%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
151       ]),
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.');
156
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.');
161
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.');
165
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.');
170
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.',
174       [
175         '%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
176         '@type' => 'Basic page',
177         '%title' => $nodes[1]->getTitle(),
178       ]),
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.');
183
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())
189       ->fields([
190         'revision_timestamp' => $old_revision_date,
191       ])
192       ->execute();
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),
198     ]));
199
200     // Create 50 more revisions in order to trigger paging on the revisions
201     // overview screen.
202     $node = $nodes[0];
203     for ($i = 0; $i < 50; $i++) {
204       $logs[] = $node->revision_log = $this->randomMachineName(32);
205
206       $node = $this->createNodeRevision($node);
207       $nodes[] = clone $node;
208     }
209
210     $this->drupalGet('node/' . $node->id() . '/revisions');
211
212     // Check that the pager exists.
213     $this->assertRaw('page=1');
214
215     // Check that the last revision is displayed on the first page.
216     $this->assertText(end($logs));
217
218     // Go to the second page and check that one of the initial three revisions
219     // is displayed.
220     $this->clickLink(t('Page 2'));
221     $this->assertText($logs[2]);
222   }
223
224 }