BuildFormPlainTest.php 18.6 KB
Newer Older
1
2
<?php

3
4
//use qfq\Store;

5
require_once(__DIR__ . '/../../qfq/BuildFormPlain.php');
6
require_once(__DIR__ . '/../../qfq/QuickFormQuery.php');
7
require_once(__DIR__ . '/AbstractDatabaseTest.php');
8
require_once(__DIR__ . '/../../qfq/store/Store.php');
9
10
11
12
13
14
15
16
17

/**
 * Created by PhpStorm.
 * User: crose
 * Date: 2/2/16
 * Time: 10:07 PM
 */
class BuildFormPlainTest extends AbstractDatabaseTest {

18

19
20
21
22
23
24
25
    public function testGetProcessFilter() {

        $build = new \qfq\BuildFormPlain(array(), array(), array());

        $this->assertEquals(FORM_ELEMENTS_NATIVE, $build->getProcessFilter());
    }

26
27
28
29
30
31
32
33
34
35
36
37
38
39
    /**
     *
     */
    public function testGetFormId() {
        $build = new \qfq\BuildFormPlain(array(), array(), array());

        $formId1 = $build->getFormId();
        $this->assertRegExp('/qfq-form-[0-9a-f]{13}/', $formId1);

        $formId2 = $build->getFormId();
        $this->assertEquals($formId1, $formId2);

    }

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    public function testWrapItem() {
        $build = new \qfq\BuildFormPlain(array(), array(), array());

        $result = $build->wrapItem(WRAP_SETUP_ELEMENT, 'Hello World', false);
        $this->assertEquals('<p>Hello World</p>', $result);

        $result = $build->wrapItem(WRAP_SETUP_ELEMENT, 'Hello World', true);
        $this->assertEquals('<p>Hello World</p>', $result);

        $result = $build->wrapItem(WRAP_SETUP_ELEMENT, '', false);
        $this->assertEquals('<p></p>', $result);

        $result = $build->wrapItem(WRAP_SETUP_ELEMENT, '', true);
        $this->assertEquals('', $result);
    }

    public function testBuildLabel() {
        $build = new \qfq\BuildFormPlain(array(), array(), array());

        $result = $build->buildLabel('myLabel:123', "Hello World");
60
        $this->assertEquals('<label for="myLabel:123" class="control-label" >Hello World</label>', $result);
61
62
63
64
65
    }

