Chris@17: drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
Chris@17: $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
Chris@17:
Chris@17: $full_html_format = FilterFormat::create([
Chris@17: 'format' => 'full_html',
Chris@17: 'name' => 'Full HTML',
Chris@17: 'weight' => 1,
Chris@17: 'filters' => [],
Chris@17: ]);
Chris@17: $full_html_format->save();
Chris@17:
Chris@17: // Create and log in an administrative user having access to the Full HTML
Chris@17: // text format.
Chris@17: $permissions = [
Chris@17: 'administer filters',
Chris@17: $full_html_format->getPermissionName(),
Chris@17: 'administer permissions',
Chris@17: 'create page content',
Chris@17: 'post comments',
Chris@17: 'skip comment approval',
Chris@17: 'access comments',
Chris@17: ];
Chris@17: $this->adminUser = $this->drupalCreateUser($permissions);
Chris@17: $this->drupalLogin($this->adminUser);
Chris@17: // Add a comment field.
Chris@17: $this->addDefaultCommentField('node', 'article');
Chris@17: }
Chris@17:
Chris@17: /**
Chris@17: * Verify that comments are rendered using proper format in search results.
Chris@17: */
Chris@17: public function testSearchResultsComment() {
Chris@17: $node_storage = $this->container->get('entity.manager')->getStorage('node');
Chris@17: // Create basic_html format that escapes all HTML.
Chris@17: $basic_html_format = FilterFormat::create([
Chris@17: 'format' => 'basic_html',
Chris@17: 'name' => 'Basic HTML',
Chris@17: 'weight' => 1,
Chris@17: 'filters' => [
Chris@17: 'filter_html_escape' => ['status' => 1],
Chris@17: ],
Chris@17: 'roles' => [RoleInterface::AUTHENTICATED_ID],
Chris@17: ]);
Chris@17: $basic_html_format->save();
Chris@17:
Chris@17: $comment_body = 'Test comment body';
Chris@17:
Chris@17: // Make preview optional.
Chris@17: $field = FieldConfig::loadByName('node', 'article', 'comment');
Chris@17: $field->setSetting('preview', DRUPAL_OPTIONAL);
Chris@17: $field->save();
Chris@17:
Chris@17: // Allow anonymous users to search content.
Chris@17: $edit = [
Chris@17: RoleInterface::ANONYMOUS_ID . '[search content]' => 1,
Chris@17: RoleInterface::ANONYMOUS_ID . '[access comments]' => 1,
Chris@17: RoleInterface::ANONYMOUS_ID . '[post comments]' => 1,
Chris@17: ];
Chris@17: $this->drupalPostForm('admin/people/permissions', $edit, t('Save permissions'));
Chris@17:
Chris@17: // Create a node.
Chris@17: $node = $this->drupalCreateNode(['type' => 'article']);
Chris@17: // Post a comment using 'Full HTML' text format.
Chris@17: $edit_comment = [];
Chris@17: $edit_comment['subject[0][value]'] = 'Test comment subject';
Chris@17: $edit_comment['comment_body[0][value]'] = '
' . $comment_body . '
';
Chris@17: $full_html_format_id = 'full_html';
Chris@17: $edit_comment['comment_body[0][format]'] = $full_html_format_id;
Chris@17: $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit_comment, t('Save'));
Chris@17:
Chris@17: // Post a comment with an evil script tag in the comment subject and a
Chris@17: // script tag nearby a keyword in the comment body. Use the 'FULL HTML' text
Chris@17: // format so the script tag stored.
Chris@17: $edit_comment2 = [];
Chris@17: $edit_comment2['subject[0][value]'] = "";
Chris@17: $edit_comment2['comment_body[0][value]'] = "nearbykeyword";
Chris@17: $edit_comment2['comment_body[0][format]'] = $full_html_format_id;
Chris@17: $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit_comment2, t('Save'));
Chris@17:
Chris@17: // Post a comment with a keyword inside an evil script tag in the comment
Chris@17: // body. Use the 'FULL HTML' text format so the script tag is stored.
Chris@17: $edit_comment3 = [];
Chris@17: $edit_comment3['subject[0][value]'] = 'asubject';
Chris@17: $edit_comment3['comment_body[0][value]'] = "";
Chris@17: $edit_comment3['comment_body[0][format]'] = $full_html_format_id;
Chris@17: $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit_comment3, t('Save'));
Chris@17:
Chris@17: // Invoke search index update.
Chris@17: $this->drupalLogout();
Chris@17: $this->cronRun();
Chris@17:
Chris@17: // Search for the comment subject.
Chris@17: $edit = [
Chris@17: 'keys' => "'" . $edit_comment['subject[0][value]'] . "'",
Chris@17: ];
Chris@17: $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@17: $node_storage->resetCache([$node->id()]);
Chris@17: $node2 = $node_storage->load($node->id());
Chris@17: $this->assertText($node2->label(), 'Node found in search results.');
Chris@17: $this->assertText($edit_comment['subject[0][value]'], 'Comment subject found in search results.');
Chris@17:
Chris@17: // Search for the comment body.
Chris@17: $edit = [
Chris@17: 'keys' => "'" . $comment_body . "'",
Chris@17: ];
Chris@17: $this->drupalPostForm(NULL, $edit, t('Search'));
Chris@17: $this->assertText($node2->label(), 'Node found in search results.');
Chris@17:
Chris@17: // Verify that comment is rendered using proper format.
Chris@17: $this->assertText($comment_body, 'Comment body text found in search results.');
Chris@17: $this->assertNoRaw(t('n/a'), 'HTML in comment body is not hidden.');
Chris@17: $this->assertNoEscaped($edit_comment['comment_body[0][value]'], 'HTML in comment body is not escaped.');
Chris@17:
Chris@17: // Search for the evil script comment subject.
Chris@17: $edit = [
Chris@17: 'keys' => 'subjectkeyword',
Chris@17: ];
Chris@17: $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@17:
Chris@17: // Verify the evil comment subject is escaped in search results.
Chris@17: $this->assertRaw('<script>alert('subjectkeyword');');
Chris@17: $this->assertNoRaw('