opnform/tests/Feature/Forms/FormLogicTest.php

116 lines
4.0 KiB
PHP
Raw Permalink Normal View History

2022-09-20 19:59:52 +00:00
<?php
2024-02-23 10:54:12 +00:00
use Illuminate\Testing\Fluent\AssertableJson;
2022-09-20 19:59:52 +00:00
it('create form with logic', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, [
'properties' => [
[
2024-02-23 10:54:12 +00:00
'id' => 'title',
'name' => 'Name',
2022-09-20 19:59:52 +00:00
'type' => 'title',
'hidden' => false,
'required' => true,
'logic' => [
2024-02-23 10:54:12 +00:00
'conditions' => [
'operatorIdentifier' => 'and',
'children' => [
2022-09-20 19:59:52 +00:00
[
2024-02-23 10:54:12 +00:00
'identifier' => 'email',
'value' => [
'operator' => 'is_empty',
'property_meta' => [
'id' => '93ea3198-353f-440b-8dc9-2ac9a7bee124',
'type' => 'email',
2022-09-20 19:59:52 +00:00
],
2024-02-23 10:54:12 +00:00
'value' => true,
],
],
],
2022-09-20 19:59:52 +00:00
],
2024-02-23 10:54:12 +00:00
'actions' => ['make-it-optional'],
],
],
2022-09-20 19:59:52 +00:00
],
]);
$this->getJson(route('forms.show', $form->slug))
->assertSuccessful()
->assertJson(function (AssertableJson $json) use ($form) {
return $json->where('id', $form->id)
2024-02-23 10:54:12 +00:00
->where('properties', function ($values) {
return count($values[0]['logic']) > 0;
2022-09-20 19:59:52 +00:00
})
->etc();
});
// Should submit form
2024-02-23 10:54:12 +00:00
$forData = ['93ea3198-353f-440b-8dc9-2ac9a7bee124' => ''];
2022-09-20 19:59:52 +00:00
$this->postJson(route('forms.answer', $form->slug), $forData)
->assertSuccessful()
->assertJson([
'type' => 'success',
2024-02-23 10:54:12 +00:00
'message' => 'Form submission saved.',
2022-09-20 19:59:52 +00:00
]);
});
it('create form with multi select logic', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, [
'properties' => [
[
2024-02-23 10:54:12 +00:00
'id' => 'title',
'name' => 'Name',
'type' => 'title',
'hidden' => false,
'required' => false,
'logic' => [
2024-02-23 10:54:12 +00:00
'conditions' => [
'operatorIdentifier' => 'and',
'children' => [
[
2024-02-23 10:54:12 +00:00
'identifier' => 'multi_select',
'value' => [
'operator' => 'contains',
'property_meta' => [
'id' => '93ea3198-353f-440b-8dc9-2ac9a7bee124',
'type' => 'multi_select',
],
2024-02-23 10:54:12 +00:00
'value' => 'One',
],
],
],
],
2024-02-23 10:54:12 +00:00
'actions' => ['require-answer'],
],
],
],
]);
$this->getJson(route('forms.show', $form->slug))
->assertSuccessful()
->assertJson(function (AssertableJson $json) use ($form) {
return $json->where('id', $form->id)
2024-02-23 10:54:12 +00:00
->where('properties', function ($values) {
return count($values[0]['logic']) > 0;
})
->etc();
});
// Should submit form
2024-02-23 10:54:12 +00:00
$forData = ['93ea3198-353f-440b-8dc9-2ac9a7bee124' => ['One']];
$this->postJson(route('forms.answer', $form->slug), $forData)
->assertStatus(422)
->assertJson([
'message' => 'The Name field is required.',
'errors' => [
'title' => [
'The Name field is required.',
],
],
2024-02-23 10:54:12 +00:00
]);
});