function SearchRankingTest::testRankings

File

drupal/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php, line 27
Definition of Drupal\search\Tests\SearchRankingTest.

Class

SearchRankingTest

Namespace

Drupal\search\Tests

Code

function testRankings() {

  // Login with sufficient privileges.
  $this
    ->drupalLogin($this
    ->drupalCreateUser(array(
    'post comments',
    'skip comment approval',
    'create page content',
  )));

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

  // Create nodes for testing.
  foreach ($node_ranks as $node_rank) {
    $settings = array(
      'type' => 'page',
      'title' => 'Drupal rocks',
      'body' => array(
        LANGUAGE_NOT_SPECIFIED => array(
          array(
            'value' => "Drupal'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_NOT_SPECIFIED][0]['value'] .= " really rocks";
            break;
          case 'recent':
            $settings['created'] = REQUEST_TIME + 3600;
            break;
          case 'comments':
            $settings['comment'] = 2;
            break;
        }
      }
      $nodes[$node_rank][$num] = $this
        ->drupalCreateNode($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['subject'] = 'my comment title';
  $edit['comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][value]'] = 'some random comment';
  $this
    ->drupalGet('comment/reply/' . $nodes['comments'][1]->nid);
  $this
    ->drupalPost(NULL, $edit, t('Preview'));
  $this
    ->drupalPost(NULL, $edit, t('Save'));

  // Enable counting of statistics.
  config('statistics.settings')
    ->set('count_content_views', 1)
    ->save();

  // Then View one of the nodes a bunch of times.
  // Manually calling statistics.php, simulating ajax behavior.
  $nid = $nodes['views'][1]->nid;
  $post = http_build_query(array(
    'nid' => $nid,
  ));
  $headers = array(
    'Content-Type' => 'application/x-www-form-urlencoded',
  );
  global $base_url;
  $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
  for ($i = 0; $i < 5; $i++) {
    drupal_http_request($stats_path, array(
      'method' => 'POST',
      'data' => $post,
      'headers' => $headers,
      'timeout' => 10000,
    ));
  }

  // Test each of the possible rankings.
  foreach ($node_ranks as $node_rank) {

    // Disable all relevancy rankings except the one we are testing.
    foreach ($node_ranks as $var) {
      variable_set('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.');
  }
}