From ba91172c0128e900ac52b3d1797b7fc92f6f0df8 Mon Sep 17 00:00:00 2001 From: ilestis Date: Sat, 2 Dec 2023 10:17:37 -0600 Subject: [PATCH] Fix tests and api perfs --- app/Http/Resources/EntityAssetResource.php | 2 +- app/Http/Resources/EntityResource.php | 2 +- app/Http/Resources/InventoryResource.php | 2 +- app/Http/Resources/PostResource.php | 2 +- app/Models/Calendar.php | 2 ++ app/Models/Relations/CalendarRelations.php | 8 -------- app/Models/Scopes/SubEntityScopes.php | 12 +++++++---- tests/TestCase.php | 23 ++++++++++++++-------- 8 files changed, 29 insertions(+), 24 deletions(-) diff --git a/app/Http/Resources/EntityAssetResource.php b/app/Http/Resources/EntityAssetResource.php index 5f00d810ff..15201573eb 100644 --- a/app/Http/Resources/EntityAssetResource.php +++ b/app/Http/Resources/EntityAssetResource.php @@ -17,7 +17,7 @@ public function toArray($request) /** @var EntityAsset $asset */ $asset = $this->resource; - $data = $this->entity([ + $data = $this->onEntity([ 'type_id' => $asset->type_id, '_file' => $asset->isFile(), '_link' => $asset->isLink(), diff --git a/app/Http/Resources/EntityResource.php b/app/Http/Resources/EntityResource.php index 1956ab8c50..e5fea0a291 100644 --- a/app/Http/Resources/EntityResource.php +++ b/app/Http/Resources/EntityResource.php @@ -205,7 +205,7 @@ public function entity(array $prepared = []) if ($misc->ancestors) { $ancestors = []; - foreach ($misc->getAncestors(['id']) as $ancestor) { + foreach ($misc->ancestors as $ancestor) { $ancestors[] = $ancestor->id; } $merged['parents'] = $ancestors; diff --git a/app/Http/Resources/InventoryResource.php b/app/Http/Resources/InventoryResource.php index 99d1fbafaf..3a13b274bd 100644 --- a/app/Http/Resources/InventoryResource.php +++ b/app/Http/Resources/InventoryResource.php @@ -16,7 +16,7 @@ public function toArray($request) { /** @var Inventory $model */ $model = $this->resource; - return $this->entity([ + return $this->onEntity([ 'item_id' => $model->item_id, 'name' => $model->name, 'position' => $model->position, diff --git a/app/Http/Resources/PostResource.php b/app/Http/Resources/PostResource.php index 489138a341..7b415a1a79 100644 --- a/app/Http/Resources/PostResource.php +++ b/app/Http/Resources/PostResource.php @@ -16,7 +16,7 @@ public function toArray($request) { /** @var Post $model */ $model = $this->resource; - return $this->entity([ + return $this->onEntity([ 'name' => $model->name, 'visibility_id' => (int) $model->visibility_id->value, 'entry' => $model->entry, diff --git a/app/Models/Calendar.php b/app/Models/Calendar.php index 3fc5d261fc..0983d9d018 100644 --- a/app/Models/Calendar.php +++ b/app/Models/Calendar.php @@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships; /** * Class Calendar @@ -40,6 +41,7 @@ class Calendar extends MiscModel use CalendarRelations; use CampaignTrait; use ExportableTrait; + use HasRecursiveRelationships; use HasFactory; use SoftDeletes; diff --git a/app/Models/Relations/CalendarRelations.php b/app/Models/Relations/CalendarRelations.php index 6b8e8a7403..3c46dbdaa1 100644 --- a/app/Models/Relations/CalendarRelations.php +++ b/app/Models/Relations/CalendarRelations.php @@ -44,12 +44,4 @@ public function calendars() { return $this->hasMany(Calendar::class); } - - /** - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ - public function children() - { - return $this->hasMany(Calendar::class); - } } diff --git a/app/Models/Scopes/SubEntityScopes.php b/app/Models/Scopes/SubEntityScopes.php index 1ade3a5e49..09312ecbcd 100644 --- a/app/Models/Scopes/SubEntityScopes.php +++ b/app/Models/Scopes/SubEntityScopes.php @@ -75,14 +75,18 @@ public function scopeWithApi(Builder $query): Builder { $relations = [ 'entity', - 'entity.tags', 'entity.posts', 'entity.events', - 'entity.relationships', 'entity.attributes', 'entity.inventories', + 'entity.tags', + 'entity.posts', 'entity.posts.permissions', + 'entity.events', + 'entity.relationships', 'entity.attributes', 'entity.inventories', 'entity.inventories', 'entity.assets', 'entity.abilities', - 'ancestors', - 'children', ]; + if (method_exists($this, 'ancestors')) { + $relations[] = 'ancestors'; + $relations[] = 'children'; + } $with = !empty($this->apiWith) ? $this->apiWith : []; foreach ($with as $relation) { $relations[] = $relation; diff --git a/tests/TestCase.php b/tests/TestCase.php index 3b947edc94..6869bc77ad 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -12,6 +12,7 @@ use App\Models\Attribute; use App\Models\Bookmark; use App\Models\Campaign; +use App\Services\Permissions\RolePermissionService; use Illuminate\Support\Facades\Storage; use App\Models\CampaignRoleUser; use App\Models\CampaignUser; @@ -120,10 +121,12 @@ public function asPlayer(): self 'user_id' => $user2->id, ]); Permissions::reset(); - CampaignRole::where('id', 3)->first()->toggle(1, 1); - CampaignRole::where('id', 3)->first()->toggle(10, 1); - CampaignRole::where('id', 3)->first()->toggle(11, 1); - CampaignRole::where('id', 3)->first()->toggle(7, 1); + /** @var RolePermissionService $service */ + $service = app()->make(RolePermissionService::class); + $service->role(CampaignRole::where('id', 3)->first())->toggle(1, 1); + $service->role(CampaignRole::where('id', 3)->first())->toggle(10, 1); + $service->role(CampaignRole::where('id', 3)->first())->toggle(11, 1); + $service->role(CampaignRole::where('id', 3)->first())->toggle(7, 1); return $this; } @@ -142,7 +145,9 @@ public function withMember(): self 'user_id' => $user3->id, ]); - CampaignRole::where('id', 3)->first()->toggle(1, 1); + /** @var RolePermissionService $service */ + $service = app()->make(RolePermissionService::class); + $service->role(CampaignRole::where('id', 3)->first())->toggle(1, 1); return $this; } @@ -176,9 +181,11 @@ public function withCampaigns(array $extra = []): self public function withPermissions(array $extra = []): self { - CampaignRole::where('id', 3)->first()->toggle(1, 1); - CampaignRole::where('id', 3)->first()->toggle(1, 2); - CampaignRole::where('id', 3)->first()->toggle(1, 3); + /** @var RolePermissionService $service */ + $service = app()->make(RolePermissionService::class); + $service->role(CampaignRole::where('id', 3)->first())->toggle(1, 1); + $service->role(CampaignRole::where('id', 3)->first())->toggle(1, 2); + $service->role(CampaignRole::where('id', 3)->first())->toggle(1, 3); return $this; }