BuildFormPlainTest.php 33 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() {

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

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

24
25
26
27
    /**
     *
     */
    public function testGetFormId() {
Carsten  Rose's avatar
Carsten Rose committed
28
        $build = new \qfq\BuildFormPlain([F_DB_INDEX => DB_INDEX_DEFAULT], 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() {
Carsten  Rose's avatar
Carsten Rose committed
42
        $build = new \qfq\BuildFormPlain([F_DB_INDEX => DB_INDEX_DEFAULT], 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() {
Carsten  Rose's avatar
Carsten Rose committed
61
        $build = new \qfq\BuildFormPlain([F_DB_INDEX => DB_INDEX_DEFAULT], 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);
Elias Villiger's avatar
Elias Villiger committed
78
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
79

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

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

88
89
90
        // Min/Max
        $formElement[FE_MIN] = 1;
        $formElement[FE_MAX] = 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="number" 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
97
        $formElement[FE_MIN] = 1;
        $formElement[FE_MAX] = '';
        $result = $build->buildInput($formElement, 'name:1', '', $json);
98
        $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);
99
100
101
102
103
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);

        $formElement[FE_MIN] = '';
        $formElement[FE_MAX] = 10;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
104
        $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);
105
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
106
107
108
        // reset so they don't interfere with next tests
        $formElement[FE_MIN] = '';
        $formElement[FE_MAX] = '';
109
110

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

117
118
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_DIGIT;
        $formElement[FE_CHECK_PATTERN] = '';
Carsten  Rose's avatar
Carsten Rose committed
119
        $result = $build->buildInput($formElement, 'name:1', '', $json);
120
        $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);
Carsten  Rose's avatar
Carsten Rose committed
121
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
122

123
124
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_EMAIL;
        $formElement[FE_CHECK_PATTERN] = '';
Carsten  Rose's avatar
Carsten Rose committed
125
        $result = $build->buildInput($formElement, 'name:1', '', $json);
126
        $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);
Carsten  Rose's avatar
Carsten Rose committed
127
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
128

129
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
130
        $formElement[FE_CHECK_PATTERN] = '';
131
132


133
134
135
        // Decimal format
        $formElement[FE_DECIMAL_FORMAT] = '5,2';
        $result = $build->buildInput($formElement, 'name:1', '', $json);
136
        $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);
137
138
139
140
141
142
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);

        $formElement[FE_DECIMAL_FORMAT] = '';


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

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

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

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

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

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

182
        // Explicit: further
183
184
185
186
        $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
187
        $label['123'][API_ELEMENT_ATTRIBUTE] = ['value' => 'Hello World'];
Carsten  Rose's avatar
Carsten Rose committed
188
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
189
190

        // textarea
191
192
193
        $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);
194

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

197
198
199
        $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);
Carsten  Rose's avatar
Carsten Rose committed
200
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
201
202
    }

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

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

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

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

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

        // clean
239
240
        unset($formElement[FE_EMPTY_ITEM_AT_START]);
        unset($formElement[FE_EMPTY_ITEM_AT_END]);
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

        // 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
267
        $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
268
269
//        $expect = ['form', 'formElement', 'phpunit_person'];
        $expect = ['form', 'formElement', 'copyForm'];
270
271
272
273
274
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // SQL (one column, no keyword) + emptyItemAtStart
275
        $formElement[FE_EMPTY_ITEM_AT_START] = '';
276
        $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
277
278
//        $expect = ['', 'form', 'formElement', 'phpunit_person'];
        $expect = ['', 'form', 'formElement', 'copyForm'];
279
280
281
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);
Carsten  Rose's avatar
Carsten Rose committed
282
        unset($formElement[FE_EMPTY_ITEM_AT_START]);
283
284
285

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

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

317
        $this->templateFormNFormElement($form, $formElement);
318

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

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

