Skip to content

Commit 71fa3d5

Browse files
committed
fix(Search): Correctly search for strings with quotes and double quotes
1 parent ae006b5 commit 71fa3d5

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/Filters/SearchableFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function filter(RestifyRequest $request, $query, $value)
4444
if (! config('restify.search.case_sensitive')) {
4545
$upper = strtoupper($value);
4646

47-
return $query->orWhereRaw("UPPER({$this->column}) LIKE \"%{$upper}%\"");
47+
return $query->orWhereRaw("UPPER({$this->column}) LIKE ?", ['%'.$upper.'%']);
4848
}
4949

5050
return $query->orWhere($this->column, $likeOperator, "%{$value}%");

src/Services/Search/RepositorySearchService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,6 @@ protected function applyGroupBy(RestifyRequest $request, Repository $repository,
244244

245245
public static function make(): static
246246
{
247-
return new static;
247+
return new static();
248248
}
249249
}

tests/Feature/RepositorySearchServiceTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,51 @@ public function test_can_search_using_belongs_to_field_with_custom_foreign_key()
125125
->assertJsonCount(2, 'data');
126126
}
127127

128+
public function test_can_search_strings_with_quotes_and_double_quotes(): void
129+
{
130+
Post::factory()->create([
131+
'title' => "A Guy Named O'Neal was Standing at 5 o'clock with a 10\" cookie.",
132+
]);
133+
134+
$this->getJson(PostRepository::route(query: ['search' => 'John']))
135+
->assertJsonCount(0, 'data');
136+
137+
$this->getJson(PostRepository::route(query: ['search' => 'Guy']))
138+
->assertJsonCount(1, 'data');
139+
140+
$this->getJson(PostRepository::route(query: ['search' => "5 o'clock"]))
141+
->assertJsonCount(1, 'data');
142+
143+
$this->getJson(PostRepository::route(query: ['search' => '10" present']))
144+
->assertJsonCount(0, 'data');
145+
146+
$this->getJson(PostRepository::route(query: ['search' => '10" cookie']))
147+
->assertJsonCount(1, 'data');
148+
149+
$this->getJson(PostRepository::route(query: ['search' => '150TPL5 (1 1/2" PVC SCH40 THREADED PLUG']))
150+
->assertJsonCount(0, 'data');
151+
152+
config()->set('restify.search.case_sensitive', false);
153+
154+
$this->getJson(PostRepository::route(query: ['search' => 'John']))
155+
->assertJsonCount(0, 'data');
156+
157+
$this->getJson(PostRepository::route(query: ['search' => 'Guy']))
158+
->assertJsonCount(1, 'data');
159+
160+
$this->getJson(PostRepository::route(query: ['search' => "5 o'clock"]))
161+
->assertJsonCount(1, 'data');
162+
163+
$this->getJson(PostRepository::route(query: ['search' => '10" present']))
164+
->assertJsonCount(0, 'data');
165+
166+
$this->getJson(PostRepository::route(query: ['search' => '10" cookie']))
167+
->assertJsonCount(1, 'data');
168+
169+
$this->getJson(PostRepository::route(query: ['search' => '150TPL5 (1 1/2" PVC SCH40 THREADED PLUG']))
170+
->assertJsonCount(0, 'data');
171+
}
172+
128173
public function test_can_match_closure(): void
129174
{
130175
User::factory(4)->create();

0 commit comments

Comments
 (0)