Commit fb67e759 authored by jeroen_drenth's avatar jeroen_drenth Committed by borisson_
Browse files

Issue #2671938 by jeroen_drenth, borisson_: OR facet - active facet displays wrong count

parent 496f94c9
......@@ -103,7 +103,14 @@ class SearchApiString extends QueryTypePluginBase {
if ($result['count'] || $query_operator == 'OR') {
$count = $result['count'];
if ($query_operator === 'OR') {
$count = $this->facet->getUnfilteredResults()[$this->facet->getFieldIdentifier()][$key]['count'];
$unfiltered_results = $this->facet->getUnfilteredResults();
$field_identifier = $this->facet->getFieldIdentifier();
foreach ($unfiltered_results[$field_identifier] as $unfiltered_result) {
if ($unfiltered_result['filter'] === $result['filter']) {
$count = $unfiltered_result['count'];
}
}
}
$result = new Result(trim($result['filter'], '"'), trim($result['filter'], '"'), $count);
......
......@@ -20,9 +20,9 @@ use Drupal\Tests\UnitTestCase;
class SearchApiStringTest extends UnitTestCase {
/**
* Tests string query type without executing the query.
* Tests string query type without executing the query with an "AND" operator.
*/
public function testQueryType() {
public function testQueryTypeAnd() {
$query = new SearchApiQuery([], 'search_api_query', []);
$facet = new Facet(
['query_operator' => 'AND'],
......@@ -59,6 +59,58 @@ class SearchApiStringTest extends UnitTestCase {
}
}
/**
* Tests string query type without executing the query with an "OR" operator.
*/
public function testQueryTypeOr() {
$query = new SearchApiQuery([], 'search_api_query', []);
$facet = new Facet(
['query_operator' => 'OR'],
'facets_facet'
);
$facet->setUnfilteredResults([
'field_animal' => [
['count' => 9, 'filter' => 'unicorn'],
['count' => 3, 'filter' => 'badger'],
['count' => 7, 'filter' => 'narwhal'],
['count' => 5, 'filter' => 'mushroom'],
],
]);
$facet->setFieldIdentifier('field_animal');
$original_results = [
['count' => 3, 'filter' => 'badger'],
['count' => 5, 'filter' => 'mushroom'],
['count' => 7, 'filter' => 'narwhal'],
['count' => 9, 'filter' => 'unicorn'],
];
$query_type = new SearchApiString(
[
'facet' => $facet,
'query' => $query,
'results' => $original_results,
],
'search_api_string',
[]
);
$built_facet = $query_type->build();
$this->assertInstanceOf('\Drupal\facets\FacetInterface', $built_facet);
$results = $built_facet->getResults();
$this->assertInternalType('array', $results);
foreach ($original_results as $k => $result) {
$this->assertInstanceOf('\Drupal\facets\Result\ResultInterface', $results[$k]);
$this->assertEquals($result['count'], $results[$k]->getCount());
$this->assertEquals($result['filter'], $results[$k]->getDisplayValue());
}
}
/**
* Tests string query type without results.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment