annotate core/modules/node/tests/src/Functional/NodeRevisionsAllTest.php @ 6:875880e46745

Styling
author Chris Cannam
date Fri, 08 Dec 2017 13:21:27 +0000
parents 4c8ae668cc8c
children c2387f117808
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 * {@inheritdoc}
Chris@0 31 */
Chris@0 32 protected function setUp() {
Chris@0 33 parent::setUp();
Chris@0 34
Chris@0 35 // Create and log in user.
Chris@0 36 $web_user = $this->drupalCreateUser(
Chris@0 37 [
Chris@0 38 'view page revisions',
Chris@0 39 'revert page revisions',
Chris@0 40 'delete page revisions',
Chris@0 41 'edit any page content',
Chris@0 42 'delete any page content'
Chris@0 43 ]
Chris@0 44 );
Chris@0 45 $this->drupalLogin($web_user);
Chris@0 46
Chris@0 47 // Create an initial node.
Chris@0 48 $node = $this->drupalCreateNode();
Chris@0 49
Chris@0 50 $settings = get_object_vars($node);
Chris@0 51 $settings['revision'] = 1;
Chris@0 52
Chris@0 53 $nodes = [];
Chris@0 54 $logs = [];
Chris@0 55
Chris@0 56 // Get the original node.
Chris@0 57 $nodes[] = clone $node;
Chris@0 58
Chris@0 59 // Create three revisions.
Chris@0 60 $revision_count = 3;
Chris@0 61 for ($i = 0; $i < $revision_count; $i++) {
Chris@0 62 $logs[] = $node->revision_log = $this->randomMachineName(32);
Chris@0 63
Chris@0 64 $node = $this->createNodeRevision($node);
Chris@0 65 $nodes[] = clone $node;
Chris@0 66 }
Chris@0 67
Chris@0 68 $this->nodes = $nodes;
Chris@0 69 $this->revisionLogs = $logs;
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * Creates a new revision for a given node.
Chris@0 74 *
Chris@0 75 * @param \Drupal\node\NodeInterface $node
Chris@0 76 * A node object.
Chris@0 77 *
Chris@0 78 * @return \Drupal\node\NodeInterface
Chris@0 79 * A node object with up to date revision information.
Chris@0 80 */
Chris@0 81 protected function createNodeRevision(NodeInterface $node) {
Chris@0 82 // Create revision with a random title and body and update variables.
Chris@0 83 $node->title = $this->randomMachineName();
Chris@0 84 $node->body = [
Chris@0 85 'value' => $this->randomMachineName(32),
Chris@0 86 'format' => filter_default_format(),
Chris@0 87 ];
Chris@0 88 $node->setNewRevision();
Chris@0 89 $node->save();
Chris@0 90
Chris@0 91 return $node;
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * Checks node revision operations.
Chris@0 96 */
Chris@0 97 public function testRevisions() {
Chris@0 98 $node_storage = $this->container->get('entity.manager')->getStorage('node');
Chris@0 99 $nodes = $this->nodes;
Chris@0 100 $logs = $this->revisionLogs;
Chris@0 101
Chris@0 102 // Get last node for simple checks.
Chris@0 103 $node = $nodes[3];
Chris@0 104
Chris@0 105 // Create and log in user.
Chris@0 106 $content_admin = $this->drupalCreateUser(
Chris@0 107 [
Chris@0 108 'view all revisions',
Chris@0 109 'revert all revisions',
Chris@0 110 'delete all revisions',
Chris@0 111 'edit any page content',
Chris@0 112 'delete any page content'
Chris@0 113 ]
Chris@0 114 );
Chris@0 115 $this->drupalLogin($content_admin);
Chris@0 116
Chris@0 117 // Confirm the correct revision text appears on "view revisions" page.
Chris@0 118 $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/view");
Chris@0 119 $this->assertText($node->body->value, 'Correct text displays for version.');
Chris@0 120
Chris@0 121 // Confirm the correct revision log message appears on the "revisions
Chris@0 122 // overview" page.
Chris@0 123 $this->drupalGet("node/" . $node->id() . "/revisions");
Chris@0 124 foreach ($logs as $revision_log) {
Chris@0 125 $this->assertText($revision_log, 'Revision log message found.');
Chris@0 126 }
Chris@0 127
Chris@0 128 // Confirm that this is the current revision.
Chris@0 129 $this->assertTrue($node->isDefaultRevision(), 'Third node revision is the current one.');
Chris@0 130
Chris@0 131 // Confirm that revisions revert properly.
Chris@0 132 $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/revert", [], t('Revert'));
Chris@0 133 $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.',
Chris@0 134 [
Chris@0 135 '@type' => 'Basic page',
Chris@0 136 '%title' => $nodes[1]->getTitle(),
Chris@0 137 '%revision-date' => format_date($nodes[1]->getRevisionCreationTime())
Chris@0 138 ]),
Chris@0 139 'Revision reverted.');
Chris@0 140 $node_storage->resetCache([$node->id()]);
Chris@0 141 $reverted_node = $node_storage->load($node->id());
Chris@0 142 $this->assertTrue(($nodes[1]->body->value == $reverted_node->body->value), 'Node reverted correctly.');
Chris@0 143
Chris@0 144 // Confirm that this is not the current version.
Chris@0 145 $node = node_revision_load($node->getRevisionId());
Chris@0 146 $this->assertFalse($node->isDefaultRevision(), 'Third node revision is not the current one.');
Chris@0 147
Chris@0 148 // Confirm that the node can still be updated.
Chris@0 149 $this->drupalPostForm("node/" . $reverted_node->id() . "/edit", ['body[0][value]' => 'We are Drupal.'], t('Save'));
Chris@0 150 $this->assertText(t('Basic page @title has been updated.', ['@title' => $reverted_node->getTitle()]), 'Node was successfully saved after reverting a revision.');
Chris@0 151 $this->assertText('We are Drupal.', 'Node was correctly updated after reverting a revision.');
Chris@0 152
Chris@0 153 // Confirm revisions delete properly.
Chris@0 154 $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete", [], t('Delete'));
Chris@0 155 $this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.',
Chris@0 156 [
Chris@0 157 '%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
Chris@0 158 '@type' => 'Basic page',
Chris@0 159 '%title' => $nodes[1]->getTitle(),
Chris@0 160 ]),
Chris@0 161 'Revision deleted.');
Chris@0 162 $this->assertTrue(db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid and vid = :vid',
Chris@0 163 [':nid' => $node->id(), ':vid' => $nodes[1]->getRevisionId()])->fetchField() == 0,
Chris@0 164 'Revision not found.');
Chris@0 165
Chris@0 166 // Set the revision timestamp to an older date to make sure that the
Chris@0 167 // confirmation message correctly displays the stored revision date.
Chris@0 168 $old_revision_date = REQUEST_TIME - 86400;
Chris@0 169 db_update('node_revision')
Chris@0 170 ->condition('vid', $nodes[2]->getRevisionId())
Chris@0 171 ->fields([
Chris@0 172 'revision_timestamp' => $old_revision_date,
Chris@0 173 ])
Chris@0 174 ->execute();
Chris@0 175 $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[2]->getRevisionId() . "/revert", [], t('Revert'));
Chris@0 176 $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', [
Chris@0 177 '@type' => 'Basic page',
Chris@0 178 '%title' => $nodes[2]->getTitle(),
Chris@0 179 '%revision-date' => format_date($old_revision_date),
Chris@0 180 ]));
Chris@0 181
Chris@0 182 // Create 50 more revisions in order to trigger paging on the revisions
Chris@0 183 // overview screen.
Chris@0 184 $node = $nodes[0];
Chris@0 185 for ($i = 0; $i < 50; $i++) {
Chris@0 186 $logs[] = $node->revision_log = $this->randomMachineName(32);
Chris@0 187
Chris@0 188 $node = $this->createNodeRevision($node);
Chris@0 189 $nodes[] = clone $node;
Chris@0 190 }
Chris@0 191
Chris@0 192 $this->drupalGet('node/' . $node->id() . '/revisions');
Chris@0 193
Chris@0 194 // Check that the pager exists.
Chris@0 195 $this->assertRaw('page=1');
Chris@0 196
Chris@0 197 // Check that the last revision is displayed on the first page.
Chris@0 198 $this->assertText(end($logs));
Chris@0 199
Chris@0 200 // Go to the second page and check that one of the initial three revisions
Chris@0 201 // is displayed.
Chris@0 202 $this->clickLink(t('Page 2'));
Chris@0 203 $this->assertText($logs[2]);
Chris@0 204 }
Chris@0 205
Chris@0 206 }