BuildFormPlainTest.php 30.8 KB
Newer Older
1
<?php
2
3
4
/**
 * @author Rafael Ostertag <rafael.ostertag@math.uzh.ch>
 */
5
6

require_once(__DIR__ . '/AbstractDatabaseTest.php');
7
8
9
require_once(__DIR__ . '/../../qfq/database/Database.php');
require_once(__DIR__ . '/../../qfq/exceptions/DbException.php');
require_once(__DIR__ . '/../../qfq/QuickFormQuery.php');
Carsten  Rose's avatar
Carsten Rose committed
10
require_once(__DIR__ . '/../../qfq/helper/Support.php');
11
12
13

class BuildFormPlainTest extends AbstractDatabaseTest {

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

19
        $build = new \qfq\BuildFormPlain(array(), array(), array(), $this->dbArray);
20
21
22
23

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

24
25
26
27
    /**
     *
     */
    public function testGetFormId() {
28
        $build = new \qfq\BuildFormPlain(array(), array(), array(), $this->dbArray);
29
30
31
32
33
34
35
36
37

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

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

    }

38
39
40
    /**
     *
     */
41
    public function testWrapItem() {
42
        $build = new \qfq\BuildFormPlain(array(), array(), array(), $this->dbArray);
43
44
45
46
47
48
49
50
51
52
53
54
55
56

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

57
58
59
    /**
     *
     */
60
    public function testBuildLabel() {
61
        $build = new \qfq\BuildFormPlain(array(), array(), array(), $this->dbArray);
62
63

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

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

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

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

79
        $label['123-l'][API_ELEMENT_CONTENT] = '<label for="name:1" class="control-label" >Name</label>';
Carsten  Rose's avatar
Carsten Rose committed
80
        $label['123'][API_ELEMENT_ATTRIBUTE] = ['value' => ''];
Carsten  Rose's avatar
Carsten Rose committed
81
        $label['123-r'][API_ELEMENT_ATTRIBUTE] = ['class' => ''];
82

Carsten  Rose's avatar
Carsten Rose committed
83
        $result = $build->buildInput($formElement, 'name:1', '', $json);
84
        $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
85
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
86
87

        // CheckType
88
        // TODO: MIN/MAX
89
90
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_MIN_MAX;
        $formElement[FE_CHECK_PATTERN] = '1|10';
Carsten  Rose's avatar
Carsten Rose committed
91
        $result = $build->buildInput($formElement, 'name:1', '', $json);
92
        $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
93
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
94

95
96
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_PATTERN;
        $formElement[FE_CHECK_PATTERN] = '^[a-z]*$';
Carsten  Rose's avatar
Carsten Rose committed
97
        $result = $build->buildInput($formElement, 'name:1', '', $json);
98
        $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
99
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
100

101
102
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_DIGIT;
        $formElement[FE_CHECK_PATTERN] = '';
Carsten  Rose's avatar
Carsten Rose committed
103
        $result = $build->buildInput($formElement, 'name:1', '', $json);
104
        $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
105
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
106

107
108
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_EMAIL;
        $formElement[FE_CHECK_PATTERN] = '';
Carsten  Rose's avatar
Carsten Rose committed
109
        $result = $build->buildInput($formElement, 'name:1', '', $json);
110
        $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
111
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
112

113
114
        $formElement[FE_CHECK_TYPE] = '';
        $formElement[FE_CHECK_PATTERN] = '';
115
116
117
118
119


        // Explizit size
        $formElement['size'] = 40;
        $formElement['maxLength'] = 40;
Carsten  Rose's avatar
Carsten Rose committed
120
        $result = $build->buildInput($formElement, 'name:1', '', $json);
121
        $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
122
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
123
124
125

        // maxlength bigger than physical spec:
        $formElement['maxLength'] = 1000;
Carsten  Rose's avatar
Carsten Rose committed
126
        $result = $build->buildInput($formElement, 'name:1', '', $json);
127
        $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
128
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
129

130
131
132
133
134
135
        // 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);
136
        $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);
137
138
139
140

        // no size,  given maxlength and column not in primary table
        $formElement2['maxLength'] = '10';
        $result = $build->buildInput($formElement2, 'specialname:1', '', $json);
141
        $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);
142
143
144
145
146

