create(); $field = FormField::factory()->for($schema, 'schema')->create(); $this->assertSame($schema->id, $field->schema->id); } public function test_form_field_stores_field_type_as_string(): void { $field = FormField::factory()->ofType(FormFieldType::SELECT)->create(); $this->assertSame('SELECT', $field->fresh()->field_type); $this->assertIsString($field->fresh()->field_type); } public function test_form_field_casts_binding_and_translations_to_array(): void { $field = FormField::factory()->create([ 'binding' => ['mode' => 'entity_owned', 'entity' => 'person', 'column' => 'first_name'], 'translations' => ['en' => ['label' => 'First name']], ]); $fresh = $field->fresh(); $this->assertIsArray($fresh->binding); $this->assertSame('entity_owned', $fresh->binding['mode']); $this->assertIsArray($fresh->translations); $this->assertSame('First name', $fresh->translations['en']['label']); } public function test_form_field_has_many_values(): void { $schema = FormSchema::factory()->create(); $field = FormField::factory()->for($schema, 'schema')->create(); $submissions = FormSubmission::factory()->count(2)->create(['form_schema_id' => $schema->id]); foreach ($submissions as $submission) { FormValue::create([ 'form_field_id' => $field->id, 'form_submission_id' => $submission->id, 'value' => ['value' => 'x'], ]); } $this->assertCount(2, $field->fresh()->values); } public function test_form_field_soft_deletes_preserve_values(): void { $schema = FormSchema::factory()->create(); $field = FormField::factory()->for($schema, 'schema')->create(); $submission = FormSubmission::factory()->create(['form_schema_id' => $schema->id]); $value = FormValue::factory()->create([ 'form_field_id' => $field->id, 'form_submission_id' => $submission->id, ]); $field->delete(); $this->assertNotNull(FormValue::find($value->id)); } public function test_log_field_change_creates_activity_entry(): void { Activity::query()->delete(); $field = FormField::factory()->create(); $field->logFieldChange('field.binding_changed', ['from' => null, 'to' => ['mode' => 'entity_owned']]); $entry = Activity::query() ->where('subject_type', $field->getMorphClass()) ->where('subject_id', $field->id) ->first(); $this->assertNotNull($entry); $this->assertSame('field.binding_changed', $entry->description); } }