Add persons() relationship to User model and include portal_events array in MeResource response, mapping each person record to its event and organisation data for the portal frontend. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
164 lines
5.0 KiB
PHP
164 lines
5.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\Api\V1;
|
|
|
|
use App\Models\CrowdType;
|
|
use App\Models\Event;
|
|
use App\Models\Organisation;
|
|
use App\Models\Person;
|
|
use App\Models\User;
|
|
use Database\Seeders\RoleSeeder;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Laravel\Sanctum\Sanctum;
|
|
use Tests\TestCase;
|
|
|
|
class MePortalEventsTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private Organisation $organisation;
|
|
private Event $event;
|
|
private CrowdType $volunteerCrowdType;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->seed(RoleSeeder::class);
|
|
|
|
$this->organisation = Organisation::factory()->create();
|
|
$this->volunteerCrowdType = CrowdType::factory()->systemType('VOLUNTEER')->create([
|
|
'organisation_id' => $this->organisation->id,
|
|
]);
|
|
$this->event = Event::factory()->create([
|
|
'organisation_id' => $this->organisation->id,
|
|
'status' => 'published',
|
|
]);
|
|
}
|
|
|
|
public function test_auth_me_includes_portal_events(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
Person::factory()->approved()->create([
|
|
'event_id' => $this->event->id,
|
|
'crowd_type_id' => $this->volunteerCrowdType->id,
|
|
'user_id' => $user->id,
|
|
'email' => $user->email,
|
|
]);
|
|
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->getJson('/api/v1/auth/me');
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonCount(1, 'data.portal_events');
|
|
$response->assertJsonPath('data.portal_events.0.event_id', $this->event->id);
|
|
$response->assertJsonPath('data.portal_events.0.event_name', $this->event->name);
|
|
$response->assertJsonPath('data.portal_events.0.event_slug', $this->event->slug);
|
|
$response->assertJsonPath('data.portal_events.0.organisation_name', $this->organisation->name);
|
|
$response->assertJsonPath('data.portal_events.0.person_status', 'approved');
|
|
}
|
|
|
|
public function test_portal_events_contains_correct_event_dates(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
Person::factory()->approved()->create([
|
|
'event_id' => $this->event->id,
|
|
'crowd_type_id' => $this->volunteerCrowdType->id,
|
|
'user_id' => $user->id,
|
|
'email' => $user->email,
|
|
]);
|
|
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->getJson('/api/v1/auth/me');
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonPath('data.portal_events.0.start_date', $this->event->start_date->toDateString());
|
|
$response->assertJsonPath('data.portal_events.0.end_date', $this->event->end_date->toDateString());
|
|
}
|
|
|
|
public function test_portal_events_only_includes_own_events(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$otherUser = User::factory()->create();
|
|
|
|
Person::factory()->approved()->create([
|
|
'event_id' => $this->event->id,
|
|
'crowd_type_id' => $this->volunteerCrowdType->id,
|
|
'user_id' => $user->id,
|
|
'email' => $user->email,
|
|
]);
|
|
|
|
// Other user's person record — should not appear
|
|
Person::factory()->approved()->create([
|
|
'event_id' => $this->event->id,
|
|
'crowd_type_id' => $this->volunteerCrowdType->id,
|
|
'user_id' => $otherUser->id,
|
|
'email' => $otherUser->email,
|
|
]);
|
|
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->getJson('/api/v1/auth/me');
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonCount(1, 'data.portal_events');
|
|
$response->assertJsonPath('data.portal_events.0.person_status', 'approved');
|
|
}
|
|
|
|
public function test_portal_events_empty_when_no_persons(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->getJson('/api/v1/auth/me');
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonCount(0, 'data.portal_events');
|
|
}
|
|
|
|
public function test_portal_events_includes_multiple_events(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$secondEvent = Event::factory()->create([
|
|
'organisation_id' => $this->organisation->id,
|
|
'status' => 'published',
|
|
]);
|
|
|
|
Person::factory()->approved()->create([
|
|
'event_id' => $this->event->id,
|
|
'crowd_type_id' => $this->volunteerCrowdType->id,
|
|
'user_id' => $user->id,
|
|
'email' => $user->email,
|
|
]);
|
|
|
|
Person::factory()->create([
|
|
'event_id' => $secondEvent->id,
|
|
'crowd_type_id' => $this->volunteerCrowdType->id,
|
|
'user_id' => $user->id,
|
|
'email' => $user->email,
|
|
'status' => 'pending',
|
|
]);
|
|
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->getJson('/api/v1/auth/me');
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonCount(2, 'data.portal_events');
|
|
}
|
|
|
|
public function test_unauthenticated_returns_401(): void
|
|
{
|
|
$response = $this->getJson('/api/v1/auth/me');
|
|
|
|
$response->assertStatus(401);
|
|
}
|
|
}
|