You are here

class SearchMatchTest in SimpleTest 6

Same name and namespace in other branches
  1. 5 tests/search_match.test \SearchMatchTest

Hierarchy

Expanded class hierarchy of SearchMatchTest

File

tests/search_match.test, line 5

View source
class SearchMatchTest extends DrupalTestCase {
  function get_info() {
    return array(
      'name' => t('Search engine queries'),
      'desc' => t('Indexes content and queries it.'),
      'group' => t('Search'),
    );
  }
  function setUp() {
    parent::setUp();
    $this
      ->drupalModuleEnable('search');
  }
  function test_matching() {
    $this
      ->_cleanup();
    $this
      ->_setup();
    $this
      ->_test_queries();
    $this
      ->_cleanup();
  }

  /**
   * Set up a small index of items to test against.
   */
  function _setup() {
    $this
      ->drupalVariableSet('minimum_word_size', 3);
    for ($i = 1; $i <= 7; ++$i) {
      search_index($i, SEARCH_TYPE, $this
        ->_get_text($i));
    }
    search_update_totals();
  }

  /**
   * Clean up the indexed test content.
   */
  function _cleanup() {
    for ($i = 1; $i < 7; ++$i) {
      search_wipe($i, SEARCH_TYPE);
    }
    search_update_totals();
  }

