annotate core/modules/node/tests/src/Functional/NodeRevisionsAllTest.php @ 4:a9cd425dd02b

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