NodeRevisionsUiTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\Core\Url;
  4. use Drupal\node\Entity\Node;
  5. use Drupal\node\Entity\NodeType;
  6. /**
  7. * Tests the UI for controlling node revision behavior.
  8. *
  9. * @group node
  10. */
  11. class NodeRevisionsUiTest extends NodeTestBase {
  12. /**
  13. * @var \Drupal\user\Entity\User
  14. */
  15. protected $editor;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. protected function setUp() {
  20. parent::setUp();
  21. // Create users.
  22. $this->editor = $this->drupalCreateUser([
  23. 'administer nodes',
  24. 'edit any page content',
  25. 'view page revisions',
  26. 'access user profiles',
  27. ]);
  28. }
  29. /**
  30. * Checks that unchecking 'Create new revision' works when editing a node.
  31. */
  32. public function testNodeFormSaveWithoutRevision() {
  33. $this->drupalLogin($this->editor);
  34. $node_storage = $this->container->get('entity.manager')->getStorage('node');
  35. // Set page revision setting 'create new revision'. This will mean new
  36. // revisions are created by default when the node is edited.
  37. $type = NodeType::load('page');
  38. $type->setNewRevision(TRUE);
  39. $type->save();
  40. // Create the node.
  41. $node = $this->drupalCreateNode();
  42. // Verify the checkbox is checked on the node edit form.
  43. $this->drupalGet('node/' . $node->id() . '/edit');
  44. $this->assertFieldChecked('edit-revision', "'Create new revision' checkbox is checked");
  45. // Uncheck the create new revision checkbox and save the node.
  46. $edit = ['revision' => FALSE];
  47. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
  48. // Load the node again and check the revision is the same as before.
  49. $node_storage->resetCache([$node->id()]);
  50. $node_revision = $node_storage->load($node->id(), TRUE);
  51. $this->assertEqual($node_revision->getRevisionId(), $node->getRevisionId(), "After an existing node is saved with 'Create new revision' unchecked, a new revision is not created.");
  52. // Verify the checkbox is checked on the node edit form.
  53. $this->drupalGet('node/' . $node->id() . '/edit');
  54. $this->assertFieldChecked('edit-revision', "'Create new revision' checkbox is checked");
  55. // Submit the form without changing the checkbox.
  56. $edit = [];
  57. $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
  58. // Load the node again and check the revision is different from before.
  59. $node_storage->resetCache([$node->id()]);
  60. $node_revision = $node_storage->load($node->id());
  61. $this->assertNotEqual($node_revision->getRevisionId(), $node->getRevisionId(), "After an existing node is saved with 'Create new revision' checked, a new revision is created.");
  62. }
  63. /**
  64. * Checks HTML double escaping of revision logs.
  65. */
  66. public function testNodeRevisionDoubleEscapeFix() {
  67. $this->drupalLogin($this->editor);
  68. $nodes = [];
  69. // Create the node.
  70. $node = $this->drupalCreateNode();
  71. $username = [
  72. '#theme' => 'username',
  73. '#account' => $this->editor,
  74. ];
  75. $editor = \Drupal::service('renderer')->renderPlain($username);
  76. // Get original node.
  77. $nodes[] = clone $node;
  78. // Create revision with a random title and body and update variables.
  79. $node->title = $this->randomMachineName();
  80. $node->body = [
  81. 'value' => $this->randomMachineName(32),
  82. 'format' => filter_default_format(),
  83. ];
  84. $node->setNewRevision();
  85. $revision_log = 'Revision <em>message</em> with markup.';
  86. $node->revision_log->value = $revision_log;
  87. $node->save();
  88. // Make sure we get revision information.
  89. $node = Node::load($node->id());
  90. $nodes[] = clone $node;
  91. $this->drupalGet('node/' . $node->id() . '/revisions');
  92. // Assert the old revision message.
  93. $date = format_date($nodes[0]->revision_timestamp->value, 'short');
  94. $url = new Url('entity.node.revision', ['node' => $nodes[0]->id(), 'node_revision' => $nodes[0]->getRevisionId()]);
  95. $this->assertRaw(\Drupal::l($date, $url) . ' by ' . $editor);
  96. // Assert the current revision message.
  97. $date = format_date($nodes[1]->revision_timestamp->value, 'short');
  98. $this->assertRaw($nodes[1]->link($date) . ' by ' . $editor . '<p class="revision-log">' . $revision_log . '</p>');
  99. }
  100. /**
  101. * Checks the Revisions tab.
  102. */
  103. public function testNodeRevisionsTabWithDefaultRevision() {
  104. $this->drupalLogin($this->editor);
  105. // Create the node.
  106. $node = $this->drupalCreateNode();
  107. $storage = \Drupal::entityTypeManager()->getStorage($node->getEntityTypeId());
  108. // Create a new revision based on the default revision.
  109. // Revision 2.
  110. $node = $storage->load($node->id());
  111. $node->setNewRevision(TRUE);
  112. $node->save();
  113. // Revision 3.
  114. $node = $storage->load($node->id());
  115. $node->setNewRevision(TRUE);
  116. $node->save();
  117. // Revision 4.
  118. // Trigger translation changes in order to show the revision.
  119. $node = $storage->load($node->id());
  120. $node->setTitle($this->randomString());
  121. $node->isDefaultRevision(FALSE);
  122. $node->setNewRevision(TRUE);
  123. $node->save();
  124. // Revision 5.
  125. $node = $storage->load($node->id());
  126. $node->isDefaultRevision(FALSE);
  127. $node->setNewRevision(TRUE);
  128. $node->save();
  129. $node_id = $node->id();
  130. $this->drupalGet('node/' . $node_id . '/revisions');
  131. // Verify that the latest affected revision having been a default revision
  132. // is displayed as the current one.
  133. $this->assertNoLinkByHref('/node/' . $node_id . '/revisions/1/revert');
  134. $elements = $this->xpath('//tr[contains(@class, "revision-current")]/td/a[1]');
  135. // The site may be installed in a subdirectory, so check if the URL is
  136. // contained in the retrieved one.
  137. $this->assertContains('/node/1', current($elements)->getAttribute('href'));
  138. // Verify that the default revision can be an older revision than the latest
  139. // one.
  140. // Assert that the revisions with translations changes are shown.
  141. $this->assertLinkByHref('/node/' . $node_id . '/revisions/4/revert');
  142. // Assert that the revisions without translations changes are filtered out:
  143. // 2, 3 and 5.
  144. $this->assertNoLinkByHref('/node/' . $node_id . '/revisions/2/revert');
  145. $this->assertNoLinkByHref('/node/' . $node_id . '/revisions/3/revert');
  146. $this->assertNoLinkByHref('/node/' . $node_id . '/revisions/5/revert');
  147. }
  148. }