summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2022-01-06 01:36:07 +0100
committerDaniel Weipert <code@drogueronin.de>2022-01-10 11:29:13 +0100
commit75f3e8ab4b3d65d6eba16575ca0a4a669b7e27ac (patch)
treeb9b60559afbfff883dfe8aaf7b70e9b8b6bc1c4f /tests
parent0b0af86264107a9c95ff37a1b3fdc845aa7a27b2 (diff)
Validate fields with custom function
Diffstat (limited to 'tests')
-rw-r--r--tests/Test.php43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/Test.php b/tests/Test.php
index 37e5f65..b8a02f3 100644
--- a/tests/Test.php
+++ b/tests/Test.php
@@ -22,6 +22,7 @@ class Test extends TestCase
@mkdir($contentRoot . '/config');
@mkdir($contentRoot . '/customername/formname/config');
@mkdir($contentRoot . '/customername/formname/fields');
+ @mkdir($contentRoot . '/customername/pagedform/config');
@mkdir($contentRoot . '/customername/pagedform/fields');
# root config
@@ -78,6 +79,18 @@ class Test extends TestCase
data-value = 123
EOF);
+ file_put_contents($contentRoot . '/customername/formname/config/functions.php', '
+ <?php
+ function validate_formname_name($field, $value)
+ {
+ if ($value !== \'Adelbert\') {
+ $field[\'is_valid\'] = false;
+ }
+
+ return $field;
+ }
+ ');
+
# pagedform fields
file_put_contents($contentRoot . '/customername/pagedform/fields/_fields.toml', <<<EOF
[page.one.field.name]
@@ -273,6 +286,36 @@ class Test extends TestCase
$this->assertArrayNotHasKey('second', $body);
$this->assertEquals(true, $body['date']['is_valid']);
$this->assertEquals(true, $body['text']['is_valid']);
+
+ // valid response validation_function
+ $response = $this->request('POST', 'customername/formname/submit?key=' . $this->apiKey, [
+ 'form_params' => [
+ 'name' => 'Adelbert',
+ 'email' => 'EMAIL',
+ 'date' => 'DATE',
+ ],
+ ]);
+ $body = json_decode((string)$response->getBody(), true);
+ $this->assertArrayNotHasKey('error', $body);
+ $body = $body['data'];
+ $this->assertEquals(true, $body['name']['is_valid']);
+ $this->assertEquals(true, $body['email']['is_valid']);
+ $this->assertEquals(true, $body['date']['is_valid']);
+
+ // invalid response validation_function
+ $response = $this->request('POST', 'customername/formname/submit?key=' . $this->apiKey, [
+ 'form_params' => [
+ 'name' => 'NAME',
+ 'email' => 'EMAIL',
+ 'date' => 'DATE',
+ ],
+ ]);
+ $body = json_decode((string)$response->getBody(), true);
+ $this->assertArrayHasKey('error', $body);
+ $body = $body['data'];
+ $this->assertEquals(false, $body['name']['is_valid']);
+ $this->assertEquals(true, $body['email']['is_valid']);
+ $this->assertEquals(true, $body['date']['is_valid']);
}
}