3 namespace Drupal\Tests\search\Functional;
5 use Behat\Mink\Exception\ResponseTextException;
6 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
7 use Drupal\comment\Tests\CommentTestTrait;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\Tests\BrowserTestBase;
10 use Drupal\Tests\Traits\Core\CronRunTrait;
11 use Drupal\user\RoleInterface;
12 use Drupal\filter\Entity\FilterFormat;
15 * Tests integration searching comments.
19 class SearchCommentTest extends BrowserTestBase {
27 protected static $modules = ['filter', 'node', 'comment', 'search'];
30 * Test subject for comments.
34 protected $commentSubject;
37 * ID for the administrator role.
44 * A user with various administrative permissions.
46 * @var \Drupal\user\UserInterface
51 * Test node for searching.
53 * @var \Drupal\node\NodeInterface
57 protected function setUp() {
60 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
61 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
63 $full_html_format = FilterFormat::create([
64 'format' => 'full_html',
65 'name' => 'Full HTML',
69 $full_html_format->save();
71 // Create and log in an administrative user having access to the Full HTML
75 $full_html_format->getPermissionName(),
76 'administer permissions',
77 'create page content',
79 'skip comment approval',
82 $this->adminUser = $this->drupalCreateUser($permissions);
83 $this->drupalLogin($this->adminUser);
84 // Add a comment field.
85 $this->addDefaultCommentField('node', 'article');
89 * Verify that comments are rendered using proper format in search results.
91 public function testSearchResultsComment() {
92 $node_storage = $this->container->get('entity.manager')->getStorage('node');
93 // Create basic_html format that escapes all HTML.
94 $basic_html_format = FilterFormat::create([
95 'format' => 'basic_html',
96 'name' => 'Basic HTML',
99 'filter_html_escape' => ['status' => 1],
101 'roles' => [RoleInterface::AUTHENTICATED_ID],
103 $basic_html_format->save();
105 $comment_body = 'Test comment body';
107 // Make preview optional.
108 $field = FieldConfig::loadByName('node', 'article', 'comment');
109 $field->setSetting('preview', DRUPAL_OPTIONAL);
112 // Allow anonymous users to search content.
114 RoleInterface::ANONYMOUS_ID . '[search content]' => 1,
115 RoleInterface::ANONYMOUS_ID . '[access comments]' => 1,
116 RoleInterface::ANONYMOUS_ID . '[post comments]' => 1,
118 $this->drupalPostForm('admin/people/permissions', $edit, t('Save permissions'));
121 $node = $this->drupalCreateNode(['type' => 'article']);
122 // Post a comment using 'Full HTML' text format.
124 $edit_comment['subject[0][value]'] = 'Test comment subject';
125 $edit_comment['comment_body[0][value]'] = '<h1>' . $comment_body . '</h1>';
126 $full_html_format_id = 'full_html';
127 $edit_comment['comment_body[0][format]'] = $full_html_format_id;
128 $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit_comment, t('Save'));
130 // Post a comment with an evil script tag in the comment subject and a
131 // script tag nearby a keyword in the comment body. Use the 'FULL HTML' text
132 // format so the script tag stored.
134 $edit_comment2['subject[0][value]'] = "<script>alert('subjectkeyword');</script>";
135 $edit_comment2['comment_body[0][value]'] = "nearbykeyword<script>alert('somethinggeneric');</script>";
136 $edit_comment2['comment_body[0][format]'] = $full_html_format_id;
137 $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit_comment2, t('Save'));
139 // Post a comment with a keyword inside an evil script tag in the comment
140 // body. Use the 'FULL HTML' text format so the script tag is stored.
142 $edit_comment3['subject[0][value]'] = 'asubject';
143 $edit_comment3['comment_body[0][value]'] = "<script>alert('insidekeyword');</script>";
144 $edit_comment3['comment_body[0][format]'] = $full_html_format_id;
145 $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit_comment3, t('Save'));
147 // Invoke search index update.
148 $this->drupalLogout();
151 // Search for the comment subject.
153 'keys' => "'" . $edit_comment['subject[0][value]'] . "'",
155 $this->drupalPostForm('search/node', $edit, t('Search'));
156 $node_storage->resetCache([$node->id()]);
157 $node2 = $node_storage->load($node->id());
158 $this->assertText($node2->label(), 'Node found in search results.');
159 $this->assertText($edit_comment['subject[0][value]'], 'Comment subject found in search results.');
161 // Search for the comment body.
163 'keys' => "'" . $comment_body . "'",
165 $this->drupalPostForm(NULL, $edit, t('Search'));
166 $this->assertText($node2->label(), 'Node found in search results.');
168 // Verify that comment is rendered using proper format.
169 $this->assertText($comment_body, 'Comment body text found in search results.');
170 $this->assertNoRaw(t('n/a'), 'HTML in comment body is not hidden.');
171 $this->assertNoEscaped($edit_comment['comment_body[0][value]'], 'HTML in comment body is not escaped.');
173 // Search for the evil script comment subject.
175 'keys' => 'subjectkeyword',
177 $this->drupalPostForm('search/node', $edit, t('Search'));
179 // Verify the evil comment subject is escaped in search results.
180 $this->assertRaw('<script>alert('<strong>subjectkeyword</strong>');');
181 $this->assertNoRaw('<script>');
183 // Search for the keyword near the evil script tag in the comment body.
185 'keys' => 'nearbykeyword',
187 $this->drupalPostForm('search/node', $edit, t('Search'));
189 // Verify that nearby script tag in the evil comment body is stripped from
191 $this->assertRaw('<strong>nearbykeyword</strong>');
192 $this->assertNoRaw('<script>');
194 // Search for contents inside the evil script tag in the comment body.
196 'keys' => 'insidekeyword',
198 $this->drupalPostForm('search/node', $edit, t('Search'));
200 // @todo Verify the actual search results.
201 // https://www.drupal.org/node/2551135
203 // Verify there is no script tag in search results.
204 $this->assertNoRaw('<script>');
207 $this->drupalLogin($this->adminUser);
208 $node->set('comment', CommentItemInterface::HIDDEN);
211 // Invoke search index update.
212 $this->drupalLogout();
215 // Search for $title.
216 $this->drupalPostForm('search/node', $edit, t('Search'));
217 $this->assertText(t('Your search yielded no results.'));
221 * Verify access rules for comment indexing with different permissions.
223 public function testSearchResultsCommentAccess() {
224 $comment_body = 'Test comment body';
225 $this->commentSubject = 'Test comment subject';
226 $roles = $this->adminUser->getRoles(TRUE);
227 $this->adminRole = $roles[0];
230 // Make preview optional.
231 $field = FieldConfig::loadByName('node', 'article', 'comment');
232 $field->setSetting('preview', DRUPAL_OPTIONAL);
234 $this->node = $this->drupalCreateNode(['type' => 'article']);
236 // Post a comment using 'Full HTML' text format.
238 $edit_comment['subject[0][value]'] = $this->commentSubject;
239 $edit_comment['comment_body[0][value]'] = '<h1>' . $comment_body . '</h1>';
240 $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit_comment, t('Save'));
242 $this->drupalLogout();
243 $this->setRolePermissions(RoleInterface::ANONYMOUS_ID);
244 $this->assertCommentAccess(FALSE, 'Anon user has search permission but no access comments permission, comments should not be indexed');
246 $this->setRolePermissions(RoleInterface::ANONYMOUS_ID, TRUE);
247 $this->assertCommentAccess(TRUE, 'Anon user has search permission and access comments permission, comments should be indexed');
249 $this->drupalLogin($this->adminUser);
250 $this->drupalGet('admin/people/permissions');
252 // Disable search access for authenticated user to test admin user.
253 $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID, FALSE, FALSE);
255 $this->setRolePermissions($this->adminRole);
256 $this->assertCommentAccess(FALSE, 'Admin user has search permission but no access comments permission, comments should not be indexed');
258 $this->drupalGet('node/' . $this->node->id());
259 $this->setRolePermissions($this->adminRole, TRUE);
260 $this->assertCommentAccess(TRUE, 'Admin user has search permission and access comments permission, comments should be indexed');
262 $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID);
263 $this->assertCommentAccess(FALSE, 'Authenticated user has search permission but no access comments permission, comments should not be indexed');
265 $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID, TRUE);
266 $this->assertCommentAccess(TRUE, 'Authenticated user has search permission and access comments permission, comments should be indexed');
268 // Verify that access comments permission is inherited from the
269 // authenticated role.
270 $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID, TRUE, FALSE);
271 $this->setRolePermissions($this->adminRole);
272 $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');
274 // Verify that search content permission is inherited from the authenticated
276 $this->setRolePermissions(RoleInterface::AUTHENTICATED_ID, TRUE, TRUE);
277 $this->setRolePermissions($this->adminRole, TRUE, FALSE);
278 $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');
282 * Set permissions for role.
284 public function setRolePermissions($rid, $access_comments = FALSE, $search_content = TRUE) {
286 'access comments' => $access_comments,
287 'search content' => $search_content,
289 user_role_change_permissions($rid, $permissions);
293 * Update search index and search for comment.
295 public function assertCommentAccess($assume_access, $message) {
296 // Invoke search index update.
297 search_mark_for_reindex('node_search', $this->node->id());
300 // Search for the comment subject.
302 'keys' => "'" . $this->commentSubject . "'",
304 $this->drupalPostForm('search/node', $edit, t('Search'));
307 if ($assume_access) {
308 $this->assertSession()->pageTextContains($this->node->label());
309 $this->assertSession()->pageTextContains($this->commentSubject);
312 $this->assertSession()->pageTextContains(t('Your search yielded no results.'));
315 catch (ResponseTextException $exception) {
316 $this->fail($message);
321 * Verify that 'add new comment' does not appear in search results or index.
323 public function testAddNewComment() {
324 // Create a node with a short body.
327 'title' => 'short title',
328 'body' => [['value' => 'short body text']],
331 $user = $this->drupalCreateUser([
333 'create article content',
338 $this->drupalLogin($user);
340 $node = $this->drupalCreateNode($settings);
341 // Verify that if you view the node on its own page, 'add new comment'
343 $this->drupalGet('node/' . $node->id());
344 $this->assertText(t('Add new comment'));
346 // Run cron to index this page.
347 $this->drupalLogout();
350 // Search for 'comment'. Should be no results.
351 $this->drupalLogin($user);
352 $this->drupalPostForm('search/node', ['keys' => 'comment'], t('Search'));
353 $this->assertText(t('Your search yielded no results'));
355 // Search for the node title. Should be found, and 'Add new comment' should
356 // not be part of the search snippet.
357 $this->drupalPostForm('search/node', ['keys' => 'short'], t('Search'));
358 $this->assertText($node->label(), 'Search for keyword worked');
359 $this->assertNoText(t('Add new comment'));