You are here

function DrupalSolrFilterSubQueryTests::testSubQueriesQuery in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 tests/solr_base_subquery.test \DrupalSolrFilterSubQueryTests::testSubQueriesQuery()
  2. 6.3 tests/solr_base_subquery.test \DrupalSolrFilterSubQueryTests::testSubQueriesQuery()

File

tests/solr_base_subquery.test, line 43
Unit tests for subquery object methods.

Class

DrupalSolrFilterSubQueryTests
@file Unit tests for subquery object methods.

Code

function testSubQueriesQuery() {
  $query1 = $this
    ->_apachesolr_drupal_query('DrupalTest');
  $query1
    ->addFilter('label', 'foo');
  $query2 = $this
    ->_apachesolr_drupal_subquery();
  $query2
    ->addFilter('label', 'bar');
  $query3 = $this
    ->_apachesolr_drupal_subquery();
  $query3
    ->addFilter('label', 'baz');
  $query1
    ->addFilterSubQuery($query2);
  $params = $query1
    ->getParam('fq');
  $this
    ->assertEqual($params[0], 'label:foo', t('First field should be label:foo'));
  $this
    ->assertEqual($params[1], '(label:bar)', t('Second field should be label:bar'));
  $query1
    ->removeFilterSubQuery($query2);
  $params = $query1
    ->getParam('fq');
  $this
    ->assertFalse(isset($params[1]), t('Second field should be empty'));
  $query1
    ->addFilterSubQuery($query2);
  $query1
    ->addFilterSubQuery($query2);
  $query1
    ->addFilterSubQuery($query2);
  $params = $query1
    ->getParam('fq');
  $this
    ->assertEqual($params[0], 'label:foo', t('First field should be label:foo'));
  $this
    ->assertEqual($params[1], '(label:bar)', t('Second field should be label:bar'));
  $this
    ->assertEqual(count($params), 2, t('Add bar several times; should only appear once.'));

  // Empty out query1
  $query1 = $this
    ->_apachesolr_drupal_query('DrupalTest');
  $query2 = $this
    ->_apachesolr_drupal_subquery('DrupalTest');
  $query2->operator = 'OR';
  $query2
    ->addFilter('label', 'bar');
  $query2
    ->addFilter('label', 'baz');
  $query1
    ->addFilterSubQuery($query2);
  $params = $query1
    ->getParam('fq');
  $this
    ->assertEqual($params[0], '(label:bar OR label:baz)', '(label:bar OR label:baz)');

  // Empty out query1
  $query1 = $this
    ->_apachesolr_drupal_query('DrupalTest');
  $query2 = $this
    ->_apachesolr_drupal_subquery('DrupalTest');
  $query2->operator = 'AND';
  $query2
    ->addFilter('label', 'bar');
  $query2
    ->addFilter('label', 'baz');
  $query1
    ->addFilterSubQuery($query2);
  $params = $query1
    ->getParam('fq');
  $this
    ->assertEqual($params[0], '(label:bar AND label:baz)', '(label:bar AND label:baz)');

  // Test with multiple filters in first query
  $query1 = $this
    ->_apachesolr_drupal_query('DrupalTest');
  $query1
    ->addFilter('is_uid', '10');
  $query2 = $this
    ->_apachesolr_drupal_subquery();
  $query2
    ->addFilter('is_uid', '1');
  $query2
    ->addFilter('tid', '5');
  $query1
    ->addFilterSubQuery($query2);
  $params = $query1
    ->getParam('fq');
  $this
    ->assertEqual($params[0], 'is_uid:10', 'First field value is is_uid:10');
  $this
    ->assertEqual($params[1], '(is_uid:1 OR tid:5)', 'Second field value is (is_uid:1 OR tid:5)');
  $query2->operator = 'AND';
  $query1
    ->addFilterSubQuery($query2);
  $params = $query1
    ->getParam('fq');
  $this
    ->assertEqual($params[0], 'is_uid:10', 'First field value is is_uid:10');
  $this
    ->assertEqual($params[1], '(is_uid:1 AND tid:5)', 'Second field value is (is_uid:1 AND tid:5)');

  // Test excluding a subquery.
  $query1 = $this
    ->_apachesolr_drupal_query('DrupalTest');
  $query2 = $this
    ->_apachesolr_drupal_subquery('AND', TRUE);
  $query2
    ->addFilter('label', 'bar');
  $query2
    ->addFilter('label', 'baz');
  $query1
    ->addFilterSubQuery($query2);
  $params = $query1
    ->getParam('fq');
  $this
    ->assertEqual($params[0], '-(label:bar AND label:baz)', t('The bar and baz label are excluded.'));

  // Test if a field is either empty or has a certain value.
  $query1 = $this
    ->_apachesolr_drupal_query('DrupalTest');
  $query2 = $this
    ->_apachesolr_drupal_subquery('AND', TRUE);
  $query2
    ->addFilter('label', '*');
  $query2
    ->addFilter('label', 'bar', TRUE);
  $query1
    ->addFilterSubQuery($query2);
  $params = $query1
    ->getParam('fq');

  // This subquery is useful when you want to retrieve empty
  // fields for the label property OR should be bar.
  $this
    ->assertEqual($params[0], '-(label:* AND -label:bar)', t('The label should either be bar or empty.'));
}