BuildFormPlainTest.php 33.5 KB
Newer Older
1
<?php
2
/**
3
 * @author Carsten Rose <carsten.rose@math.uzh.ch>
4
 */
5

6
7
8
namespace qfq;

require_once(__DIR__ . '/database/AbstractDatabaseTest.php');
9

10
11
12
/**
 * Class BuildFormPlainTest
 */
13
14
class BuildFormPlainTest extends AbstractDatabaseTest {

15
16
17
    /**
     *
     */
18
19
    public function testGetProcessFilter() {

Carsten  Rose's avatar
Carsten Rose committed
20
        $build = new \qfq\BuildFormPlain([F_DB_INDEX => DB_INDEX_DEFAULT], array(), array(), $this->dbArray);
21
22
23
24

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

25
26
27
28
    /**
     *
     */
    public function testGetFormId() {
Carsten  Rose's avatar
Carsten Rose committed
29
        $build = new \qfq\BuildFormPlain([F_DB_INDEX => DB_INDEX_DEFAULT], array(), array(), $this->dbArray);
30
31
32
33
34
35
36
37
38

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

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

    }

39
40
41
    /**
     *
     */
42
    public function testWrapItem() {
Carsten  Rose's avatar
Carsten Rose committed
43
        $build = new \qfq\BuildFormPlain([F_DB_INDEX => DB_INDEX_DEFAULT], array(), array(), $this->dbArray);
44
45
46
47
48
49
50
51
52
53
54
55
56
57

        $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);
    }

58
59
60
    /**
     *
     */
61
    public function testBuildLabel() {
Carsten  Rose's avatar
Carsten Rose committed
62
        $build = new \qfq\BuildFormPlain([F_DB_INDEX => DB_INDEX_DEFAULT], array(), array(), $this->dbArray);
63
64

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

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

76
        $this->templateFormNFormElement($form, $formElement);
77

78
        $build = new BuildFormPlain($form, array(), [$formElement], $this->dbArray);
Elias Villiger's avatar
Elias Villiger committed
79
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
80
        $formElement[FE_DECIMAL_FORMAT]='';
81

82
        $label['123-l'][API_ELEMENT_CONTENT] = '<label for="name:1" class="control-label" >Name</label>';
Carsten  Rose's avatar
Carsten Rose committed
83
        $label['123'][API_ELEMENT_ATTRIBUTE] = ['value' => ''];
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
91
92
        // Min/Max
        $formElement[FE_MIN] = 1;
        $formElement[FE_MAX] = 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="number" value="" min="1" max="10" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
95
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
96

97
98
99
        $formElement[FE_MIN] = 1;
        $formElement[FE_MAX] = '';
        $result = $build->buildInput($formElement, 'name:1', '', $json);
100
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" min="1" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
101
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
102
103
104
105

        $formElement[FE_MIN] = '';
        $formElement[FE_MAX] = 10;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
106
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" max="10" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
107
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
108
109
110
        // reset so they don't interfere with next tests
        $formElement[FE_MIN] = '';
        $formElement[FE_MAX] = '';
111
112

        // CheckType
113
114
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_PATTERN;
        $formElement[FE_CHECK_PATTERN] = '^[a-z]*$';
Carsten  Rose's avatar
Carsten Rose committed
115
        $result = $build->buildInput($formElement, 'name:1', '', $json);
116
        $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);
117
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
118

119
120
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_DIGIT;
        $formElement[FE_CHECK_PATTERN] = '';
Carsten  Rose's avatar
Carsten Rose committed
121
        $result = $build->buildInput($formElement, 'name:1', '', $json);
122
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="^[\d]*$" data-pattern-error="Allowed characters: 0...9" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
123
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
124

125
126
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_EMAIL;
        $formElement[FE_CHECK_PATTERN] = '';
Carsten  Rose's avatar
Carsten Rose committed
127
        $result = $build->buildInput($formElement, 'name:1', '', $json);
128
        $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-pattern-error="Requested format: string@domain" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
129
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
130

131
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
132
        $formElement[FE_CHECK_PATTERN] = '';
133
134


135
136
137
        // Decimal format
        $formElement[FE_DECIMAL_FORMAT] = '5,2';
        $result = $build->buildInput($formElement, 'name:1', '', $json);
138
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="^-?[0-9]{0,3}(\.[0-9]{0,2})?$" data-pattern-error="Requested decimal format (mantis,decimal): 5,2" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
139
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
140
141
142
143
144

        $formElement[FE_DECIMAL_FORMAT] = '';


