You are here

class DrupalSolrMatchTestCase in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 tests/solr_index_and_search.test \DrupalSolrMatchTestCase
  2. 6 tests/solr_index_and_search.test \DrupalSolrMatchTestCase
  3. 6.2 tests/solr_index_and_search.test \DrupalSolrMatchTestCase
  4. 7 tests/solr_index_and_search.test \DrupalSolrMatchTestCase

Hierarchy

Expanded class hierarchy of DrupalSolrMatchTestCase

File

tests/solr_index_and_search.test, line 182

View source
class DrupalSolrMatchTestCase extends DrupalSolrOnlineWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Solr Index Data and test live queries',
      'description' => 'Indexes content and queries it.',
      'group' => 'ApacheSolr',
    );
  }

  /**
   * Test search indexing.
   */
  function testMatching() {
    if ($this->solr_available) {

      // workaround for drupal.org test bot
      $this
        ->assertTrue($this->solr
        ->ping(), "The Server could be Pinged");
      $response = $this->solr
        ->search("*:*", array());
      $response = $response->response;
      $this
        ->assertEqual($response->numFound, 0, "There should not be any documents in the index");
      $this
        ->populateIndex(7);
      $response = $this->solr
        ->search("*:*", array());
      $response = $response->response;
      $this
        ->assertEqual($response->numFound, 7, "There should be 7 documents in the index");
      $this
        ->_testQueries();
    }
  }

  /**
   * Set up a small index of items to test against.
   */
  protected function populateIndex($count) {
    variable_set('minimum_word_size', 3);
    for ($i = 1; $i <= $count; ++$i) {
      $documents[] = $this
        ->buildDocument(array(
        'entity_id' => $i,
        'content' => $this
          ->getText($i),
      ));
    }
    $this->solr
      ->addDocuments($documents);
    $this->solr
      ->commit();
  }
  protected function buildDocument($values = array()) {
    $document = new ApacheSolrDocument();
    if (!isset($values['entity_type'])) {
      $values['entity_type'] = 'fake.';
    }
    $document->id = apachesolr_document_id($values['entity_id'], $values['entity_type']);
    foreach ($values as $key => $value) {
      $document->{$key} = $value;
    }
    return $document;
  }

  /**
   * Helper method for generating snippets of content.
   *
   * Generated items to test against:
   *   1  ipsum
   *   2  dolore sit
   *   3  sit am ut
   *   4  am ut enim am
   *   5  ut enim am minim veniam
   *   6  enim am minim veniam es cillum
   *   7  am minim veniam es cillum dolore eu
   */
  function getText($n) {

    // Start over after 7.
    $n = $n % 7;
    $words = explode(' ', "Ipsum dolore sit am. Ut enim am minim veniam. Es cillum dolore eu.");
    return implode(' ', array_slice($words, $n - 1, $n));
  }

  /**
   * Run predefine queries looking for indexed terms.
   */
  function _testQueries() {

    /*
      Note: OR queries that include short words in OR groups are only accepted
      if the ORed terms are ANDed with at least one long word in the rest of the query.

      e.g. enim dolore OR ut = enim (dolore OR ut) = (enim dolor) OR (enim ut) -> good
      e.g. dolore OR ut = (dolore) OR (ut) -> bad

      This is a design limitation to avoid full table scans.

      APACHESOLR NOTE: These are not all in lucene syntax... @TODO.  Still works for text searching
    */
    $queries = array(
      // Simple AND queries.
      'ipsum' => array(
        1,
      ),
      'enim' => array(
        4,
        5,
        6,
      ),
      'xxxxx' => array(),
      // Mixed queries.
      '"minim am veniam es" OR "dolore sit"' => array(
        2,
      ),
      '"minim am veniam es" OR "sit dolore"' => array(),
      '"am minim veniam es" -eu' => array(
        6,
      ),
      '"am minim veniam" -"cillum dolore"' => array(
        5,
        6,
      ),
    );
    $broken = array(
      'enim minim' => array(
        5,
        6,
      ),
      'enim xxxxx' => array(),
      'dolore eu' => array(
        7,
      ),
      'dolore xx' => array(),
      'ut minim' => array(
        5,
      ),
      'xx minim' => array(),
      'enim veniam am minim ut' => array(
        5,
      ),
      // Simple OR queries.
      'dolore OR ipsum' => array(
        1,
        2,
        7,
      ),
      'dolore OR xxxxx' => array(
        2,
        7,
      ),
      'dolore OR ipsum OR enim' => array(
        1,
        2,
        4,
        5,
        6,
        7,
      ),
      'minim dolore OR ipsum OR enim' => array(
        5,
        6,
        7,
      ),
      'xxxxx dolore OR ipsum' => array(),
      // Negative queries.
      'dolore -sit' => array(
        7,
      ),
      'dolore -eu' => array(
        2,
      ),
      'dolore -xxxxx' => array(
        2,
        7,
      ),
      'dolore -xx' => array(
        2,
        7,
      ),
      // Phrase queries.
      '"dolore sit"' => array(
        2,
      ),
      '"sit dolore"' => array(),
      '"am minim veniam es"' => array(
        6,
        7,
      ),
      '"minim am veniam es"' => array(),
      'xxxxx "minim am veniam es" OR dolore' => array(),
      'xx "minim am veniam es" OR dolore' => array(),
      // Mixed queries.
      '"am minim veniam es" OR dolore' => array(
        2,
        6,
        7,
      ),
      '"am minim veniam" -"dolore cillum"' => array(
        5,
        6,
        7,
      ),
    );
    foreach ($queries as $query => $results) {
      $response = $this->solr
        ->search($query, array());
      $this
        ->_testQueryMatching($query, $response->response->docs, $results);

      //@TODO: We might get to this later

      #$this->_testQueryScores($query, $response->responses->docs, $results);
    }
  }

  /**
   * Test the matching abilities of the engine.
   *
   * Verify if a query produces the correct results.
   */
  function _testQueryMatching($query, $set, $results) {

    // Get result IDs.
    $found = array();
    foreach ($set as $item) {
      $found[] = $item->entity_id;
    }

    // Compare $results and $found.
    sort($found);
    sort($results);
    $this
      ->assertEqual($found, $results, strtr("Query matching '{$query}' found: @found expected: @expected", array(
      '@found' => implode(',', $found),
      '@expected' => implode(',', $results),
    )));
  }

  /**
   * Test the scoring abilities of the engine.
   *
   * Verify if a query produces normalized, monotonous scores.
   */
  function _testQueryScores($query, $set, $results) {

    // Get result scores.
    $scores = array();
    foreach ($set as $item) {
      $scores[] = $item->score;
    }

    // Check order.
    $sorted = $scores;
    sort($sorted);
    $this
      ->assertEqual($scores, array_reverse($sorted), "Query order '{$query}'");

    // Check range.
    $this
      ->assertEqual(!count($scores) || min($scores) > 0.0 && max($scores) <= 1.0001, TRUE, "Query scoring '{$query}'");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractDrupalSolrOnlineWebTestCase::$solr protected property
AbstractDrupalSolrOnlineWebTestCase::$solr_available protected property
AbstractDrupalSolrOnlineWebTestCase::checkCoreStatus protected function Helper function to verify that the expected core exists.
AbstractDrupalSolrOnlineWebTestCase::coreAdmin protected function Helper function to invoke core admin actions.
AbstractDrupalSolrOnlineWebTestCase::coreAdminAvailable protected function
AbstractDrupalSolrOnlineWebTestCase::getSolrVersion protected function
AbstractDrupalSolrOnlineWebTestCase::setUpSolr function
AbstractDrupalSolrOnlineWebTestCase::tearDown function
DrupalSolrMatchTestCase::buildDocument protected function
DrupalSolrMatchTestCase::getInfo public static function
DrupalSolrMatchTestCase::getText function Helper method for generating snippets of content.
DrupalSolrMatchTestCase::populateIndex protected function Set up a small index of items to test against.
DrupalSolrMatchTestCase::testMatching function Test search indexing.
DrupalSolrMatchTestCase::_testQueries function Run predefine queries looking for indexed terms.
DrupalSolrMatchTestCase::_testQueryMatching function Test the matching abilities of the engine.
DrupalSolrMatchTestCase::_testQueryScores function Test the scoring abilities of the engine.
DrupalSolrOnlineWebTestCase::setUp function Implementation of setUp(). Overrides AbstractDrupalSolrOnlineWebTestCase::setUp