BuildFormPlainTest.php 28.9 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
26
27
    public function testGetProcessFilter() {

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

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

28
29
30
31
32
33
34
35
36
37
38
39
40
41
    /**
     *
     */
    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);

    }

42
43
44
    /**
     *
     */
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    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);
    }

61
62
63
    /**
     *
     */
64
65
66
67
    public function testBuildLabel() {
        $build = new \qfq\BuildFormPlain(array(), array(), array());

        $result = $build->buildLabel('myLabel:123', "Hello World");
68
        $this->assertEquals('<label for="myLabel:123" class="control-label" >Hello World</label>', $result);
69
70
    }

71
72
73
    /**
     *
     */
74
75
76
    public function testBuildInput() {
        $form = array();
        $formElement = array();
Carsten  Rose's avatar
Carsten Rose committed
77
        $json = array();
78

79
        $this->templateFormNFormElement($form, $formElement);
80
81
82

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

83
        $label['123-l'][API_ELEMENT_CONTENT] = '<label for="name:1" class="control-label" >Name</label>';
Carsten  Rose's avatar
Carsten Rose committed
84
        $label['123-r'][API_ELEMENT_ATTRIBUTE] = ['class' => ''];
85

Carsten  Rose's avatar
Carsten Rose committed
86
        $result = $build->buildInput($formElement, 'name:1', '', $json);
87
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
88
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
89
90

        // CheckType
91
92
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_MIN_MAX;
        $formElement[FE_CHECK_PATTERN] = '1|10';
Carsten  Rose's avatar
Carsten Rose committed
93
        $result = $build->buildInput($formElement, 'name:1', '', $json);
94
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" min="1" max="10" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
95
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
96

97
98
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_PATTERN;
        $formElement[FE_CHECK_PATTERN] = '^[a-z]*$';
Carsten  Rose's avatar
Carsten Rose committed
99
        $result = $build->buildInput($formElement, 'name:1', '', $json);
100
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="^[a-z]*$" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
101
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
102

103
104
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_DIGIT;
        $formElement[FE_CHECK_PATTERN] = '';
Carsten  Rose's avatar
Carsten Rose committed
105
        $result = $build->buildInput($formElement, 'name:1', '', $json);
106
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="^[\d]*$" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
107
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
108

109
110
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_EMAIL;
        $formElement[FE_CHECK_PATTERN] = '';
Carsten  Rose's avatar
Carsten Rose committed
111
        $result = $build->buildInput($formElement, 'name:1', '', $json);
112
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
113
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
114

115
116
        $formElement[FE_CHECK_TYPE] = '';
        $formElement[FE_CHECK_PATTERN] = '';
117
118
119
120
121


        // Explizit size
        $formElement['size'] = 40;
        $formElement['maxLength'] = 40;
Carsten  Rose's avatar
Carsten Rose committed
122
        $result = $build->buildInput($formElement, 'name:1', '', $json);
123
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="40" type="input" size="40" value="" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
124
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
125
126
127

        // maxlength bigger than physical spec:
        $formElement['maxLength'] = 1000;
Carsten  Rose's avatar
Carsten Rose committed
128
        $result = $build->buildInput($formElement, 'name:1', '', $json);
129
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" size="40" value="" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
130
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
131

132
133
134
135
136
137
        // 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);
138
        $this->assertEquals('<input id="123" name="specialname:1" class="form-control" type="input" value="" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
139
140
141
142

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

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

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


158
159
160
        // Explicit: further
        $formElement['tooltip'] = 'Nice Tooltip';
        $formElement['placeholder'] = 'Please type a name';
Carsten  Rose's avatar
Carsten Rose committed
161
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
162
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" size="40" value="Hello World" placeholder="Please type a name" title="Nice Tooltip" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
163
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
164
165
166

        // textarea
        $formElement['size'] = '40,10';
Carsten  Rose's avatar
Carsten Rose committed
167
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
168
        $this->assertEquals('<textarea id="123" name="name:1" class="form-control" maxlength="255" cols="40" rows="10" placeholder="Please type a name" title="Nice Tooltip" data-hidden="no" data-required="no" >Hello World</textarea><div class="help-block with-errors hidden"></div>', $result);
169

Carsten  Rose's avatar
Carsten Rose committed
170
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
171
172

        $formElement['size'] = ' 40     , 10 ';
Carsten  Rose's avatar
Carsten Rose committed
173
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
174
        $this->assertEquals('<textarea id="123" name="name:1" class="form-control" maxlength="255" cols="40" rows="10" placeholder="Please type a name" title="Nice Tooltip" data-hidden="no" data-required="no" >Hello World</textarea><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
175
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
176
177
    }

178
179
180
181
    /**
     *
     */
    private function templateFormNFormElement(array &$form, array &$formElement) {
182
183
184
185
186
187
188
189
190
191
192
193
194
195
        $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' => '',
Carsten  Rose's avatar
Carsten Rose committed
196
197
            F_FORWARD_MODE => 'auto',
            F_FORWARD_PAGE => '',
198
199
200
201
            'bsLabelColumns' => '',
            'bsInputColumns' => '',
            'bsNoteColumns' => '',
            'parameter' => 'maxVisiblePill=>3',
Carsten  Rose's avatar
Carsten Rose committed
202
            'deleted' => 'no',
203
204
205
206
        ];

        $formElement = [
            'id' => 123,
207
208
209
            FE_ID => 1,
            FE_ID_CONTAINER => 0,
            FE_DYNAMIC_UPDATE => 'no',
210
            'enabled' => 'yes',
211
212
            FE_NAME => 'name',
            FE_LABEL => 'Name',
213
            FE_MODE => 'show',
214
            'class' => 'native',
215
            FE_TYPE => 'input',
216
217
218
219
220
221
            'value' => '',
            'sql1' => '',
            'parameter' => '',
            'debug' => 'no',
            'deleted' => 'no',
            'size' => '',
222
            'maxLength' => '255',
223
224
            'tooltip' => '',
            'placeholder' => '',
225
226
            FE_CHECK_TYPE => '',
            FE_CHECK_PATTERN => '',
227
            FE_HTML_ID => '123',
Carsten  Rose's avatar
Carsten Rose committed
228
            FE_HIDE_ZERO => '0',
229
230
231
232
233
234
235

            'tabindex' => 0
        ];

    }

    /**
236
     * @expectedException \qfq\UserFormException
237
238
239
240
241
     *
     */
    public function testBuildInputException() {
        $form = array();
        $formElement = array();
Carsten  Rose's avatar
Carsten Rose committed
242
        $json = array();
243

244
        $this->templateFormNFormElement($form, $formElement);
245
246
247

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

248
249
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_MIN_MAX;
        $formElement[FE_CHECK_PATTERN] = '';
Carsten  Rose's avatar
Carsten Rose committed
250
        $result = $build->buildInput($formElement, 'name:1', '', $json);
251
252
    }

253
254
255
    /**
     *
     */
256
257
258
259
    public function testGetKeyValueListFromSqlEnumSpec() {
        $form = array();
        $formElement = array();

260
        $this->templateFormNFormElement($form, $formElement);
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
        $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');
Carsten  Rose's avatar
Carsten Rose committed
318
319
//        $expect = ['form', 'formElement', 'phpunit_person'];
        $expect = ['form', 'formElement', 'copyForm'];
320
321
322
323
324
325
326
        $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');
Carsten  Rose's avatar
Carsten Rose committed
327
328
//        $expect = ['', 'form', 'formElement', 'phpunit_person'];
        $expect = ['', 'form', 'formElement', 'copyForm'];
329
330
331
332
333
334
335
        $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'];
336
        $expectValues = ['basic', 'access', 'various'];
337
338
339
340
341
342
343
        $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'];
344
        $expectValues = ['basic', 'access', 'various'];
345
346
347
348
349
350
351
        $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'];
352
        $expectValues = ['basic', 'access', 'various'];
353
354
355
356
357
358
359
        $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);
    }

    /**
360
     * @expectedException \qfq\UserFormException
361
362
363
364
365
366
     *
     */
    public function testGetKeyValueListFromSqlEnumSpecException() {
        $form = array();
        $formElement = array();

367
        $this->templateFormNFormElement($form, $formElement);
368
369
370
371

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

        $formElement['name'] = 'noteInternal';
372
373
        $keys = array();
        $values = array();
374
375
376
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
    }