  /**
   * 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 _get_text($n) {
    $words = explode(' ', "Ipsum dolore sit am. Ut enim am minim veniam. Es cillum dolore eu.");
    return implode(' ', array_slice($words, $n - 1, $n));
  }

  /**
   */
  function _test_queries() {

    /*
      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.
    */
    $queries = array(
      // Simple AND queries.
      'ipsum' => array(
        1,
      ),
      'enim' => array(
        4,
        5,
        6,
      ),
      'xxxxx' => 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,
      ),
      'ipsum OR dolore sit OR cillum' => array(
        2,
        7,
      ),
      'minim dolore OR ipsum' => array(
        7,
      ),
      'dolore OR ipsum veniam' => array(
        7,
      ),
      'minim dolore OR ipsum OR enim' => array(
        5,
        6,
        7,
      ),
      'dolore xx OR yy' => array(),
      '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(),
      // Mixed queries.
      '"am minim veniam es" OR dolore' => array(
        2,
        6,
        7,
      ),
      '"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,
      ),
      '"am minim veniam" -"dolore cillum"' => array(
        5,
        6,
        7,
      ),
      'xxxxx "minim am veniam es" OR dolore' => array(),
      'xx "minim am veniam es" OR dolore' => array(),
    );
    foreach ($queries as $query => $results) {
      $set = do_search($query, SEARCH_TYPE);
      $this
        ->_test_query_matching($query, $set, $results);
      $this
        ->_test_query_scores($query, $set, $results);
      $this
        ->_cleanup_query();
    }
  }

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

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

    // Compare $results and $found.
    sort($found);
    sort($results);
    $this
      ->assertEqual($found, $results, "Query matching '{$query}'");
  }

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

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

    // 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}'");
  }

  /**
   * Remove the temporary tables created in a query, since multiple queries per page
   * are not supported.
   *
   * (Drupal 5.0 and below)
   */
  function _cleanup_query() {
    db_query('DROP TABLE IF EXISTS temp_search_sids');
    db_query('DROP TABLE IF EXISTS temp_search_results');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTestCase::$_cleanupContentTypes property
DrupalTestCase::$_cleanupNodes property
DrupalTestCase::$_cleanupRoles property
DrupalTestCase::$_cleanupUsers property
DrupalTestCase::$_cleanupVariables property
DrupalTestCase::$_content property
DrupalTestCase::$_modules property
DrupalTestCase::$_originalModules property
DrupalTestCase::assertCopy function Will trigger a pass if both parameters refer to different objects. Fail otherwise.
DrupalTestCase::assertEqual function Will trigger a pass if the two parameters have the same value only. Otherwise a fail.
DrupalTestCase::assertError function Confirms that an error has occurred and optionally that the error text matches exactly.
DrupalTestCase::assertErrorPattern function Confirms that an error has occurred and that the error text matches a Perl regular expression.
DrupalTestCase::assertIdentical function Will trigger a pass if the two parameters have the same value and same type. Otherwise a fail.
DrupalTestCase::assertIsA function Type and class test. Will pass if class matches the type name or is a subclass or if not an object, but the type is correct.
DrupalTestCase::assertNoErrors function Confirms that no errors have occurred so far in the test method.
DrupalTestCase::assertNotA function Type and class mismatch test. Will pass if class name or underling type does not match the one specified.
DrupalTestCase::assertNotEqual function Will trigger a pass if the two parameters have a different value. Otherwise a fail.
DrupalTestCase::assertNotIdentical function Will trigger a pass if the two parameters have the different value or different type.
DrupalTestCase::assertNotNull function Will be true if the value is set.
DrupalTestCase::assertNoUnwantedPattern function Will trigger a pass if the Perl regex pattern is not present in subject. Fail if found.
DrupalTestCase::assertNoUnwantedRaw function Will trigger a pass if the raw text is NOT found on the loaded page Fail otherwise.
DrupalTestCase::assertNull function Will be true if the value is null.
DrupalTestCase::assertReference function Will trigger a pass if both parameters refer to the same object. Fail otherwise.
DrupalTestCase::assertWantedPattern function Will trigger a pass if the Perl regex pattern is found in the subject. Fail otherwise.
DrupalTestCase::assertWantedRaw function Will trigger a pass if the raw text is found on the loaded page Fail otherwise.
DrupalTestCase::checkOriginalModules function Retrieves and saves current modules list into $_originalModules and $_modules.
DrupalTestCase::clickLink function Follows a link by name.
DrupalTestCase::drupalCheckAuth function @abstract Checks to see if we need to send a http-auth header to authenticate when browsing a site.
DrupalTestCase::drupalCreateContentType function Creates a custom content type based on default settings.
DrupalTestCase::drupalCreateNode function Creates a node based on default settings.
DrupalTestCase::drupalCreateRolePerm function Create a role / perm combination specified by permissions
DrupalTestCase::drupalCreateUserRolePerm function Creates a user / role / permissions combination specified by permissions
DrupalTestCase::drupalGet function @abstract Broker for the get function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::drupalGetContent function @TODO: needs documentation
DrupalTestCase::drupalLoginUser function Logs in a user with the internal browser
DrupalTestCase::drupalModuleDisable function Disables a drupal module
DrupalTestCase::drupalModuleEnable function Enables a drupal module
DrupalTestCase::drupalPost function Do a post request on a drupal page. It will be done as usual post request with SimpleBrowser By $reporting you specify if this request does assertions or not Warning: empty ("") returns will cause fails with $reporting
DrupalTestCase::drupalRawPost function @abstract Broker for the post function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::DrupalTestCase function
DrupalTestCase::drupalVariableSet function Set a drupal variable and keep track of the changes for tearDown()
DrupalTestCase::randomName function Generates a random string, to be used as name or whatever
DrupalTestCase::run function Just some info for the reporter
DrupalTestCase::tearDown function tearDown implementation, setting back switched modules etc 8
SearchMatchTest::get_info function
SearchMatchTest::setUp function
SearchMatchTest::test_matching function
SearchMatchTest::_cleanup function Clean up the indexed test content.
SearchMatchTest::_cleanup_query function Remove the temporary tables created in a query, since multiple queries per page are not supported.
SearchMatchTest::_get_text function Helper method for generating snippets of content.
SearchMatchTest::_setup function Set up a small index of items to test against.
SearchMatchTest::_test_queries function
SearchMatchTest::_test_query_matching function Test the matching abilities of the engine.
SearchMatchTest::_test_query_scores function Test the scoring abilities of the engine.