1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response as GuzzleHttpResponse;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;
class Test extends TestCase
{
protected $client;
private $apiKey = '1234';
protected function setUp(): void
{
$this->client = new Client([
'base_uri' => 'http://localhost:8080',
]);
// add content
$contentRoot = dirname(__DIR__) . '/content';
@mkdir($contentRoot . '/config', recursive: true);
@mkdir($contentRoot . '/customername/formname/config', recursive: true);
@mkdir($contentRoot . '/customername/formname/fields', recursive: true);
@mkdir($contentRoot . '/customername/pagedform/config', recursive: true);
@mkdir($contentRoot . '/customername/pagedform/fields', recursive: true);
# root config
file_put_contents($contentRoot . '/config/config.yaml', <<<EOF
app:
title: Flat-File Forms
email:
host: localhost
user: root
password: 123456
api:
keys:
- 1234
cors:
origins:
- https://localhost:8081
EOF);
# formname config
file_put_contents($contentRoot . '/customername/formname/config/config.yaml', <<<EOF
api:
keys:
- asdfghjklö0987654321
- 123
email:
host: gmx.de
EOF);
# formname fields
file_put_contents($contentRoot . '/customername/formname/fields/_fields.yaml', <<<EOF
name:
file: name.yaml
email:
file: email.yaml
required: true
date:
required: true
EOF);
file_put_contents($contentRoot . '/customername/formname/fields/email.yaml', <<<EOF
type: email
name: email
placeholder: E-Mail
attributes:
data-email: test@example.org
EOF);
file_put_contents($contentRoot . '/customername/formname/fields/name.yaml', <<<EOF
type: text
name: name
attributes:
data-value: 123
EOF);
file_put_contents($contentRoot . '/customername/formname/config/functions.php', <<<EOF
<?php
function validate_formname_name(\$field, \$value)
{
if (\$value !== 'Harry') {
\$field['is_valid'] = false;
}
return \$field;
}
global \$hooks;
\$hooks->addFilter('validator:formname:field:name', 'validate_formname_name');
EOF);
# pagedform fields
file_put_contents($contentRoot . '/customername/pagedform/fields/_fields.yaml', <<<EOF
pages:
one:
fields:
name:
file: name.yaml
email:
file: email.yaml
required: true
second:
file: second.yaml
fields:
date:
required: true
EOF);
file_put_contents($contentRoot . '/customername/pagedform/fields/second.yaml', <<<EOF
fields:
text:
placeholder: Text placeholder
validation:
pattern: "\\\d+"
manythings:
type: select
options:
first: First level
second: Second level
multiplethings:
type: checkbox
options:
- thing-1
- thing-2
- thing-3
EOF);
file_put_contents($contentRoot . '/customername/pagedform/fields/email.yaml', <<<EOF
type: email
name: email
placeholder: E-Mail
attributes:
data-email: test@example.org
EOF);
file_put_contents($contentRoot . '/customername/pagedform/fields/name.yaml', <<<EOF
type: text
name: name
attributes:
data-value: 123
EOF);
}
public function request($method, $path, $options = []): GuzzleHttpResponse
{
$response = null;
try {
$response = $this->client->request($method, $path, ['http_errors' => false] + $options);
} catch (\Exception $e) {}
return $response;
}
public function testApiKey()
{
$formnameApiKey = '123';
$rootApiKey = '1234';
// formname
$formnameResponse = $this->request('GET', 'customername/formname/fields');
$this->assertEquals(Response::HTTP_BAD_REQUEST, $formnameResponse->getStatusCode());
$formnameResponse = $this->request('GET', 'customername/formname/fields?key=321');
$this->assertEquals(Response::HTTP_UNAUTHORIZED, $formnameResponse->getStatusCode());
$formnameResponse = $this->request('GET', 'customername/formname/fields?key=' . $formnameApiKey);
$this->assertEquals(Response::HTTP_OK, $formnameResponse->getStatusCode());
$formnameResponse = $this->request('GET', 'customername/formname/fields?key=' . $rootApiKey);
$this->assertEquals(Response::HTTP_OK, $formnameResponse->getStatusCode());
// pagedform
$pagedformResponse = $this->request('GET', 'customername/pagedform/fields?key=' . $formnameApiKey);
$this->assertEquals(Response::HTTP_UNAUTHORIZED, $pagedformResponse->getStatusCode());
$pagedformResponse = $this->request('GET', 'customername/pagedform/fields?key=' . $rootApiKey);
$this->assertEquals(Response::HTTP_OK, $pagedformResponse->getStatusCode());
}
public function testFields()
{
$formnameResponse = $this->request('GET', 'customername/formname/fields?key=' . $this->apiKey);
$formnameBody = json_decode((string)$formnameResponse->getBody(), true)['data'];
$this->assertArrayHasKey('name', $formnameBody);
$this->assertArrayHasKey('email', $formnameBody);
$this->assertArrayHasKey('date', $formnameBody);
$this->assertArrayHasKey('name', $formnameBody['name']);
$this->assertArrayHasKey('name', $formnameBody['date']);
$this->assertArrayHasKey('required', $formnameBody['email']);
}
public function testFieldsPaged()
{
// all pages
$pagedformResponse = $this->request('GET', 'customername/pagedform/fields?key=' . $this->apiKey);
$pagedformBody = json_decode((string)$pagedformResponse->getBody(), true)['data'];
$this->assertArrayHasKey('one', $pagedformBody);
$this->assertArrayHasKey('second', $pagedformBody);
$this->assertArrayHasKey('name', $pagedformBody['one']);
$this->assertArrayHasKey('email', $pagedformBody['one']);
$this->assertArrayHasKey('date', $pagedformBody['second']);
$this->assertArrayHasKey('text', $pagedformBody['second']);
// page=one
$pagedformResponse = $this->request('GET', 'customername/pagedform/fields?page=one&key=' . $this->apiKey);
$pagedformBody = json_decode((string)$pagedformResponse->getBody(), true)['data'];
$this->assertArrayNotHasKey('one', $pagedformBody);
$this->assertArrayHasKey('name', $pagedformBody);
$this->assertArrayHasKey('email', $pagedformBody);
// page=second
$pagedformResponse = $this->request('GET', 'customername/pagedform/fields?page=second&key=' . $this->apiKey);
$pagedformBody = json_decode((string)$pagedformResponse->getBody(), true)['data'];
$this->assertArrayNotHasKey('second', $pagedformBody);
$this->assertArrayHasKey('date', $pagedformBody);
$this->assertArrayHasKey('text', $pagedformBody);
$this->assertArrayHasKey('first', $pagedformBody['manythings']['options']);
$this->assertEquals('thing-2', $pagedformBody['multiplethings']['options'][1]);
}
public function testValidation()
{
// valid response
$response = $this->request('POST', 'customername/pagedform/submit?key=' . $this->apiKey, [
'form_params' => [
'name' => 'NAME',
'email' => 'EMAIL',
'date' => 'DATE',
'text' => '123',
'manythings' => 'second',
'multiplethings' => [
'thing-1', 'thing-3',
],
],
]);
$body = json_decode((string)$response->getBody(), true);
$this->assertArrayNotHasKey('error', $body);
$body = $body['data'];
$this->assertEquals(true, $body['one']['email']['is_valid']);
$this->assertEquals(true, $body['second']['date']['is_valid']);
$this->assertEquals(true, $body['second']['text']['is_valid']);
// invalid response
$response = $this->request('POST', 'customername/pagedform/submit?key=' . $this->apiKey, [
'form_params' => [
'name' => 'NAME',
'date' => 'DATE',
'text' => 'einszweidrei',
],
]);
$body = json_decode((string)$response->getBody(), true);
$this->assertArrayHasKey('error', $body);
$body = $body['data'];
$this->assertEquals(false, $body['one']['email']['is_valid']);
$this->assertEquals(true, $body['second']['date']['is_valid']);
$this->assertEquals(false, $body['second']['text']['is_valid']);
// valid response "one" page
$response = $this->request('POST', 'customername/pagedform/validate?page=one&key=' . $this->apiKey, [
'form_params' => [
'name' => 'NAME',
'email' => 'EMAIL',
],
]);
$body = json_decode((string)$response->getBody(), true);
$this->assertArrayNotHasKey('error', $body);
$body = $body['data'];
$this->assertArrayNotHasKey('one', $body);
$this->assertArrayNotHasKey('second', $body);
$this->assertEquals(true, $body['name']['is_valid']);
$this->assertEquals(true, $body['email']['is_valid']);
// valid response "second" page
$response = $this->request('POST', 'customername/pagedform/validate?page=second&key=' . $this->apiKey, [
'form_params' => [
'date' => 'DATE',
'text' => '123',
],
]);
$body = json_decode((string)$response->getBody(), true);
$this->assertArrayNotHasKey('error', $body);
$body = $body['data'];
$this->assertArrayNotHasKey('one', $body);
$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' => 'Harry',
'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']);
}
}
|