Skip to content

Commit

Permalink
Use double quotes to allow searching string with single quotes in them (
Browse files Browse the repository at this point in the history
#592)

* Use double quotes to allow searching string with single quotes in them

* Fix styling

* Fix psalm

---------

Co-authored-by: arthurkirkosa <arthurkirkosa@users.noreply.github.com>
  • Loading branch information
arthurkirkosa and arthurkirkosa committed Nov 22, 2023
1 parent 318e86e commit e04841e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/Filters/SearchableFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/Http/Controllers/RestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion src/Repositories/ValidatingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Restify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 21 additions & 1 deletion tests/Feature/RepositorySearchServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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([
Expand Down
1 change: 0 additions & 1 deletion tests/Fixtures/Company/CompanyPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions tests/Fixtures/MailTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit e04841e

Please sign in to comment.