    public function testBuildInput() {
        $form = array();
        $formElement = array();
Carsten  Rose's avatar
Carsten Rose committed
66
        $json = array();
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

        $this->setFormFormElement($form, $formElement);

        $build = new \qfq\BuildFormPlain($form, array(), [$formElement]);

//        $formElement = $this->db->sql("SELECT * FROM FormElement AS fe WHERE fe.id=114", ROW_EXACT_1);
//        $this->assertEquals('', $formElement);

//        $formElement = [
//            'id' => 123,
//            'formId' => 2,
//            'feIdContainer' => 0,
//            'enabled' => 'yes',
//            'name' => 'name',
//            'label' => 'Name',
//            'mode' => 'show',
//            'class' => 'native',
//            'type' => 'input',
//            'value' => '',
//            'sql1' => '',
//            'parameter' => '',
//            'debug' => 'no',
//            'deleted' => 'no',
//
//            'size' => '',
//            'maxLength' => '',
//            'tooltip' => '',
//            'placeholder' => '',
//            'checkType' => '',
//            'checkPattern' => '',
//
//            'tabindex' => 0
//        ];

101

102

Carsten  Rose's avatar
Carsten Rose committed
103
        $result = $build->buildInput($formElement, 'name:1', '', $json);
104
105
        $this->assertEquals('<input name="name:1" class="form-control" type="input" maxlength="255" value="" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => ''], $json);
106
107

        // CheckType
108
        $formElement['checkType'] = SANITIZE_ALLOW_MIN_MAX;
109
        $formElement['checkPattern'] = '1|10';
Carsten  Rose's avatar
Carsten Rose committed
110
        $result = $build->buildInput($formElement, 'name:1', '', $json);
111
112
        $this->assertEquals('<input name="name:1" class="form-control" type="input" maxlength="255" value="" min="1" max="10" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false], $json);
113

114
        $formElement['checkType'] = SANITIZE_ALLOW_PATTERN;
115
        $formElement['checkPattern'] = '^[a-z]*$';
Carsten  Rose's avatar
Carsten Rose committed
116
        $result = $build->buildInput($formElement, 'name:1', '', $json);
117
118
        $this->assertEquals('<input name="name:1" class="form-control" type="input" maxlength="255" value="" pattern="^[a-z]*$" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false], $json);
119

120
        $formElement['checkType'] = SANITIZE_ALLOW_DIGIT;
121
        $formElement['checkPattern'] = '';
Carsten  Rose's avatar
Carsten Rose committed
122
        $result = $build->buildInput($formElement, 'name:1', '', $json);
123
124
        $this->assertEquals('<input name="name:1" class="form-control" type="input" maxlength="255" value="" pattern="^[\d]*$" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false], $json);
125

126
        $formElement['checkType'] = SANITIZE_ALLOW_EMAIL;
127
        $formElement['checkPattern'] = '';
Carsten  Rose's avatar
Carsten Rose committed
128
        $result = $build->buildInput($formElement, 'name:1', '', $json);
129
130
        $this->assertEquals('<input name="name:1" class="form-control" type="input" maxlength="255" value="" pattern="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false], $json);
131
132
133
134
135
136
137
138

        $formElement['checkType'] = '';
        $formElement['checkPattern'] = '';


        // Explizit size
        $formElement['size'] = 40;
        $formElement['maxLength'] = 40;
Carsten  Rose's avatar
Carsten Rose committed
139
        $result = $build->buildInput($formElement, 'name:1', '', $json);
140
141
        $this->assertEquals('<input name="name:1" class="form-control" type="input" size="40" maxlength="40" value="" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false], $json);
142
143
144

        // maxlength bigger than physical spec:
        $formElement['maxLength'] = 1000;
Carsten  Rose's avatar
Carsten Rose committed
145
        $result = $build->buildInput($formElement, 'name:1', '', $json);
146
147
        $this->assertEquals('<input name="name:1" class="form-control" type="input" size="40" maxlength="255" value="" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false], $json);
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
        // no size, no maxlength and column not in primary table
        $formElement2 = $formElement;
        $formElement2['maxLength'] = '';
        $formElement2['size'] = '';
        $formElement2['name'] = 'specialname';
        $result = $build->buildInput($formElement2, 'specialname:1', '', $json);
        $this->assertEquals('<input name="specialname:1" class="form-control" type="input" value="" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);

        // no size,  given maxlength and column not in primary table
        $formElement2['maxLength'] = '10';
        $result = $build->buildInput($formElement2, 'specialname:1', '', $json);
        $this->assertEquals('<input name="specialname:1" class="form-control" type="input" maxlength="10" value="" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);

        // size given, no maxlength and column not in primary table
        $formElement2['maxLength'] = '';
        $formElement2['size'] = '10';
        $result = $build->buildInput($formElement2, 'specialname:1', '', $json);
        $this->assertEquals('<input name="specialname:1" class="form-control" type="input" size="10" value="" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);

        // size given, maxlength given and column not in primary table
        $formElement2['maxLength'] = '20';
        $formElement2['size'] = '10';
        $result = $build->buildInput($formElement2, 'specialname:1', '', $json);
        $this->assertEquals('<input name="specialname:1" class="form-control" type="input" size="10" maxlength="20" value="" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);


175
176
177
        // Explicit: further
        $formElement['tooltip'] = 'Nice Tooltip';
        $formElement['placeholder'] = 'Please type a name';
Carsten  Rose's avatar
Carsten Rose committed
178
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
179
180
        $this->assertEquals('<input name="name:1" class="form-control" type="input" size="40" maxlength="255" value="Hello World" placeholder="Please type a name" title="Nice Tooltip" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false], $json);
181
182
183

        // textarea
        $formElement['size'] = '40,10';
Carsten  Rose's avatar
Carsten Rose committed
184
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
185
186
        $this->assertEquals('<textarea name="name:1" class="form-control" cols="40" rows="10" placeholder="Please type a name" title="Nice Tooltip" data-hidden="no" data-disabled="no" data-required="no" >Hello World</textarea><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false], $json);
187
188

        $formElement['size'] = ' 40     , 10 ';
Carsten  Rose's avatar
Carsten Rose committed
189
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
190
191
        $this->assertEquals('<textarea name="name:1" class="form-control" cols="40" rows="10" placeholder="Please type a name" title="Nice Tooltip" data-hidden="no" data-disabled="no" data-required="no" >Hello World</textarea><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false], $json);
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
    }

    private function setFormFormElement(array &$form, array &$formElement) {
        $form = [
            'id' => '1',
            'name' => 'form',
            'title' => 'Form Editor: {{SELECT id, " / ", title FROM Form WHERE id => {{recordId:S0}}}}',
            'noteInternal' => 'Please secure the form',
            'tableName' => 'Form',
            'permitNew' => 'always',
            'permitEdit' => 'always',
            'permitUrlParameter' => '',
            'render' => 'bootstrap',
            'multiMode' => 'none',
            'multiSql' => '',
            'multiDetailForm' => '',
            'multiDetailFormParameter' => '',
            'forwardMode' => 'auto',
            'forwardPage' => '',
            'bsLabelColumns' => '',
            'bsInputColumns' => '',
            'bsNoteColumns' => '',
            'parameter' => 'maxVisiblePill=>3',
            'deleted' => 'no'
        ];

        $formElement = [
            'id' => 123,
            'formId' => 1,
            'feIdContainer' => 0,
Carsten  Rose's avatar
Carsten Rose committed
222
            'dynamicUpdate' => 'no',
223
224
225
            'enabled' => 'yes',
            'name' => 'name',
            'label' => 'Name',
226
            FE_MODE => 'show',
227
            'class' => 'native',
228
            FE_TYPE => 'input',
229
230
231
232
233
234
            'value' => '',
            'sql1' => '',
            'parameter' => '',
            'debug' => 'no',
            'deleted' => 'no',
            'size' => '',
235
            'maxLength' => '255',
236
237
238
239
240
241
242
243
244
245
246
            'tooltip' => '',
            'placeholder' => '',
            'checkType' => '',
            'checkPattern' => '',

            'tabindex' => 0
        ];

    }

    /**
247
     * @expectedException \qfq\UserFormException
248
249
250
251
252
     *
     */
    public function testBuildInputException() {
        $form = array();
        $formElement = array();
Carsten  Rose's avatar
Carsten Rose committed
253
        $json = array();
254
255
256
257
258

        $this->setFormFormElement($form, $formElement);

        $build = new \qfq\BuildFormPlain($form, array(), [$formElement]);

259
        $formElement['checkType'] = SANITIZE_ALLOW_MIN_MAX;
260
        $formElement['checkPattern'] = '';
Carsten  Rose's avatar
Carsten Rose committed
261
        $result = $build->buildInput($formElement, 'name:1', '', $json);
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
    }

    public function testGetKeyValueListFromSqlEnumSpec() {
        $form = array();
        $formElement = array();

        $this->setFormFormElement($form, $formElement);
        $formElement['name'] = 'deleted';

        $build = new \qfq\BuildFormPlain($form, array(), [$formElement]);

        $keys = array();
        $values = array();

        // Spec Enum
        $expect = ['yes', 'no'];
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // Spec Enum + emptyItemAtStart
        $expect = ['', 'yes', 'no'];
        $formElement['emptyItemAtStart'] = '';
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // Spec Enum + emptyItemAtEnd
        $expect = ['', 'yes', 'no', ''];
        $formElement['emptyItemAtEnd'] = '';
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // clean
        unset($formElement['emptyItemAtStart']);
        unset($formElement['emptyItemAtEnd']);

        // listItem: only value
        $expect = ['a', 'b', 'c'];
        $formElement['itemList'] = 'a,b,c';
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // listItem: key/value
        $expectKeys = ['A', 'B', 'C'];
        $expectValues = ['a', 'b', 'c'];
        $formElement['itemList'] = 'A:a,B:b,C:c';
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);

        // listItem: key/value + emptyItemAtEnd
        $formElement['emptyItemAtEnd'] = '';
        $expectKeys = ['A', 'B', 'C', ''];
        $expectValues = ['a', 'b', 'c', ''];
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);
        unset($formElement['emptyItemAtEnd']);

        // SQL
        $formElement['sql1'] = $this->db->sql('SELECT name FROM Form AS f ORDER BY f.id LIMIT 3');
        $expect = ['form', 'formElement', 'phpunit_person'];
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // SQL (one column, no keyword) + emptyItemAtStart
        $formElement['emptyItemAtStart'] = '';
        $formElement['sql1'] = $this->db->sql('SELECT name FROM Form AS f ORDER BY f.id LIMIT 3');
        $expect = ['', 'form', 'formElement', 'phpunit_person'];
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);
        unset($formElement['emptyItemAtStart']);

        // SQL (4 columns, none 'id' nor 'label') - Take the first two columns
        $expectKeys = ['10', '20', '30'];
        $expectValues = ['basic', 'permission', 'various'];
        $formElement['sql1'] = $this->db->sql('SELECT ord, name, created, modified  FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);

        // SQL (4 columns, none 'id', one 'label' ) - Take the first and the fourth two columns
        $expectKeys = ['10', '20', '30'];
        $expectValues = ['basic', 'permission', 'various'];
        $formElement['sql1'] = $this->db->sql('SELECT ord, created, modified, name AS label FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);

        // SQL (4 columns, none 'id', one 'label' ) - Take the first and the fourth two columns
        $expectKeys = ['1', '2', '3'];
        $expectValues = ['basic', 'permission', 'various'];
        $formElement['sql1'] = $this->db->sql('SELECT ord, created, modified, name AS label, id FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);
    }

    /**
366
     * @expectedException \qfq\UserFormException
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
     *
     */
    public function testGetKeyValueListFromSqlEnumSpecException() {
        $form = array();
        $formElement = array();

        $this->setFormFormElement($form, $formElement);

        $build = new \qfq\BuildFormPlain($form, array(), [$formElement]);

        $formElement['name'] = 'noteInternal';
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
    }

    /**
     * @throws Exception
     */
    protected function setUp() {
        $_GET['form'] = 'phpunit_person';

        parent::setUp();

389

390
391
392
        $this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);
//        $this->executeSQLFile(__DIR__ . '/fixtures/TestFormEditor.sql', true);

393
394
395
396
397

        // Defaults
        $this->store->setVar('name', 'varchar(255)', STORE_TABLE_COLUMN_TYPES, true);
        $this->store->setVar('deleted', "enum('yes','no')", STORE_TABLE_COLUMN_TYPES, true);

398
        $GLOBALS["TSFE"] = new FakeTSFEBuildPlain();
399

Carsten  Rose's avatar
Carsten Rose committed
400
401
402
//        $this->form = new \qfq\QuickFormQuery(['bodytext' => "form=form\nr=3", 'uid' => 1234], true);
        $form = new \qfq\QuickFormQuery(['bodytext' => "form=form\nr=3", 'uid' => 1234], true);

403
        // this is necessary to initialize SIP
Carsten  Rose's avatar
Carsten Rose committed
404
405
//        $content = $this->form->process();
        $form->process();
406
407
    }
}
408

409
class FakeTSFEBuildPlain {
410
    public $id = 1;
411
412
    public $type = 1;
    public $sys_language_uid = 1;
413
414
}

415