opnform-host-nginx/api/tests/Feature/Forms/FormPropertyLogicTest.php

256 lines
9.5 KiB
PHP
Raw Normal View History

2024-02-23 11:54:12 +01:00
<?php
2022-09-20 21:59:52 +02:00
use App\Rules\FormPropertyLogicRule;
it('can validate form logic rules for actions', function () {
$rules = [
2024-02-23 11:54:12 +01:00
'properties.*.logic' => ['array', 'nullable', new FormPropertyLogicRule()],
2022-09-20 21:59:52 +02:00
];
2024-02-23 11:54:12 +01:00
2022-09-20 21:59:52 +02:00
$data = [
'properties' => [
[
2024-02-23 11:54:12 +01:00
'id' => 'title',
'name' => 'Name',
2022-09-20 21:59:52 +02:00
'type' => 'title',
'hidden' => false,
'required' => false,
'logic' => [
2024-02-23 11:54:12 +01:00
'conditions' => null,
'actions' => [],
],
],
],
2022-09-20 21:59:52 +02:00
];
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertTrue($validatorObj->passes());
$data = [
'properties' => [
[
2024-02-23 11:54:12 +01:00
'id' => 'title',
'name' => 'Name',
2022-09-20 21:59:52 +02:00
'type' => 'title',
'hidden' => true,
'required' => false,
'logic' => [
2024-02-23 11:54:12 +01:00
'conditions' => [
'operatorIdentifier' => 'and',
'children' => [
[
2024-02-23 11:54:12 +01:00
'identifier' => 'title',
'value' => [
'operator' => 'equals',
'property_meta' => [
'id' => 'title',
'type' => 'text',
],
2024-02-23 11:54:12 +01:00
'value' => 'TEST',
],
],
],
],
2024-02-23 11:54:12 +01:00
'actions' => ['hide-block'],
],
],
],
2022-09-20 21:59:52 +02:00
];
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertFalse($validatorObj->passes());
2024-02-23 11:54:12 +01:00
expect($validatorObj->errors()->messages()['properties.0.logic'][0])->toBe('The logic actions for Name are not valid.');
2022-09-20 21:59:52 +02:00
$data = [
'properties' => [
[
2024-02-23 11:54:12 +01:00
'id' => 'text',
'name' => 'Custom Test',
2022-09-20 21:59:52 +02:00
'type' => 'nf-text',
'logic' => [
2024-02-23 11:54:12 +01:00
'conditions' => [
'operatorIdentifier' => 'and',
'children' => [
[
2024-02-23 11:54:12 +01:00
'identifier' => 'title',
'value' => [
'operator' => 'equals',
'property_meta' => [
'id' => 'title',
'type' => 'text',
],
2024-02-23 11:54:12 +01:00
'value' => 'TEST',
],
],
],
],
2024-02-23 11:54:12 +01:00
'actions' => ['require-answer'],
],
],
],
2022-09-20 21:59:52 +02:00
];
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertFalse($validatorObj->passes());
2024-02-23 11:54:12 +01:00
expect($validatorObj->errors()->messages()['properties.0.logic'][0])->toBe('The logic actions for Custom Test are not valid.');
2022-09-20 21:59:52 +02:00
});
it('can validate form logic rules for conditions', function () {
$rules = [
2024-02-23 11:54:12 +01:00
'properties.*.logic' => ['array', 'nullable', new FormPropertyLogicRule()],
2022-09-20 21:59:52 +02:00
];
2024-02-23 11:54:12 +01:00
2022-09-20 21:59:52 +02:00
$data = [
'properties' => [
[
2024-02-23 11:54:12 +01:00
'id' => 'title',
'name' => 'Name',
2022-09-20 21:59:52 +02:00
'type' => 'text',
'hidden' => false,
'required' => false,
'logic' => [
2024-02-23 11:54:12 +01:00
'conditions' => [
'operatorIdentifier' => 'and',
'children' => [
2022-09-20 21:59:52 +02:00
[
2024-02-23 11:54:12 +01:00
'identifier' => 'title',
'value' => [
'operator' => 'equals',
'property_meta' => [
'id' => 'title',
'type' => 'text',
2022-09-20 21:59:52 +02:00
],
2024-02-23 11:54:12 +01:00
'value' => 'TEST',
],
],
],
2022-09-20 21:59:52 +02:00
],
2024-02-23 11:54:12 +01:00
'actions' => ['hide-block'],
],
],
],
2022-09-20 21:59:52 +02:00
];
2024-02-23 11:54:12 +01:00
2022-09-20 21:59:52 +02:00
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertTrue($validatorObj->passes());
$data = [
'properties' => [
[
2024-02-23 11:54:12 +01:00
'id' => 'title',
'name' => 'Name',
2022-09-20 21:59:52 +02:00
'type' => 'text',
'hidden' => false,
'required' => false,
'logic' => [
2024-02-23 11:54:12 +01:00
'conditions' => [
'operatorIdentifier' => 'and',
'children' => [
2022-09-20 21:59:52 +02:00
[
2024-02-23 11:54:12 +01:00
'identifier' => 'title',
'value' => [
'operator' => 'starts_with',
'property_meta' => [
'id' => 'title',
'type' => 'text',
2022-09-20 21:59:52 +02:00
],
2024-02-23 11:54:12 +01:00
],
],
],
2022-09-20 21:59:52 +02:00
],
2024-02-23 11:54:12 +01:00
'actions' => ['hide-block'],
],
],
],
2022-09-20 21:59:52 +02:00
];
2024-02-23 11:54:12 +01:00
2022-09-20 21:59:52 +02:00
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertFalse($validatorObj->passes());
2024-02-23 11:54:12 +01:00
expect($validatorObj->errors()->messages()['properties.0.logic'][0])->toBe('The logic conditions for Name are not complete. Error detail(s): missing condition value');
$data = [
'properties' => [
[
2024-02-23 11:54:12 +01:00
'id' => 'title',
'name' => 'Name',
'type' => 'text',
'hidden' => false,
'required' => false,
'logic' => [
2024-02-23 11:54:12 +01:00
'conditions' => [
'operatorIdentifier' => null,
'children' => [
[
2024-02-23 11:54:12 +01:00
'identifier' => 'title',
'value' => [
'operator' => 'starts_with',
'property_meta' => [
'id' => 'title',
'type' => 'text',
],
2024-02-23 11:54:12 +01:00
],
],
],
],
2024-02-23 11:54:12 +01:00
'actions' => ['hide-block'],
],
],
],
];
2024-02-23 11:54:12 +01:00
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertFalse($validatorObj->passes());
2024-02-23 11:54:12 +01:00
expect($validatorObj->errors()->messages()['properties.0.logic'][0])->toBe('The logic conditions for Name are not complete. Error detail(s): missing operator');
});
it('can validate form logic rules for operators without values', function () {
$rules = [
'properties.*.logic' => ['array', 'nullable', new FormPropertyLogicRule()],
];
// Test checkbox is_checked without value
$data = [
'properties' => [
[
'id' => 'checkbox1',
'name' => 'Checkbox Field',
'type' => 'checkbox',
'logic' => [
'conditions' => [
'operatorIdentifier' => 'and',
'children' => [
[
'identifier' => 'test-id',
'value' => [
'operator' => 'is_checked',
'property_meta' => [
'id' => 'test-id',
'type' => 'checkbox'
]
]
]
]
],
'actions' => ['show-block']
]
]
]
];
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertTrue($validatorObj->passes());
// Test checkbox is_checked with value (should still pass for backward compatibility)
$data['properties'][0]['logic']['conditions']['children'][0]['value']['value'] = true;
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertTrue($validatorObj->passes());
// Test checkbox is_not_checked without value
$data['properties'][0]['logic']['conditions']['children'][0]['value']['operator'] = 'is_not_checked';
unset($data['properties'][0]['logic']['conditions']['children'][0]['value']['value']);
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertTrue($validatorObj->passes());
// Test checkbox with operator that doesn't exist
$data['properties'][0]['logic']['conditions']['children'][0]['value']['operator'] = 'invalid_operator';
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertFalse($validatorObj->passes());
expect($validatorObj->errors()->messages()['properties.0.logic'][0])->toBe('The logic conditions for Checkbox Field are not complete. Error detail(s): configuration not found for condition operator');
});