Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Handle multiple spaces in text search #10444

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ public static function scopeNegationNameAndEmail(Builder $query, ?array $negatio
public static function scopeGeneralSearch(Builder $query, ?string $searchTerm): Builder
{
if ($searchTerm) {
$combinedSearchTerm = trim($searchTerm);
$combinedSearchTerm = trim(preg_replace('/\s{2,}/', ' ', $searchTerm));

$query
// attach the tsquery to every row to use for filtering
Expand Down
62 changes: 62 additions & 0 deletions api/tests/Feature/KeywordSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,4 +905,66 @@ public function testUserSearchPartialNamesEmailsWithPunctuation()
'id' => $user2->id,
])->assertJsonCount(2, 'data.usersPaginated.data');
}

public function testUserSearchMultiSpacing()
{
$user1 = User::factory()->create([
'first_name' => 'john',
'last_name' => 'smith',
'email' => 'john@test.com',
'current_city' => 'abc',
]);
$user2 = User::factory()->create([
'first_name' => 'bob',
'last_name' => 'johnson',
'email' => 'bob@test.com',
'current_city' => 'efg',
]);
$user3 = User::factory()->create([
'first_name' => 'jones',
'last_name' => 'hall',
'email' => 'johnson@test.com',
'current_city' => 'hij',
]);

// search operators unaffected by multiple spaces being present, two users returned regardless of spacing
$this->actingAs($this->platformAdmin, 'api')->graphQL(
/** @lang GraphQL */
'
query getUsersPaginated($where: UserFilterInput) {
usersPaginated(where: $where) {
data {
id
}
}
}
', [
'where' => [
'generalSearch' => '"johnson" OR john@',
],
])->assertJsonFragment([
'id' => $user1->id,
])->assertJsonFragment([
'id' => $user2->id,
])->assertJsonCount(2, 'data.usersPaginated.data');
$this->actingAs($this->platformAdmin, 'api')->graphQL(
/** @lang GraphQL */
'
query getUsersPaginated($where: UserFilterInput) {
usersPaginated(where: $where) {
data {
id
}
}
}
', [
'where' => [
'generalSearch' => ' "johnson" OR john@ ',
],
])->assertJsonFragment([
'id' => $user1->id,
])->assertJsonFragment([
'id' => $user2->id,
])->assertJsonCount(2, 'data.usersPaginated.data');
}
}