1 search.test SearchRankingTestCase::testRankings()

File

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

Class

SearchRankingTestCase
Indexes content and tests ranking factors.

Code

function testRankings() {
  // Login with sufficient privileges.
  $this->backdropLogin($this->backdropCreateUser(array('skip comment approval', 'create page content')));

  // Build a list of the rankings to test.
  $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments');

  // Create nodes for testing.
  foreach ($node_ranks as $node_rank) {
    $settings = array(
      'type' => 'page',
      'title' => 'Backdrop rocks',
      'body' => array(LANGUAGE_NONE => array(array('value' => "Backdrop's search rocks"))),
    );
    foreach (array(0, 1) as $num) {
      if ($num == 1) {
        switch ($node_rank) {
          case 'sticky':
          case 'promote':
            $settings[$node_rank] = 1;
            break;
          case 'relevance':
            $settings['body'][LANGUAGE_NONE][0]['value'] .= " really rocks";
            break;
          case 'recent':
            $settings['created'] = REQUEST_TIME + 3600;
            break;
          case 'comments':
            $settings['comment'] = 2;
            break;
        }
      }
      $nodes[$node_rank][$num] = $this->backdropCreateNode($settings);
    }
  }

  // Update the search index.
  module_invoke_all('update_index');
  search_update_totals();

  // Refresh variables after the treatment.
  $this->refreshVariables();

  // Add a comment to one of the nodes.
  $edit = array();
  $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = 'some random comment';
  $this->backdropGet('comment/reply/' . $nodes['comments'][1]->nid);
  $this->backdropPost(NULL, $edit, t('Preview'));
  $this->backdropPost(NULL, $edit, t('Save'));

  // Test each of the possible rankings.
  foreach ($node_ranks as $node_rank) {
    // Disable all relevance rankings except the one we are testing.
    foreach ($node_ranks as $var) {
      config_set('search.settings', 'node_rank_' . $var, $var == $node_rank ? 10 : 0);
    }

    // Do the search and assert the results.
    $set = node_search_execute('rocks');
    $this->assertEqual($set[0]['node']->nid, $nodes[$node_rank][1]->nid, 'Search ranking "' . $node_rank . '" order.');
  }
}