From e04841e34c05c834ade84912d0af9ba07b3bb546 Mon Sep 17 00:00:00 2001 From: Arthur Kirkosa Date: Wed, 22 Nov 2023 12:43:36 +0200 Subject: [PATCH] Use double quotes to allow searching string with single quotes in them (#592) * Use double quotes to allow searching string with single quotes in them * Fix styling * Fix psalm --------- Co-authored-by: arthurkirkosa --- src/Filters/SearchableFilter.php | 6 +++-- src/Http/Controllers/RestResponse.php | 2 -- src/Repositories/ValidatingTrait.php | 1 - src/Restify.php | 2 +- tests/Feature/RepositorySearchServiceTest.php | 22 ++++++++++++++++++- tests/Fixtures/Company/CompanyPolicy.php | 1 - tests/Fixtures/MailTracking.php | 6 ----- 7 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/Filters/SearchableFilter.php b/src/Filters/SearchableFilter.php index 29985c74..d5a97803 100644 --- a/src/Filters/SearchableFilter.php +++ b/src/Filters/SearchableFilter.php @@ -42,10 +42,12 @@ public function filter(RestifyRequest $request, $query, $value) } if (! config('restify.search.case_sensitive')) { - return $query->orWhereRaw("UPPER({$this->column}) LIKE '%".strtoupper($value)."%'"); + $upper = strtoupper($value); + + return $query->orWhereRaw("UPPER({$this->column}) LIKE \"%{$upper}%\""); } - return $query->orWhere($this->column, $likeOperator, '%'.$value.'%'); + return $query->orWhere($this->column, $likeOperator, "%{$value}%"); } public function usingBelongsTo(BelongsTo $field): self diff --git a/src/Http/Controllers/RestResponse.php b/src/Http/Controllers/RestResponse.php index fc0d6e80..4387401e 100644 --- a/src/Http/Controllers/RestResponse.php +++ b/src/Http/Controllers/RestResponse.php @@ -108,8 +108,6 @@ class RestResponse extends JsonResponse implements Responsable /** * Where specified, a meta member can be used to include non-standard meta-information. * The value of each meta member MUST be an object (a “meta object”). - * - * @var array */ protected ?array $meta = null; diff --git a/src/Repositories/ValidatingTrait.php b/src/Repositories/ValidatingTrait.php index 0a3db492..ef90c7a0 100644 --- a/src/Repositories/ValidatingTrait.php +++ b/src/Repositories/ValidatingTrait.php @@ -22,7 +22,6 @@ abstract public function collectFields(RestifyRequest $request); abstract public static function newModel(); /** - * @param array $plainPayload * @return \Illuminate\Contracts\Validation\Validator */ public static function validatorForStoring(RestifyRequest $request, array $plainPayload = null) diff --git a/src/Restify.php b/src/Restify.php index a62f3acf..95d2ffce 100644 --- a/src/Restify.php +++ b/src/Restify.php @@ -237,7 +237,7 @@ public static function sortResourcesWith() /** * Humanize the given value into a proper name. * - * @param string $value + * @param string|object $value * @return string */ public static function humanize($value) diff --git a/tests/Feature/RepositorySearchServiceTest.php b/tests/Feature/RepositorySearchServiceTest.php index e2126d43..992a9005 100644 --- a/tests/Feature/RepositorySearchServiceTest.php +++ b/tests/Feature/RepositorySearchServiceTest.php @@ -31,7 +31,7 @@ public function test_can_search_using_filter_searchable_definition(): void $this->getJson(UserRepository::route(query: ['search' => 'John']))->assertJsonCount(4, 'data'); } - public function test_can_search_incase_sensitive(): void + public function test_can_search_case_insensitive(): void { config()->set('restify.search.case_sensitive', false); @@ -50,6 +50,26 @@ public function test_can_search_incase_sensitive(): void $this->getJson(UserRepository::route(query: ['search' => 'John']))->assertJsonCount(4, 'data'); } + public function test_search_correctly_using_quotes(): void + { + config()->set('restify.search.case_sensitive', false); + + User::factory(4)->create([ + 'name' => "Brian O'Donnel", + ]); + + User::factory(4)->create([ + 'name' => 'wew', + ]); + + UserRepository::$search = [ + 'name', + ]; + + $this->getJson(UserRepository::route(query: ['search' => "O'Donnel"])) + ->assertJsonCount(4, 'data'); + } + public function test_can_search_using_belongs_to_field(): void { $foreignUser = User::factory()->create([ diff --git a/tests/Fixtures/Company/CompanyPolicy.php b/tests/Fixtures/Company/CompanyPolicy.php index b7cf6f7d..789445ea 100644 --- a/tests/Fixtures/Company/CompanyPolicy.php +++ b/tests/Fixtures/Company/CompanyPolicy.php @@ -14,7 +14,6 @@ class CompanyPolicy * Determine whether the user can use restify feature for each CRUD operation. * So if this is not allowed, all operations will be disabled. * - * @param User $user * @return mixed */ public function allowRestify(User $user = null) diff --git a/tests/Fixtures/MailTracking.php b/tests/Fixtures/MailTracking.php index 70187774..df35b3c0 100644 --- a/tests/Fixtures/MailTracking.php +++ b/tests/Fixtures/MailTracking.php @@ -76,7 +76,6 @@ protected function assertEmailsSent($count) * Assert that the last email's body equals the given text. * * @param string $body - * @param Swift_Message $message * @return MailTracking */ protected function assertEmailEquals($body, Swift_Message $message = null) @@ -94,7 +93,6 @@ protected function assertEmailEquals($body, Swift_Message $message = null) * Assert that the last email's body contains the given text. * * @param string $excerpt - * @param Swift_Message $message * @return MailTracking */ protected function assertEmailContains($excerpt, Swift_Message $message = null) @@ -112,7 +110,6 @@ protected function assertEmailContains($excerpt, Swift_Message $message = null) * Assert that the last email's subject matches the given string. * * @param string $subject - * @param Swift_Message $message * @return MailTracking */ protected function assertEmailSubject($subject, Swift_Message $message = null) @@ -130,7 +127,6 @@ protected function assertEmailSubject($subject, Swift_Message $message = null) * Assert that the last email was sent to the given recipient. * * @param string $recipient - * @param Swift_Message $message * @return MailTracking */ protected function assertEmailTo($recipient, Swift_Message $message = null) @@ -148,7 +144,6 @@ protected function assertEmailTo($recipient, Swift_Message $message = null) * Assert that the last email was delivered by the given address. * * @param string $sender - * @param Swift_Message $message * @return MailTracking */ protected function assertEmailFrom($sender, Swift_Message $message = null) @@ -173,7 +168,6 @@ public function addEmail(Swift_Message $email) /** * Retrieve the appropriate swift message. * - * @param Swift_Message $message * @return mixed */ protected function getEmail(Swift_Message $message = null)