You are here

protected function BackendTestBase::checkFacets in Search API 8

Tests whether facets work correctly.

1 call to BackendTestBase::checkFacets()
BackendTestBase::testBackend in tests/src/Kernel/BackendTestBase.php
Tests various indexing scenarios for the search backend.

File

tests/src/Kernel/BackendTestBase.php, line 388

Class

BackendTestBase
Provides a base class for backend tests.

Namespace

Drupal\Tests\search_api\Kernel

Code

protected function checkFacets() {
  $query = $this
    ->buildSearch();
  $conditions = $query
    ->createConditionGroup('OR', [
    'facet:' . 'category',
  ]);
  $conditions
    ->addCondition('category', 'article_category');
  $query
    ->addConditionGroup($conditions);
  $facets['category'] = [
    'field' => 'category',
    'limit' => 0,
    'min_count' => 1,
    'missing' => TRUE,
    'operator' => 'or',
  ];
  $query
    ->setOption('search_api_facets', $facets);
  $results = $query
    ->execute();
  $this
    ->assertResults([
    4,
    5,
  ], $results, 'OR facets query');
  $expected = [
    [
      'count' => 2,
      'filter' => '"article_category"',
    ],
    [
      'count' => 2,
      'filter' => '"item_category"',
    ],
    [
      'count' => 1,
      'filter' => '!',
    ],
  ];
  $category_facets = $results
    ->getExtraData('search_api_facets')['category'];
  usort($category_facets, [
    $this,
    'facetCompare',
  ]);
  $this
    ->assertEquals($expected, $category_facets, 'Correct OR facets were returned');
  $query = $this
    ->buildSearch();
  $conditions = $query
    ->createConditionGroup('OR', [
    'facet:' . 'category',
  ]);
  $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);
  $results = $query
    ->execute();
  $this
    ->assertResults([
    4,
    5,
  ], $results, 'OR facets query');
  $expected = [
    [
      'count' => 2,
      'filter' => '"article_category"',
    ],
    [
      'count' => 2,
      'filter' => '"item_category"',
    ],
  ];
  $category_facets = $results
    ->getExtraData('search_api_facets')['category'];
  usort($category_facets, [
    $this,
    'facetCompare',
  ]);
  $this
    ->assertEquals($expected, $category_facets, 'Correct OR facets were returned');
}