feat: registration form fields, section preferences, tag sync & schema updates

Implement EAV system for dynamic event-specific registration fields
with organisation-level templates, person section preferences with
priority ranking, and TagSyncService for deferred tag_picker sync.

New tables: registration_field_templates, registration_form_fields,
person_field_values, person_section_preferences.
New columns: persons.remarks, events.registration_show_section_preferences,
events.registration_show_availability.

58 tests, 126 assertions — all 432 tests pass (zero regressions).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-12 22:10:16 +02:00
parent fcff3b0344
commit f6e3568011
51 changed files with 3774 additions and 1 deletions

View File

@@ -0,0 +1,366 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\PersonFieldValue;
use App\Enums\RegistrationFieldType;
use App\Models\CrowdType;
use App\Models\Event;
use App\Models\Organisation;
use App\Models\Person;
use App\Models\PersonTag;
use App\Models\RegistrationFormField;
use App\Models\User;
use App\Models\UserOrganisationTag;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class PersonFieldValueTest extends TestCase
{
use RefreshDatabase;
private User $orgAdmin;
private User $outsider;
private Organisation $organisation;
private Organisation $otherOrganisation;
private Event $event;
private Person $person;
private CrowdType $crowdType;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->organisation = Organisation::factory()->create();
$this->otherOrganisation = Organisation::factory()->create();
$this->event = Event::factory()->create(['organisation_id' => $this->organisation->id]);
$this->crowdType = CrowdType::factory()->create(['organisation_id' => $this->organisation->id]);
$this->person = Person::factory()->create([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
]);
$this->orgAdmin = User::factory()->create();
$this->organisation->users()->attach($this->orgAdmin, ['role' => 'org_admin']);
$this->outsider = User::factory()->create();
$this->otherOrganisation->users()->attach($this->outsider, ['role' => 'org_admin']);
}
public function test_upsert_text_value(): void
{
$field = RegistrationFormField::factory()->textField()->create([
'event_id' => $this->event->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values", [
'values' => [
$field->slug => 'Jan Jansen',
],
]);
$response->assertOk();
$this->assertDatabaseHas('person_field_values', [
'person_id' => $this->person->id,
'registration_form_field_id' => $field->id,
'value' => 'Jan Jansen',
]);
}
public function test_upsert_select_value(): void
{
$field = RegistrationFormField::factory()->selectField()->create([
'event_id' => $this->event->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values", [
'values' => [
$field->slug => 'M',
],
]);
$response->assertOk();
$this->assertDatabaseHas('person_field_values', [
'person_id' => $this->person->id,
'registration_form_field_id' => $field->id,
'value' => 'M',
]);
}
public function test_upsert_select_invalid_option_rejected(): void
{
$field = RegistrationFormField::factory()->selectField()->create([
'event_id' => $this->event->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values", [
'values' => [
$field->slug => 'XXXXL',
],
]);
$response->assertUnprocessable();
}
public function test_upsert_multiselect_value(): void
{
$field = RegistrationFormField::factory()->multiselectField()->create([
'event_id' => $this->event->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values", [
'values' => [
$field->slug => ['Vegetarisch', 'Glutenvrij'],
],
]);
$response->assertOk();
$this->assertDatabaseHas('person_field_values', [
'person_id' => $this->person->id,
'registration_form_field_id' => $field->id,
]);
$storedValue = \App\Models\PersonFieldValue::where('person_id', $this->person->id)
->where('registration_form_field_id', $field->id)
->first();
$this->assertEquals(['Vegetarisch', 'Glutenvrij'], $storedValue->selected_options);
}
public function test_upsert_boolean_value(): void
{
$field = RegistrationFormField::factory()->booleanField()->create([
'event_id' => $this->event->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values", [
'values' => [
$field->slug => true,
],
]);
$response->assertOk();
$this->assertDatabaseHas('person_field_values', [
'person_id' => $this->person->id,
'registration_form_field_id' => $field->id,
'value' => '1',
]);
}
public function test_upsert_required_field_rejects_empty(): void
{
$field = RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
'label' => 'Verplicht veld',
'slug' => 'verplicht-veld',
'field_type' => RegistrationFieldType::TEXT,
'is_required' => true,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values", [
'values' => [
$field->slug => null,
],
]);
$response->assertUnprocessable();
}
public function test_upsert_tag_picker_with_valid_tags(): void
{
$tag1 = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
'name' => 'Tapper',
]);
$tag2 = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
'name' => 'EHBO',
]);
$field = RegistrationFormField::factory()->tagPickerField()->create([
'event_id' => $this->event->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values", [
'values' => [
$field->slug => [$tag1->id, $tag2->id],
],
]);
$response->assertOk();
$storedValue = \App\Models\PersonFieldValue::where('person_id', $this->person->id)
->where('registration_form_field_id', $field->id)
->first();
$this->assertContains($tag1->id, $storedValue->selected_options);
$this->assertContains($tag2->id, $storedValue->selected_options);
}
public function test_upsert_tag_picker_with_invalid_tag_rejected(): void
{
$field = RegistrationFormField::factory()->tagPickerField()->create([
'event_id' => $this->event->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values", [
'values' => [
$field->slug => ['01JFAKE00000000000000TAGID'],
],
]);
$response->assertUnprocessable();
}
public function test_tag_sync_triggered_when_person_has_user_id(): void
{
$linkedUser = User::factory()->create();
$this->person->update(['user_id' => $linkedUser->id]);
$tag = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
'name' => 'Tapper',
]);
$field = RegistrationFormField::factory()->tagPickerField()->create([
'event_id' => $this->event->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values", [
'values' => [
$field->slug => [$tag->id],
],
]);
$response->assertOk();
$this->assertDatabaseHas('user_organisation_tags', [
'user_id' => $linkedUser->id,
'organisation_id' => $this->organisation->id,
'person_tag_id' => $tag->id,
'source' => 'self_reported',
]);
}
public function test_tag_sync_not_triggered_when_no_user_id(): void
{
$this->assertNull($this->person->user_id);
$tag = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
'name' => 'Tapper',
]);
$field = RegistrationFormField::factory()->tagPickerField()->create([
'event_id' => $this->event->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values", [
'values' => [
$field->slug => [$tag->id],
],
]);
$response->assertOk();
$this->assertDatabaseMissing('user_organisation_tags', [
'person_tag_id' => $tag->id,
'source' => 'self_reported',
]);
}
public function test_index_returns_person_values(): void
{
$field = RegistrationFormField::factory()->textField()->create([
'event_id' => $this->event->id,
]);
\App\Models\PersonFieldValue::create([
'person_id' => $this->person->id,
'registration_form_field_id' => $field->id,
'value' => 'Test waarde',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values");
$response->assertOk();
$this->assertCount(1, $response->json('data'));
}
public function test_values_persist_after_field_deleted(): void
{
$field = RegistrationFormField::factory()->textField()->create([
'event_id' => $this->event->id,
]);
\App\Models\PersonFieldValue::create([
'person_id' => $this->person->id,
'registration_form_field_id' => $field->id,
'value' => 'Persisted value',
]);
$fieldId = $field->id;
Sanctum::actingAs($this->orgAdmin);
$this->deleteJson("/api/v1/events/{$this->event->id}/registration-fields/{$field->id}")
->assertNoContent();
// Value persists with FK set to null (nullOnDelete)
$this->assertDatabaseHas('person_field_values', [
'person_id' => $this->person->id,
'registration_form_field_id' => null,
'value' => 'Persisted value',
]);
}
public function test_cross_org_returns_403(): void
{
Sanctum::actingAs($this->outsider);
$response = $this->getJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values");
$response->assertForbidden();
}
public function test_unauthenticated_returns_401(): void
{
$response = $this->getJson("/api/v1/events/{$this->event->id}/persons/{$this->person->id}/field-values");
$response->assertUnauthorized();
}
}

View File

@@ -0,0 +1,279 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\PersonSectionPreference;
use App\Models\CrowdType;
use App\Models\Event;
use App\Models\FestivalSection;
use App\Models\Organisation;
use App\Models\Person;
use App\Models\PersonSectionPreference;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class PersonSectionPreferenceTest extends TestCase
{
use RefreshDatabase;
private User $orgAdmin;
private User $outsider;
private Organisation $organisation;
private Organisation $otherOrganisation;
private Event $festival;
private Event $subEvent;
private FestivalSection $sectionA;
private FestivalSection $sectionB;
private FestivalSection $sectionC;
private FestivalSection $crossEventSection;
private Person $person;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->organisation = Organisation::factory()->create();
$this->otherOrganisation = Organisation::factory()->create();
$this->festival = Event::factory()->festival()->create([
'organisation_id' => $this->organisation->id,
]);
$this->subEvent = Event::factory()->subEvent($this->festival)->create();
$this->sectionA = FestivalSection::factory()->create([
'event_id' => $this->subEvent->id,
'name' => 'Horeca',
]);
$this->sectionB = FestivalSection::factory()->create([
'event_id' => $this->subEvent->id,
'name' => 'Backstage',
]);
$this->sectionC = FestivalSection::factory()->create([
'event_id' => $this->subEvent->id,
'name' => 'Techniek',
]);
$this->crossEventSection = FestivalSection::factory()->crossEvent()->create([
'event_id' => $this->festival->id,
'name' => 'Security',
]);
$crowdType = CrowdType::factory()->create(['organisation_id' => $this->organisation->id]);
$this->person = Person::factory()->create([
'event_id' => $this->subEvent->id,
'crowd_type_id' => $crowdType->id,
]);
$this->orgAdmin = User::factory()->create();
$this->organisation->users()->attach($this->orgAdmin, ['role' => 'org_admin']);
$this->outsider = User::factory()->create();
$this->otherOrganisation->users()->attach($this->outsider, ['role' => 'org_admin']);
}
public function test_replace_preferences_happy_path(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->subEvent->id}/persons/{$this->person->id}/section-preferences", [
'preferences' => [
['festival_section_id' => $this->sectionA->id, 'priority' => 1],
['festival_section_id' => $this->sectionB->id, 'priority' => 2],
['festival_section_id' => $this->sectionC->id, 'priority' => 3],
],
]);
$response->assertOk();
$this->assertDatabaseHas('person_section_preferences', [
'person_id' => $this->person->id,
'festival_section_id' => $this->sectionA->id,
'priority' => 1,
]);
$this->assertDatabaseHas('person_section_preferences', [
'person_id' => $this->person->id,
'festival_section_id' => $this->sectionB->id,
'priority' => 2,
]);
$this->assertDatabaseHas('person_section_preferences', [
'person_id' => $this->person->id,
'festival_section_id' => $this->sectionC->id,
'priority' => 3,
]);
}
public function test_replace_deletes_old_and_creates_new(): void
{
Sanctum::actingAs($this->orgAdmin);
// First: set 2 preferences
$this->putJson("/api/v1/events/{$this->subEvent->id}/persons/{$this->person->id}/section-preferences", [
'preferences' => [
['festival_section_id' => $this->sectionA->id, 'priority' => 1],
['festival_section_id' => $this->sectionB->id, 'priority' => 2],
],
])->assertOk();
$this->assertEquals(2, PersonSectionPreference::where('person_id', $this->person->id)->count());
// Second: replace with 1 different preference
$this->putJson("/api/v1/events/{$this->subEvent->id}/persons/{$this->person->id}/section-preferences", [
'preferences' => [
['festival_section_id' => $this->sectionC->id, 'priority' => 1],
],
])->assertOk();
$this->assertEquals(1, PersonSectionPreference::where('person_id', $this->person->id)->count());
$this->assertDatabaseHas('person_section_preferences', [
'person_id' => $this->person->id,
'festival_section_id' => $this->sectionC->id,
'priority' => 1,
]);
$this->assertDatabaseMissing('person_section_preferences', [
'person_id' => $this->person->id,
'festival_section_id' => $this->sectionA->id,
]);
}
public function test_priority_must_be_unique(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->subEvent->id}/persons/{$this->person->id}/section-preferences", [
'preferences' => [
['festival_section_id' => $this->sectionA->id, 'priority' => 1],
['festival_section_id' => $this->sectionB->id, 'priority' => 1],
],
]);
$response->assertUnprocessable();
}
public function test_priority_range_1_to_5(): void
{
Sanctum::actingAs($this->orgAdmin);
// Priority 0 should fail
$response = $this->putJson("/api/v1/events/{$this->subEvent->id}/persons/{$this->person->id}/section-preferences", [
'preferences' => [
['festival_section_id' => $this->sectionA->id, 'priority' => 0],
],
]);
$response->assertUnprocessable();
// Priority 6 should fail
$response = $this->putJson("/api/v1/events/{$this->subEvent->id}/persons/{$this->person->id}/section-preferences", [
'preferences' => [
['festival_section_id' => $this->sectionA->id, 'priority' => 6],
],
]);
$response->assertUnprocessable();
}
public function test_max_5_preferences(): void
{
// Create 3 more sections to have 6 total
$sectionD = FestivalSection::factory()->create(['event_id' => $this->subEvent->id, 'name' => 'Catering']);
$sectionE = FestivalSection::factory()->create(['event_id' => $this->subEvent->id, 'name' => 'Logistiek']);
$sectionF = FestivalSection::factory()->create(['event_id' => $this->subEvent->id, 'name' => 'Opbouw']);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->subEvent->id}/persons/{$this->person->id}/section-preferences", [
'preferences' => [
['festival_section_id' => $this->sectionA->id, 'priority' => 1],
['festival_section_id' => $this->sectionB->id, 'priority' => 2],
['festival_section_id' => $this->sectionC->id, 'priority' => 3],
['festival_section_id' => $sectionD->id, 'priority' => 4],
['festival_section_id' => $sectionE->id, 'priority' => 5],
['festival_section_id' => $sectionF->id, 'priority' => 6],
],
]);
$response->assertUnprocessable();
}
public function test_section_must_belong_to_event(): void
{
$otherEvent = Event::factory()->create(['organisation_id' => $this->organisation->id]);
$otherSection = FestivalSection::factory()->create([
'event_id' => $otherEvent->id,
'name' => 'Andere sectie',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->subEvent->id}/persons/{$this->person->id}/section-preferences", [
'preferences' => [
['festival_section_id' => $otherSection->id, 'priority' => 1],
],
]);
$response->assertUnprocessable();
}
public function test_cross_event_section_from_parent_accepted(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->subEvent->id}/persons/{$this->person->id}/section-preferences", [
'preferences' => [
['festival_section_id' => $this->crossEventSection->id, 'priority' => 1],
],
]);
$response->assertOk();
$this->assertDatabaseHas('person_section_preferences', [
'person_id' => $this->person->id,
'festival_section_id' => $this->crossEventSection->id,
'priority' => 1,
]);
}
public function test_index_returns_preferences(): void
{
PersonSectionPreference::create([
'person_id' => $this->person->id,
'festival_section_id' => $this->sectionA->id,
'priority' => 1,
]);
PersonSectionPreference::create([
'person_id' => $this->person->id,
'festival_section_id' => $this->sectionB->id,
'priority' => 2,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson("/api/v1/events/{$this->subEvent->id}/persons/{$this->person->id}/section-preferences");
$response->assertOk();
$this->assertCount(2, $response->json('data'));
}
public function test_cross_org_returns_403(): void
{
Sanctum::actingAs($this->outsider);
$response = $this->getJson("/api/v1/events/{$this->subEvent->id}/persons/{$this->person->id}/section-preferences");
$response->assertForbidden();
}
public function test_unauthenticated_returns_401(): void
{
$response = $this->getJson("/api/v1/events/{$this->subEvent->id}/persons/{$this->person->id}/section-preferences");
$response->assertUnauthorized();
}
}

View File

@@ -0,0 +1,202 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\RegistrationFieldTemplate;
use App\Models\Event;
use App\Models\Organisation;
use App\Models\RegistrationFieldTemplate;
use App\Models\RegistrationFormField;
use App\Models\User;
use App\Services\RegistrationFieldTemplateService;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class RegistrationFieldTemplateTest extends TestCase
{
use RefreshDatabase;
private User $orgAdmin;
private User $outsider;
private Organisation $organisation;
private Organisation $otherOrganisation;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->organisation = Organisation::factory()->create();
$this->otherOrganisation = Organisation::factory()->create();
$this->orgAdmin = User::factory()->create();
$this->organisation->users()->attach($this->orgAdmin, ['role' => 'org_admin']);
$this->outsider = User::factory()->create();
$this->otherOrganisation->users()->attach($this->outsider, ['role' => 'org_admin']);
}
public function test_index_returns_active_templates(): void
{
RegistrationFieldTemplate::factory()->count(3)->create([
'organisation_id' => $this->organisation->id,
]);
RegistrationFieldTemplate::factory()->inactive()->create([
'organisation_id' => $this->organisation->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/registration-field-templates");
$response->assertOk();
$this->assertCount(3, $response->json('data'));
}
public function test_store_creates_template(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/registration-field-templates", [
'label' => 'Allergieën',
'field_type' => 'text',
]);
$response->assertCreated();
$this->assertDatabaseHas('registration_field_templates', [
'organisation_id' => $this->organisation->id,
'label' => 'Allergieën',
'slug' => 'allergieen',
]);
}
public function test_store_select_requires_options(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/registration-field-templates", [
'label' => 'Voorkeur',
'field_type' => 'select',
]);
$response->assertUnprocessable()
->assertJsonValidationErrors('options');
}
public function test_update_template(): void
{
$template = RegistrationFieldTemplate::factory()->create([
'organisation_id' => $this->organisation->id,
'label' => 'Oude label',
'slug' => 'oude-label',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/organisations/{$this->organisation->id}/registration-field-templates/{$template->id}", [
'label' => 'Nieuwe label',
]);
$response->assertOk();
$this->assertDatabaseHas('registration_field_templates', [
'id' => $template->id,
'label' => 'Nieuwe label',
'slug' => 'nieuwe-label',
]);
}
public function test_destroy_org_template_deletes(): void
{
$template = RegistrationFieldTemplate::factory()->create([
'organisation_id' => $this->organisation->id,
'is_system' => false,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->deleteJson("/api/v1/organisations/{$this->organisation->id}/registration-field-templates/{$template->id}");
$response->assertNoContent();
$this->assertDatabaseMissing('registration_field_templates', [
'id' => $template->id,
]);
}
public function test_destroy_system_template_deactivates(): void
{
$template = RegistrationFieldTemplate::factory()->system()->create([
'organisation_id' => $this->organisation->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->deleteJson("/api/v1/organisations/{$this->organisation->id}/registration-field-templates/{$template->id}");
$response->assertNoContent();
$this->assertDatabaseHas('registration_field_templates', [
'id' => $template->id,
'is_active' => false,
]);
}
public function test_system_templates_seeded_on_org_creation(): void
{
RegistrationFieldTemplateService::seedSystemTemplates($this->organisation);
$count = RegistrationFieldTemplate::where('organisation_id', $this->organisation->id)
->where('is_system', true)
->count();
$this->assertEquals(11, $count);
}
public function test_create_field_from_template(): void
{
$template = RegistrationFieldTemplate::factory()->create([
'organisation_id' => $this->organisation->id,
'label' => 'Shirtmaat',
'slug' => 'shirtmaat',
'field_type' => 'select',
'options' => ['XS', 'S', 'M', 'L', 'XL'],
]);
$event = Event::factory()->create([
'organisation_id' => $this->organisation->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/events/{$event->id}/registration-fields/from-template", [
'template_id' => $template->id,
]);
$response->assertCreated();
$this->assertDatabaseHas('registration_form_fields', [
'event_id' => $event->id,
'label' => 'Shirtmaat',
'field_type' => 'select',
]);
}
public function test_cross_org_returns_403(): void
{
Sanctum::actingAs($this->outsider);
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/registration-field-templates");
$response->assertForbidden();
}
public function test_unauthenticated_returns_401(): void
{
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/registration-field-templates");
$response->assertUnauthorized();
}
}

View File

@@ -0,0 +1,363 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\RegistrationFormField;
use App\Enums\RegistrationFieldType;
use App\Models\Event;
use App\Models\Organisation;
use App\Models\RegistrationFieldTemplate;
use App\Models\RegistrationFormField;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class RegistrationFormFieldTest extends TestCase
{
use RefreshDatabase;
private User $orgAdmin;
private User $outsider;
private Organisation $organisation;
private Organisation $otherOrganisation;
private Event $event;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->organisation = Organisation::factory()->create();
$this->otherOrganisation = Organisation::factory()->create();
$this->event = Event::factory()->create(['organisation_id' => $this->organisation->id]);
$this->orgAdmin = User::factory()->create();
$this->organisation->users()->attach($this->orgAdmin, ['role' => 'org_admin']);
$this->outsider = User::factory()->create();
$this->otherOrganisation->users()->attach($this->outsider, ['role' => 'org_admin']);
}
public function test_index_returns_fields_ordered_by_sort_order(): void
{
RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
'label' => 'Third',
'slug' => 'third',
'sort_order' => 3,
]);
RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
'label' => 'First',
'slug' => 'first',
'sort_order' => 1,
]);
RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
'label' => 'Second',
'slug' => 'second',
'sort_order' => 2,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson("/api/v1/events/{$this->event->id}/registration-fields");
$response->assertOk();
$this->assertCount(3, $response->json('data'));
$this->assertEquals('First', $response->json('data.0.label'));
$this->assertEquals('Second', $response->json('data.1.label'));
$this->assertEquals('Third', $response->json('data.2.label'));
}
public function test_store_creates_field_with_auto_slug(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/events/{$this->event->id}/registration-fields", [
'label' => 'Shirtmaat',
'field_type' => 'select',
'options' => ['XS', 'S', 'M', 'L', 'XL'],
]);
$response->assertCreated()
->assertJsonPath('data.slug', 'shirtmaat')
->assertJsonPath('data.label', 'Shirtmaat');
$this->assertDatabaseHas('registration_form_fields', [
'event_id' => $this->event->id,
'slug' => 'shirtmaat',
]);
}
public function test_store_select_field_requires_options(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/events/{$this->event->id}/registration-fields", [
'label' => 'Shirtmaat',
'field_type' => 'select',
]);
$response->assertUnprocessable()
->assertJsonValidationErrors('options');
}
public function test_store_text_field_rejects_options(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/events/{$this->event->id}/registration-fields", [
'label' => 'Naam',
'field_type' => 'text',
'options' => ['A', 'B'],
]);
$response->assertUnprocessable()
->assertJsonValidationErrors('options');
}
public function test_store_tag_picker_accepts_tag_category(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/events/{$this->event->id}/registration-fields", [
'label' => 'Vaardigheden',
'field_type' => 'tag_picker',
'tag_category' => 'Vaardigheid',
]);
$response->assertCreated()
->assertJsonPath('data.tag_category', 'Vaardigheid');
}
public function test_store_text_field_rejects_tag_category(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/events/{$this->event->id}/registration-fields", [
'label' => 'Naam',
'field_type' => 'text',
'tag_category' => 'Vaardigheid',
]);
$response->assertUnprocessable()
->assertJsonValidationErrors('tag_category');
}
public function test_slug_uniqueness_per_event(): void
{
RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
'label' => 'Shirtmaat',
'slug' => 'shirtmaat',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/events/{$this->event->id}/registration-fields", [
'label' => 'Shirtmaat',
'field_type' => 'text',
]);
$response->assertCreated()
->assertJsonPath('data.slug', 'shirtmaat-2');
}
public function test_same_slug_allowed_on_different_events(): void
{
$otherEvent = Event::factory()->create(['organisation_id' => $this->organisation->id]);
RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
'label' => 'Shirtmaat',
'slug' => 'shirtmaat',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/events/{$otherEvent->id}/registration-fields", [
'label' => 'Shirtmaat',
'field_type' => 'text',
]);
$response->assertCreated()
->assertJsonPath('data.slug', 'shirtmaat');
}
public function test_update_field(): void
{
$field = RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
'label' => 'Old Label',
'slug' => 'old-label',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->event->id}/registration-fields/{$field->id}", [
'label' => 'New Label',
]);
$response->assertOk()
->assertJsonPath('data.label', 'New Label')
->assertJsonPath('data.slug', 'new-label');
}
public function test_cannot_change_field_type(): void
{
$field = RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
'field_type' => RegistrationFieldType::TEXT,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/events/{$this->event->id}/registration-fields/{$field->id}", [
'field_type' => 'select',
]);
// field_type is not in UpdateRegistrationFormFieldRequest rules, so it's ignored
$response->assertOk();
$this->assertDatabaseHas('registration_form_fields', [
'id' => $field->id,
'field_type' => 'text',
]);
}
public function test_destroy_deletes_field(): void
{
$field = RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->deleteJson("/api/v1/events/{$this->event->id}/registration-fields/{$field->id}");
$response->assertNoContent();
$this->assertDatabaseMissing('registration_form_fields', [
'id' => $field->id,
]);
}
public function test_reorder_fields(): void
{
$fieldA = RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
'sort_order' => 0,
]);
$fieldB = RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
'sort_order' => 1,
]);
$fieldC = RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
'sort_order' => 2,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/events/{$this->event->id}/registration-fields/reorder", [
'ids' => [$fieldC->id, $fieldA->id, $fieldB->id],
]);
$response->assertNoContent();
$this->assertDatabaseHas('registration_form_fields', ['id' => $fieldC->id, 'sort_order' => 0]);
$this->assertDatabaseHas('registration_form_fields', ['id' => $fieldA->id, 'sort_order' => 1]);
$this->assertDatabaseHas('registration_form_fields', ['id' => $fieldB->id, 'sort_order' => 2]);
}
public function test_import_from_event(): void
{
$sourceEvent = Event::factory()->create(['organisation_id' => $this->organisation->id]);
RegistrationFormField::factory()->create([
'event_id' => $sourceEvent->id,
'label' => 'Shirtmaat',
'slug' => 'shirtmaat',
'field_type' => RegistrationFieldType::SELECT,
'options' => ['S', 'M', 'L'],
]);
RegistrationFormField::factory()->create([
'event_id' => $sourceEvent->id,
'label' => 'Opmerkingen',
'slug' => 'opmerkingen',
'field_type' => RegistrationFieldType::TEXTAREA,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/events/{$this->event->id}/registration-fields/import-from-event", [
'source_event_id' => $sourceEvent->id,
]);
$response->assertOk();
$this->assertDatabaseHas('registration_form_fields', [
'event_id' => $this->event->id,
'slug' => 'shirtmaat',
]);
$this->assertDatabaseHas('registration_form_fields', [
'event_id' => $this->event->id,
'slug' => 'opmerkingen',
]);
}
public function test_import_from_different_org_rejected(): void
{
$otherEvent = Event::factory()->create(['organisation_id' => $this->otherOrganisation->id]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/events/{$this->event->id}/registration-fields/import-from-event", [
'source_event_id' => $otherEvent->id,
]);
$response->assertUnprocessable()
->assertJsonValidationErrors('source_event_id');
}
public function test_from_template_creates_copy(): void
{
$template = RegistrationFieldTemplate::factory()->selectField()->create([
'organisation_id' => $this->organisation->id,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/events/{$this->event->id}/registration-fields/from-template", [
'template_id' => $template->id,
]);
$response->assertCreated()
->assertJsonPath('data.label', $template->label)
->assertJsonPath('data.field_type', $template->field_type->value);
$this->assertDatabaseHas('registration_form_fields', [
'event_id' => $this->event->id,
'label' => $template->label,
]);
}
public function test_cross_org_returns_403(): void
{
Sanctum::actingAs($this->outsider);
$response = $this->getJson("/api/v1/events/{$this->event->id}/registration-fields");
$response->assertForbidden();
}
public function test_unauthenticated_returns_401(): void
{
$response = $this->getJson("/api/v1/events/{$this->event->id}/registration-fields");
$response->assertUnauthorized();
}
}

