You are here

solr_base_query.test in Apache Solr Search 5

File

tests/solr_base_query.test
View source
<?php

class DrupalSolrQueryTests extends DrupalTestCase {
  function get_info() {
    return array(
      'name' => 'Solr/query handling',
      'desc' => 'Throw various queries at the query object and make sure they all parse correctly.',
      'group' => 'Apache Solr tests',
    );
  }
  private $queries = array(
    'foo',
    'foo bar',
    'foo bar "hubba baz"',
    'uid:1',
    'uid:1 uid:2',
    'foo bar uid:1 uid:2',
    'foo bar "I love you"',
    'foo bar baz -hubba',
    'foo -bar -term:31',
    'foo*',
    'foo?',
    'somefield:"I love you"',
  );
  function testParseSimple() {
    $result = TRUE;
    foreach ($this->queries as $string) {
      $query =& apachesolr_drupal_query($string, TRUE);

      // force the query to be rebuilt without removing any fields.
      $query
        ->remove_field('fake-field-name');
      if (!$this
        ->assertEqual($string, $query
        ->get_query())) {
        $result = FALSE;
      }
    }
    return $result;
  }
  function testAddTerm() {
    $result = TRUE;
    foreach ($this->queries as $string) {
      $query =& apachesolr_drupal_query($string, TRUE);
      $query
        ->add_field('wham', '1');
      if (!$this
        ->assertEqual($string . ' wham:1', $query
        ->get_query())) {
        $result = FALSE;
      }
    }
    return $result;
  }
  function testRemoveTerm() {
    $result = TRUE;
    $string = 'foo';
    $query =& apachesolr_drupal_query($string, TRUE);
    $query
      ->remove_field('', 'foo');
    if (!$this
      ->assertEqual('foo', $query
      ->get_query())) {
      $result = FALSE;
    }
    $string = 'foo bar';
    $query =& apachesolr_drupal_query($string, TRUE);
    $query
      ->remove_field('', 'foo');
    if (!$this
      ->assertEqual('foo bar', $query
      ->get_query())) {
      $result = FALSE;
    }
    $string = 'foo uid:1 bar';
    $query =& apachesolr_drupal_query($string, TRUE);
    $query
      ->remove_field('uid', '1');
    if (!$this
      ->assertEqual('foo bar', $query
      ->get_query())) {
      $result = FALSE;
    }
    $string = 'foo uid:1 bar';
    $query =& apachesolr_drupal_query($string, TRUE);
    $query
      ->remove_field('uid');
    if (!$this
      ->assertEqual('foo bar', $query
      ->get_query())) {
      $result = FALSE;
    }
    $string = 'foo uid:1 bar uid:2 tid:3';
    $query =& apachesolr_drupal_query($string, TRUE);
    $query
      ->remove_field('uid', '1');

    /*// May not work because query doesn't necessarily get rebuilt in same order
      if (!$this->assertEqual('foo bar uid:2 tid:3', $query->get_query())) {
        $result = FALSE;
      }*/

    // Not very beautiful, but probably best way there is:
    $pass = TRUE;
    $components = array(
      'foo',
      'bar',
      'uid:2',
      'tid:3',
    );
    $q = $query
      ->get_query();
    if (count($components) != count(explode(' ', $q))) {
      $pass = FALSE;
    }
    else {
      foreach ($components as $s) {
        if (strpos($q, $s) === FALSE) {
          $pass = false;
          break;
        }
      }
    }
    if ($pass) {
      $this
        ->assertEqual($q, $q);
    }
    else {
      $this
        ->assertEqual('foo bar tid:3 uid:2', $q);
      $result = FALSE;
    }
    $string = 'foo uid:1 bar uid:2 tid:3';
    $query =& apachesolr_drupal_query($string, TRUE);
    $query
      ->remove_field('uid');
    if (!$this
      ->assertEqual('foo bar tid:3', $query
      ->get_query())) {
      $result = FALSE;
    }
    return $result;
  }

}

Classes