You are here

public function SearchApiSolrTest::testQueryParsers in Search API Solr 8.2

Tests the conversion of Search API queries into Solr queries.

File

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

Class

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

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

public function testQueryParsers() {

  /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
  $backend = Server::load($this->serverId)
    ->getBackend();
  $query = $this
    ->buildSearch('foo "apple pie" bar');
  $flat = $this
    ->invokeMethod($backend, 'flattenKeys', [
    $query
      ->getKeys(),
    [],
    'phrase',
  ]);
  $this
    ->assertEquals('(+"foo" +"apple pie" +"bar")', $flat);
  $flat = $this
    ->invokeMethod($backend, 'flattenKeys', [
    $query
      ->getKeys(),
    [],
    'terms',
  ]);
  $this
    ->assertEquals('(+foo +apple\\ pie +bar)', $flat);
  $exception = FALSE;
  try {
    $flat = $this
      ->invokeMethod($backend, 'flattenKeys', [
      $query
        ->getKeys(),
      [],
      'direct',
    ]);
  } catch (SearchApiSolrException $e) {
    $exception = TRUE;
  }
  $this
    ->assertTrue($exception);
  $flat = $this
    ->invokeMethod($backend, 'flattenKeys', [
    $query
      ->getKeys(),
    [
      'solr_field',
    ],
    'phrase',
  ]);
  $this
    ->assertEquals('(+solr_field:"foo" +solr_field:"apple pie" +solr_field:"bar")', $flat);
  $flat = $this
    ->invokeMethod($backend, 'flattenKeys', [
    $query
      ->getKeys(),
    [
      'solr_field',
    ],
    'terms',
  ]);
  $this
    ->assertEquals('(+solr_field:foo +solr_field:apple\\ pie +solr_field:bar)', $flat);
  $flat = $this
    ->invokeMethod($backend, 'flattenKeys', [
    $query
      ->getKeys(),
    [
      'solr_field_1',
      'solr_field_2',
    ],
    'phrase',
  ]);
  $this
    ->assertEquals('(+(solr_field_1:"foo" solr_field_2:"foo") +(solr_field_1:"apple pie" solr_field_2:"apple pie") +(solr_field_1:"bar" solr_field_2:"bar"))', $flat);
  $flat = $this
    ->invokeMethod($backend, 'flattenKeys', [
    $query
      ->getKeys(),
    [
      'solr_field_1',
      'solr_field_2',
    ],
    'terms',
  ]);
  $this
    ->assertEquals('(+(solr_field_1:foo solr_field_2:foo) +(solr_field_1:apple\\ pie solr_field_2:apple\\ pie) +(solr_field_1:bar solr_field_2:bar))', $flat);
}