Skip to content

Commit 887ce27

Browse files
authored
🐛 include in include (#190)
1 parent 394f6e9 commit 887ce27

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

src/Rules/Search/SearchInclude.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ public function buildValidationRules(string $attribute, mixed $value): array
2525
'required',
2626
(new ResourceRelationOrNested())->setResource($this->resource),
2727
],
28-
$attribute.'.includes' => [
29-
'prohibited',
30-
],
3128
$attribute.'.text' => [
3229
'prohibited',
3330
],

tests/Feature/Controllers/SearchIncludingRelationshipsOperationsTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,90 @@ public function test_getting_a_list_of_resources_including_belongs_to_has_many_r
290290
);
291291
}
292292

293+
public function test_getting_a_list_of_resources_including_belongs_to_has_many_relation_using_nested_include(): void
294+
{
295+
$belongsTo = BelongsToRelationFactory::new()->create();
296+
$matchingModel = ModelFactory::new()
297+
->for($belongsTo)
298+
->create()->fresh();
299+
300+
$matchingModel2 = ModelFactory::new()->create()->fresh();
301+
302+
Gate::policy(Model::class, GreenPolicy::class);
303+
Gate::policy(BelongsToRelation::class, GreenPolicy::class);
304+
305+
$response = $this->post(
306+
'/api/models/search',
307+
[
308+
'search' => [
309+
'includes' => [
310+
[
311+
'relation' => 'belongsToRelation',
312+
'includes' => [
313+
['relation' => 'models'],
314+
],
315+
],
316+
],
317+
],
318+
],
319+
['Accept' => 'application/json']
320+
);
321+
322+
$matchingModelBelongsToRelation = $matchingModel->belongsToRelation;
323+
324+
$this->assertResourcePaginated(
325+
$response,
326+
[$matchingModel, $matchingModel2],
327+
new ModelResource(),
328+
[
329+
[
330+
'belongs_to_relation' => array_merge(
331+
$matchingModelBelongsToRelation
332+
->only((new BelongsToResource())->getFields(app()->make(RestRequest::class))),
333+
[
334+
'models' => $matchingModelBelongsToRelation->models()
335+
->orderBy('id')
336+
->get()
337+
->map(function ($model) {
338+
return $model->only((new ModelResource())->getFields(app()->make(RestRequest::class)));
339+
})
340+
->toArray(),
341+
]
342+
),
343+
],
344+
[
345+
'belongs_to_relation' => null,
346+
],
347+
]
348+
);
349+
}
350+
351+
public function test_including_unauthorized_nested_relation_returns_validation_error(): void
352+
{
353+
Gate::policy(Model::class, GreenPolicy::class);
354+
Gate::policy(BelongsToRelation::class, GreenPolicy::class);
355+
356+
$response = $this->post(
357+
'/api/models/search',
358+
[
359+
'search' => [
360+
'includes' => [
361+
[
362+
'relation' => 'belongsToRelation',
363+
'includes' => [
364+
['relation' => 'unauthorized'],
365+
],
366+
],
367+
],
368+
],
369+
],
370+
['Accept' => 'application/json']
371+
);
372+
373+
$response->assertStatus(422);
374+
$response->assertExactJsonStructure(['message', 'errors' => ['search.includes.0.includes.0.relation']]);
375+
}
376+
293377
public function test_getting_a_list_of_resources_including_distant_relation_with_intermediary_search_query_condition(): void
294378
{
295379
$matchingModel = ModelFactory::new()->create(

0 commit comments

Comments
 (0)