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

Marc Egger's avatar
Marc Egger committed
6
namespace IMATHUZH\Qfq\Tests\Unit\Core;
7

Marc Egger's avatar
Marc Egger committed
8
9
10
11
12
13
14
use IMATHUZH\Qfq\Core\BuildFormPlain;
 
use IMATHUZH\Qfq\Core\Helper\Support;
use IMATHUZH\Qfq\Core\QuickFormQuery;
use IMATHUZH\Qfq\Tests\Unit\Core\Database\AbstractDatabaseTest;

require_once(__DIR__ . '/Database/AbstractDatabaseTest.php');
15

16
17
18
/**
 * Class BuildFormPlainTest
 */
19
20
class BuildFormPlainTest extends AbstractDatabaseTest {

21
    /**
Marc Egger's avatar
Marc Egger committed
22
23
24
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
25
     */
26
27
    public function testGetProcessFilter() {

Carsten  Rose's avatar
Carsten Rose committed
28
        $build = new BuildFormPlain([F_DB_INDEX => DB_INDEX_DEFAULT], array(), array(), $this->dbArray);
Marc Egger's avatar
Marc Egger committed
29
      $this->assertEquals(FORM_ELEMENTS_NATIVE, $build->getProcessFilter());
30
31
    }

32
    /**
Marc Egger's avatar
Marc Egger committed
33
34
35
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
36
37
     */
    public function testGetFormId() {
Carsten  Rose's avatar
Carsten Rose committed
38
        $build = new BuildFormPlain([F_DB_INDEX => DB_INDEX_DEFAULT], array(), array(), $this->dbArray);
39
40
41
42
43
44
45
46
47

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

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

    }

48
    /**
Marc Egger's avatar
Marc Egger committed
49
50
51
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
52
     */
53
    public function testWrapItem() {
Carsten  Rose's avatar
Carsten Rose committed
54
        $build = new BuildFormPlain([F_DB_INDEX => DB_INDEX_DEFAULT], array(), array(), $this->dbArray);
55
56
57
58
59
60
61
62
63
64
65
66
67
68

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

69
    /**
Marc Egger's avatar
Marc Egger committed
70
71
72
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
73
     */
74
    public function testBuildLabel() {
Carsten  Rose's avatar
Carsten Rose committed
75
        $build = new BuildFormPlain([F_DB_INDEX => DB_INDEX_DEFAULT], array(), array(), $this->dbArray);
76
77

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

81
    /**
Marc Egger's avatar
Marc Egger committed
82
83
84
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
85
     */
86
87
88
    public function testBuildInput() {
        $form = array();
        $formElement = array();
Carsten  Rose's avatar
Carsten Rose committed
89
        $json = array();
90

91
        $this->templateFormNFormElement($form, $formElement);
92

93
        $build = new BuildFormPlain($form, array(), [$formElement], $this->dbArray);
Elias Villiger's avatar
Elias Villiger committed
94
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
Carsten  Rose's avatar
Carsten Rose committed
95
        $formElement[FE_DECIMAL_FORMAT] = '';
96

97
        $label['123-l'][API_ELEMENT_CONTENT] = '<label for="name:1" class="control-label" >Name</label>';
Carsten  Rose's avatar
Carsten Rose committed
98
99
//        $label['123'][API_ELEMENT_ATTRIBUTE] = ['value' => ''];
        $label['123'][API_ELEMENT_ATTRIBUTE] = ['value' => '', 'required' => false, 'hidden' => false];
Carsten  Rose's avatar
Carsten Rose committed
100
        $label['123-r'][API_ELEMENT_ATTRIBUTE] = ['class' => ''];
101

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

106
        // Min/Max
107
108
        $formElement[FE_MIN] = '1';
        $formElement[FE_MAX] = '10';
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="number" value="" data-error="Error" min="1" max="10" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
111
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
112

113
        $formElement[FE_MIN] = '1';
114
115
        $formElement[FE_MAX] = '';
        $result = $build->buildInput($formElement, 'name:1', '', $json);
116
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" data-error="Error" min="1" 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

        $formElement[FE_MIN] = '';
120
        $formElement[FE_MAX] = '10';
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="" data-error="Error" max="10" 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
127

        $formElement[FE_MIN] = '0';
        $formElement[FE_MAX] = '10';
        $result = $build->buildInput($formElement, 'name:1', '', $json);
128
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" data-error="Error" min="0" max="10" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
129
130
131
132
133
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);

        $formElement[FE_MIN] = '-5';
        $formElement[FE_MAX] = '0';
        $result = $build->buildInput($formElement, 'name:1', '', $json);
134
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" data-error="Error" min="-5" max="0" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
135
136
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);

137
138
139
        // reset so they don't interfere with next tests
        $formElement[FE_MIN] = '';
        $formElement[FE_MAX] = '';
140

141
        // CheckType: custom pattern, standard data-pattern-error
142
143
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_PATTERN;
        $formElement[FE_CHECK_PATTERN] = '^[a-z]*$';
Carsten  Rose's avatar
Carsten Rose committed
144
        $result = $build->buildInput($formElement, 'name:1', '', $json);
145
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="^[a-z]*$" data-pattern-error="' . F_FE_DATA_PATTERN_ERROR_DEFAULT . '" data-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
146
        $label['123'][API_ELEMENT_ATTRIBUTE]['pattern'] = '^[a-z]*$';
147
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
148

149
150
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_DIGIT;
        $formElement[FE_CHECK_PATTERN] = '';
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="255" type="input" value="" pattern="^[\d]*$" data-pattern-error="' . SANITIZE_ALLOW_DIGIT_MESSAGE . '" data-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
153
154

        $label['123'][API_ELEMENT_ATTRIBUTE]['pattern'] = '^[\d]*$';
155
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
156

157
158
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_EMAIL;
        $formElement[FE_CHECK_PATTERN] = '';
Carsten  Rose's avatar
Carsten Rose committed
159
        $result = $build->buildInput($formElement, 'name:1', '', $json);
Marc Egger's avatar
Marc Egger committed
160
        $label['123'][API_ELEMENT_ATTRIBUTE]['pattern'] = '^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})?$';
Marc Egger's avatar
Marc Egger committed
161
        $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="' . SANITIZE_ALLOW_EMAIL_MESSAGE . '" data-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
162
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
163

164
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
165
        $formElement[FE_CHECK_PATTERN] = '';
166
167


168
169
170
        // Decimal format
        $formElement[FE_DECIMAL_FORMAT] = '5,2';
        $result = $build->buildInput($formElement, 'name:1', '', $json);
171
        $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-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
172
        $label['123'][API_ELEMENT_ATTRIBUTE]['pattern'] = '^-?[0-9]{0,3}(\.[0-9]{0,2})?$';
173
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
174
175
176
177
178

        $formElement[FE_DECIMAL_FORMAT] = '';


        // Explicit size
179
180
        $formElement['size'] = 40;
        $formElement['maxLength'] = 40;
Carsten  Rose's avatar
Carsten Rose committed
181
        $result = $build->buildInput($formElement, 'name:1', '', $json);
182
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="40" type="input" size="40" value="" data-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
Carsten  Rose's avatar
Carsten Rose committed
183
        unset($label['123'][API_ELEMENT_ATTRIBUTE]['pattern']);
184
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
185
186

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

192
193
194
195
196
197
        // 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);
198
        $this->assertEquals('<input id="123" name="specialname:1" class="form-control" type="input" value="" data-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
199
200
201
202

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

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

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

219
        // Explicit: further
220
221
222
        $formElement2['tooltip'] = 'Nice Tooltip';
        $formElement2['placeholder'] = 'Please type a name';
        $result = $build->buildInput($formElement2, 'name:1', 'Hello World', $json);
223
        $this->assertEquals('<input id="123" name="name:1" class="form-control" type="input" value="Hello World" placeholder="Please type a name" data-error="Error" 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
224
        $label['123'][API_ELEMENT_ATTRIBUTE] = ['value' => 'Hello World', 'required' => false, 'hidden' => false];
225
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', API_ELEMENT_UPDATE => $label], $json);
226
227

        // textarea
228
229
        $formElement2['size'] = '40,10';
        $result = $build->buildInput($formElement2, 'name:1', 'Hello World', $json);
230
        $this->assertEquals('<textarea id="123" name="name:1" class="form-control" cols="40" rows="10" placeholder="Please type a name" data-error="Error" title="Nice Tooltip" data-hidden="no" data-required="no" >Hello World</textarea><div class="help-block with-errors hidden"></div>', $result);
231

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

234
235
        $formElement2['size'] = ' 40     , 10 ';
        $result = $build->buildInput($formElement2, 'name:1', 'Hello World', $json);
236
        $this->assertEquals('<textarea id="123" name="name:1" class="form-control" cols="40" rows="10" placeholder="Please type a name" data-error="Error" title="Nice Tooltip" data-hidden="no" data-required="no" >Hello World</textarea><div class="help-block with-errors hidden"></div>', $result);
237
        $this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', API_ELEMENT_UPDATE => $label], $json);
238
239
    }

240
    /**
Marc Egger's avatar
Marc Egger committed
241
242
243
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
     */
    public function testBuildInputPattern() {
        $form = array();
        $formElement = array();
        $json = array();

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

        $build = new BuildFormPlain($form, array(), [$formElement], $this->dbArray);
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
        $formElement[FE_DECIMAL_FORMAT] = '';

        $label['123-l'][API_ELEMENT_CONTENT] = '<label for="name:1" class="control-label" >Name</label>';
        $label['123'][API_ELEMENT_ATTRIBUTE] = ['value' => '', 'required' => false, 'hidden' => false];
        $label['123-r'][API_ELEMENT_ATTRIBUTE] = ['class' => ''];

        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALNUMX;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
262
263
264
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="' . PATTERN_ALNUMX .
            '" data-pattern-error="' . SANITIZE_ALLOW_ALNUMX_MESSAGE .
            '" data-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
265
266
267

        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_DIGIT;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
268
269
270
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="' . PATTERN_DIGIT .
            '" data-pattern-error="' . SANITIZE_ALLOW_DIGIT_MESSAGE .
            '" data-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
271
272
273

        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_NUMERICAL;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
274
275
276
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="' . PATTERN_NUMERICAL .
            '" data-pattern-error="' . SANITIZE_ALLOW_NUMERICAL_MESSAGE .
            '" data-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
277
278
279

        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_EMAIL;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
280
281
282
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="' . PATTERN_EMAIL .
            '" data-pattern-error="' . SANITIZE_ALLOW_EMAIL_MESSAGE .
            '" data-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
283
284
285

        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALLBUT;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
286
287
288
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="' . PATTERN_ALLBUT .
            '" data-pattern-error="' . SANITIZE_ALLOW_ALLBUT_MESSAGE .
            '" data-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
289
290
291

        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
292
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" data-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
293
294
295
296

        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_PATTERN;
        $formElement[FE_CHECK_PATTERN] = '^[0-2]$';
        $result = $build->buildInput($formElement, 'name:1', '', $json);
297
298
299
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="' . $formElement[FE_CHECK_PATTERN] .
            '" data-pattern-error="' . F_FE_DATA_PATTERN_ERROR_DEFAULT .
            '" data-error="Error" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
300
301
302
    }

    /**
Marc Egger's avatar
Marc Egger committed
303
304
305
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
306
     */
Carsten  Rose's avatar
Carsten Rose committed
307
    public function testBuildInputSpecificErrorTextDefault() {
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
        $form = array();
        $formElement = array();
        $json = array();

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

        $build = new BuildFormPlain($form, array(), [$formElement], $this->dbArray);
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
        $formElement[FE_DECIMAL_FORMAT] = '';

        $label['123-l'][API_ELEMENT_CONTENT] = '<label for="name:1" class="control-label" >Name</label>';
        $label['123'][API_ELEMENT_ATTRIBUTE] = ['value' => '', 'required' => false, 'hidden' => false];
        $label['123-r'][API_ELEMENT_ATTRIBUTE] = ['class' => ''];

        // Defaults data-pattern-error: ALNUMX
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALNUMX;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="' . PATTERN_ALNUMX .
326
            '" data-pattern-error="' . SANITIZE_ALLOW_ALNUMX_MESSAGE .
327
328
            '" data-error="' . F_FE_DATA_ERROR_DEFAULT . '" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);

Carsten  Rose's avatar
Carsten Rose committed
329
330
331
332
333
334
        // Defaults data-pattern-error: DIGIT
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_DIGIT;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="' . PATTERN_DIGIT .
            '" data-pattern-error="' . SANITIZE_ALLOW_DIGIT_MESSAGE .
            '" data-error="' . F_FE_DATA_ERROR_DEFAULT . '" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
335

Carsten  Rose's avatar
Carsten Rose committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
        // Defaults data-pattern-error: NUMERICAL
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_NUMERICAL;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="' . PATTERN_NUMERICAL .
            '" data-pattern-error="' . SANITIZE_ALLOW_NUMERICAL_MESSAGE .
            '" data-error="' . F_FE_DATA_ERROR_DEFAULT . '" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);

        // Defaults data-pattern-error: EMAIL
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_EMAIL;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="' . PATTERN_EMAIL .
            '" data-pattern-error="' . SANITIZE_ALLOW_EMAIL_MESSAGE .
            '" data-error="' . F_FE_DATA_ERROR_DEFAULT . '" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);

        // Defaults data-pattern-error: ALLBUT
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALLBUT;
        $result = $build->buildInput($formElement, 'name:1', '', $json);
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="' . PATTERN_ALLBUT .
            '" data-pattern-error="' . SANITIZE_ALLOW_ALLBUT_MESSAGE .
            '" data-error="' . F_FE_DATA_ERROR_DEFAULT . '" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
    }

    /**
Marc Egger's avatar
Marc Egger committed
359
360
361
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
Carsten  Rose's avatar
Carsten Rose committed
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
     */
    public function testBuildInputCustomErrorText() {
        $form = array();
        $formElement = array();
        $json = array();

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

        $build = new BuildFormPlain($form, array(), [$formElement], $this->dbArray);
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
        $formElement[FE_DECIMAL_FORMAT] = '';

        $label['123-l'][API_ELEMENT_CONTENT] = '<label for="name:1" class="control-label" >Name</label>';
        $label['123'][API_ELEMENT_ATTRIBUTE] = ['value' => '', 'required' => false, 'hidden' => false];
        $label['123-r'][API_ELEMENT_ATTRIBUTE] = ['class' => ''];

        // Defaults data-error: ALNUMX
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALNUMX;
        $formElement[F_FE_DATA_ERROR] = 'custom data text';
        $formElement[F_FE_DATA_PATTERN_ERROR] = 'custom pattern text';
        $result = $build->buildInput($formElement, 'name:1', '', $json);
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="' . PATTERN_ALNUMX .
            '" data-pattern-error="' . $formElement[F_FE_DATA_PATTERN_ERROR] .
            '" data-error="' . $formElement[F_FE_DATA_ERROR] . '" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);

        // Defaults data-error: ALNUMX
        $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_PATTERN;
        $formElement[FE_CHECK_PATTERN] = '[0-4]';
        $formElement[F_FE_DATA_ERROR] = 'custom data text';
        $formElement[F_FE_DATA_PATTERN_ERROR] = 'custom pattern text';
        $result = $build->buildInput($formElement, 'name:1', '', $json);
        $this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="[0-4]' .
            '" data-pattern-error="' . $formElement[F_FE_DATA_PATTERN_ERROR] .
            '" data-error="' . $formElement[F_FE_DATA_ERROR] . '" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
396
397
    }

398
    /**
Marc Egger's avatar
Marc Egger committed
399
400
401
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
402
     */
403
404
405
406
    public function testGetKeyValueListFromSqlEnumSpec() {
        $form = array();
        $formElement = array();

407
        $this->templateFormNFormElement($form, $formElement);
408
409
        $formElement['name'] = 'deleted';

Carsten  Rose's avatar
Carsten Rose committed
410
        $build = new BuildFormPlain($form, array(), [$formElement], $this->dbArray);
411
412
413
414
415
416
417
418
419
420
421
422

        $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'];
423
        $formElement[FE_EMPTY_ITEM_AT_START] = '';
424
425
426
427
428
429
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // Spec Enum + emptyItemAtEnd
        $expect = ['', 'yes', 'no', ''];
430
        $formElement[FE_EMPTY_ITEM_AT_END] = '';
431
432
433
434
435
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // clean
436
437
        unset($formElement[FE_EMPTY_ITEM_AT_START]);
        unset($formElement[FE_EMPTY_ITEM_AT_END]);
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462

        // 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']);

463
        // SQL: any content is fine, but should always be the same to do checks
464
        $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
465
//        $expect = ['form', 'formElement', 'phpunit_person'];
466
        $expect = ['form', 'formElement', 'copyForm'];
467
468
469
470
471
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // SQL (one column, no keyword) + emptyItemAtStart
472
        $formElement[FE_EMPTY_ITEM_AT_START] = '';
473
        $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
474
//        $expect = ['', 'form', 'formElement', 'phpunit_person'];
475
        $expect = ['', 'form', 'formElement', 'copyForm'];
476
477
478
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);
Carsten  Rose's avatar
Carsten Rose committed
479
        unset($formElement[FE_EMPTY_ITEM_AT_START]);
480
481

        // SQL (4 columns, none 'id' nor 'label') - Take the first two columns
482
483
        $expectKeys = ['100', '200', '300'];
        $expectValues = ['basic', 'formelement', 'layout'];
484
        $formElement['sql1'] = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT ord, name, created, modified  FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
485
486
487
488
489
        $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
490
        $expectKeys = ['100', '200', '300'];
491
        $expectValues = ['basic', 'formelement', 'layout'];
492
        $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');
493
494
495
496
497
498
        $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'];
499
        $expectValues = ['basic', 'formelement', 'layout'];
500
        $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');
501
502
503
504
505
506
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);
    }

