You are here

protected function BackendTest::searchSuccessStartsWith in Search API 8

Tests whether prefix matching works.

1 call to BackendTest::searchSuccessStartsWith()
BackendTest::checkBackendSpecificFeatures in modules/search_api_db/tests/src/Kernel/BackendTest.php
Checks backend specific features.

File

modules/search_api_db/tests/src/Kernel/BackendTest.php, line 348

Class

BackendTest
Tests index and search capabilities using the Database search backend.

Namespace

Drupal\Tests\search_api_db\Kernel

Code

protected function searchSuccessStartsWith() {
  $results = $this
    ->buildSearch('foobaz')
    ->range(0, 1)
    ->execute();
  $this
    ->assertResults([
    1,
  ], $results, 'Prefix search for »foobaz«');
  $results = $this
    ->buildSearch('foo', [], [], FALSE)
    ->sort('search_api_relevance', QueryInterface::SORT_DESC)
    ->sort('id')
    ->execute();
  $this
    ->assertResults([
    1,
    2,
    4,
    3,
    5,
  ], $results, 'Prefix search for »foo«');
  $results = $this
    ->buildSearch('foo tes')
    ->execute();
  $this
    ->assertResults([
    1,
    2,
    3,
    4,
  ], $results, 'Prefix search for »foo tes«');
  $results = $this
    ->buildSearch('oob est')
    ->execute();
  $this
    ->assertResults([], $results, 'Prefix search for »oob est«');
  $results = $this
    ->buildSearch('foo nonexistent')
    ->execute();
  $this
    ->assertResults([], $results, 'Prefix search for »foo nonexistent«');
  $results = $this
    ->buildSearch('bar nonexistent')
    ->execute();
  $this
    ->assertResults([], $results, 'Prefix search for »bar nonexistent«');
  $keys = [
    '#conjunction' => 'AND',
    'foob',
    [
      '#conjunction' => 'OR',
      'tes',
      'nonexistent',
    ],
  ];
  $results = $this
    ->buildSearch($keys)
    ->execute();
  $this
    ->assertResults([
    1,
    2,
    3,
  ], $results, 'Prefix search for complex keys');
  $results = $this
    ->buildSearch('foo', [
    'category,item_category',
  ], [], FALSE)
    ->sort('id', QueryInterface::SORT_DESC)
    ->execute();
  $this
    ->assertResults([
    2,
    1,
  ], $results, 'Prefix search for »foo« with additional filter');
  $query = $this
    ->buildSearch();
  $conditions = $query
    ->createConditionGroup('OR');
  $conditions
    ->addCondition('name', 'test');
  $conditions
    ->addCondition('body', 'test');
  $query
    ->addConditionGroup($conditions);
  $results = $query
    ->execute();
  $this
    ->assertResults([
    1,
    2,
    3,
    4,
  ], $results, 'Prefix search with multi-field fulltext filter');
}