View File

@@ -0,0 +1,335 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\TagSync;
use App\Enums\RegistrationFieldType;
use App\Models\CrowdType;
use App\Models\Event;
use App\Models\Organisation;
use App\Models\Person;
use App\Models\PersonFieldValue;
use App\Models\PersonTag;
use App\Models\RegistrationFormField;
use App\Models\User;
use App\Models\UserOrganisationTag;
use App\Services\TagSyncService;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class TagSyncServiceTest extends TestCase
{
use RefreshDatabase;
private Organisation $organisation;
private Event $event;
private CrowdType $crowdType;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->organisation = Organisation::factory()->create();
$this->event = Event::factory()->create([
'organisation_id' => $this->organisation->id,
]);
$this->crowdType = CrowdType::factory()->systemType('VOLUNTEER')->create([
'organisation_id' => $this->organisation->id,
]);
}
public function test_sync_creates_self_reported_tags_when_person_has_user_id(): void
{
$user = User::factory()->create();
$person = Person::factory()->create([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
'user_id' => $user->id,
]);
$tag = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
]);
$field = RegistrationFormField::factory()->tagPickerField()->create([
'event_id' => $this->event->id,
]);
PersonFieldValue::create([
'person_id' => $person->id,
'registration_form_field_id' => $field->id,
'selected_options' => [$tag->id],
]);
(new TagSyncService())->syncFromRegistration($person);
$this->assertDatabaseHas('user_organisation_tags', [
'user_id' => $user->id,
'organisation_id' => $this->organisation->id,
'person_tag_id' => $tag->id,
'source' => 'self_reported',
]);
}
public function test_sync_skips_when_person_has_no_user_id(): void
{
$person = Person::factory()->create([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
'user_id' => null,
]);
$tag = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
]);
$field = RegistrationFormField::factory()->tagPickerField()->create([
'event_id' => $this->event->id,
]);
PersonFieldValue::create([
'person_id' => $person->id,
'registration_form_field_id' => $field->id,
'selected_options' => [$tag->id],
]);
(new TagSyncService())->syncFromRegistration($person);
$this->assertDatabaseCount('user_organisation_tags', 0);
}
public function test_sync_does_not_touch_organiser_assigned_tags(): void
{
$user = User::factory()->create();
$person = Person::factory()->create([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
'user_id' => $user->id,
]);
$organisedTag = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
]);
UserOrganisationTag::create([
'user_id' => $user->id,
'organisation_id' => $this->organisation->id,
'person_tag_id' => $organisedTag->id,
'source' => 'organiser_assigned',
'assigned_at' => now(),
]);
$selfTag = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
]);
$field = RegistrationFormField::factory()->tagPickerField()->create([
'event_id' => $this->event->id,
]);
PersonFieldValue::create([
'person_id' => $person->id,
'registration_form_field_id' => $field->id,
'selected_options' => [$selfTag->id],
]);
(new TagSyncService())->syncFromRegistration($person);
$this->assertDatabaseHas('user_organisation_tags', [
'user_id' => $user->id,
'organisation_id' => $this->organisation->id,
'person_tag_id' => $organisedTag->id,
'source' => 'organiser_assigned',
]);
$this->assertDatabaseHas('user_organisation_tags', [
'user_id' => $user->id,
'organisation_id' => $this->organisation->id,
'person_tag_id' => $selfTag->id,
'source' => 'self_reported',
]);
}
public function test_sync_removes_self_reported_tags_no_longer_in_selection(): void
{
$user = User::factory()->create();
$person = Person::factory()->create([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
'user_id' => $user->id,
]);
$tag = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
]);
UserOrganisationTag::create([
'user_id' => $user->id,
'organisation_id' => $this->organisation->id,
'person_tag_id' => $tag->id,
'source' => 'self_reported',
'assigned_at' => now(),
]);
$field = RegistrationFormField::factory()->tagPickerField()->create([
'event_id' => $this->event->id,
]);
PersonFieldValue::create([
'person_id' => $person->id,
'registration_form_field_id' => $field->id,
'selected_options' => [],
]);
(new TagSyncService())->syncFromRegistration($person);
$this->assertDatabaseMissing('user_organisation_tags', [
'user_id' => $user->id,
'person_tag_id' => $tag->id,
'source' => 'self_reported',
]);
}
public function test_sync_is_idempotent(): void
{
$user = User::factory()->create();
$person = Person::factory()->create([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
'user_id' => $user->id,
]);
$tag = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
]);
$field = RegistrationFormField::factory()->tagPickerField()->create([
'event_id' => $this->event->id,
]);
PersonFieldValue::create([
'person_id' => $person->id,
'registration_form_field_id' => $field->id,
'selected_options' => [$tag->id],
]);
$service = new TagSyncService();
$service->syncFromRegistration($person);
$service->syncFromRegistration($person);
$count = UserOrganisationTag::where('user_id', $user->id)
->where('organisation_id', $this->organisation->id)
->where('person_tag_id', $tag->id)
->where('source', 'self_reported')
->count();
$this->assertEquals(1, $count);
}
public function test_sync_handles_empty_tag_selection(): void
{
$user = User::factory()->create();
$person = Person::factory()->create([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
'user_id' => $user->id,
]);
$tagA = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
]);
$tagB = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
]);
UserOrganisationTag::create([
'user_id' => $user->id,
'organisation_id' => $this->organisation->id,
'person_tag_id' => $tagA->id,
'source' => 'self_reported',
'assigned_at' => now(),
]);
UserOrganisationTag::create([
'user_id' => $user->id,
'organisation_id' => $this->organisation->id,
'person_tag_id' => $tagB->id,
'source' => 'self_reported',
'assigned_at' => now(),
]);
$field = RegistrationFormField::factory()->tagPickerField()->create([
'event_id' => $this->event->id,
]);
PersonFieldValue::create([
'person_id' => $person->id,
'registration_form_field_id' => $field->id,
'selected_options' => [],
]);
(new TagSyncService())->syncFromRegistration($person);
$count = UserOrganisationTag::where('user_id', $user->id)
->where('source', 'self_reported')
->count();
$this->assertEquals(0, $count);
}
public function test_sync_works_across_multiple_tag_picker_fields(): void
{
$user = User::factory()->create();
$person = Person::factory()->create([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
'user_id' => $user->id,
]);
$tagA = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
]);
$tagB = PersonTag::factory()->create([
'organisation_id' => $this->organisation->id,
]);
$field1 = RegistrationFormField::factory()->tagPickerField()->create([
'event_id' => $this->event->id,
'label' => 'Vaardigheden',
'slug' => 'vaardigheden',
]);
$field2 = RegistrationFormField::factory()->tagPickerField()->create([
'event_id' => $this->event->id,
'label' => 'Talen',
'slug' => 'talen',
]);
PersonFieldValue::create([
'person_id' => $person->id,
'registration_form_field_id' => $field1->id,
'selected_options' => [$tagA->id],
]);
PersonFieldValue::create([
'person_id' => $person->id,
'registration_form_field_id' => $field2->id,
'selected_options' => [$tagB->id],
]);
(new TagSyncService())->syncFromRegistration($person);
$this->assertDatabaseHas('user_organisation_tags', [
'user_id' => $user->id,
'organisation_id' => $this->organisation->id,
'person_tag_id' => $tagA->id,
'source' => 'self_reported',
]);
$this->assertDatabaseHas('user_organisation_tags', [
'user_id' => $user->id,
'organisation_id' => $this->organisation->id,
'person_tag_id' => $tagB->id,
'source' => 'self_reported',
]);
}
}