    /**
Marc Egger's avatar
Marc Egger committed
507
     * @expectedException \UserFormException
508
     *
Marc Egger's avatar
Marc Egger committed
509
510
511
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
512
513
514
515
516
     */
    public function testGetKeyValueListFromSqlEnumSpecException() {
        $form = array();
        $formElement = array();

517
        $this->templateFormNFormElement($form, $formElement);
518

Carsten  Rose's avatar
Carsten Rose committed
519
        $build = new BuildFormPlain($form, array(), [$formElement], $this->dbArray);
520
521

        $formElement['name'] = 'noteInternal';
522
523
        $keys = array();
        $values = array();
524
525
526
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
    }

527
528

    /**
Marc Egger's avatar
Marc Egger committed
529
530
531
532
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
533
534
535
536
537
538
539
     */
    public function testBuildSubrecord() {
        $form = array();
        $formElement = array();
        $json = array();

        $this->templateFormNFormElement($form, $formElement);
Carsten  Rose's avatar
Carsten Rose committed
540
        $formElement = Support::setFeDefaults($formElement);
Carsten  Rose's avatar
Carsten Rose committed
541

542
        // CheckType
Carsten  Rose's avatar
Carsten Rose committed
543
        $build = new BuildFormPlain($form, array(), [$formElement], $this->dbArray);
544
545

        // id: 1, firstName: John, name: Doe
546
        $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
547
        $formElement[SUBRECORD_PARAMETER_FORM] = 'Person';
Carsten  Rose's avatar
Carsten Rose committed
548
        $formElement[FE_SUBRECORD_OPTION] = '';
Carsten  Rose's avatar
Carsten Rose committed
549

550
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
551
        $this->assertEquals('<table class="table table-hover qfq-subrecord-table qfq-color-grey-2" ><thead><tr><th>id</th><th>name</th><th>firstName</th></tr></thead><tbody ><tr class="record" ><td><span class="text-muted">1</span></td><td>Doe</td><td>John</td></tr><tr class="record" ><td><span class="text-muted">2</span></td><td>Smith</td><td>Jane</td></tr></tbody></table>', $result);
Marc Egger's avatar
Marc Egger committed
552
 //       $this->assertEquals('Please save this record first.', $result);
553
554

        $this->store->setStore(['id' => 1], STORE_RECORD, true);
555
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
556
        $this->assertEquals('<table class="table table-hover qfq-subrecord-table qfq-color-grey-2" ><thead><tr><th>id</th><th>name</th><th>firstName</th></tr></thead><tbody ><tr class="record" ><td><span class="text-muted">1</span></td><td>Doe</td><td>John</td></tr><tr class="record" ><td><span class="text-muted">2</span></td><td>Smith</td><td>Jane</td></tr></tbody></table>', $result);
557
558

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

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

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

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

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

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

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

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

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

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

608
        // _id: 1, name: <b>Doe</b>, width=2
609
        $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');
610
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
611
        $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);
612

613
        // _id: 1, name: <b>Doe</b> , nostrip
614
        $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');
615
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
616
        $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);