        // size given, no maxlength and column not in primary table
        $formElement2['maxLength'] = '';
        $formElement2['size'] = '10';
        $result = $build->buildInput($formElement2, 'specialname:1', '', $json);
147
        $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);
148
149
150
151
152

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

155
156
157
        // Explicit: further
        $formElement['tooltip'] = 'Nice Tooltip';
        $formElement['placeholder'] = 'Please type a name';
Carsten  Rose's avatar
Carsten Rose committed
158
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
159
        $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
160
        $label['123'][API_ELEMENT_ATTRIBUTE] = ['value' => 'Hello World'];
Carsten  Rose's avatar
Carsten Rose committed
161
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
162
163
164

        // textarea
        $formElement['size'] = '40,10';
Carsten  Rose's avatar
Carsten Rose committed
165
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
166
        $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);
167

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

        $formElement['size'] = ' 40     , 10 ';
Carsten  Rose's avatar
Carsten Rose committed
171
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
172
        $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
173
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
174
175
    }

176
177
178
    /**
     *
     */
179
180
181
182
    public function testGetKeyValueListFromSqlEnumSpec() {
        $form = array();
        $formElement = array();

183
        $this->templateFormNFormElement($form, $formElement);
184
185
        $formElement['name'] = 'deleted';

186
        $build = new \qfq\BuildFormPlain($form, array(), [$formElement], $this->dbArray);
187
188
189
190
191
192
193
194
195
196
197
198

        $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'];
199
        $formElement[FE_EMPTY_ITEM_AT_START] = '';
200
201
202
203
204
205
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // Spec Enum + emptyItemAtEnd
        $expect = ['', 'yes', 'no', ''];
206
        $formElement[FE_EMPTY_ITEM_AT_END] = '';
207
208
209
210
211
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // clean
212
213
        unset($formElement[FE_EMPTY_ITEM_AT_START]);
        unset($formElement[FE_EMPTY_ITEM_AT_END]);
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

        // 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
240
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT name FROM Form AS f ORDER BY f.id LIMIT 3');
Carsten  Rose's avatar
Carsten Rose committed
241
242
//        $expect = ['form', 'formElement', 'phpunit_person'];
        $expect = ['form', 'formElement', 'copyForm'];
243
244
245
246
247
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // SQL (one column, no keyword) + emptyItemAtStart
248
        $formElement[FE_EMPTY_ITEM_AT_START] = '';
249
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT name FROM Form AS f ORDER BY f.id LIMIT 3');
Carsten  Rose's avatar
Carsten Rose committed
250
251
//        $expect = ['', 'form', 'formElement', 'phpunit_person'];
        $expect = ['', 'form', 'formElement', 'copyForm'];
252
253
254
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);
Carsten  Rose's avatar
Carsten Rose committed
255
        unset($formElement[FE_EMPTY_ITEM_AT_START]);
256
257
258

        // SQL (4 columns, none 'id' nor 'label') - Take the first two columns
        $expectKeys = ['10', '20', '30'];
259
        $expectValues = ['basic', 'access', 'various'];
260
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT ord, name, created, modified  FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
261
262
263
264
265
266
        $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'];
267
        $expectValues = ['basic', 'access', 'various'];
268
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT ord, created, modified, name AS label FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
269
270
271
272
273
274
        $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'];
275
        $expectValues = ['basic', 'access', 'various'];
276
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT ord, created, modified, name AS label, id FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
277
278
279
280
281
282
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);
    }

    /**
283
     * @expectedException \qfq\UserFormException
284
285
286
287
288
289
     *
     */
    public function testGetKeyValueListFromSqlEnumSpecException() {
        $form = array();
        $formElement = array();

290
        $this->templateFormNFormElement($form, $formElement);
291

292
        $build = new \qfq\BuildFormPlain($form, array(), [$formElement], $this->dbArray);
293
294

        $formElement['name'] = 'noteInternal';
295
296
        $keys = array();
        $values = array();
297
298
299
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
    }

