You are here

public function SearchApiSolrTest::testQueryConditions in Search API Solr 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/SearchApiSolrTest.php \Drupal\Tests\search_api_solr\Kernel\SearchApiSolrTest::testQueryConditions()

Tests the conversion of Search API queries into Solr queries.

File

tests/src/Kernel/SearchApiSolrTest.php, line 376

Class

SearchApiSolrTest
Tests index and search capabilities using the Solr search backend.

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

public function testQueryConditions() {

  /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
  $backend = Server::load($this->serverId)
    ->getBackend();
  list($fields, $mapping) = $this
    ->getFieldsAndMapping($backend);
  $options = [];
  $query = $this
    ->buildSearch();
  $query
    ->addCondition('x', 5, '=');
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals('solr_x:"5"', $fq[0]['query']);
  $this
    ->assertFalse(isset($fq[1]));
  $query = $this
    ->buildSearch();
  $query
    ->addCondition('x', 5, '<>');
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals('(*:* -solr_x:"5")', $fq[0]['query']);
  $this
    ->assertFalse(isset($fq[1]));
  $query = $this
    ->buildSearch();
  $query
    ->addCondition('x', 3, '<>');
  $query
    ->addCondition('x', 5, '<>');
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals('(*:* -solr_x:"3")', $fq[0]['query']);
  $this
    ->assertEquals('(*:* -solr_x:"5")', $fq[1]['query']);
  $query = $this
    ->buildSearch();
  $condition_group = $query
    ->createConditionGroup();
  $condition_group
    ->addCondition('x', 3, '<>');
  $condition_group
    ->addCondition('x', 5, '<>');
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals('(+(*:* -solr_x:"3") +(*:* -solr_x:"5"))', $fq[0]['query']);
  $this
    ->assertFalse(isset($fq[1]));
  $query = $this
    ->buildSearch();
  $condition_group = $query
    ->createConditionGroup();
  $condition_group
    ->addCondition('x', 5, '<>');
  $condition_group
    ->addCondition('y', 3);
  $condition_group
    ->addCondition('z', 7);
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals('(+(*:* -solr_x:"5") +solr_y:"3" +solr_z:"7")', $fq[0]['query']);
  $this
    ->assertFalse(isset($fq[1]));
  $query = $this
    ->buildSearch();
  $condition_group = $query
    ->createConditionGroup();
  $inner_condition_group = $query
    ->createConditionGroup('OR');
  $condition_group
    ->addCondition('x', 5, '<>');
  $inner_condition_group
    ->addCondition('y', 3);
  $inner_condition_group
    ->addCondition('z', 7);
  $condition_group
    ->addConditionGroup($inner_condition_group);
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals('(+(*:* -solr_x:"5") +(solr_y:"3" solr_z:"7"))', $fq[0]['query']);
  $this
    ->assertFalse(isset($fq[1]));

  // Condition groups with null value queries are special snowflakes.
  // @see https://www.drupal.org/node/2888629
  $query = $this
    ->buildSearch();
  $condition_group = $query
    ->createConditionGroup();
  $inner_condition_group = $query
    ->createConditionGroup('OR');
  $condition_group
    ->addCondition('x', 5, '<>');
  $inner_condition_group
    ->addCondition('y', 3);
  $inner_condition_group
    ->addCondition('z', NULL);
  $condition_group
    ->addConditionGroup($inner_condition_group);
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals('(+(*:* -solr_x:"5") +(solr_y:"3" (*:* -solr_z:[* TO *])))', $fq[0]['query']);
  $this
    ->assertFalse(isset($fq[1]));
  $query = $this
    ->buildSearch();
  $condition_group = $query
    ->createConditionGroup();
  $inner_condition_group_or = $query
    ->createConditionGroup('OR');
  $inner_condition_group_or
    ->addCondition('x', 3);
  $inner_condition_group_or
    ->addCondition('y', 7, '<>');
  $inner_condition_group_and = $query
    ->createConditionGroup();
  $inner_condition_group_and
    ->addCondition('x', 1);
  $inner_condition_group_and
    ->addCondition('y', 2, '<>');
  $inner_condition_group_and
    ->addCondition('z', 5, '<');
  $condition_group
    ->addConditionGroup($inner_condition_group_or);
  $condition_group
    ->addConditionGroup($inner_condition_group_and);
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals('(+(solr_x:"3" (*:* -solr_y:"7")) +(+solr_x:"1" +(*:* -solr_y:"2") +solr_z:{* TO "5"}))', $fq[0]['query']);
  $this
    ->assertFalse(isset($fq[1]));
  $query = $this
    ->buildSearch();
  $condition_group = $query
    ->createConditionGroup();
  $condition_group
    ->addCondition('x', 5);
  $condition_group
    ->addCondition('y', [
    1,
    2,
    3,
  ], 'NOT IN');
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals('(+solr_x:"5" +(*:* -solr_y:"1" -solr_y:"2" -solr_y:"3"))', $fq[0]['query']);
  $this
    ->assertFalse(isset($fq[1]));
  $query = $this
    ->buildSearch();
  $condition_group = $query
    ->createConditionGroup();
  $condition_group
    ->addCondition('x', 5);
  $inner_condition_group = $query
    ->createConditionGroup();
  $inner_condition_group
    ->addCondition('y', [
    1,
    2,
    3,
  ], 'NOT IN');
  $condition_group
    ->addConditionGroup($inner_condition_group);
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals('(+solr_x:"5" +(*:* -solr_y:"1" -solr_y:"2" -solr_y:"3"))', $fq[0]['query']);
  $this
    ->assertFalse(isset($fq[1]));

  // Test tagging of a single filter query of a facet query.
  $query = $this
    ->buildSearch();
  $conditions = $query
    ->createConditionGroup('OR', array(
    'facet:' . 'tagtosearchfor',
  ));
  $conditions
    ->addCondition('category', 'article_category');
  $query
    ->addConditionGroup($conditions);
  $conditions = $query
    ->createConditionGroup('AND');
  $conditions
    ->addCondition('category', NULL, '<>');
  $query
    ->addConditionGroup($conditions);
  $facets['category'] = array(
    'field' => 'category',
    'limit' => 0,
    'min_count' => 1,
    'missing' => TRUE,
    'operator' => 'or',
  );
  $query
    ->setOption('search_api_facets', $facets);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals('ss_category:"article_category"', $fq[0]['query'], 'Condition found in tagged first filter query');
  $this
    ->assertEquals([
    'facet:tagtosearchfor' => 'facet:tagtosearchfor',
  ], $fq[0]['tags'], 'Tag found in tagged first filter query');
  $this
    ->assertEquals('ss_category:[* TO *]', $fq[1]['query'], 'Condition found in unrelated second filter query');
  $this
    ->assertEquals([], $fq[1]['tags'], 'No tag found in second filter query');

  // @see https://www.drupal.org/node/2753917
  $query = $this
    ->buildSearch();
  $conditions = $query
    ->createConditionGroup('OR', array(
    'facet:x',
  ));
  $conditions
    ->addCondition('x', 'A');
  $conditions
    ->addCondition('x', 'B');
  $query
    ->addConditionGroup($conditions);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals(1, count($fq));
  $this
    ->assertEquals([
    'facet:x' => 'facet:x',
  ], $fq[0]['tags']);
  $this
    ->assertEquals('(solr_x:"A" solr_x:"B")', $fq[0]['query']);
  $query = $this
    ->buildSearch();
  $conditions = $query
    ->createConditionGroup('AND', array(
    'facet:x',
  ));
  $conditions
    ->addCondition('x', 'A');
  $conditions
    ->addCondition('x', 'B');
  $query
    ->addConditionGroup($conditions);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    $mapping,
    $fields,
    &$options,
  ]);
  $this
    ->assertEquals(1, count($fq));
  $this
    ->assertEquals([
    'facet:x' => 'facet:x',
  ], $fq[0]['tags']);
  $this
    ->assertEquals('(+solr_x:"A" +solr_x:"B")', $fq[0]['query']);
}