3646ab3e010779a6080ad4e5f7a397bd406b563b
[yaffs-website] / web / core / modules / node / tests / src / Functional / NodeRevisionsUiTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 use Drupal\Core\Url;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\Entity\NodeType;
8
9 /**
10  * Tests the UI for controlling node revision behavior.
11  *
12  * @group node
13  */
14 class NodeRevisionsUiTest extends NodeTestBase {
15
16   /**
17    * @var \Drupal\user\Entity\User
18    */
19   protected $editor;
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26
27     // Create users.
28     $this->editor = $this->drupalCreateUser([
29       'administer nodes',
30       'edit any page content',
31       'view page revisions',
32       'access user profiles',
33     ]);
34   }
35
36   /**
37    * Checks that unchecking 'Create new revision' works when editing a node.
38    */
39   public function testNodeFormSaveWithoutRevision() {
40     $this->drupalLogin($this->editor);
41     $node_storage = $this->container->get('entity.manager')->getStorage('node');
42
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);
47     $type->save();
48
49     // Create the node.
50     $node = $this->drupalCreateNode();
51
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
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'));
59
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.");
64
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");
68
69     // Submit the form without changing the checkbox.
70     $edit = [];
71     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
72
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.");
77   }
78
79   /**
80    * Checks HTML double escaping of revision logs.
81    */
82   public function testNodeRevisionDoubleEscapeFix() {
83     $this->drupalLogin($this->editor);
84     $nodes = [];
85
86     // Create the node.
87     $node = $this->drupalCreateNode();
88
89     $username = [
90       '#theme' => 'username',
91       '#account' => $this->editor,
92     ];
93     $editor = \Drupal::service('renderer')->renderPlain($username);
94
95     // Get original node.
96     $nodes[] = clone $node;
97
98     // Create revision with a random title and body and update variables.
99     $node->title = $this->randomMachineName();
100     $node->body = [
101       'value' => $this->randomMachineName(32),
102       'format' => filter_default_format(),
103     ];
104     $node->setNewRevision();
105     $revision_log = 'Revision <em>message</em> with markup.';
106     $node->revision_log->value = $revision_log;
107     $node->save();
108     // Make sure we get revision information.
109     $node = Node::load($node->id());
110     $nodes[] = clone $node;
111
112     $this->drupalGet('node/' . $node->id() . '/revisions');
113
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);
118
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>');
122   }
123
124   /**
125    * Checks the Revisions tab.
126    */
127   public function testNodeRevisionsTabWithDefaultRevision() {
128     $this->drupalLogin($this->editor);
129
130     // Create the node.
131     $node = $this->drupalCreateNode();
132     $storage = \Drupal::entityTypeManager()->getStorage($node->getEntityTypeId());
133
134     // Create a new revision based on the default revision.
135     // Revision 2.
136     $node = $storage->load($node->id());
137     $node->setNewRevision(TRUE);
138     $node->save();
139
140     // Revision 3.
141     $node = $storage->load($node->id());
142     $node->setNewRevision(TRUE);
143     $node->save();
144
145     // Revision 4.
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);
151     $node->save();
152
153     // Revision 5.
154     $node = $storage->load($node->id());
155     $node->isDefaultRevision(FALSE);
156     $node->setNewRevision(TRUE);
157     $node->save();
158
159     $node_id = $node->id();
160
161     $this->drupalGet('node/' . $node_id . '/revisions');
162
163     // Verify that the latest affected revision having been a default revision
164     // is displayed as the current one.
165     $this->assertNoLinkByHref('/node/' . $node_id . '/revisions/1/revert');
166     $elements = $this->xpath('//tr[contains(@class, "revision-current")]/td/a[1]');
167     // The site may be installed in a subdirectory, so check if the URL is
168     // contained in the retrieved one.
169     $this->assertContains('/node/1', current($elements)->getAttribute('href'));
170
171     // Verify that the default revision can be an older revision than the latest
172     // one.
173     // Assert that the revisions with translations changes are shown.
174     $this->assertLinkByHref('/node/' . $node_id . '/revisions/4/revert');
175
176     // Assert that the revisions without translations changes are filtered out:
177     // 2, 3 and 5.
178     $this->assertNoLinkByHref('/node/' . $node_id . '/revisions/2/revert');
179     $this->assertNoLinkByHref('/node/' . $node_id . '/revisions/3/revert');
180     $this->assertNoLinkByHref('/node/' . $node_id . '/revisions/5/revert');
181   }
182
183 }