Files
crewli/api/tests/Feature/RegistrationFormField/RegistrationFormFieldTest.php
bert.hausmans 6a8d21a5b6 feat: registration field polish, multi-category tags, file uploads, Partner icon
- Restructure field editor dialog: move Options section to bottom with
  divider and subheader, fix delete button with flex layout
- Change tag_category (single string) to tag_categories (JSON array)
  supporting multiple category selection in tag picker fields
- Portal tag picker now groups tags by category with subheaders
- Add generic file upload endpoint (FileUploadService + UploadController)
- Replace email branding logo URL text field with ImageUploadField
- Update Partner crowd type default icon to tabler-affiliate
- Apply changes consistently to both field and template dialogs

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

550 lines
19 KiB
PHP

<?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/organisations/{$this->organisation->id}/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/organisations/{$this->organisation->id}/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/organisations/{$this->organisation->id}/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/organisations/{$this->organisation->id}/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_categories(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields", [
'label' => 'Vaardigheden',
'field_type' => 'tag_picker',
'tag_categories' => ['Vaardigheid', 'Horeca'],
]);
$response->assertCreated()
->assertJsonPath('data.tag_categories', ['Vaardigheid', 'Horeca']);
}
public function test_store_text_field_rejects_tag_categories(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields", [
'label' => 'Naam',
'field_type' => 'text',
'tag_categories' => ['Vaardigheid'],
]);
$response->assertUnprocessable()
->assertJsonValidationErrors('tag_categories');
}
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/organisations/{$this->organisation->id}/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/organisations/{$this->organisation->id}/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/organisations/{$this->organisation->id}/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/organisations/{$this->organisation->id}/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/organisations/{$this->organisation->id}/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/organisations/{$this->organisation->id}/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/organisations/{$this->organisation->id}/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/organisations/{$this->organisation->id}/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/organisations/{$this->organisation->id}/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/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields");
$response->assertForbidden();
}
public function test_unauthenticated_returns_401(): void
{
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields");
$response->assertUnauthorized();
}
public function test_store_field_with_display_width(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields", [
'label' => 'Noodcontact naam',
'field_type' => 'text',
'display_width' => 'half',
]);
$response->assertCreated()
->assertJsonPath('data.display_width', 'half');
$this->assertDatabaseHas('registration_form_fields', [
'event_id' => $this->event->id,
'slug' => 'noodcontact-naam',
'display_width' => 'half',
]);
}
public function test_store_field_defaults_display_width_by_type(): void
{
Sanctum::actingAs($this->orgAdmin);
// Text fields default to 'half'
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields", [
'label' => 'Korte tekst',
'field_type' => 'text',
]);
$response->assertCreated()
->assertJsonPath('data.display_width', 'half');
// Textarea fields default to 'full'
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields", [
'label' => 'Opmerkingen',
'field_type' => 'textarea',
]);
$response->assertCreated()
->assertJsonPath('data.display_width', 'full');
}
public function test_update_field_display_width(): void
{
$field = RegistrationFormField::factory()->create([
'event_id' => $this->event->id,
'display_width' => 'full',
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->putJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields/{$field->id}", [
'display_width' => 'half',
]);
$response->assertOk()
->assertJsonPath('data.display_width', 'half');
}
public function test_store_field_with_option_descriptions(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields", [
'label' => 'Vergoeding',
'field_type' => 'radio',
'options' => [
['label' => 'Pro Deo', 'description' => 'Geen vergoeding'],
['label' => 'Entreeticket', 'description' => 'Gratis ticket'],
],
]);
$response->assertCreated()
->assertJsonPath('data.normalized_options.0.label', 'Pro Deo')
->assertJsonPath('data.normalized_options.0.description', 'Geen vergoeding')
->assertJsonPath('data.normalized_options.1.label', 'Entreeticket')
->assertJsonPath('data.normalized_options.1.description', 'Gratis ticket');
}
public function test_options_backwards_compatible_with_string_array(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields", [
'label' => 'Shirtmaat',
'field_type' => 'select',
'options' => ['XS', 'S', 'M', 'L'],
]);
$response->assertCreated()
->assertJsonPath('data.normalized_options.0.label', 'XS')
->assertJsonPath('data.normalized_options.0.description', null)
->assertJsonPath('data.normalized_options.3.label', 'L');
}
public function test_normalized_options_converts_strings_to_objects(): void
{
$field = RegistrationFormField::factory()->selectField()->create([
'event_id' => $this->event->id,
]);
$this->assertNotNull($field->normalized_options);
$this->assertIsArray($field->normalized_options);
// Each option should be an array with label and description keys
foreach ($field->normalized_options as $option) {
$this->assertArrayHasKey('label', $option);
$this->assertArrayHasKey('description', $option);
$this->assertIsString($option['label']);
}
}
public function test_index_returns_display_width_and_normalized_options(): void
{
RegistrationFormField::factory()->radioField()->create([
'event_id' => $this->event->id,
'sort_order' => 0,
]);
Sanctum::actingAs($this->orgAdmin);
$response = $this->getJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields");
$response->assertOk();
$field = $response->json('data.0');
$this->assertArrayHasKey('display_width', $field);
$this->assertArrayHasKey('normalized_options', $field);
$this->assertEquals('full', $field['display_width']);
$this->assertNotNull($field['normalized_options']);
}
public function test_store_heading_field_without_options(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields", [
'label' => 'Persoonlijke voorkeuren',
'field_type' => 'heading',
'help_text' => 'Vertel ons wat we over jou moeten weten',
]);
$response->assertCreated()
->assertJsonPath('data.field_type', 'heading')
->assertJsonPath('data.label', 'Persoonlijke voorkeuren')
->assertJsonPath('data.help_text', 'Vertel ons wat we over jou moeten weten')
->assertJsonPath('data.display_width', 'full');
}
public function test_heading_field_skips_options_validation(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields", [
'label' => 'Sectiekop',
'field_type' => 'heading',
]);
$response->assertCreated();
}
public function test_heading_field_display_width_defaults_to_full(): void
{
Sanctum::actingAs($this->orgAdmin);
$response = $this->postJson("/api/v1/organisations/{$this->organisation->id}/events/{$this->event->id}/registration-fields", [
'label' => 'Noodcontact',
'field_type' => 'heading',
]);
$response->assertCreated()
->assertJsonPath('data.display_width', 'full');
}
public function test_seeder_creates_heading_fields(): void
{
\App\Services\RegistrationFieldTemplateService::seedSystemTemplates($this->organisation);
$headingCount = \App\Models\RegistrationFieldTemplate::where('organisation_id', $this->organisation->id)
->where('is_system', true)
->where('field_type', 'heading')
->count();
$this->assertEquals(5, $headingCount);
}
}