Version 1
[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    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34
35     // Create and log in user.
36     $web_user = $this->drupalCreateUser(
37       [
38         'view page revisions',
39         'revert page revisions',
40         'delete page revisions',
41         'edit any page content',
42         'delete any page content'
43       ]
44     );
45     $this->drupalLogin($web_user);
46
47     // Create an initial node.
48     $node = $this->drupalCreateNode();
49
50     $settings = get_object_vars($node);
51     $settings['revision'] = 1;
52
53     $nodes = [];
54     $logs = [];
55
56     // Get the original node.
57     $nodes[] = clone $node;
58
59     // Create three revisions.
60     $revision_count = 3;
61     for ($i = 0; $i < $revision_count; $i++) {
62       $logs[] = $node->revision_log = $this->randomMachineName(32);
63
64       $node = $this->createNodeRevision($node);
65       $nodes[] = clone $node;
66     }
67
68     $this->nodes = $nodes;
69     $this->revisionLogs = $logs;
70   }
71
72   /**
73    * Creates a new revision for a given node.
74    *
75    * @param \Drupal\node\NodeInterface $node
76    *   A node object.
77    *
78    * @return \Drupal\node\NodeInterface
79    *   A node object with up to date revision information.
80    */
81   protected function createNodeRevision(NodeInterface $node) {
82     // Create revision with a random title and body and update variables.
83     $node->title = $this->randomMachineName();
84     $node->body = [
85       'value' => $this->randomMachineName(32),
86       'format' => filter_default_format(),
87     ];
88     $node->setNewRevision();
89     $node->save();
90
91     return $node;
92   }
93
94   /**
95    * Checks node revision operations.
96    */
97   public function testRevisions() {
98     $node_storage = $this->container->get('entity.manager')->getStorage('node');
99     $nodes = $this->nodes;
100     $logs = $this->revisionLogs;
101
102     // Get last node for simple checks.
103     $node = $nodes[3];
104
105     // Create and log in user.
106     $content_admin = $this->drupalCreateUser(
107       [
108         'view all revisions',
109         'revert all revisions',
110         'delete all revisions',
111         'edit any page content',
112         'delete any page content'
113       ]
114     );
115     $this->drupalLogin($content_admin);
116
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.');
120
121     // Confirm the correct revision log message appears on the "revisions
122     // overview" page.
123     $this->drupalGet("node/" . $node->id() . "/revisions");
124     foreach ($logs as $revision_log) {
125       $this->assertText($revision_log, 'Revision log message found.');
126     }
127
128     // Confirm that this is the current revision.
129     $this->assertTrue($node->isDefaultRevision(), 'Third node revision is the current one.');
130
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.',
134       [
135         '@type' => 'Basic page',
136         '%title' => $nodes[1]->getTitle(),
137         '%revision-date' => format_date($nodes[1]->getRevisionCreationTime())
138       ]),
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.');
143
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.');
147
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.');
152
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.',
156       [
157         '%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
158         '@type' => 'Basic page',
159         '%title' => $nodes[1]->getTitle(),
160       ]),
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.');
165
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())
171       ->fields([
172         'revision_timestamp' => $old_revision_date,
173       ])
174       ->execute();
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),
180     ]));
181
182     // Create 50 more revisions in order to trigger paging on the revisions
183     // overview screen.
184     $node = $nodes[0];
185     for ($i = 0; $i < 50; $i++) {
186       $logs[] = $node->revision_log = $this->randomMachineName(32);
187
188       $node = $this->createNodeRevision($node);
189       $nodes[] = clone $node;
190     }
191
192     $this->drupalGet('node/' . $node->id() . '/revisions');
193
194     // Check that the pager exists.
195     $this->assertRaw('page=1');
196
197     // Check that the last revision is displayed on the first page.
198     $this->assertText(end($logs));
199
200     // Go to the second page and check that one of the initial three revisions
201     // is displayed.
202     $this->clickLink(t('Page 2'));
203     $this->assertText($logs[2]);
204   }
205
206 }