3 namespace Drupal\Tests\node\Functional;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\Entity\NodeType;
10 * Tests the UI for controlling node revision behavior.
14 class NodeRevisionsUiTest extends NodeTestBase {
17 * @var \Drupal\user\Entity\User
24 protected function setUp() {
28 $this->editor = $this->drupalCreateUser([
30 'edit any page content',
31 'view page revisions',
32 'access user profiles',
37 * Checks that unchecking 'Create new revision' works when editing a node.
39 public function testNodeFormSaveWithoutRevision() {
40 $this->drupalLogin($this->editor);
41 $node_storage = $this->container->get('entity.manager')->getStorage('node');
43 // Set page revision setting 'create new revision'. This will mean new
44 // revisions are created by default when the node is edited.
45 $type = NodeType::load('page');
46 $type->setNewRevision(TRUE);
50 $node = $this->drupalCreateNode();
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");
56 // Uncheck the create new revision checkbox and save the node.
57 $edit = ['revision' => FALSE];
58 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
60 // Load the node again and check the revision is the same as before.
61 $node_storage->resetCache([$node->id()]);
62 $node_revision = $node_storage->load($node->id(), TRUE);
63 $this->assertEqual($node_revision->getRevisionId(), $node->getRevisionId(), "After an existing node is saved with 'Create new revision' unchecked, a new revision is not created.");
65 // Verify the checkbox is checked on the node edit form.
66 $this->drupalGet('node/' . $node->id() . '/edit');
67 $this->assertFieldChecked('edit-revision', "'Create new revision' checkbox is checked");
69 // Submit the form without changing the checkbox.
71 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
73 // Load the node again and check the revision is different from before.
74 $node_storage->resetCache([$node->id()]);
75 $node_revision = $node_storage->load($node->id());
76 $this->assertNotEqual($node_revision->getRevisionId(), $node->getRevisionId(), "After an existing node is saved with 'Create new revision' checked, a new revision is created.");
80 * Checks HTML double escaping of revision logs.
82 public function testNodeRevisionDoubleEscapeFix() {
83 $this->drupalLogin($this->editor);
87 $node = $this->drupalCreateNode();
90 '#theme' => 'username',
91 '#account' => $this->editor,
93 $editor = \Drupal::service('renderer')->renderPlain($username);
96 $nodes[] = clone $node;
98 // Create revision with a random title and body and update variables.
99 $node->title = $this->randomMachineName();
101 'value' => $this->randomMachineName(32),
102 'format' => filter_default_format(),
104 $node->setNewRevision();
105 $revision_log = 'Revision <em>message</em> with markup.';
106 $node->revision_log->value = $revision_log;
108 // Make sure we get revision information.
109 $node = Node::load($node->id());
110 $nodes[] = clone $node;
112 $this->drupalGet('node/' . $node->id() . '/revisions');
114 // Assert the old revision message.
115 $date = format_date($nodes[0]->revision_timestamp->value, 'short');
116 $url = new Url('entity.node.revision', ['node' => $nodes[0]->id(), 'node_revision' => $nodes[0]->getRevisionId()]);
117 $this->assertRaw(\Drupal::l($date, $url) . ' by ' . $editor);
119 // Assert the current revision message.
120 $date = format_date($nodes[1]->revision_timestamp->value, 'short');
121 $this->assertRaw($nodes[1]->link($date) . ' by ' . $editor . '<p class="revision-log">' . $revision_log . '</p>');
125 * Checks the Revisions tab.
127 public function testNodeRevisionsTabWithDefaultRevision() {
128 $this->drupalLogin($this->editor);
131 $node = $this->drupalCreateNode();
132 $storage = \Drupal::entityTypeManager()->getStorage($node->getEntityTypeId());
134 // Create a new revision based on the default revision.
136 $node = $storage->load($node->id());
137 $node->setNewRevision(TRUE);
141 $node = $storage->load($node->id());
142 $node->setNewRevision(TRUE);
146 // Trigger translation changes in order to show the revision.
147 $node = $storage->load($node->id());
148 $node->setTitle($this->randomString());
149 $node->isDefaultRevision(FALSE);
150 $node->setNewRevision(TRUE);
154 $node = $storage->load($node->id());
155 $node->isDefaultRevision(FALSE);
156 $node->setNewRevision(TRUE);
159 $node_id = $node->id();
161 $this->drupalGet('node/' . $node_id . '/revisions');
163 // Verify that the default revision can be an older revision than the latest
165 // Assert that the revisions with translations changes are shown: 1 and 4.
166 $this->assertLinkByHref('/node/' . $node_id . '/revisions/1/revert');
167 $this->assertLinkByHref('/node/' . $node_id . '/revisions/4/revert');
169 // Assert that the revisions without translations changes are filtered out:
171 $this->assertNoLinkByHref('/node/' . $node_id . '/revisions/2/revert');
172 $this->assertNoLinkByHref('/node/' . $node_id . '/revisions/3/revert');
173 $this->assertNoLinkByHref('/node/' . $node_id . '/revisions/5/revert');