NodeRevisionsAllTest.php 7.4 KB

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