You are here

class SolrBaseQueryTests in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 tests/solr_base_query.test \SolrBaseQueryTests
  2. 6.3 tests/solr_base_query.test \SolrBaseQueryTests

Unit tests for query object methods.

Hierarchy

Expanded class hierarchy of SolrBaseQueryTests

File

tests/solr_base_query.test, line 6

View source
class SolrBaseQueryTests extends DrupalUnitTestCase {
  public static function getInfo() {
    return array(
      'name' => 'SolrBaseQuery Unit tests',
      'description' => 'Unit Tests for queries.',
      'group' => 'ApacheSolr',
    );
  }
  function setUp() {
    parent::setUp();
    require_once dirname(dirname(realpath(__FILE__))) . '/apachesolr.module';
    require_once dirname(dirname(realpath(__FILE__))) . '/apachesolr.interface.inc';
    require_once dirname(dirname(realpath(__FILE__))) . '/Solr_Base_Query.php';
    require_once dirname(dirname(realpath(__FILE__))) . '/Drupal_Apache_Solr_Service.php';
    require_once dirname(dirname(realpath(__FILE__))) . '/tests/Dummy_Solr.php';
  }

  /**
   * Helper function to simulate the auto loading and other non-needed functions
   * that otherwise require a database
   * @see apachesolr_drupal_query().
   * @return SolrBaseQuery
   */
  private function _apachesolr_drupal_query($name, $params = array(), $solrsort = '', $base_path = '', $solr = NULL) {
    if (empty($solr)) {
      $solr = new DummySolr(NULL);
    }
    return new SolrBaseQuery($name, $solr, $params, $solrsort, $base_path);
  }
  private function _apachesolr_drupal_subquery($operator = 'OR') {
    return new SolrFilterSubQuery($operator);
  }