        // Explicit size
145
146
        $formElement['size'] = 40;
        $formElement['maxLength'] = 40;
Carsten  Rose's avatar
Carsten Rose committed
147
        $result = $build->buildInput($formElement, 'name:1', '', $json);
148
        $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);
149
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
150
151

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

157
158
159
160
161
162
        // 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);
163
        $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);
164
165
166
167

        // no size,  given maxlength and column not in primary table
        $formElement2['maxLength'] = '10';
        $result = $build->buildInput($formElement2, 'specialname:1', '', $json);
168
        $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);
169
170
171
172
173

        // size given, no maxlength and column not in primary table
        $formElement2['maxLength'] = '';
        $formElement2['size'] = '10';
        $result = $build->buildInput($formElement2, 'specialname:1', '', $json);
174
        $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);
175
176
177
178
179

        // size given, maxlength given and column not in primary table
        $formElement2['maxLength'] = '20';
        $formElement2['size'] = '10';
        $result = $build->buildInput($formElement2, 'specialname:1', '', $json);
180
        $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);
181
182
        $formElement2['maxLength'] = '';
        $formElement2['size'] = '';
183

184
        // Explicit: further
185
186
187
188
        $formElement2['tooltip'] = 'Nice Tooltip';
        $formElement2['placeholder'] = 'Please type a name';
        $result = $build->buildInput($formElement2, 'name:1', 'Hello World', $json);
        $this->assertEquals('<input id="123" name="name:1" class="form-control" type="input" 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
189
        $label['123'][API_ELEMENT_ATTRIBUTE] = ['value' => 'Hello World'];
190
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', API_ELEMENT_UPDATE => $label], $json);
191
192

        // textarea
193
194
195
        $formElement2['size'] = '40,10';
        $result = $build->buildInput($formElement2, 'name:1', 'Hello World', $json);
        $this->assertEquals('<textarea id="123" name="name:1" class="form-control" 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);
196

197
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', API_ELEMENT_UPDATE => $label], $json);
198

199
200
201
        $formElement2['size'] = ' 40     , 10 ';
        $result = $build->buildInput($formElement2, 'name:1', 'Hello World', $json);
        $this->assertEquals('<textarea id="123" name="name:1" class="form-control" 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);
202
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', API_ELEMENT_UPDATE => $label], $json);
203
204
    }

205
206
207
    /**
     *
     */
208
209
210
211
    public function testGetKeyValueListFromSqlEnumSpec() {
        $form = array();
        $formElement = array();

212
        $this->templateFormNFormElement($form, $formElement);
213
214
        $formElement['name'] = 'deleted';

215
        $build = new \qfq\BuildFormPlain($form, array(), [$formElement], $this->dbArray);
216
217
218
219
220
221
222
223
224
225
226
227

        $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'];
228
        $formElement[FE_EMPTY_ITEM_AT_START] = '';
229
230
231
232
233
234
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // Spec Enum + emptyItemAtEnd
        $expect = ['', 'yes', 'no', ''];
235
        $formElement[FE_EMPTY_ITEM_AT_END] = '';
236
237
238
239
240
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // clean
241
242
        unset($formElement[FE_EMPTY_ITEM_AT_START]);
        unset($formElement[FE_EMPTY_ITEM_AT_END]);
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

        // 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
269
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT name FROM Form AS f ORDER BY f.id LIMIT 3');
Carsten  Rose's avatar
Carsten Rose committed
270
271
//        $expect = ['form', 'formElement', 'phpunit_person'];
        $expect = ['form', 'formElement', 'copyForm'];
272
273
274
275
276
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // SQL (one column, no keyword) + emptyItemAtStart
277
        $formElement[FE_EMPTY_ITEM_AT_START] = '';
278
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT name FROM Form AS f ORDER BY f.id LIMIT 3');
Carsten  Rose's avatar
Carsten Rose committed
279
280
//        $expect = ['', 'form', 'formElement', 'phpunit_person'];
        $expect = ['', 'form', 'formElement', 'copyForm'];
281
282
283
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);
Carsten  Rose's avatar
Carsten Rose committed
284
        unset($formElement[FE_EMPTY_ITEM_AT_START]);
285
286
287

        // SQL (4 columns, none 'id' nor 'label') - Take the first two columns
        $expectKeys = ['10', '20', '30'];
288
        $expectValues = ['basic', 'access', 'various'];
289
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT ord, name, created, modified  FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
290
291
292
293
294
295
        $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'];
296
        $expectValues = ['basic', 'access', 'various'];
297
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT ord, created, modified, name AS label FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
298
299
300
301
302
303
        $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'];
304
        $expectValues = ['basic', 'access', 'various'];
305
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT ord, created, modified, name AS label, id FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
306
307
308
309
310
311
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);
    }

    /**
312
     * @expectedException \qfq\UserFormException
313
314
315
316
317
318
     *
     */
    public function testGetKeyValueListFromSqlEnumSpecException() {
        $form = array();
        $formElement = array();

319
        $this->templateFormNFormElement($form, $formElement);
320

321
        $build = new \qfq\BuildFormPlain($form, array(), [$formElement], $this->dbArray);
322
323

        $formElement['name'] = 'noteInternal';
324
325
        $keys = array();
        $values = array();
326
327
328
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
    }

