opnform/tests/Feature/Forms/FormPropertyLogicTest.php

202 lines
7.1 KiB
PHP
Raw Normal View History

2024-02-23 10:54:12 +00:00
<?php
2022-09-20 19:59:52 +00:00
use App\Rules\FormPropertyLogicRule;
it('can validate form logic rules for actions', function () {
$rules = [
2024-02-23 10:54:12 +00:00
'properties.*.logic' => ['array', 'nullable', new FormPropertyLogicRule()],
2022-09-20 19:59:52 +00:00
];
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
$data = [
'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' => false,
'logic' => [
2024-02-23 10:54:12 +00:00
'conditions' => null,
'actions' => [],
],
],
],
2022-09-20 19:59:52 +00:00
];
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertTrue($validatorObj->passes());
$data = [
'properties' => [
[
2024-02-23 10:54:12 +00:00
'id' => 'title',
'name' => 'Name',
2022-09-20 19:59:52 +00:00
'type' => 'title',
'hidden' => true,
'required' => false,
'logic' => [
2024-02-23 10:54:12 +00:00
'conditions' => [
'operatorIdentifier' => 'and',
'children' => [
[
2024-02-23 10:54:12 +00:00
'identifier' => 'title',
'value' => [
'operator' => 'equals',
'property_meta' => [
'id' => 'title',
'type' => 'text',
],
2024-02-23 10:54:12 +00:00
'value' => 'TEST',
],
],
],
],
2024-02-23 10:54:12 +00:00
'actions' => ['hide-block'],
],
],
],
2022-09-20 19:59:52 +00:00
];
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertFalse($validatorObj->passes());
2024-02-23 10:54:12 +00:00
expect($validatorObj->errors()->messages()['properties.0.logic'][0])->toBe('The logic actions for Name are not valid.');
2022-09-20 19:59:52 +00:00
$data = [
'properties' => [
[
2024-02-23 10:54:12 +00:00
'id' => 'text',
'name' => 'Custom Test',
2022-09-20 19:59:52 +00:00
'type' => 'nf-text',
'logic' => [
2024-02-23 10:54:12 +00:00
'conditions' => [
'operatorIdentifier' => 'and',
'children' => [
[
2024-02-23 10:54:12 +00:00
'identifier' => 'title',
'value' => [
'operator' => 'equals',
'property_meta' => [
'id' => 'title',
'type' => 'text',
],
2024-02-23 10:54:12 +00:00
'value' => 'TEST',
],
],
],
],
2024-02-23 10:54:12 +00:00
'actions' => ['require-answer'],
],
],
],
2022-09-20 19:59:52 +00:00
];
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertFalse($validatorObj->passes());
2024-02-23 10:54:12 +00:00
expect($validatorObj->errors()->messages()['properties.0.logic'][0])->toBe('The logic actions for Custom Test are not valid.');
2022-09-20 19:59:52 +00:00
});
it('can validate form logic rules for conditions', function () {
$rules = [
2024-02-23 10:54:12 +00:00
'properties.*.logic' => ['array', 'nullable', new FormPropertyLogicRule()],
2022-09-20 19:59:52 +00:00
];
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
$data = [
'properties' => [
[
2024-02-23 10:54:12 +00:00
'id' => 'title',
'name' => 'Name',
2022-09-20 19:59:52 +00:00
'type' => 'text',
'hidden' => false,
'required' => false,
'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' => 'title',
'value' => [
'operator' => 'equals',
'property_meta' => [
'id' => 'title',
'type' => 'text',
2022-09-20 19:59:52 +00:00
],
2024-02-23 10:54:12 +00:00
'value' => 'TEST',
],
],
],
2022-09-20 19:59:52 +00:00
],
2024-02-23 10:54:12 +00:00
'actions' => ['hide-block'],
],
],
],
2022-09-20 19:59:52 +00:00
];
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertTrue($validatorObj->passes());
$data = [
'properties' => [
[
2024-02-23 10:54:12 +00:00
'id' => 'title',
'name' => 'Name',
2022-09-20 19:59:52 +00:00
'type' => 'text',
'hidden' => false,
'required' => false,
'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' => 'title',
'value' => [
'operator' => 'starts_with',
'property_meta' => [
'id' => 'title',
'type' => 'text',
2022-09-20 19:59:52 +00:00
],
2024-02-23 10:54:12 +00:00
],
],
],
2022-09-20 19:59:52 +00:00
],
2024-02-23 10:54:12 +00:00
'actions' => ['hide-block'],
],
],
],
2022-09-20 19:59:52 +00:00
];
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertFalse($validatorObj->passes());
2024-02-23 10:54:12 +00: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 10:54:12 +00:00
'id' => 'title',
'name' => 'Name',
'type' => 'text',
'hidden' => false,
'required' => false,
'logic' => [
2024-02-23 10:54:12 +00:00
'conditions' => [
'operatorIdentifier' => null,
'children' => [
[
2024-02-23 10:54:12 +00:00
'identifier' => 'title',
'value' => [
'operator' => 'starts_with',
'property_meta' => [
'id' => 'title',
'type' => 'text',
],
2024-02-23 10:54:12 +00:00
],
],
],
],
2024-02-23 10:54:12 +00:00
'actions' => ['hide-block'],
],
],
],
];
2024-02-23 10:54:12 +00:00
$validatorObj = $this->app['validator']->make($data, $rules);
$this->assertFalse($validatorObj->passes());
2024-02-23 10:54:12 +00:00
expect($validatorObj->errors()->messages()['properties.0.logic'][0])->toBe('The logic conditions for Name are not complete. Error detail(s): missing operator');
});