327
328
329
330
331
332
333
334
335
336

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

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

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

        // id: 1, firstName: John, name: Doe
343
        $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
344

345
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
346
        $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);
347
348

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

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

        // _id: 1, name: Doe, column: _Person
359
        $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');
360
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
361
        $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);
362

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

        // _id: 1, "This is a much longer text than necessary": Default max:20
369
        $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');
370
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
371
        $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);
372
373

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

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

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

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

        // _id: 1, name: <b>Doe</b>
394
        $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');
395
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
396
        $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);
397

398
        // _id: 1, name: <b>Doe</b>, width=2
399
        $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');
400
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
401
        $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);
402

403
        // _id: 1, name: <b>Doe</b> , nostrip
404
        $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');
405
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
406
        $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);
407
408

        // _id: 1, icon: bullet-green.gif
409
        $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');
410
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
411
        $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);
412
413

        // _id: 1, mailto: john@doe.com
414
        $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');
415
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
416
        $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);
417
418

        // _id: 1, url: www.uzh.ch
419
        $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');
420
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
421
        $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);
422
423

        // _id: 1, name: Doe, _rowclass (text)
424
        $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');
425
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
426
        $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);
427
428

        // _id: 1, name: Doe, _rowClass (text & background)
429
        $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');
430
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
431
        $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);
432
433

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

        // _id: 1, name: Doe, title, width, nostrip
439
        $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');
440
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
441
        $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);
442
443

        // _id: 1, name: Doe, link
444
        $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');
445
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
446
        $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" >Doe</a></td></tr><tr class="record" ><td>Smith</td><td><a href="index.php?id=form&s=badcaffee1234" >Smith</a></td></tr></tbody></table>', $result);
447
448
    }

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

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

Carsten  Rose's avatar
Carsten Rose committed
503
            'tabindex' => 0,
504
505
506
507
508
509
510
511
512
513
514
515
516
        ];

    }

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

517
518
519
520
521
522
523
524
    /**
     * @throws Exception
     */
    protected function setUp() {
        $_GET['form'] = 'phpunit_person';

        parent::setUp();

525
        // Use always the latest (production) FormEditor: played during QuickFormQuery()
526
527
        $this->dbArray[DB_INDEX_DEFAULT]->sql("DROP TABLE IF EXISTS `Form`");
        $this->dbArray[DB_INDEX_DEFAULT]->sql("DROP TABLE IF EXISTS `FormElement`");
528
529
        $this->executeSQLFile(__DIR__ . '/../../sql/formEditor.sql', true);

530
        $this->store->unsetStore(STORE_ADDITIONAL_FORM_ELEMENTS);
531
        $this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);
532
        $this->executeSQLFile(__DIR__ . '/fixtures/TestForm.sql', true);
533
534
535
536

        // Defaults
        $this->store->setVar('name', 'varchar(255)', STORE_TABLE_COLUMN_TYPES, true);
        $this->store->setVar('deleted', "enum('yes','no')", STORE_TABLE_COLUMN_TYPES, true);
537
538
539
//        $this->store->setVar(SYSTEM_DB_INDEX_DATA, DB_INDEX_DEFAULT, true);
//        $this->store->setVar(SYSTEM_DB_INDEX_QFQ, DB_INDEX_DEFAULT, true);

540

541
        $GLOBALS["TSFE"] = new FakeTSFEBuildPlain();
542

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

545
        // After instantiating QuickFormQuery, the latest updates will be played. The SIP is by default activated which breaks current tests.
546
        $this->dbArray[DB_INDEX_DEFAULT]->sql("UPDATE Form SET permitNew='always', permitEdit='always' WHERE name='form' ");
Carsten  Rose's avatar
Carsten Rose committed
547
        $form->process();
548
549
    }
}
550

551
class FakeTSFEBuildPlain {
552
    public $id = 1;
553
554
    public $type = 1;
    public $sys_language_uid = 1;
555
556
}

557

558