329
330
331
332
333
334
335
336
337
338

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

        $this->templateFormNFormElement($form, $formElement);
Carsten  Rose's avatar
Carsten Rose committed
339
        $formElement = \qfq\Support::setFeDefaults($formElement);
Carsten  Rose's avatar
Carsten Rose committed
340

341
        // CheckType
342
        $build = new \qfq\BuildFormPlain($form, array(), [$formElement], $this->dbArray);
343
344

        // id: 1, firstName: John, name: Doe
345
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id, name, firstName FROM Person ORDER BY id LIMIT 2');
Carsten  Rose's avatar
Carsten Rose committed
346

347
348
349
350
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
        $this->assertEquals('Please save this record first.', $result);

        $this->store->setStore(['id' => 1], STORE_RECORD, true);
351
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
352
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>id</th><th>name</th><th>firstName</th></tr></thead><tbody><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></tbody></table>', $result);
353
354

        // _id: 1, name: Doe,
355
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", name FROM Person ORDER BY id LIMIT 2');
356
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
357
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>name</th></tr></thead><tbody><tr class="record" ><td>Doe</td></tr><tr class="record" ><td>Smith</td></tr></tbody></table>', $result);
358

359
        // _id: 1, name: Doe,title=''
360
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", name AS "title=" FROM Person ORDER BY id LIMIT 2');
361
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
362
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th></th></tr></thead><tbody><tr class="record" ><td>Doe</td></tr><tr class="record" ><td>Smith</td></tr></tbody></table>', $result);
363
364

        // _id: 1, name: Doe, column: _Person
365
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", name AS "unused|width=2|title=_Person", firstName  FROM Person ORDER BY id LIMIT 2');
366
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
367
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>firstName</th></tr></thead><tbody><tr class="record" ><td>John</td></tr><tr class="record" ><td>Jane</td></tr></tbody></table>', $result);
368

369
        // _id: 1, name: Doe, title: PERSON
370
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", name AS "PERSON" FROM Person ORDER BY id LIMIT 2');
371
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
372
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>PERSON</th></tr></thead><tbody><tr class="record" ><td>Doe</td></tr><tr class="record" ><td>Smith</td></tr></tbody></table>', $result);
373
374

        // _id: 1, "This is a much longer text than necessary": Default max:20
375
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", "This is a much longer text than necessary" FROM Person ORDER BY id LIMIT 1');
376
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
377
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>This is a much longe</th></tr></thead><tbody><tr class="record" ><td>This is a much longe</td></tr></tbody></table>', $result);
378
379

        // _id: 1, name: Jo (width:2)
380
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", name AS "2" FROM Person ORDER BY id LIMIT 2');
381
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
382
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th></th></tr></thead><tbody><tr class="record" ><td>Do</td></tr><tr class="record" ><td>Sm</td></tr></tbody></table>', $result);
383
384

        // _id: 1, name: Jo (width:2)
385
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", name AS "2|PERSON" FROM Person ORDER BY id LIMIT 2');
386
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
387
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>PE</th></tr></thead><tbody><tr class="record" ><td>Do</td></tr><tr class="record" ><td>Sm</td></tr></tbody></table>', $result);
388
389

        // _id: 1, name: Doe ('width':3)
390
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", name AS "Name|width=3" FROM Person ORDER BY id LIMIT 2');
391
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
392
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>Nam</th></tr></thead><tbody><tr class="record" ><td>Doe</td></tr><tr class="record" ><td>Smi</td></tr></tbody></table>', $result);
393
394

        // _id: 1, name: Doe (width:3, title:PERSON)
395
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", name AS "3|title=PERSON" FROM Person ORDER BY id LIMIT 2');
396
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
397
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>PER</th></tr></thead><tbody><tr class="record" ><td>Doe</td></tr><tr class="record" ><td>Smi</td></tr></tbody></table>', $result);
398
399

        // _id: 1, name: <b>Doe</b>
