function SearchCommentTestCase::testSearchResultsComment

7.x search.test SearchCommentTestCase::testSearchResultsComment()

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

File

drupal-7.x/modules/search/search.test, line 756
Tests for search.module.

Class

SearchCommentTestCase
Test integration searching comments.

Code

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

  variable_set('comment_preview_article', DRUPAL_OPTIONAL);
  // Enable check_plain() for 'Filtered HTML' text format.
  $filtered_html_format_id = 'filtered_html';
  $edit = array(
    'filters[filter_html_escape][status]' => TRUE,
  );
  $this->drupalPost('admin/config/content/formats/' . $filtered_html_format_id, $edit, t('Save configuration'));
  // Allow anonymous users to search content.
  $edit = array(
    DRUPAL_ANONYMOUS_RID . '[search content]' => 1,
    DRUPAL_ANONYMOUS_RID . '[access comments]' => 1,
    DRUPAL_ANONYMOUS_RID . '[post comments]' => 1,
  );
  $this->drupalPost('admin/people/permissions', $edit, t('Save permissions'));

  // Create a node.
  $node = $this->drupalCreateNode(array('type' => 'article'));
  // Post a comment using '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->drupalPost('comment/reply/' . $node->nid, $edit_comment, t('Save'));

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

  // Search for the comment subject.
  $edit = array(
    'search_block_form' => "'" . $edit_comment['subject'] . "'",
  );
  $this->drupalPost('', $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->drupalPost('', $edit, t('Search'));
  $this->assertText($node->title, 'Node found in search results.');

  // Verify that comment is rendered using proper format.
  $this->assertText($comment_body, 'Comment body text found in search results.');
  $this->assertNoRaw(t('n/a'), '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->drupalLogin($this->admin_user);
  $node->comment = 0;
  node_save($node);

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

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