From 34ebfb923e4cdddcd43c5dd0ab8c0f6945a2ad15 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Fri, 19 Nov 2021 16:30:10 +0200 Subject: [PATCH] sort has one (#438) * fix: Sorting by has one * fix: Return null for hasMany and belongsToMany * Fix styling Co-authored-by: binaryk --- src/Fields/BelongsToMany.php | 8 ++------ src/Fields/HasMany.php | 8 ++------ src/Repositories/Repository.php | 12 +++++++++--- tests/Fields/HasManyTest.php | 22 ++++++++++++---------- tests/Fields/MorphOneFieldTest.php | 6 ++++-- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index a4b20230..15c0bed6 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -9,7 +9,6 @@ use Closure; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Gate; class BelongsToMany extends EagerField { @@ -64,11 +63,8 @@ public function resolve($repository, $attribute = null) ->resolveFromPivot($item->pivot) ) ->eagerState(); - } catch (AuthorizationException $e) { - $class = get_class($item); - $policy = get_class(Gate::getPolicyFor($item)); - - abort(403, "You are not authorized to see the [{$class}] relationship from the HasMany field from the BelongsTo field. Check the [show] method from the [$policy]"); + } catch (AuthorizationException) { + return null; } }); diff --git a/src/Fields/HasMany.php b/src/Fields/HasMany.php index 0094ea6f..f74ae4da 100644 --- a/src/Fields/HasMany.php +++ b/src/Fields/HasMany.php @@ -7,7 +7,6 @@ use Binaryk\LaravelRestify\Repositories\Repository; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Gate; class HasMany extends EagerField { @@ -46,11 +45,8 @@ public function resolve($repository, $attribute = null) return $this->repositoryClass::resolveWith($item) ->allowToShow(app(Request::class)) ->eagerState(); - } catch (AuthorizationException $e) { - $class = get_class($item); - $policy = get_class(Gate::getPolicyFor($item)); - - abort(403, "You are not authorized to see the [{$class}] relationship from the HasMany field from the BelongsTo field. Check the [show] method from the [$policy]"); + } catch (AuthorizationException) { + return null; } }); diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index e35b010f..baf671a1 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -515,9 +515,15 @@ public function resolveRelationships($request): array return $collection->forIndex($request, $this); }) ->mapIntoRelated($request) - ->map(function (Related $related) use ($request) { - return $related->resolve($request, $this)->getValue(); - })->all(); + ->map(fn (Related $related) => $related->resolve($request, $this)->getValue()) + ->map(function (mixed $items) { + if ($items instanceof Collection) { + return $items->filter(); + } + + return $items; + }) + ->all(); } /** diff --git a/tests/Fields/HasManyTest.php b/tests/Fields/HasManyTest.php index 82df34f4..0b97125d 100644 --- a/tests/Fields/HasManyTest.php +++ b/tests/Fields/HasManyTest.php @@ -13,6 +13,7 @@ use Binaryk\LaravelRestify\Tests\IntegrationTest; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Gate; +use Illuminate\Testing\Fluent\AssertableJson; class HasManyTest extends IntegrationTest { @@ -36,7 +37,7 @@ protected function tearDown(): void Repository::clearResolvedInstances(); } - public function test_has_many_present_on_relations() + public function test_has_many_present_on_relations(): void { $user = User::factory()->create(); @@ -59,7 +60,7 @@ public function test_has_many_present_on_relations() ]); } - public function test_has_many_paginated_on_relation() + public function test_has_many_paginated_on_relation(): void { $user = tap($this->mockUsers()->first(), function ($user) { $this->mockPosts($user->id, 22); @@ -69,7 +70,7 @@ public function test_has_many_paginated_on_relation() ->assertJsonCount(20, 'data.relationships.posts'); } - public function test_has_many_unauthorized_see_relationship_posts() + public function test_has_many_filter_unauthorized_to_see_relationship_posts(): void { $_SERVER['restify.post.show'] = false; @@ -79,7 +80,8 @@ public function test_has_many_unauthorized_see_relationship_posts() }); $this->getJson(UserWithPosts::uriKey()."/$user->id?related=posts") - ->assertForbidden(); + ->assertOk() + ->assertJson(fn (AssertableJson $json) => $json->count('data.relationships.posts', 0)->etc()); } public function test_field_ignored_when_storing() @@ -107,7 +109,7 @@ public function test_can_display_other_pages() field('email'), field('password'), - HasMany::make('posts', PostRepository::class), + HasMany::make('posts', PostRepository::class), ]); $this->getJson(UserWithPosts::uriKey()."/{$u->id}/posts?perPage=5") @@ -131,7 +133,7 @@ public function test_can_apply_filters(): void field('email'), field('password'), - HasMany::make('posts', PostRepository::class), + HasMany::make('posts', PostRepository::class), ]); $this->getJson(UserWithPosts::uriKey()."/{$u->id}/posts?title=wew") @@ -155,7 +157,7 @@ public function test_filter_unauthorized_posts() field('email'), field('password'), - HasMany::make('posts', PostRepository::class), + HasMany::make('posts', PostRepository::class), ]); $this->getJson(UserWithPosts::uriKey()."/{$u->id}/posts") @@ -183,7 +185,7 @@ public function test_can_store() field('email'), field('password'), - HasMany::make('posts', PostRepository::class), + HasMany::make('posts', PostRepository::class), ]); $this->postJson(UserWithPosts::uriKey()."/{$u->id}/posts", [ @@ -204,7 +206,7 @@ public function test_can_show(): void field('email'), field('password'), - HasMany::make('posts', PostRepository::class), + HasMany::make('posts', PostRepository::class), ]); $this->getJson(UserWithPosts::to("$userId/posts/$post->id"), [ @@ -300,7 +302,7 @@ class UserWithPosts extends Repository public static function related(): array { return [ - 'posts' => HasMany::make('posts', PostRepository::class), + 'posts' => HasMany::make('posts', PostRepository::class), ]; } diff --git a/tests/Fields/MorphOneFieldTest.php b/tests/Fields/MorphOneFieldTest.php index 8a275f94..cd508050 100644 --- a/tests/Fields/MorphOneFieldTest.php +++ b/tests/Fields/MorphOneFieldTest.php @@ -30,13 +30,15 @@ protected function tearDown(): void Repository::clearResolvedInstances(); } - public function test_morph_one_present_on_show_when_specified_related() + public function test_morph_one_present_on_show_when_specified_related(): void { $post = Post::factory()->create([ 'user_id' => User::factory(), ]); - $relationships = $this->getJson(PostWithMophOneRepository::uriKey()."/$post->id?related=user") + $relationships = $this + ->withoutExceptionHandling() + ->getJson(PostWithMophOneRepository::uriKey()."/$post->id?related=user") ->assertJsonStructure([ 'data' => [ 'relationships' => [