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' => [
2023-08-30 11:28:52 +00:00
[
2024-02-23 10:54:12 +00:00
'identifier' => 'title' ,
'value' => [
'operator' => 'equals' ,
'property_meta' => [
'id' => 'title' ,
'type' => 'text' ,
2023-08-30 11:28:52 +00:00
],
2024-02-23 10:54:12 +00:00
'value' => 'TEST' ,
],
],
],
2023-08-30 11:28:52 +00:00
],
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' => [
2023-08-30 11:28:52 +00:00
[
2024-02-23 10:54:12 +00:00
'identifier' => 'title' ,
'value' => [
'operator' => 'equals' ,
'property_meta' => [
'id' => 'title' ,
'type' => 'text' ,
2023-08-30 11:28:52 +00:00
],
2024-02-23 10:54:12 +00:00
'value' => 'TEST' ,
],
],
],
2023-08-30 11:28:52 +00:00
],
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' );
2023-03-15 17:11:25 +00:00
$data = [
'properties' => [
[
2024-02-23 10:54:12 +00:00
'id' => 'title' ,
'name' => 'Name' ,
2023-03-15 17:11:25 +00:00
'type' => 'text' ,
'hidden' => false ,
'required' => false ,
'logic' => [
2024-02-23 10:54:12 +00:00
'conditions' => [
'operatorIdentifier' => null ,
'children' => [
2023-03-15 17:11:25 +00:00
[
2024-02-23 10:54:12 +00:00
'identifier' => 'title' ,
'value' => [
'operator' => 'starts_with' ,
'property_meta' => [
'id' => 'title' ,
'type' => 'text' ,
2023-03-15 17:11:25 +00:00
],
2024-02-23 10:54:12 +00:00
],
],
],
2023-03-15 17:11:25 +00:00
],
2024-02-23 10:54:12 +00:00
'actions' => [ 'hide-block' ],
],
],
],
2023-03-15 17:11:25 +00:00
];
2024-02-23 10:54:12 +00:00
2023-03-15 17:11:25 +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' );
});