300
301
302
303
304
305
306
307
308
309

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

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

312
        // CheckType
313
        $build = new \qfq\BuildFormPlain($form, array(), [$formElement], $this->dbArray);
314
315

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

318
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
319
        $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);
320
321

        // _id: 1, name: Doe,
322
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", name FROM Person ORDER BY id LIMIT 2');
323
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
324
        $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);
325

326
        // _id: 1, name: Doe,title=''
327
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", name AS "title=" FROM Person ORDER BY id LIMIT 2');
328
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
329
        $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);
330
331

        // _id: 1, name: Doe, column: _Person
332
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", name AS "unused|width=2|title=_Person", firstName  FROM Person ORDER BY id LIMIT 2');
333
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
334
        $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);
335

336
        // _id: 1, name: Doe, title: PERSON
337
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", name AS "PERSON" FROM Person ORDER BY id LIMIT 2');
338
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
339
        $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);
340
341

        // _id: 1, "This is a much longer text than necessary": Default max:20
342
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", "This is a much longer text than necessary" FROM Person ORDER BY id LIMIT 1');
343
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
344
        $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);
345
346

        // _id: 1, name: Jo (width:2)
347
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", name AS "2" FROM Person ORDER BY id LIMIT 2');
348
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
349
        $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);
350
351

        // _id: 1, name: Jo (width:2)
352
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", name AS "2|PERSON" FROM Person ORDER BY id LIMIT 2');
353
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
354
        $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);
355
356

        // _id: 1, name: Doe ('width':3)
357
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", name AS "Name|width=3" FROM Person ORDER BY id LIMIT 2');
358
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
359
        $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);
360
361

        // _id: 1, name: Doe (width:3, title:PERSON)
362
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", name AS "3|title=PERSON" FROM Person ORDER BY id LIMIT 2');
363
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
364
        $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);
365
366

        // _id: 1, name: <b>Doe</b>
367
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", CONCAT("<b>", name, "</b>") AS "Name" FROM Person ORDER BY id LIMIT 2');
368
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
369
        $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);
370

371
        // _id: 1, name: <b>Doe</b>, width=2
372
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", CONCAT("<b>", name, "</b>") AS "Name|2" FROM Person ORDER BY id LIMIT 2');
373
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
374
        $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);
375

376
        // _id: 1, name: <b>Doe</b> , nostrip
377
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", CONCAT("<b>", name, "</b>") AS "Name|nostrip" FROM Person ORDER BY id LIMIT 2');
378
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
379
        $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);
380
381

        // _id: 1, icon: bullet-green.gif
382
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", "bullet-green.gif" AS "Status|icon" FROM Person ORDER BY id LIMIT 2');
383
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
384
        $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);
385
386

        // _id: 1, mailto: john@doe.com
387
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", "john@doe.com" AS "EMail|mailto" FROM Person ORDER BY id LIMIT 2');
388
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
389
        $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);
390
391

        // _id: 1, url: www.uzh.ch
392
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", "www.uzh.ch" AS "URL|url" FROM Person ORDER BY id LIMIT 2');
393
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
394
        $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);
395
396

        // _id: 1, name: Doe, _rowclass (text)
397
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", name, IF(id=1,"text-warning", "text-danger") AS _rowClass FROM Person ORDER BY id LIMIT 2');
398
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
399
        $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);
400
401

        // _id: 1, name: Doe, _rowClass (text & background)
402
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_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');
403
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
404
        $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);
405
406

        // _id: 1, name: Doe, _rowTitle
407
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql('SELECT id AS "_id", name, firstName AS _rowTitle FROM Person ORDER BY id LIMIT 2');
408
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
409
        $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);
410
411

        // _id: 1, name: Doe, title, width, nostrip
412
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_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');
413
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
414
        $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);