  /**
   * Test ordering of parsed filter positions.
   *
   * Regression test for http://drupal.org/node/891962
   */
  function testParseFilters() {
    $fq = array(
      'tid:3',
      'sort_label:hello',
      'tid:11',
      'tid:1',
      'tid:12',
      'label:hello',
    );

    // Setup dummy Solr object.
    $query = $this
      ->_apachesolr_drupal_query("apachesolr_tests", array(
      'q' => 'mykeys',
      'fq' => $fq,
    ), 'sort_label asc', 'search/test');

    // Check setSolrSort().
    $this
      ->assertEqual(array(
      'solrsort' => 'sort_label asc',
    ), $query
      ->getSolrsortUrlQuery());
    $query
      ->setSolrsort('sort_name', 'desc');
    $this
      ->assertEqual(array(
      'solrsort' => 'sort_name desc',
    ), $query
      ->getSolrsortUrlQuery());
    $query
      ->setSolrsort('score', 'desc');
    $this
      ->assertEqual(array(), $query
      ->getSolrsortUrlQuery());

    // We cannot set a sort if the sort was not made available.
    $query
      ->setSolrsort('geodist()', 'asc');
    $this
      ->assertNotEqual(array(
      'solrsort' => 'geodist() asc',
    ), $query
      ->getSolrsortUrlQuery(), t('Sort was not applied as it was not available'));

    // Set the sort after it was made available.
    $query
      ->setAvailableSort('geodist()', array(
      'title' => 'Distance',
      'default' => 'asc',
    ));
    $query
      ->setSolrsort('geodist()', 'asc');
    $this
      ->assertEqual(array(
      'solrsort' => 'geodist() asc',
    ), $query
      ->getSolrsortUrlQuery());

    // Check escaping of the setSolrSort functinality
    // Check getPath() functionality
    $this
      ->assertEqual('search/test/mykeys', $query
      ->getPath());
    $this
      ->assertEqual('search/test/newkeys', $query
      ->getPath('newkeys'));

    // Check hasFilter functionality
    $this
      ->assertFalse($query
      ->hasFilter('label', 'Jello'), "hasFilter('label', 'Jello') is FALSE");
    $this
      ->assertTrue($query
      ->hasFilter('label', 'hello'), "hasFilter('label', 'hello') is TRUE");
    $this
      ->assertTrue($query
      ->hasFilter('label', 'hello', FALSE), "hasFilter('label', 'hello', FALSE) is TRUE");
    $this
      ->assertFalse($query
      ->hasFilter('label', 'hello', TRUE), "hasFilter('label', 'hello', TRUE) is FALSE");
    $filters = $query
      ->getFilters();
    $this
      ->assertEqual(count($filters), 6, count($filters) . ' filters found, expected 6 filters');

    // Check positions of filters
    foreach ($fq as $idx => $filter) {
      $this
        ->assertEqual($filter, $query
        ->makeFilterQuery($filters[$idx]));
    }

    // Check that the query string is re-assembled correctly
    $this
      ->assertEqual($fq, $query
      ->getParam('fq'));
    $this
      ->assertEqual('mykeys', $query
      ->getParam('q'));
    $query
      ->removeFilter('tid', '11');
    $filters = $query
      ->getFilters();
    $this
      ->assertEqual(count($filters), 5, count($filters) . ' filters found, expected 5 filters');
    $this
      ->assertEqual(array(
      'tid:3',
      'sort_label:hello',
      'tid:1',
      'tid:12',
      'label:hello',
    ), $query
      ->getParam('fq'));
    $query
      ->removeFilter('tid');
    $filters = $query
      ->getFilters();
    $this
      ->assertEqual(count($filters), 2, count($filters) . ' filters found, expected 2 filters');
    $this
      ->assertEqual(array(
      'sort_label:hello',
      'label:hello',
    ), $query
      ->getParam('fq'));
    $subquery = $this
      ->_apachesolr_drupal_subquery();
    $subquery
      ->addFilter('access__all', 0);
    $subquery
      ->addFilter('hash', 'randomhash');
    $query
      ->addFilterSubQuery($subquery);
    $this
      ->assertEqual(count($query
      ->getParam('fq')), 3, count($query
      ->getParam('fq')) . ' fq params found, expected 3 after adding subquery');
    $this
      ->assertEqual(array(
      'sort_label:hello',
      'label:hello',
      '(access__all:0 OR hash:randomhash' . ')',
    ), $query
      ->getParam('fq'));
  }
  function testAddParams() {
    $examples = array();
    $examples['{!cache=false}inStock:true'] = array(
      '#local' => 'cache=false',
      '#exclude' => FALSE,
      '#name' => 'inStock',
      '#value' => 'true',
    );
    $examples['{!frange l=1 u=4 cache=false}sqrt(popularity)'] = array(
      '#local' => 'frange l=1 u=4 cache=false',
      '#exclude' => FALSE,
      '#name' => NULL,
      '#value' => 'sqrt(popularity)',
    );
    $examples['{!cache=false cost=5}inStock:true'] = array(
      '#local' => 'cache=false cost=5',
      '#exclude' => FALSE,
      '#name' => 'inStock',
      '#value' => 'true',
    );
    $examples['{!tag=impala}model:Impala'] = array(
      '#local' => 'tag=impala',
      '#exclude' => FALSE,
      '#name' => 'model',
      '#value' => 'Impala',
    );
    $examples['{!anything that appears to be local}'] = array(
      '#local' => 'anything that appears to be local',
      '#exclude' => FALSE,
      '#name' => NULL,
      '#value' => NULL,
    );
    $examples['bundle:(article OR page)'] = array(
      '#local' => NULL,
      '#exclude' => FALSE,
      '#name' => 'bundle',
      '#value' => '(article OR page)',
    );
    $examples['-bundle:(article OR page)'] = array(
      '#local' => NULL,
      '#exclude' => TRUE,
      '#name' => 'bundle',
      '#value' => '(article OR page)',
    );
    $examples['-{!anything that appears to be local}'] = array(
      '#local' => 'anything that appears to be local',
      '#exclude' => TRUE,
      '#name' => NULL,
      '#value' => NULL,
    );
    $examples['title:"double words"'] = array(
      '#local' => NULL,
      '#exclude' => FALSE,
      '#name' => 'title',
      '#value' => '"double words"',
    );
    $examples['field_date:[1970-12-31T23:59:59Z TO NOW]'] = array(
      '#local' => NULL,
      '#exclude' => FALSE,
      '#name' => 'field_date',
      '#value' => '[1970-12-31T23:59:59Z TO NOW]',
    );
    $query = $this
      ->_apachesolr_drupal_query("apachesolr_tests");
    foreach ($examples as $fq => $example) {
      $name = !empty($example['#name']) ? $example['#name'] : '_QUERY_';
      $value = !empty($example['#value']) ? $example['#value'] : '_VALUE_';
      $filter = $name . ':' . $value;

      // Check if filter is seen as a valid one
      $message = t('Filter (@fq) is Valid', array(
        '@fq' => $filter,
      ));
      $this
        ->assertTrue($query
        ->validFilterValue($filter), $message);
      $query
        ->addParam('fq', $fq);

      // Check if filter was added
      $message = t('hasFilter(@name, @value) is True', array(
        '@name' => $example['#name'],
        '@value' => $example['#value'],
      ));
      $this
        ->assertTrue($query
        ->hasFilter($example['#name'], $example['#value'], $example['#exclude']), $message);
      $filters = $query
        ->getFilters();
      $filter = reset($filters);
      $message = t('The filter "@fq" was added with all the given properties', array(
        '@fq' => $fq,
      ));
      $this
        ->assertTrue(!array_diff($example, $filter), $message);
      $query
        ->removeFilter($example['#name']);
      $message = t('The filter "@fq" was correctly removed', array(
        '@fq' => $fq,
      ));
      $this
        ->assertFalse($query
        ->hasFilter($example['#name'], $example['#value'], $example['#exclude']), $message);

      // Since we cannot remove filters without names yet we have to clear the whole fq array
      $query
        ->removeParam('fq');

      // Check the ones without the name also
    }

    // Check for invalid combinations
    $bad_examples['wrong name:"double words"'] = array(
      '#local' => NULL,
      '#exclude' => FALSE,
      '#name' => 'wrong name',
      '#value' => '"double words"',
    );
    $bad_examples['field_date:[1970-12-31 TO NOW]'] = array(
      '#local' => NULL,
      '#exclude' => FALSE,
      '#name' => 'field_date',
      '#value' => '[1970-12-31 TO NOW]',
    );
    $bad_examples['bundle:((article OR page)]'] = array(
      '#local' => NULL,
      '#exclude' => FALSE,
      '#name' => 'bundle',
      '#value' => '((article OR page)]',
    );
    foreach ($bad_examples as $fq => $example) {
      $name = !empty($example['#name']) ? $example['#name'] : '_QUERY_';
      $value = !empty($example['#value']) ? $example['#value'] : '_VALUE_';
      $filter = $name . ':' . $value;

      // Check if filter is seen as a valid one
      $message = t('Filter (@fq) is not Valid', array(
        '@fq' => $filter,
      ));
      $this
        ->assertFalse($query
        ->validFilterValue($filter), $message);
    }

    // Check parameter normalization.
    $query
      ->addParam('spellcheck', TRUE);
    $this
      ->assertTrue($query
      ->getParam('spellcheck') === 'true', "TRUE normalized to string 'true'");
    $query
      ->replaceParam('spellcheck', FALSE);
    $this
      ->assertTrue($query
      ->getParam('spellcheck') === 'false', "FALSE normalized to string 'false'");
    $query
      ->addParam('fl', array(
      ' x ',
      TRUE,
    ));
    $this
      ->assertTrue($query
      ->getParam('fl') === array(
      'x',
      'true',
    ), "Array of params all normalized");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$setup protected property Flag to indicate whether the test has been set up.
DrupalTestCase::$setupDatabasePrefix protected property
DrupalTestCase::$setupEnvironment protected property
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::$useSetupInstallationCache public property Whether to cache the installation part of the setUp() method.
DrupalTestCase::$useSetupModulesCache public property Whether to cache the modules installation part of the setUp() method.
DrupalTestCase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion. 1
DrupalTestCase::errorHandler public function Handle errors during test runs. 1
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs a verbose message in a text file.
DrupalUnitTestCase::tearDown protected function 1
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct
SolrBaseQueryTests::getInfo public static function
SolrBaseQueryTests::setUp function Sets up unit test environment. Overrides DrupalUnitTestCase::setUp
SolrBaseQueryTests::testAddParams function
SolrBaseQueryTests::testParseFilters function Test ordering of parsed filter positions.
SolrBaseQueryTests::_apachesolr_drupal_query private function Helper function to simulate the auto loading and other non-needed functions that otherwise require a database
SolrBaseQueryTests::_apachesolr_drupal_subquery private function