Skip to content

Commit

Permalink
[5.x] Fix error with disallowed words in Comb search driver (#11336)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
duncanmcclean and jasonvarga authored Jan 22, 2025
1 parent 6a3bcde commit b30bc74
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Search/Comb/Comb.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,11 @@ private function removeDisallowedMatches($params)

// string pruned results together
foreach ($item['pruned'] as $pruned) {
$record .= ' '.$pruned;
if (is_array($pruned)) {
$record .= ' '.$this->flattenArray($pruned);
} else {
$record .= ' '.$pruned;
}
}

// check for disallowed
Expand Down
28 changes: 28 additions & 0 deletions tests/Search/CombTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,34 @@ public function it_can_search_for_umlauts()
$this->assertSame(2, $result['info']['total_results']);
}

#[Test]
public function it_filters_out_results_with_disallowed_words()
{
$comb = new Comb([
['title' => 'Pizza', 'ingredients' => 'Tomato, Cheese, Bread'],
['title' => 'Tomato Soup', 'ingredients' => 'Tomato, Water, Salt'],
['title' => 'Chicken & Sweetcorn Soup', 'ingredients' => 'Chicken, Sweetcorn, Water'],
]);

$results = $comb->lookUp('soup -tomato');

$this->assertEquals(['Chicken & Sweetcorn Soup'], collect($results['data'] ?? [])->pluck('data.title')->all());
}

#[Test]
public function it_filters_out_results_with_disallowed_words_where_results_are_arrays()
{
$comb = new Comb([
['title' => 'Pizza', 'ingredients' => ['Tomato', 'Cheese', 'Bread']],
['title' => 'Tomato Soup', 'ingredients' => ['Tomato', 'Water', 'Salt']],
['title' => 'Chicken & Sweetcorn Soup', 'ingredients' => ['Chicken', 'Sweetcorn', 'Water']],
]);

$results = $comb->lookUp('soup -tomato');

$this->assertEquals(['Chicken & Sweetcorn Soup'], collect($results['data'] ?? [])->pluck('data.title')->all());
}

public static function searchesProvider()
{
return [
Expand Down

0 comments on commit b30bc74

Please sign in to comment.