You are here

protected function SearchApiSolrTest::checkQueryConditions in Search API Solr 4.x

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

Tests the conversion of Search API queries into Solr queries.

1 call to SearchApiSolrTest::checkQueryConditions()
SearchApiSolrTest::checkBackendSpecificFeatures in tests/src/Kernel/SearchApiSolrTest.php
Checks backend specific features.

File

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

Class

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

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

protected function checkQueryConditions() {

  /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
  $backend = Server::load($this->serverId)
    ->getBackend();
  $options = [];
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $query
    ->addCondition('id', 5, '=');
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('its_id:"5"', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $query
    ->addCondition('id', 5, '<>');
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('(*:* -its_id:"5")', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $query
    ->addCondition('id', 3, '<>');
  $query
    ->addCondition('id', 5, '<>');
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('(*:* -its_id:"3")', $fq[0]['query']);
  $this
    ->assertEquals('(*:* -its_id:"5")', $fq[1]['query']);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $condition_group = $query
    ->createConditionGroup();
  $condition_group
    ->addCondition('id', 3, '<>');
  $condition_group
    ->addCondition('id', 5, '<>');
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('(+(*:* -its_id:"3") +(*:* -its_id:"5"))', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $condition_group = $query
    ->createConditionGroup();
  $condition_group
    ->addCondition('id', 5, '<>');
  $condition_group
    ->addCondition('type', 3);
  $condition_group
    ->addCondition('category', 7);
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('(+(*:* -its_id:"5") +ss_type:"3" +ss_category:"7")', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $condition_group = $query
    ->createConditionGroup();
  $inner_condition_group = $query
    ->createConditionGroup('OR');
  $condition_group
    ->addCondition('id', 5, '<>');
  $inner_condition_group
    ->addCondition('type', 3);
  $inner_condition_group
    ->addCondition('category', 7);
  $condition_group
    ->addConditionGroup($inner_condition_group);
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('(+(*:* -its_id:"5") +(ss_type:"3" ss_category:"7"))', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);

  // Condition groups with null value queries are special snowflakes.
  // @see https://www.drupal.org/node/2888629
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $condition_group = $query
    ->createConditionGroup();
  $inner_condition_group = $query
    ->createConditionGroup('OR');
  $condition_group
    ->addCondition('id', 5, '<>');
  $inner_condition_group
    ->addCondition('type', 3);
  $inner_condition_group
    ->addCondition('category', NULL);
  $condition_group
    ->addConditionGroup($inner_condition_group);
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('(+(*:* -its_id:"5") +(ss_type:"3" (*:* -ss_category:[* TO *])))', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $condition_group = $query
    ->createConditionGroup();
  $inner_condition_group_or = $query
    ->createConditionGroup('OR');
  $inner_condition_group_or
    ->addCondition('id', 3);
  $inner_condition_group_or
    ->addCondition('type', 7, '<>');
  $inner_condition_group_and = $query
    ->createConditionGroup();
  $inner_condition_group_and
    ->addCondition('id', 1);
  $inner_condition_group_and
    ->addCondition('type', 2, '<>');
  $inner_condition_group_and
    ->addCondition('category', 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,
    &$options,
  ]);
  $this
    ->assertEquals('(+(its_id:"3" (*:* -ss_type:"7")) +(+its_id:"1" +(*:* -ss_type:"2") +ss_category:{* TO "5"}))', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $condition_group = $query
    ->createConditionGroup();
  $condition_group
    ->addCondition('id', 5);
  $condition_group
    ->addCondition('type', [
    1,
    2,
    3,
  ], 'NOT IN');
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('(+its_id:"5" +(*:* -ss_type:("1" "2" "3")))', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $condition_group = $query
    ->createConditionGroup();
  $condition_group
    ->addCondition('id', 5);
  $inner_condition_group = $query
    ->createConditionGroup();
  $inner_condition_group
    ->addCondition('type', [
    1,
    2,
    3,
  ], 'NOT IN');
  $condition_group
    ->addConditionGroup($inner_condition_group);
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('(+its_id:"5" +(*:* -ss_type:("1" "2" "3")))', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);

  // Test tagging of a single filter query of a facet query.
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $conditions = $query
    ->createConditionGroup('OR', [
    'facet:' . 'tagtosearchfor',
  ]);
  $conditions
    ->addCondition('category', 'article_category');
  $query
    ->addConditionGroup($conditions);
  $conditions = $query
    ->createConditionGroup('AND');
  $conditions
    ->addCondition('category', NULL, '<>');
  $query
    ->addConditionGroup($conditions);
  $facets['category'] = [
    'field' => 'category',
    'limit' => 0,
    'min_count' => 1,
    'missing' => TRUE,
    'operator' => 'or',
  ];
  $query
    ->setOption('search_api_facets', $facets);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$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();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $conditions = $query
    ->createConditionGroup('OR', [
    'facet:id',
  ]);
  $conditions
    ->addCondition('id', 'A');
  $conditions
    ->addCondition('id', 'B');
  $query
    ->addConditionGroup($conditions);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals(1, count($fq));
  $this
    ->assertEquals([
    'facet:id' => 'facet:id',
  ], $fq[0]['tags']);
  $this
    ->assertEquals('(its_id:"A" its_id:"B")', $fq[0]['query']);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $conditions = $query
    ->createConditionGroup('AND', [
    'facet:id',
  ]);
  $conditions
    ->addCondition('id', 'A');
  $conditions
    ->addCondition('id', 'B');
  $query
    ->addConditionGroup($conditions);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals(1, count($fq));
  $this
    ->assertEquals([
    'facet:id' => 'facet:id',
  ], $fq[0]['tags']);
  $this
    ->assertEquals('(+its_id:"A" +its_id:"B")', $fq[0]['query']);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    'en',
  ]);
  $query
    ->addCondition('id', 5, '=');
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('its_id:"5"', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    'en',
    'de',
  ]);
  $condition_group = $query
    ->createConditionGroup();
  $condition_group
    ->addCondition('id', 5);
  $inner_condition_group = $query
    ->createConditionGroup();
  $inner_condition_group
    ->addCondition('type', [
    1,
    2,
    3,
  ], 'NOT IN');
  $condition_group
    ->addConditionGroup($inner_condition_group);
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('(+its_id:"5" +(*:* -ss_type:("1" "2" "3")))', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    'en',
  ]);
  $condition_group = $query
    ->createConditionGroup();
  $condition_group
    ->addCondition('id', 5);
  $condition_group
    ->addCondition('search_api_language', 'de');
  $inner_condition_group = $query
    ->createConditionGroup();
  $inner_condition_group
    ->addCondition('type', [
    1,
    2,
    3,
  ], 'NOT IN');
  $condition_group
    ->addConditionGroup($inner_condition_group);
  $query
    ->addConditionGroup($condition_group);
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('(+its_id:"5" +ss_search_api_language:"de" +(*:* -ss_type:("1" "2" "3")))', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    'en',
  ]);
  $query
    ->addCondition('body', 'some text', '=');
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('tm_X3b_en_body:("some text")', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
  $parse_mode_manager = \Drupal::service('plugin.manager.search_api.parse_mode');
  $parse_mode_phrase = $parse_mode_manager
    ->createInstance('phrase');
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    'en',
  ]);
  $query
    ->setParseMode($parse_mode_phrase);
  $query
    ->addCondition('body', 'some text', '=');
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('tm_X3b_en_body:("some text")', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
  $query = $this
    ->buildSearch();
  $query
    ->setLanguages([
    'en',
  ]);
  $query
    ->setParseMode($parse_mode_phrase);
  $query
    ->addCondition('body', [
    'some',
    'text',
  ], '=');
  $fq = $this
    ->invokeMethod($backend, 'getFilterQueries', [
    $query,
    &$options,
  ]);
  $this
    ->assertEquals('tm_X3b_en_body:("some" "text")', $fq[0]['query']);
  $this
    ->assertArrayNotHasKey(1, $fq);
}