-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathFormTests.php
397 lines (321 loc) · 11 KB
/
FormTests.php
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
use Gregwar\Formidable\Form;
use Gregwar\Formidable\PostIndicator;
/**
* Testing Formidable forms
*
* @author Grégoire Passault <[email protected]>
*/
class FormTests extends \PHPUnit\Framework\TestCase
{
/**
* Testing that toString() give the same thing as getHtml()
*/
public function testToString()
{
$form = $this->getForm('test.html');
$this->assertEquals((string)$form, $form->getHtml());
}
/**
* Testing that Formidable guess if it should interpret the contents
*/
public function testGuessPathOrContent()
{
$form = new Form('<form>
<input type="text" name="foo" value="bar" />
</form>');
$this->assertEquals('bar', $form->foo);
}
/**
* Testing that the enctype becomes multipart when there is file
*/
public function testEnctype()
{
$form = $this->getForm('enctype_normal.html');
$this->assertFalse(strpos("$form", 'enctype='));
$form = $this->getForm('enctype_file.html');
$this->assertContains('enctype=', "$form");
}
/**
* Testing attributes handling
*/
public function testAttributes()
{
$form = $this->getForm('attributes.html');
$this->assertEquals('red rounded', $form->getAttribute('name', 'class'));
$this->assertEquals('Your name', $form->getAttribute('name', 'title'));
$form->setAttribute('name', 'title', 'Outside attribute');
$this->assertContains('title="Outside attribute"', "$form");
}
/**
* Testing attributes handling
*/
public function testButton()
{
$form = $this->getForm('button.html');
$this->assertEquals('Click here', $form->getValue('button'));
}
/**
* Testing default values
*/
public function testGetValues()
{
$form = $this->getForm('values.html');
$this->assertEquals('Hello with spaces and "!', $form->message);
$this->assertEquals('1', $form->gender);
$this->assertEquals('blue', $form->color);
$this->assertEquals('42', $form->getValue('checkme'));
$this->assertEquals('Hello world, i\'m a long message', $form->area);
}
/**
* Testing defining values
*/
public function testSetValues()
{
$form = $this->getForm('test.html');
$form->message = 'Setting a value';
$form->choices = '1';
$form->checkme = '1';
$html = "$form";
$this->assertContains('value="Setting a value"', $html);
$this->assertContains('selected=', $html);
$this->assertContains('checked=', $html);
}
/**
* Testing definition with multiple values
*/
public function testSetMultipleValues()
{
$form = $this->getForm('test.html');
$form->setValues(array(
'message' => 'something',
'choices' => '1',
'color' => 'blue'
));
$this->assertEquals('something', $form->message);
$this->assertEquals('1', $form->choices);
$this->assertEquals('blue', $form->color);
}
/**
* Testing that the emitted <select> HTML contains the required attribute
*/
public function testSelectRequiredAttribute()
{
$form = $this->getForm('select-required.html');
$this->assertContains('required="required"', $form->getHtml());
}
/**
* Testing that the CSRF token is t he same if computer twice on
* the same file, and different for another file with a different form name
*/
public function testCsrfTokenGeneration()
{
$form1 = $this->getForm('empty.html');
$form2 = $this->getForm('empty.html');
$this->assertNotEquals('', $form1->getToken());
$this->assertEquals($form1->getToken(), $form2->getToken());
$form3 = $this->getForm('empty2.html');
$this->assertNotEquals($form1->getToken(), $form3->getToken());
}
/**
* Testing that the generated secret is different
*/
public function testCsrfSecretGeneration()
{
$form1 = $this->getForm('empty.html');
$token1 = $form1->getToken();
$form2 = $this->getForm('empty.html');
$token2 = $form2->getToken();
// Deleting session variales
foreach ($_SESSION as $key => $v) {
unset($_SESSION[$key]);
}
$form3 = $this->getForm('empty.html');
$token3 = $form3->getToken();
// Assert that the token is in the form
$this->assertContains($token1, "$form1");
// Tokens from two forms sharing the session should be equals
$this->assertEquals($token1, $token2);
// Tokens from two forms with the session destroyed should be different
$this->assertNotEquals($token1, $token3);
}
/**
* Testing a post indicator without session
*/
public function testPostIndicator()
{
unset($_SESSION);
$form1 = $this->getForm('post-indicator.html');
$token1 = $form1->getToken();
unset($_SESSION);
$form2 = $this->getForm('post-indicator.html');
$token2 = $form2->getToken();
$this->assertEquals($token1, $token2);
$this->assertContains($token1, "$form1");
$this->assertContains($token1, "$form2");
}
/**
* Testing posted() returns true when the csrd token is in the request
*/
public function testCsrfTokenCheck()
{
$form = $this->getForm('empty.html');
$_POST = array();
$this->assertFalse($form->posted());
$_POST = array(PostIndicator::$fieldName => $form->getToken());
$this->assertTrue($form->posted());
}
/**
* Testing the filling of the values in the form using post
*/
public function testPostValue()
{
$form = $this->getForm('post-values.html');
$_POST = array(
PostIndicator::$fieldName => $form->getToken(),
'message' => 'Hello with spaces and "!',
'gender' => '1',
'color' => 'blue',
'checkme' => '42',
'area' => 'Hello world, i\'m a long message'
);
$this->assertTrue($form->posted());
$this->assertEquals('Hello with spaces and "!', $form->message);
$this->assertEquals('1', $form->gender);
$this->assertEquals('blue', $form->color);
$this->assertEquals('42', $form->getValue('checkme'));
$this->assertEquals('Hello world, i\'m a long message', $form->area);
$this->assertContains('checked=', "$form");
$this->assertContains('selected=', "$form");
$this->assertContains('Hello with spaces and "!', "$form");
$this->assertContains('Hello world, i\'m a long message', "$form");
}
/**
* Testing that Formidable output given back to Formidable gives the same output
*/
public function testOutIn()
{
$form = $this->getForm('out_in.html');
$html = $form->getHtml();
$otherForm = new Form($html);
$otherHtml = $otherForm->getHtml();
$this->assertEquals($html, $otherHtml);
}
/**
* Testing running the PHP interpreter on the form
*/
public function testPHPInterpreter()
{
$form = $this->getForm('custom.php', array('label' => 'Hello world!'));
$html = $form->getHtml();
$this->assertNotContains('php', $html);
$this->assertContains('Hello world!', $html);
}
/**
* Testing that accessing a non-existent field raise an exception
*
* @expectedException \InvalidArgumentException
*/
public function testAccessingNotExistingField()
{
$form = $this->getForm('basic.html');
$form->getField('titi');
}
public function testQuotesAttributes()
{
$form = $this->getForm('quotes.html');
$field = $form->getField('xxx');
$this->assertTrue($field->hasAttribute('foo'));
$this->assertEquals($field->getAttribute('foo'), 'bar baz "bax"');
$doc = new DOMDocument();
$doc->loadHTML("$form");
$element = $doc->getElementById('theinput');
$this->assertFalse($element == null);
$this->assertTrue($element->hasAttribute('foo'));
$this->assertEquals($element->getAttribute('foo'), 'bar baz "bax"');
}
public function testPlaceholder()
{
$form = $this->getForm('placeholder.html');
$html = "$form";
$this->assertNotContains('something', $html);
$form->setPlaceholder('something', 'hello!');
$html = "$form";
$this->assertContains('hello!', $html);
///
$form = $this->getForm('placeholder_repeat.html');
$form->setPlaceholder('repeat', 'repeatme');
$html = "$form";
$this->assertContains('<b>repeatme</b>', $html);
$this->assertContains('<u>repeatme</u>', $html);
///
$form = $this->getForm('placeholder_multiline.html');
$form->setPlaceholder('name', 'Bob');
$form->setPlaceholder('age', 30);
$html = "$form";
$this->assertContains('Bob', $html);
$this->assertContains('30', $html);
}
/**
* Testing caching a form
*/
public function testCache()
{
$cache = new Gregwar\Cache\Cache;
$cache->setCacheDirectory($this->getCacheDirectory());
$form = $this->getForm('basic.html', null, $cache);
$html = "$form";
$this->assertContains('toto', $html);
$this->assertEquals(false, $form->isCached);
$form = $this->getForm('basic.html', null, $cache);
$html = "$form";
$this->assertContains('toto', $html);
$this->assertEquals(true, $form->isCached);
}
/**
* Testing the order of sourced select
*/
public function testSourceOrder()
{
$form = $this->getForm('source.html');
$form->source('data', array(
'A' => 'a',
'B' => 'b'
));
$html = "$form";
preg_match_all('#option value="(.+)"#mUsi', $html, $matches);
$this->assertEquals(3, count($matches[1]));
$this->assertEquals('X', $matches[1][0]);
$this->assertEquals('A', $matches[1][1]);
$this->assertEquals('B', $matches[1][2]);
}
/**
* Testing the order of sourced select
*/
public function testSourceMultiple()
{
$form = $this->getForm('multiple_source.html');
$form->source('colors', array(
'red', 'green', 'blue', 'purple'
));
$html = "$form";
$this->assertEquals(2, substr_count($html, '<option value="0">red</option>'));
}
private function getForm($file, $vars = array(), $cache = false)
{
return new Form(__DIR__.'/files/form/'.$file, $vars, $cache);
}
public function setup()
{
$_SESSION = array();
}
public function getCacheDirectory()
{
return __DIR__ . '/cache';
}
public function teardown()
{
$cacheDir = $this->getCacheDirectory();
`rm -rf $cacheDir`;
}
}