617
618

        // _id: 1, icon: bullet-green.gif
619
        $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');
620
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
621
        $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);
622
623

        // _id: 1, mailto: john@doe.com
624
        $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');
625
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
626
        $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);
627
628

        // _id: 1, url: www.uzh.ch
629
        $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');
630
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
631
        $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);
632
633

        // _id: 1, name: Doe, _rowclass (text)
634
        $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');
635
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
636
        $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);
637
638

        // _id: 1, name: Doe, _rowClass (text & background)
639
        $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');
640
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
641
        $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);
642
643

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

        // _id: 1, name: Doe, title, width, nostrip
649
        $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');
650
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
651
        $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);
652
653

        // _id: 1, name: Doe, link
654
        $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');
655
        $result = $build->buildSubrecord($formElement, 'name:1', '', $json);
Carsten  Rose's avatar
Carsten Rose committed
656
        $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);
657
658
    }

659
    /**
660
661
     * @param array $form
     * @param array $formElement
662
663
664
     */
    private function templateFormNFormElement(array &$form, array &$formElement) {
        $form = [
Carsten  Rose's avatar
Carsten Rose committed
665
666
667
668
669
670
671
            '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',
672
            'permitUrlParameter' => '',
Carsten  Rose's avatar
Carsten Rose committed
673
674
675
            'render' => 'bootstrap',
            'multiMode' => 'none',
            'multiSql' => '',
676
677
            'multiDetailForm' => '',
            'multiDetailFormParameter' => '',
Carsten  Rose's avatar
Carsten Rose committed
678
679
680
681
682
683
684
            F_FORWARD_MODE => 'auto',
            F_FORWARD_PAGE => '',
            'bsLabelColumns' => '',
            'bsInputColumns' => '',
            'bsNoteColumns' => '',
            'parameter' => 'maxVisiblePill=>3',
            'deleted' => 'no',
Carsten  Rose's avatar
Carsten Rose committed
685
            F_DB_INDEX => DB_INDEX_DEFAULT,
686
687
688
        ];

        $formElement = [
Carsten  Rose's avatar
Carsten Rose committed
689
            'id' => 123,
Carsten  Rose's avatar
Carsten Rose committed
690
            FE_FORM_ID => 1,
691
692
            FE_ID_CONTAINER => 0,
            FE_DYNAMIC_UPDATE => 'no',
Carsten  Rose's avatar
Carsten Rose committed
693
694
695
696
            'enabled' => 'yes',
            FE_NAME => 'name',
            FE_LABEL => 'Name',
            FE_MODE => 'show',
Carsten  Rose's avatar
Carsten Rose committed
697
            FE_MODE_SQL => '',
Carsten  Rose's avatar
Carsten Rose committed
698
699
700
701
702
703
704
705
706
707
708
709
            'class' => 'native',
            FE_TYPE => 'input',
            'value' => '',
            'sql1' => '',
            'parameter' => '',
            'debug' => 'no',
            'deleted' => 'no',
            'size' => '',
            'maxLength' => '255',
            'tooltip' => '',
            'placeholder' => '',
            FE_CHECK_TYPE => '',
710
            FE_CHECK_PATTERN => '',
Carsten  Rose's avatar
Carsten Rose committed
711
712
            FE_HTML_ID => '123',
            FE_HIDE_ZERO => '0',
Carsten  Rose's avatar
Carsten Rose committed
713
            FE_DATA_REFERENCE => '',
714

715
716
            F_FE_DATA_ERROR => F_FE_DATA_ERROR_DEFAULT,
            F_FE_DATA_PATTERN_ERROR => F_FE_DATA_PATTERN_ERROR_DEFAULT,
717
            F_FE_DATA_PATTERN_ERROR_SYSTEM => F_FE_DATA_PATTERN_ERROR_DEFAULT,
718

Carsten  Rose's avatar
Carsten Rose committed
719
            'tabindex' => 0,
720
721
722
723
724
725
726
727
728
729
730
731
732
        ];

    }

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