377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399

    /**
     *
     */
    public function testBuildSubrecord() {
        $form = array();
        $formElement = array();
        $json = array();

        $this->templateFormNFormElement($form, $formElement);
        // CheckType
        $build = new \qfq\BuildFormPlain($form, array(), [$formElement]);

        // id: 1, firstName: John, name: Doe
        $formElement['sql1'] = $this->db->sql('SELECT id, name, firstName FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>id</th><th>name</th><th>firstName</th></tr><tr class="record" ><td>1</td><td>Doe</td><td>John</td></tr><tr class="record" ><td>2</td><td>Smith</td><td>Jane</td></tr></table>', $result);

        // _id: 1, name: Doe,
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>name</th></tr><tr class="record" ><td>Doe</td></tr><tr class="record" ><td>Smith</td></tr></table>', $result);

400
401
402
403
404
405
406
407
408
409
        // _id: 1, name: Doe,title=''
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name AS "title=" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th></th></tr><tr class="record" ><td>Doe</td></tr><tr class="record" ><td>Smith</td></tr></table>', $result);

        // _id: 1, name: Doe, column: _Person
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name AS "unused|width=2|title=_Person", firstName  FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>firstName</th></tr><tr class="record" ><td>John</td></tr><tr class="record" ><td>Jane</td></tr></table>', $result);

410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
        // _id: 1, name: Doe, title: PERSON
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name AS "PERSON" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>PERSON</th></tr><tr class="record" ><td>Doe</td></tr><tr class="record" ><td>Smith</td></tr></table>', $result);

        // _id: 1, "This is a much longer text than necessary": Default max:20
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", "This is a much longer text than necessary" FROM Person ORDER BY id LIMIT 1');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>This is a much longe</th></tr><tr class="record" ><td>This is a much longe</td></tr></table>', $result);

        // _id: 1, name: Jo (width:2)
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name AS "2" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th></th></tr><tr class="record" ><td>Do</td></tr><tr class="record" ><td>Sm</td></tr></table>', $result);

        // _id: 1, name: Jo (width:2)
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name AS "2|PERSON" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>PE</th></tr><tr class="record" ><td>Do</td></tr><tr class="record" ><td>Sm</td></tr></table>', $result);

        // _id: 1, name: Doe ('width':3)
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name AS "Name|width=3" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>Nam</th></tr><tr class="record" ><td>Doe</td></tr><tr class="record" ><td>Smi</td></tr></table>', $result);

        // _id: 1, name: Doe (width:3, title:PERSON)
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name AS "3|title=PERSON" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>PER</th></tr><tr class="record" ><td>Doe</td></tr><tr class="record" ><td>Smi</td></tr></table>', $result);

        // _id: 1, name: <b>Doe</b>
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", CONCAT("<b>", name, "</b>") AS "Name" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>Name</th></tr><tr class="record" ><td>Doe</td></tr><tr class="record" ><td>Smith</td></tr></table>', $result);

445
446
447
448
449
        // _id: 1, name: <b>Doe</b>, width=2
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", CONCAT("<b>", name, "</b>") AS "Name|2" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>Na</th></tr><tr class="record" ><td>Do</td></tr><tr class="record" ><td>Sm</td></tr></table>', $result);

450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
        // _id: 1, name: <b>Doe</b> , nostrip
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", CONCAT("<b>", name, "</b>") AS "Name|nostrip" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>Name</th></tr><tr class="record" ><td><b>Doe</b></td></tr><tr class="record" ><td><b>Smith</b></td></tr></table>', $result);

        // _id: 1, icon: bullet-green.gif
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", "bullet-green.gif" AS "Status|icon" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>Status</th></tr><tr class="record" ><td><image src=\'typo3conf/ext/qfq/Resources/Public/icons/bullet-green.gif\'></td></tr><tr class="record" ><td><image src=\'typo3conf/ext/qfq/Resources/Public/icons/bullet-green.gif\'></td></tr></table>', $result);

        // _id: 1, mailto: john@doe.com
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", "john@doe.com" AS "EMail|mailto" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>EMail</th></tr><tr class="record" ><td><a href="mailto:john@doe.com" >john@doe.com</a></td></tr><tr class="record" ><td><a href="mailto:john@doe.com" >john@doe.com</a></td></tr></table>', $result);

        // _id: 1, url: www.uzh.ch
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", "www.uzh.ch" AS "URL|url" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>URL</th></tr><tr class="record" ><td><a href="www.uzh.ch" >www.uzh.ch</a></td></tr><tr class="record" ><td><a href="www.uzh.ch" >www.uzh.ch</a></td></tr></table>', $result);

        // _id: 1, name: Doe, _rowclass (text)
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name, IF(id=1,"text-warning", "text-danger") AS _rowClass FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>name</th></tr><tr class="record text-warning" ><td>Doe</td></tr><tr class="record text-danger" ><td>Smith</td></tr></table>', $result);

        // _id: 1, name: Doe, _rowClass (text & background)
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name, IF(id=1,"text-warning active", "text-danger success") AS _rowClass FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>name</th></tr><tr class="record text-warning active" ><td>Doe</td></tr><tr class="record text-danger success" ><td>Smith</td></tr></table>', $result);

        // _id: 1, name: Doe, _rowTitle
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name, firstName AS _rowTitle FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>name</th></tr><tr class="record" title="John" ><td>Doe</td></tr><tr class="record" title="Jane" ><td>Smith</td></tr></table>', $result);

        // _id: 1, name: Doe, title, width, nostrip
486
487
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name, "<b>This again is a very long text</b>" AS "title=Important|width=10|nostrip" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
488
        $this->assertEquals('<table class="table table-hover"><tr><th>name</th><th>Important</th></tr><tr class="record" ><td>Doe</td><td><b>This again is a very long text</b></td></tr><tr class="record" ><td>Smith</td><td><b>This again is a very long text</b></td></tr></table>', $result);
489
490
491
492
493

        // _id: 1, name: Doe, link
        $formElement['sql1'] = $this->db->sql('SELECT id AS "_id", name, CONCAT("s:1|p:form&form=person&r=" , id , "|t:", name) AS "link" FROM Person ORDER BY id LIMIT 2');
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('<table class="table table-hover"><tr><th>name</th><th></th></tr><tr class="record" ><td>Doe</td><td><a href="index.php?id=form&s=badcaffee1234" class="internal" >Doe</a></td></tr><tr class="record" ><td>Smith</td><td><a href="index.php?id=form&s=badcaffee1234" class="internal" >Smith</a></td></tr></table>', $result);
494
495
    }

