Files
crewli/api/tests/Feature/Api/V1/Admin/AdminStatsControllerTest.php
bert.hausmans ddf26dad33 feat: platform admin backend — controllers, services, routes, tests
Add cross-organisation admin API endpoints behind role:super_admin middleware:
- AdminOrganisationController: CRUD with search, filter, billing_status management
- AdminUserController: user management with role assignment across orgs
- AdminStatsController: platform-wide aggregate statistics
- AdminActivityLogController: filterable activity log viewer
- AdminImpersonationController + ImpersonationService: user impersonation with
  token-based session management and activity logging
- BillingStatus enum, form requests, API resources, 23 feature tests

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 23:33:16 +02:00

67 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\V1\Admin;
use App\Models\Event;
use App\Models\Organisation;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class AdminStatsControllerTest extends TestCase
{
use RefreshDatabase;
private User $superAdmin;
private User $regularUser;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->superAdmin = User::factory()->create();
$this->superAdmin->assignRole('super_admin');
$this->regularUser = User::factory()->create();
}
public function test_returns_aggregate_counts(): void
{
$org = Organisation::factory()->create(['billing_status' => 'active']);
Event::factory()->count(2)->create([
'organisation_id' => $org->id,
'status' => 'draft',
]);
Sanctum::actingAs($this->superAdmin);
$response = $this->getJson('/api/v1/admin/stats');
$response->assertOk();
$response->assertJsonStructure([
'data' => [
'organisations' => ['total', 'by_billing_status'],
'events' => ['total', 'by_status'],
'users' => ['total', 'verified'],
'persons' => ['total'],
],
]);
$this->assertGreaterThanOrEqual(1, $response->json('data.organisations.total'));
$this->assertGreaterThanOrEqual(2, $response->json('data.events.total'));
}
public function test_denied_for_non_super_admin(): void
{
Sanctum::actingAs($this->regularUser);
$response = $this->getJson('/api/v1/admin/stats');
$response->assertForbidden();
}
}