400
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", CONCAT("<b>", name, "</b>") AS "Name" FROM Person ORDER BY id LIMIT 2');
401
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
402
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>Name</th></tr></thead><tbody><tr class="record" ><td>Doe</td></tr><tr class="record" ><td>Smith</td></tr></tbody></table>', $result);
403

404
        // _id: 1, name: <b>Doe</b>, width=2
405
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", CONCAT("<b>", name, "</b>") AS "Name|2" FROM Person ORDER BY id LIMIT 2');
406
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
407
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>Na</th></tr></thead><tbody><tr class="record" ><td>Do</td></tr><tr class="record" ><td>Sm</td></tr></tbody></table>', $result);
408

409
        // _id: 1, name: <b>Doe</b> , nostrip
410
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", CONCAT("<b>", name, "</b>") AS "Name|nostrip" FROM Person ORDER BY id LIMIT 2');
411
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
412
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>Name</th></tr></thead><tbody><tr class="record" ><td><b>Doe</b></td></tr><tr class="record" ><td><b>Smith</b></td></tr></tbody></table>', $result);
413
414

        // _id: 1, icon: bullet-green.gif
415
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", "bullet-green.gif" AS "Status|icon" FROM Person ORDER BY id LIMIT 2');
416
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
417
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>Status</th></tr></thead><tbody><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></tbody></table>', $result);
418
419

        // _id: 1, mailto: john@doe.com
420
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", "john@doe.com" AS "EMail|mailto" FROM Person ORDER BY id LIMIT 2');
421
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
422
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>EMail</th></tr></thead><tbody><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></tbody></table>', $result);
423
424

        // _id: 1, url: www.uzh.ch
425
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", "www.uzh.ch" AS "URL|url" FROM Person ORDER BY id LIMIT 2');
426
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
427
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>URL</th></tr></thead><tbody><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></tbody></table>', $result);
428
429

        // _id: 1, name: Doe, _rowclass (text)
430
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", name, IF(id=1,"text-warning", "text-danger") AS _rowClass FROM Person ORDER BY id LIMIT 2');
431
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
432
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>name</th></tr></thead><tbody><tr class="record text-warning" ><td>Doe</td></tr><tr class="record text-danger" ><td>Smith</td></tr></tbody></table>', $result);
433
434

        // _id: 1, name: Doe, _rowClass (text & background)
435
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", name, IF(id=1,"text-warning active", "text-danger success") AS _rowClass FROM Person ORDER BY id LIMIT 2');
436
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
437
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>name</th></tr></thead><tbody><tr class="record text-warning active" ><td>Doe</td></tr><tr class="record text-danger success" ><td>Smith</td></tr></tbody></table>', $result);
438
439

        // _id: 1, name: Doe, _rowTitle
440
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id AS "_id", name, firstName AS _rowTitle FROM Person ORDER BY id LIMIT 2');
441
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
442
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>name</th></tr></thead><tbody><tr class="record" title="John" ><td>Doe</td></tr><tr class="record" title="Jane" ><td>Smith</td></tr></tbody></table>', $result);
443
444

        // _id: 1, name: Doe, title, width, nostrip
445
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->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');
446
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
447
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>name</th><th>Important</th></tr></thead><tbody><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></tbody></table>', $result);
448
449

        // _id: 1, name: Doe, link
450
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->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');
451
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
452
        $this->assertEquals('<table class="' . SUBRECORD_TABLE_CLASS_DEFAULT . '" ><thead><tr><th>name</th><th></th></tr></thead><tbody><tr class="record" ><td>Doe</td><td><a href="../../../../../../../../../scratch/software/typo3/typo3_src-8.7.20/index.php?id=form&s=badcaffee1234" >Doe</a></td></tr><tr class="record" ><td>Smith</td><td><a href="../../../../../../../../../scratch/software/typo3/typo3_src-8.7.20/index.php?id=form&s=badcaffee1234" >Smith</a></td></tr></tbody></table>', $result);
453
454
    }