733
    /**
Marc Egger's avatar
Marc Egger committed
734
     *
735
736
737
738
     */
    protected function setUp() {
        $_GET['form'] = 'phpunit_person';

739
740
        try {
            parent::setUp();
741

742
743
744
            // 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`");
Marc Egger's avatar
Marc Egger committed
745
            $this->executeSQLFile(__DIR__ . '/../../../Classes/Sql/formEditor.sql', true);
746

747
            $this->store->unsetStore(STORE_ADDITIONAL_FORM_ELEMENTS);
Marc Egger's avatar
Marc Egger committed
748
749
            $this->executeSQLFile(__DIR__ . '/Database/fixtures/Generic.sql', true);
            $this->executeSQLFile(__DIR__ . '/Database/fixtures/TestForm.sql', true);
750

751
752
753
            // Defaults
            $this->store->setVar('name', 'varchar(255)', STORE_TABLE_COLUMN_TYPES, true);
            $this->store->setVar('deleted', "enum('yes','no')", STORE_TABLE_COLUMN_TYPES, true);
754
755
756
//        $this->store->setVar(SYSTEM_DB_INDEX_DATA, DB_INDEX_DEFAULT, true);
//        $this->store->setVar(SYSTEM_DB_INDEX_QFQ, DB_INDEX_DEFAULT, true);

757

758
            $GLOBALS["TSFE"] = new FakeTSFEBuildPlain();
759

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

762
763
764
765
766
767
            // 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';
        }
768
769
    }
}
770

771
772
773
/**
 * Class FakeTSFEBuildPlain
 */
774
class FakeTSFEBuildPlain {
775
    public $id = 1;
776
777
    public $type = 1;
    public $sys_language_uid = 1;
778
779
}

780

781