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
|
<?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');
@mkdir($contentRoot . '/customername/formname/config');
@mkdir($contentRoot . '/customername/formname/fields');
@mkdir($contentRoot . '/customername/pagedform/fields');
# root config
file_put_contents($contentRoot . '/config/config.toml', <<<EOF
[app]
title = "Flat-File Forms"
[email]
host = "localhost"
user = "root"
password = "123456"
[api]
keys = ["1234"]
[api.cors]
origins = []
EOF);
# formname config
file_put_contents($contentRoot . '/customername/formname/config/config.toml', <<<EOF
[api]
keys = ["asdfghjklö0987654321", "123"]
[email]
host = "gmx"
EOF);
# formname fields
file_put_contents($contentRoot . '/customername/formname/fields/_fields.toml', <<<EOF
[field.name]
file = "name.toml"
[field.email]
file = "email.toml"
required = true
[field.date]
required = true
EOF);
file_put_contents($contentRoot . '/customername/formname/fields/email.toml', <<<EOF
type = "email"
name = "email"
placeholder = "E-Mail"
[attributes]
data-email = "test@example.org"
EOF);
file_put_contents($contentRoot . '/customername/formname/fields/name.toml', <<<EOF
type = "text"
name = "name"
[attributes]
data-value = 123
EOF);
# pagedform fields
file_put_contents($contentRoot . '/customername/pagedform/fields/_fields.toml', <<<EOF
[page.one.field.name]
file = "name.toml"
[page.one.field.email]
file = "email.toml"
required = true
[page.second]
file = "second.toml"
[page.second.field.date]
required = true
EOF);
file_put_contents($contentRoot . '/customername/pagedform/fields/second.toml', <<<EOF
[field.text]
placeholder = "Text placeholder"
EOF);
file_put_contents($contentRoot . '/customername/pagedform/fields/email.toml', <<<EOF
type = "email"
name = "email"
placeholder = "E-Mail"
[attributes]
data-email = "test@example.org"
EOF);
file_put_contents($contentRoot . '/customername/pagedform/fields/name.toml', <<<EOF
type = "text"
name = "name"
[attributes]
data-value = 123
EOF);
}
public function request($method, $path): GuzzleHttpResponse
{
$response = null;
try {
$response = $this->client->request($method, $path, ['http_errors' => false]);
} 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);
}
}
|