496
497
498
499
500
501
502
503
    /**
     * @throws Exception
     */
    protected function setUp() {
        $_GET['form'] = 'phpunit_person';

        parent::setUp();

504
505
506
507
508
        // Use always the latest (production) FormEditor: played during QuickFormQuery()
        $this->db->sql("DROP TABLE IF EXISTS `Form`");
        $this->db->sql("DROP TABLE IF EXISTS `FormElement`");
        $this->executeSQLFile(__DIR__ . '/../../sql/formEditor.sql', true);

509
        $this->store->unsetStore(STORE_ADDITIONAL_FORM_ELEMENTS);
510
        $this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);
511
        $this->executeSQLFile(__DIR__ . '/fixtures/TestForm.sql', true);
512
513
514
515
516

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

517
        $GLOBALS["TSFE"] = new FakeTSFEBuildPlain();
518

Carsten  Rose's avatar
Carsten Rose committed
519
520
        $form = new \qfq\QuickFormQuery(['bodytext' => "form=form\nr=3", 'uid' => 1234], true);

521
522
        // After instantiating QuickFormQuery, the latest updates will be played. The SIP is by default activated which breaks current tests.
        $this->db->sql("UPDATE Form SET permitNew='always', permitEdit='always' WHERE name='form' ");
Carsten  Rose's avatar
Carsten Rose committed
523
        $form->process();
524
525
    }
}
526

527
class FakeTSFEBuildPlain {
528
    public $id = 1;
529
530
    public $type = 1;
    public $sys_language_uid = 1;
531
532
}

533