1 search.test SearchCommentTestCase::testSearchResultsComment()

Verify that comments are rendered using proper format in search results.

File

core/modules/search/tests/search.test, line 758
Tests for search.module.

Class

SearchCommentTestCase
Test integration searching comments.

Code

function testSearchResultsComment() {
  $comment_body = 'Test comment body';

  // Allow anonymous users to search content.
  $edit = array(
    BACKDROP_ANONYMOUS_ROLE . '[search content]' => 1,
    BACKDROP_ANONYMOUS_ROLE . '[access comments]' => 1,
    BACKDROP_ANONYMOUS_ROLE . '[post comments]' => 1,
  );
  $this->backdropPost('admin/config/people/permissions', $edit, t('Save permissions'));

  // Create a node.
  $node = $this->backdropCreateNode(array('type' => 'post'));
  // Post a comment using 'Raw HTML' (full_html) text format.
  $edit_comment = array();
  $edit_comment['subject'] = 'Test comment subject';
  $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]'] = '<h1>' . $comment_body . '</h1>';
  $full_html_format_id = 'full_html';
  $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][format]'] = $full_html_format_id;
  $this->backdropPost('comment/reply/' . $node->nid, $edit_comment, t('Save'));

  // Invoke search index update.
  $this->backdropLogout();
  $this->cronRun();

  // Search for the comment subject.
  $edit = array(
    'search_block_form' => "'" . $edit_comment['subject'] . "'",
  );
  $this->backdropPost('user', $edit, t('Search'));
  $this->assertText($node->title, 'Node found in search results.');
  $this->assertText($edit_comment['subject'], 'Comment subject found in search results.');

  // Search for the comment body.
  $edit = array(
    'search_block_form' => "'" . $comment_body . "'",
  );
  $this->backdropPost('user', $edit, t('Search'));
  $this->assertText($node->title, t('Node found in search results.'));

  // Verify that comment is rendered using proper format.
  $this->assertText($comment_body, t('Comment body text found in search results.'));
  $this->assertNoRaw(t('n/a'), t('HTML in comment body is not hidden.'));
  $this->assertNoRaw(check_plain($edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]']), 'HTML in comment body is not escaped.');

  // Hide comments.
  $this->backdropLogin($this->admin_user);
  $node->comment = 0;
  $node->save();

  // Invoke search index update.
  $this->backdropLogout();
  $this->cronRun();

  // Search for $title.
  $this->backdropPost('user', $edit, t('Search'));
  $this->assertNoText($comment_body, 'Comment body text not found in search results.');
}