You are here

public function SearchApiGranularTest::testQueryTypeAnd in Facets 8

Tests string query type without executing the query with an "AND" operator.

File

tests/src/Unit/Plugin/query_type/SearchApiGranularTest.php, line 55

Class

SearchApiGranularTest
Unit test for granular query type.

Namespace

Drupal\Tests\facets\Unit\Plugin\query_type

Code

public function testQueryTypeAnd() {
  $backend = $this
    ->prophesize(BackendInterface::class);
  $backend
    ->getSupportedFeatures()
    ->willReturn([]);
  $server = $this
    ->prophesize(ServerInterface::class);
  $server
    ->getBackend()
    ->willReturn($backend);
  $index = $this
    ->prophesize(IndexInterface::class);
  $index
    ->getServerInstance()
    ->willReturn($server);
  $query = $this
    ->prophesize(SearchApiQuery::class);
  $query
    ->getIndex()
    ->willReturn($index);
  $facet = new Facet([
    'query_operator' => 'AND',
    'widget' => 'links',
  ], 'facets_facet');
  $facet
    ->addProcessor([
    'processor_id' => 'granularity_item',
    'weights' => [],
    'settings' => [
      'granularity' => 10,
    ],
  ]);

  // Results for the widget.
  $original_results = [
    [
      'count' => 3,
      'filter' => '2',
    ],
    [
      'count' => 5,
      'filter' => '4',
    ],
    [
      'count' => 7,
      'filter' => '9',
    ],
    [
      'count' => 9,
      'filter' => '11',
    ],
  ];

  // Facets the widget should produce.
  $grouped_results = [
    0 => [
      'count' => 15,
      'filter' => '0',
    ],
    10 => [
      'count' => 9,
      'filter' => 10,
    ],
  ];
  $query_type = new SearchApiGranular([
    'facet' => $facet,
    'query' => $query
      ->reveal(),
    'results' => $original_results,
  ], 'search_api_string', []);
  $built_facet = $query_type
    ->build();
  $this
    ->assertInstanceOf(FacetInterface::class, $built_facet);
  $results = $built_facet
    ->getResults();
  $this
    ->assertSame('array', gettype($results));
  foreach ($grouped_results as $k => $result) {
    $this
      ->assertInstanceOf(ResultInterface::class, $results[$k]);
    $this
      ->assertEquals($result['count'], $results[$k]
      ->getCount());
    $this
      ->assertEquals($result['filter'], $results[$k]
      ->getDisplayValue());
  }
}