455
    /**
456
457
     * @param array $form
     * @param array $formElement
458
459
460
     */
    private function templateFormNFormElement(array &$form, array &$formElement) {
        $form = [
Carsten  Rose's avatar
Carsten Rose committed
461
462
463
464
465
466
467
            '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',
468
            'permitUrlParameter' => '',
Carsten  Rose's avatar
Carsten Rose committed
469
470
471
            'render' => 'bootstrap',
            'multiMode' => 'none',
            'multiSql' => '',
472
473
            'multiDetailForm' => '',
            'multiDetailFormParameter' => '',
Carsten  Rose's avatar
Carsten Rose committed
474
475
476
477
478
479
480
            F_FORWARD_MODE => 'auto',
            F_FORWARD_PAGE => '',
            'bsLabelColumns' => '',
            'bsInputColumns' => '',
            'bsNoteColumns' => '',
            'parameter' => 'maxVisiblePill=>3',
            'deleted' => 'no',
Carsten  Rose's avatar
Carsten Rose committed
481
            F_DB_INDEX => DB_INDEX_DEFAULT,
482
483
484
        ];

        $formElement = [
Carsten  Rose's avatar
Carsten Rose committed
485
            'id' => 123,
Carsten  Rose's avatar
Carsten Rose committed
486
            FE_FORM_ID => 1,
487
488
            FE_ID_CONTAINER => 0,
            FE_DYNAMIC_UPDATE => 'no',
Carsten  Rose's avatar
Carsten Rose committed
489
490
491
492
            'enabled' => 'yes',
            FE_NAME => 'name',
            FE_LABEL => 'Name',
            FE_MODE => 'show',
Carsten  Rose's avatar
Carsten Rose committed
493
            FE_MODE_SQL => '',
Carsten  Rose's avatar
Carsten Rose committed
494
495
496
497
498
499
500
501
502
503
504
505
            'class' => 'native',
            FE_TYPE => 'input',
            'value' => '',
            'sql1' => '',
            'parameter' => '',
            'debug' => 'no',
            'deleted' => 'no',
            'size' => '',
            'maxLength' => '255',
            'tooltip' => '',
            'placeholder' => '',
            FE_CHECK_TYPE => '',
506
            FE_CHECK_PATTERN => '',
Carsten  Rose's avatar
Carsten Rose committed
507
508
            FE_HTML_ID => '123',
            FE_HIDE_ZERO => '0',
509

Carsten  Rose's avatar
Carsten Rose committed
510
            'tabindex' => 0,
511
512
513
514
515
516
517
518
519
520
521
522
523
        ];

    }

//    /**
//     * @throws Exception
//     */
//    protected function setUp() {
//        parent::setUp();
//
//        $this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);
//    }

524
    /**
Carsten  Rose's avatar
Carsten Rose committed
525
526
527
528
529
530
531
532
     * @throws CodeException
     * @throws DbException
     * @throws DownloadException
     * @throws UserFormException
     * @throws UserReportException
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
533
534
535
536
     */
    protected function setUp() {
        $_GET['form'] = 'phpunit_person';

537
538
        try {
            parent::setUp();
539

540
541
542
543
            // Use always the latest (production) FormEditor: played during QuickFormQuery()
            $this->dbArray[DB_INDEX_DEFAULT]->sql("DROP TABLE IF EXISTS `Form`");
            $this->dbArray[DB_INDEX_DEFAULT]->sql("DROP TABLE IF EXISTS `FormElement`");
            $this->executeSQLFile(__DIR__ . '/../../../Source/sql/formEditor.sql', true);
544

545
546
547
            $this->store->unsetStore(STORE_ADDITIONAL_FORM_ELEMENTS);
            $this->executeSQLFile(__DIR__ . '/database/fixtures/Generic.sql', true);
            $this->executeSQLFile(__DIR__ . '/database/fixtures/TestForm.sql', true);
548

549
550
551
            // Defaults
            $this->store->setVar('name', 'varchar(255)', STORE_TABLE_COLUMN_TYPES, true);
            $this->store->setVar('deleted', "enum('yes','no')", STORE_TABLE_COLUMN_TYPES, true);
552
553
554
//        $this->store->setVar(SYSTEM_DB_INDEX_DATA, DB_INDEX_DEFAULT, true);
//        $this->store->setVar(SYSTEM_DB_INDEX_QFQ, DB_INDEX_DEFAULT, true);

555

556
            $GLOBALS["TSFE"] = new FakeTSFEBuildPlain();
557

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

560
561
562
563
564
565
            // After instantiating QuickFormQuery, the latest updates will be played. The SIP is by default activated which breaks current tests.
            $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Form SET permitNew='always', permitEdit='always' WHERE name='form' ");
            $form->process();
        } catch (\Exception $e) {
            echo 'unhandled exception';
        }
566
567
    }
}
568

569
570
571
/**
 * Class FakeTSFEBuildPlain
 */
572
class FakeTSFEBuildPlain {
573
    public $id = 1;
574
575
    public $type = 1;
    public $sys_language_uid = 1;
576
577
}

578

579