Skip to content
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
24 changes: 23 additions & 1 deletion src/Query/ScoutBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ScoutBuilder implements QueryBuilder
* underlying search operation.
*
* @param array $parameters An associative array of search criteria, which may include:
* - 'text': The search query string.
* - 'text': An array containing 'value' (search string) and optionally 'trashed' ('with'|'only').
* - 'filters': An array of filter conditions.
* - 'sorts': An array of sorting directives.
* - 'instructions': Additional query instructions.
Expand All @@ -41,6 +41,10 @@ public function search(array $parameters = [])
$this->applyFilters($parameters['filters']);
});

$this->when(isset($parameters['text']['trashed']), function () use ($parameters) {
$this->applyTrashed($parameters['text']['trashed']);
});

$this->when(isset($parameters['sorts']), function () use ($parameters) {
$this->applySorts($parameters['sorts']);
});
Expand Down Expand Up @@ -125,6 +129,24 @@ public function applySorts($sorts)
}
}

/**
* Apply soft-delete visibility to the underlying Scout query builder.
*
* Sets the query to include only soft-deleted records when `$trashed` is `"only"`,
* or to include both deleted and non-deleted records when `$trashed` is `"with"`.
* Any other value is ignored (no change).
*
* @param string $trashed One of: "only" (only trashed), "with" (include trashed), or other (no-op).
*/
public function applyTrashed(string $trashed): void
{
if ($trashed === 'only') {
$this->queryBuilder->onlyTrashed();
} elseif ($trashed === 'with') {
$this->queryBuilder->withTrashed();
}
}

/**
* Apply an instruction to the query builder.
*
Expand Down
8 changes: 6 additions & 2 deletions src/Rules/Search/SearchText.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lomkit\Rest\Rules\Search;

use Illuminate\Validation\Rule;
use Lomkit\Rest\Rules\RestRule;

class SearchText extends RestRule
Expand All @@ -15,8 +16,11 @@ public function buildValidationRules(string $attribute, mixed $value): array
}

return [
$attribute => ['sometimes', 'array'],
$attribute.'.value' => ['nullable', 'string'],
$attribute => ['sometimes', 'array'],
$attribute.'.value' => ['nullable', 'string'],
$attribute.'.trashed' => [
Rule::in('with', 'only'),
],
];
}
}
72 changes: 72 additions & 0 deletions tests/Feature/Controllers/SearchScoutOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,76 @@ public function test_getting_a_list_of_resources_with_null_scout(): void
new ModelResource()
);
}

public function test_getting_a_list_of_resources_with_unknown_trashed(): void
{
ModelFactory::new()->count(2)->create();

Gate::policy(Model::class, GreenPolicy::class);

$response = $this->post(
'/api/searchable-models/search',
[
'search' => [
'text' => [
'trashed' => 'unknown',
],
],
],
['Accept' => 'application/json']
);

$response->assertStatus(422);
$response->assertExactJsonStructure(['message', 'errors' => ['search.text.trashed']]);
}

public function test_getting_a_list_of_resources_with_trashed(): void
{
ModelFactory::new()->count(2)->create();

Gate::policy(Model::class, GreenPolicy::class);

$response = $this->post(
'/api/searchable-models/search',
[
'search' => [
'text' => [
'trashed' => 'with',
],
],
],
['Accept' => 'application/json']
);

$this->assertResourcePaginated(
$response,
[],
new ModelResource()
);
}

public function test_getting_a_list_of_resources_only_trashed(): void
{
ModelFactory::new()->count(2)->create();

Gate::policy(Model::class, GreenPolicy::class);

$response = $this->post(
'/api/searchable-models/search',
[
'search' => [
'text' => [
'trashed' => 'only',
],
],
],
['Accept' => 'application/json']
);

$this->assertResourcePaginated(
$response,
[],
new ModelResource()
);
}
}
58 changes: 49 additions & 9 deletions tests/Unit/LaravelScoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public function test_building_scout_with_filters()

$scoutQueryBuilderMock
->search([
'text' => [
['value' => 'test'],
],
'text' => ['value' => 'test'],
'filters' => [
['field' => 'test', 'value' => 1],
],
Expand All @@ -69,9 +67,7 @@ public function test_building_scout_with_sorts()

$scoutQueryBuilderMock
->search([
'text' => [
['value' => 'test'],
],
'text' => ['value' => 'test'],
'sorts' => [
['field' => 'id'],
],
Expand All @@ -91,14 +87,58 @@ public function test_building_scout_with_instructions()

$scoutQueryBuilderMock
->search([
'text' => [
['value' => 'test'],
],
'text' => ['value' => 'test'],
'instructions' => [
['name' => 'my_instruction'],
],
]);

($scoutQueryBuilderMock->toBase()->queryCallback)(Model::query());
}

public function test_building_scout_with_trashed()
{
Auth::setUser(Mockery::mock(User::class));
Gate::policy(Model::class, GreenPolicy::class);

$scoutQueryBuilderMock = Mockery::mock(ScoutBuilder::class, [new SearchableModelResource()])->makePartial();

$scoutQueryBuilderMock->shouldReceive('applyFilters')->never();
$scoutQueryBuilderMock->shouldReceive('applySorts')->never();
$scoutQueryBuilderMock->shouldReceive('applyInstructions')->never();
$scoutQueryBuilderMock->shouldReceive('applyTrashed')->with('with')->once();

$scoutQueryBuilderMock
->search([
'text' => [
'value' => 'test',
'trashed' => 'with',
],
]);

($scoutQueryBuilderMock->toBase()->queryCallback)(Model::query());
}

public function test_building_scout_only_trashed()
{
Auth::setUser(Mockery::mock(User::class));
Gate::policy(Model::class, GreenPolicy::class);

$scoutQueryBuilderMock = Mockery::mock(ScoutBuilder::class, [new SearchableModelResource()])->makePartial();

$scoutQueryBuilderMock->shouldReceive('applyFilters')->never();
$scoutQueryBuilderMock->shouldReceive('applySorts')->never();
$scoutQueryBuilderMock->shouldReceive('applyInstructions')->never();
$scoutQueryBuilderMock->shouldReceive('applyTrashed')->with('only')->once();

$scoutQueryBuilderMock
->search([
'text' => [
'value' => 'test',
'trashed' => 'only',
],
]);

($scoutQueryBuilderMock->toBase()->queryCallback)(Model::query());
}
}