415
416

        // _id: 1, name: Doe, link
417
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DATA_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');
418
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
419
        $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="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></tbody></table>', $result);
420
421
    }

422
423
424
425
426
    /**
     *
     */
    private function templateFormNFormElement(array &$form, array &$formElement) {
        $form = [
Carsten  Rose's avatar
Carsten Rose committed
427
428
429
430
431
432
433
            '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',
434
            'permitUrlParameter' => '',
Carsten  Rose's avatar
Carsten Rose committed
435
436
437
            'render' => 'bootstrap',
            'multiMode' => 'none',
            'multiSql' => '',
438
439
            'multiDetailForm' => '',
            'multiDetailFormParameter' => '',
Carsten  Rose's avatar
Carsten Rose committed
440
441
442
443
444
445
446
            F_FORWARD_MODE => 'auto',
            F_FORWARD_PAGE => '',
            'bsLabelColumns' => '',
            'bsInputColumns' => '',
            'bsNoteColumns' => '',
            'parameter' => 'maxVisiblePill=>3',
            'deleted' => 'no',
447
448
449
        ];

        $formElement = [
Carsten  Rose's avatar
Carsten Rose committed
450
            'id' => 123,
Carsten  Rose's avatar
Carsten Rose committed
451
            FE_FORM_ID => 1,
452
453
            FE_ID_CONTAINER => 0,
            FE_DYNAMIC_UPDATE => 'no',
Carsten  Rose's avatar
Carsten Rose committed
454
455
456
457
            'enabled' => 'yes',
            FE_NAME => 'name',
            FE_LABEL => 'Name',
            FE_MODE => 'show',
Carsten  Rose's avatar
Carsten Rose committed
458
            FE_MODE_SQL => '',
Carsten  Rose's avatar
Carsten Rose committed
459
460
461
462
463
464
465
466
467
468
469
470
            'class' => 'native',
            FE_TYPE => 'input',
            'value' => '',
            'sql1' => '',
            'parameter' => '',
            'debug' => 'no',
            'deleted' => 'no',
            'size' => '',
            'maxLength' => '255',
            'tooltip' => '',
            'placeholder' => '',
            FE_CHECK_TYPE => '',
471
            FE_CHECK_PATTERN => '',
Carsten  Rose's avatar
Carsten Rose committed
472
473
            FE_HTML_ID => '123',
            FE_HIDE_ZERO => '0',
474

Carsten  Rose's avatar
Carsten Rose committed
475
            'tabindex' => 0,
476
477
478
479
480
481
482
483
484
485
486
487
488
        ];

    }

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

489
490
491
492
493
494
495
496
    /**
     * @throws Exception
     */
    protected function setUp() {
        $_GET['form'] = 'phpunit_person';

        parent::setUp();

497
        // Use always the latest (production) FormEditor: played during QuickFormQuery()
498
499
        $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql("DROP TABLE IF EXISTS `Form`");
        $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql("DROP TABLE IF EXISTS `FormElement`");
500
501
        $this->executeSQLFile(__DIR__ . '/../../sql/formEditor.sql', true);

502
        $this->store->unsetStore(STORE_ADDITIONAL_FORM_ELEMENTS);
503
        $this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);
504
        $this->executeSQLFile(__DIR__ . '/fixtures/TestForm.sql', true);
505
506
507
508
509

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

510
        $GLOBALS["TSFE"] = new FakeTSFEBuildPlain();
511

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

514
        // After instantiating QuickFormQuery, the latest updates will be played. The SIP is by default activated which breaks current tests.
515
        $this->dbArray[DB_INDEX_DATA_DEFAULT]->sql("UPDATE Form SET permitNew='always', permitEdit='always' WHERE name='form' ");
Carsten  Rose's avatar
Carsten Rose committed
516
        $form->process();
517
518
    }
}
519

520
class FakeTSFEBuildPlain {
521
    public $id = 1;
522
523
    public $type = 1;
    public $sys_language_uid = 1;
524
525
}

526

527