actingAs($this->operationalUser(), 'sanctum'); UserRole::factory()->count(2)->create(); $this->getJson('/api/user-roles') ->assertOk() ->assertJsonStructure(['message', 'data' => [['id', 'name', 'description']]]); } public function test_show_returns_a_single_role(): void { $this->actingAs($this->operationalUser(), 'sanctum'); $role = UserRole::factory()->create(); $this->getJson("/api/user-roles/{$role->id}") ->assertOk() ->assertJsonPath('data.id', $role->id) ->assertJsonPath('data.name', $role->name); } // --- Statistiche (tier.admin) --------------------------------------------- public function test_admin_can_see_the_user_count_of_a_role(): void { $this->actingAs($this->adminUser(), 'sanctum'); $role = UserRole::factory()->create(); $this->operationalUser(['role_id' => $role->id]); $this->operationalUser(['role_id' => $role->id]); $this->getJson("/api/user-roles/{$role->id}/usage") ->assertOk() ->assertJsonPath('user_count', 2); } public function test_a_non_admin_cannot_see_usage_stats(): void { $this->actingAs($this->operationalUser(), 'sanctum'); $role = UserRole::factory()->create(); $this->getJson("/api/user-roles/{$role->id}/usage")->assertForbidden(); } // --- Gating --------------------------------------------------------------- public function test_guests_cannot_access_roles(): void { $this->getJson('/api/user-roles')->assertUnauthorized(); } }