PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/node/src/Tests/NodeRevisionsAllTest.php

http://github.com/drupal/drupal
PHP | 187 lines | 116 code | 31 blank | 40 comment | 4 complexity | 7d2d0c2c3983e0fed50c7608d57f726f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\node\Tests;
  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. protected $nodes;
  12. protected $revisionLogs;
  13. protected $profile = "standard";
  14. protected function setUp() {
  15. parent::setUp();
  16. // Create and log in user.
  17. $web_user = $this->drupalCreateUser(
  18. array(
  19. 'view page revisions',
  20. 'revert page revisions',
  21. 'delete page revisions',
  22. 'edit any page content',
  23. 'delete any page content'
  24. )
  25. );
  26. $this->drupalLogin($web_user);
  27. // Create an initial node.
  28. $node = $this->drupalCreateNode();
  29. $settings = get_object_vars($node);
  30. $settings['revision'] = 1;
  31. $nodes = array();
  32. $logs = array();
  33. // Get the original node.
  34. $nodes[] = clone $node;
  35. // Create three revisions.
  36. $revision_count = 3;
  37. for ($i = 0; $i < $revision_count; $i++) {
  38. $logs[] = $node->revision_log = $this->randomMachineName(32);
  39. $node = $this->createNodeRevision($node);
  40. $nodes[] = clone $node;
  41. }
  42. $this->nodes = $nodes;
  43. $this->revisionLogs = $logs;
  44. }
  45. /**
  46. * Creates a new revision for a given node.
  47. *
  48. * @param \Drupal\node\NodeInterface $node
  49. * A node object.
  50. *
  51. * @return \Drupal\node\NodeInterface
  52. * A node object with up to date revision information.
  53. */
  54. protected function createNodeRevision(NodeInterface $node) {
  55. // Create revision with a random title and body and update variables.
  56. $node->title = $this->randomMachineName();
  57. $node->body = array(
  58. 'value' => $this->randomMachineName(32),
  59. 'format' => filter_default_format(),
  60. );
  61. $node->setNewRevision();
  62. $node->save();
  63. return $node;
  64. }
  65. /**
  66. * Checks node revision operations.
  67. */
  68. function testRevisions() {
  69. $node_storage = $this->container->get('entity.manager')->getStorage('node');
  70. $nodes = $this->nodes;
  71. $logs = $this->revisionLogs;
  72. // Get last node for simple checks.
  73. $node = $nodes[3];
  74. // Create and log in user.
  75. $content_admin = $this->drupalCreateUser(
  76. array(
  77. 'view all revisions',
  78. 'revert all revisions',
  79. 'delete all revisions',
  80. 'edit any page content',
  81. 'delete any page content'
  82. )
  83. );
  84. $this->drupalLogin($content_admin);
  85. // Confirm the correct revision text appears on "view revisions" page.
  86. $this->drupalGet("node/" . $node->id() . "/revisions/" . $node->getRevisionId() . "/view");
  87. $this->assertText($node->body->value, 'Correct text displays for version.');
  88. // Confirm the correct revision log message appears on the "revisions
  89. // overview" page.
  90. $this->drupalGet("node/" . $node->id() . "/revisions");
  91. foreach ($logs as $revision_log) {
  92. $this->assertText($revision_log, 'Revision log message found.');
  93. }
  94. // Confirm that this is the current revision.
  95. $this->assertTrue($node->isDefaultRevision(), 'Third node revision is the current one.');
  96. // Confirm that revisions revert properly.
  97. $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/revert", array(), t('Revert'));
  98. $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.',
  99. array(
  100. '@type' => 'Basic page',
  101. '%title' => $nodes[1]->getTitle(),
  102. '%revision-date' => format_date($nodes[1]->getRevisionCreationTime())
  103. )),
  104. 'Revision reverted.');
  105. $node_storage->resetCache(array($node->id()));
  106. $reverted_node = $node_storage->load($node->id());
  107. $this->assertTrue(($nodes[1]->body->value == $reverted_node->body->value), 'Node reverted correctly.');
  108. // Confirm that this is not the current version.
  109. $node = node_revision_load($node->getRevisionId());
  110. $this->assertFalse($node->isDefaultRevision(), 'Third node revision is not the current one.');
  111. // Confirm revisions delete properly.
  112. $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete", array(), t('Delete'));
  113. $this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.',
  114. array(
  115. '%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
  116. '@type' => 'Basic page',
  117. '%title' => $nodes[1]->getTitle(),
  118. )),
  119. 'Revision deleted.');
  120. $this->assertTrue(db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid and vid = :vid',
  121. array(':nid' => $node->id(), ':vid' => $nodes[1]->getRevisionId()))->fetchField() == 0,
  122. 'Revision not found.');
  123. // Set the revision timestamp to an older date to make sure that the
  124. // confirmation message correctly displays the stored revision date.
  125. $old_revision_date = REQUEST_TIME - 86400;
  126. db_update('node_revision')
  127. ->condition('vid', $nodes[2]->getRevisionId())
  128. ->fields(array(
  129. 'revision_timestamp' => $old_revision_date,
  130. ))
  131. ->execute();
  132. $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[2]->getRevisionId() . "/revert", array(), t('Revert'));
  133. $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', array(
  134. '@type' => 'Basic page',
  135. '%title' => $nodes[2]->getTitle(),
  136. '%revision-date' => format_date($old_revision_date),
  137. )));
  138. // Create 50 more revisions in order to trigger paging on the revisions
  139. // overview screen.
  140. $node = $nodes[0];
  141. for ($i = 0; $i < 50; $i++) {
  142. $logs[] = $node->revision_log = $this->randomMachineName(32);
  143. $node = $this->createNodeRevision($node);
  144. $nodes[] = clone $node;
  145. }
  146. $this->drupalGet('node/' . $node->id() . '/revisions');
  147. // Check that the pager exists.
  148. $this->assertRaw('page=1');
  149. // Check that the last revision is displayed on the first page.
  150. $this->assertText(end($logs));
  151. // Go to the second page and check that one of the initial three revisions
  152. // is displayed.
  153. $this->clickLink(t('Page 2'));
  154. $this->assertText($logs[2]);
  155. }
  156. }