Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / search / src / Tests / SearchCommentTest.php
1 <?php
2
3 namespace Drupal\search\Tests;
4
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\comment\Tests\CommentTestTrait;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\user\RoleInterface;
9 use Drupal\filter\Entity\FilterFormat;
10
11 /**
12  * Tests integration searching comments.
13  *
14  * @group search
15  */
16 class SearchCommentTest extends SearchTestBase {
17
18   use CommentTestTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['filter', 'node', 'comment'];
26
27   /**
28    * Test subject for comments.
29    *
30    * @var string
31    */
32   protected $commentSubject;
33
34   /**
35    * ID for the administrator role.
36    *
37    * @var string
38    */
39   protected $adminRole;
40
41   /**
42    * A user with various administrative permissions.
43    *
44    * @var \Drupal\user\UserInterface
45    */
46   protected $adminUser;
47
48   /**
49    * Test node for searching.
50    *
51    * @var \Drupal\node\NodeInterface
52    */
53   protected $node;
54
55   protected function setUp() {
56     parent::setUp();
57
58     $full_html_format = FilterFormat::create([
59       'format' => 'full_html',
60       'name' => 'Full HTML',
61       'weight' => 1,
62       'filters' => [],
63     ]);
64     $full_html_format->save();
65
66     // Create and log in an administrative user having access to the Full HTML
67     // text format.
68     $permissions = [
69       'administer filters',
70       $full_html_format->getPermissionName(),
71       'administer permissions',
72       'create page content',
73       'post comments',
74       'skip comment approval',
75       'access comments',
76     ];
77     $this->adminUser = $this->drupalCreateUser($permissions);
78     $this->drupalLogin($this->adminUser);
79     // Add a comment field.
80     $this->addDefaultCommentField('node', 'article');
81   }
82
83   /**
84    * Verify that comments are rendered using proper format in search results.
85    */
86   public function testSearchResultsComment() {
87     $node_storage = $this->container->get('entity.manager')->getStorage('node');
88     // Create basic_html format that escapes all HTML.
89     $basic_html_format = FilterFormat::create([
90       'format' => 'basic_html',
91       'name' => 'Basic HTML',
92       'weight' => 1,
93       'filters' => [
94         'filter_html_escape' => ['status' => 1],
95       ],
96       'roles' => [RoleInterface::AUTHENTICATED_ID],
97     ]);
98     $basic_html_format->save();
99
100     $comment_body = 'Test comment body';
101
102     // Make preview optional.
103     $field = FieldConfig::loadByName('node', 'article', 'comment');
104     $field->setSetting('preview', DRUPAL_OPTIONAL);
105     $field->save();
106
107     // Allow anonymous users to search content.
108     $edit = [
109       RoleInterface::ANONYMOUS_ID . '[search content]' => 1,
110       RoleInterface::ANONYMOUS_ID . '[access comments]' => 1,
111       RoleInterface::ANONYMOUS_ID . '[post comments]' => 1,
112     ];
113     $this->drupalPostForm('admin/people/permissions', $edit, t('Save permissions'));
114
115     // Create a node.
116     $node = $this->drupalCreateNode(['type' => 'article']);
117     // Post a comment using 'Full HTML' text format.
118     $edit_comment = [];
119     $edit_comment['subject[0][value]'] = 'Test comment subject';
120     $edit_comment['comment_body[0][value]'] = '<h1>' . $comment_body . '</h1>';
121     $full_html_format_id = 'full_html';
122     $edit_comment['comment_body[0][format]'] = $full_html_format_id;
123     $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit_comment, t('Save'));
124
125     // Post a comment with an evil script tag in the comment subject and a
126     // script tag nearby a keyword in the comment body. Use the 'FULL HTML' text
127     // format so the script tag stored.
128     $edit_comment2 = [];
129     $edit_comment2['subject[0][value]'] = "<script>alert('subjectkeyword');</script>";
130     $edit_comment2['comment_body[0][value]'] = "nearbykeyword<script>alert('somethinggeneric');</script>";
131     $edit_comment2['comment_body[0][format]'] = $full_html_format_id;
132     $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit_comment2, t('Save'));
133
134     // Post a comment with a keyword inside an evil script tag in the comment
135     // body. Use the 'FULL HTML' text format so the script tag is stored.
136     $edit_comment3 = [];
137     $edit_comment3['subject[0][value]'] = 'asubject';
138     $edit_comment3['comment_body[0][value]'] = "<script>alert('insidekeyword');</script>";
139     $edit_comment3['comment_body[0][format]'] = $full_html_format_id;
140     $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit_comment3, t('Save'));
141
142     // Invoke search index update.
143     $this->drupalLogout();
144     $this->cronRun();
145
146     // Search for the comment subject.
147     $edit = [
148       'keys' => "'" . $edit_comment['subject[0][value]'] . "'",
149     ];
150     $this->drupalPostForm('search/node', $edit, t('Search'));
151     $node_storage->resetCache([$node->id()]);
152     $node2 = $node_storage->load($node->id());
153     $this->assertText($node2->label(), 'Node found in search results.');
154     $this->assertText($edit_comment['subject[0][value]'], 'Comment subject found in search results.');
155
156     // Search for the comment body.
157     $edit = [
158       'keys' => "'" . $comment_body . "'",
159     ];
160     $this->drupalPostForm(NULL, $edit, t('Search'));
161     $this->assertText($node2->label(), 'Node found in search results.');
162
163     // Verify that comment is rendered using proper format.
164     $this->assertText($comment_body, 'Comment body text found in search results.');
165     $this->assertNoRaw(t('n/a'), 'HTML in comment body is not hidden.');
166     $this->assertNoEscaped($edit_comment['comment_body[0][value]'], 'HTML in comment body is not escaped.');
167
168     // Search for the evil script comment subject.
169     $edit = [
170       'keys' => 'subjectkeyword',
171     ];
172     $this->drupalPostForm('search/node', $edit, t('Search'));
173
174     // Verify the evil comment subject is escaped in search results.
175     $this->assertRaw('&lt;script&gt;alert(&#039;<strong>subjectkeyword</strong>&#039;);');
176     $this->assertNoRaw('<script>');
177
178     // Search for the keyword near the evil script tag in the comment body.
179     $edit = [
180       'keys' => 'nearbykeyword',
181     ];
182     $this->drupalPostForm('search/node', $edit, t('Search'));
183
184     // Verify that nearby script tag in the evil comment body is stripped from
185     // search results.
186     $this->assertRaw('<strong>nearbykeyword</strong>');
187     $this->assertNoRaw('<script>');
188
189     // Search for contents inside the evil script tag in the comment body.
190     $edit = [
191       'keys' => 'insidekeyword',
192     ];
193     $this->drupalPostForm('search/node', $edit, t('Search'));
194
195     // @todo Verify the actual search results.
196     //   https://www.drupal.org/node/2551135
197
198     // Verify there is no script tag in search results.
199     $this->assertNoRaw('<script>');
200
201     // Hide comments.
202     $this->drupalLogin($this->adminUser);
203     $node->set('comment', CommentItemInterface::HIDDEN);
204     $node->save();
205
206     // Invoke search index update.
207     $this->drupalLogout();
208     $this->cronRun();
209
210     // Search for $title.
211     $this->drupalPostForm('search/node', $edit, t('Search'));
212     $this->assertText(t('Your search yielded no results.'));
213   }
214
215   /**
216    * Verify access rules for comment indexing with different permissions.
217    */
218   public function testSearchResultsCommentAccess() {
219     $comment_body = 'Test comment body';
220     $this->commentSubject = 'Test comment subject';
221     $roles = $this->adminUser->getRoles(TRUE);
222     $this->adminRole = $roles[0];
223
224     // Create a node.
225     // Make preview optional.
226     $field = FieldConfig::loadByName('node', 'article', 'comment');
227     $field->setSetting('preview', DRUPAL_OPTIONAL);
228     $field->save();
229     $this->node = $this->drupalCreateNode(['type' => 'article']);
230
231     // Post a comment using 'Full HTML' text format.
232     $edit_comment = [];
233     $edit_comment['subject[0][value]'] = $this->commentSubject;
234     $edit_comment['comment_body[0][value]'] = '<h1>' . $comment_body . '</h1>';
235     $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit_comment, t('Save'));
236
237     $this->drupalLogout();
238     $this->setRolePermissions(RoleInterface::ANONYMOUS_ID);
239     $this->assertCommentAccess(FALSE, 'Anon user has search permission but no access comments permission, comments should not be indexed');
240
241     $this->setRolePermissions(RoleInterface::ANONYMOUS_ID, TRUE);
242     $this->assertCommentAccess(TRUE, 'Anon user has search permission and access comments permission, comments should be indexed');
243
244     $this->drupalLogin($this->adminUser);
245     $this->drupalGet('admin/people/permissions');
246
247     // Disable search access for authenticated user to test admin user.
248     $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID, FALSE, FALSE);
249
250     $this->setRolePermissions($this->adminRole);
251     $this->assertCommentAccess(FALSE, 'Admin user has search permission but no access comments permission, comments should not be indexed');
252
253     $this->drupalGet('node/' . $this->node->id());
254     $this->setRolePermissions($this->adminRole, TRUE);
255     $this->assertCommentAccess(TRUE, 'Admin user has search permission and access comments permission, comments should be indexed');
256
257     $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID);
258     $this->assertCommentAccess(FALSE, 'Authenticated user has search permission but no access comments permission, comments should not be indexed');
259
260     $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID, TRUE);
261     $this->assertCommentAccess(TRUE, 'Authenticated user has search permission and access comments permission, comments should be indexed');
262
263     // Verify that access comments permission is inherited from the
264     // authenticated role.
265     $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID, TRUE, FALSE);
266     $this->setRolePermissions($this->adminRole);
267     $this->assertCommentAccess(TRUE, 'Admin user has search permission and no access comments permission, but comments should be indexed because admin user inherits authenticated user\'s permission to access comments');
268
269     // Verify that search content permission is inherited from the authenticated
270     // role.
271     $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID, TRUE, TRUE);
272     $this->setRolePermissions($this->adminRole, TRUE, FALSE);
273     $this->assertCommentAccess(TRUE, 'Admin user has access comments permission and no search permission, but comments should be indexed because admin user inherits authenticated user\'s permission to search');
274   }
275
276   /**
277    * Set permissions for role.
278    */
279   public function setRolePermissions($rid, $access_comments = FALSE, $search_content = TRUE) {
280     $permissions = [
281       'access comments' => $access_comments,
282       'search content' => $search_content,
283     ];
284     user_role_change_permissions($rid, $permissions);
285   }
286
287   /**
288    * Update search index and search for comment.
289    */
290   public function assertCommentAccess($assume_access, $message) {
291     // Invoke search index update.
292     search_mark_for_reindex('node_search', $this->node->id());
293     $this->cronRun();
294
295     // Search for the comment subject.
296     $edit = [
297       'keys' => "'" . $this->commentSubject . "'",
298     ];
299     $this->drupalPostForm('search/node', $edit, t('Search'));
300
301     if ($assume_access) {
302       $expected_node_result = $this->assertText($this->node->label());
303       $expected_comment_result = $this->assertText($this->commentSubject);
304     }
305     else {
306       $expected_node_result = $this->assertText(t('Your search yielded no results.'));
307       $expected_comment_result = $this->assertText(t('Your search yielded no results.'));
308     }
309     $this->assertTrue($expected_node_result && $expected_comment_result, $message);
310   }
311
312   /**
313    * Verify that 'add new comment' does not appear in search results or index.
314    */
315   public function testAddNewComment() {
316     // Create a node with a short body.
317     $settings = [
318       'type' => 'article',
319       'title' => 'short title',
320       'body' => [['value' => 'short body text']],
321     ];
322
323     $user = $this->drupalCreateUser([
324       'search content',
325       'create article content',
326       'access content',
327       'post comments',
328       'access comments',
329     ]);
330     $this->drupalLogin($user);
331
332     $node = $this->drupalCreateNode($settings);
333     // Verify that if you view the node on its own page, 'add new comment'
334     // is there.
335     $this->drupalGet('node/' . $node->id());
336     $this->assertText(t('Add new comment'));
337
338     // Run cron to index this page.
339     $this->drupalLogout();
340     $this->cronRun();
341
342     // Search for 'comment'. Should be no results.
343     $this->drupalLogin($user);
344     $this->drupalPostForm('search/node', ['keys' => 'comment'], t('Search'));
345     $this->assertText(t('Your search yielded no results'));
346
347     // Search for the node title. Should be found, and 'Add new comment' should
348     // not be part of the search snippet.
349     $this->drupalPostForm('search/node', ['keys' => 'short'], t('Search'));
350     $this->assertText($node->label(), 'Search for keyword worked');
351     $this->assertNoText(t('Add new comment'));
352   }
353
354 }