BuildFormPlainTest.php 16.6 KB
Newer Older
1
2
3
<?php

require_once(__DIR__ . '/../../qfq/BuildFormPlain.php');
4
require_once(__DIR__ . '/../../qfq/QuickFormQuery.php');
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
require_once(__DIR__ . '/AbstractDatabaseTest.php');

/**
 * Created by PhpStorm.
 * User: crose
 * Date: 2/2/16
 * Time: 10:07 PM
 */
class BuildFormPlainTest extends AbstractDatabaseTest {

    public function testGetProcessFilter() {

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

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

22
23
24
25
26
27
28
29
30
31
32
33
34
35
    /**
     *
     */
    public function testGetFormId() {
        $build = new \qfq\BuildFormPlain(array(), array(), array());

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

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

    }

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    public function testWrapItem() {
        $build = new \qfq\BuildFormPlain(array(), array(), array());

        $result = $build->wrapItem(WRAP_SETUP_ELEMENT, 'Hello World', false);
        $this->assertEquals('<p>Hello World</p>', $result);

        $result = $build->wrapItem(WRAP_SETUP_ELEMENT, 'Hello World', true);
        $this->assertEquals('<p>Hello World</p>', $result);

        $result = $build->wrapItem(WRAP_SETUP_ELEMENT, '', false);
        $this->assertEquals('<p></p>', $result);

        $result = $build->wrapItem(WRAP_SETUP_ELEMENT, '', true);
        $this->assertEquals('', $result);
    }

    public function testBuildLabel() {
        $build = new \qfq\BuildFormPlain(array(), array(), array());

        $result = $build->buildLabel('myLabel:123', "Hello World");
56
        $this->assertEquals('<label for="myLabel:123" class="control-label" >Hello World</label>', $result);
57
58
59
60
61
    }

    public function testBuildInput() {
        $form = array();
        $formElement = array();
Carsten  Rose's avatar
Carsten Rose committed
62
        $json = array();
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

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

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

//        $formElement = $this->db->sql("SELECT * FROM FormElement AS fe WHERE fe.id=114", ROW_EXACT_1);
//        $this->assertEquals('', $formElement);

//        $formElement = [
//            'id' => 123,
//            'formId' => 2,
//            'feIdContainer' => 0,
//            'enabled' => 'yes',
//            'name' => 'name',
//            'label' => 'Name',
//            'mode' => 'show',
//            'class' => 'native',
//            'type' => 'input',
//            'value' => '',
//            'sql1' => '',
//            'parameter' => '',
//            'debug' => 'no',
//            'deleted' => 'no',
//
//            'size' => '',
//            'maxLength' => '',
//            'tooltip' => '',
//            'placeholder' => '',
//            'checkType' => '',
//            'checkPattern' => '',
//
//            'tabindex' => 0
//        ];

97

98
        // Defaults
Carsten  Rose's avatar
Carsten Rose committed
99
        $result = $build->buildInput($formElement, 'name:1', '', $json);
100
101
        $this->assertEquals('<input name="name:1" class="form-control" type="input" maxlength="255" value="" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => ''], $json);
102
103

        // CheckType
104
        $formElement['checkType'] = SANITIZE_ALLOW_MIN_MAX;
105
        $formElement['checkPattern'] = '1|10';
Carsten  Rose's avatar
Carsten Rose committed
106
        $result = $build->buildInput($formElement, 'name:1', '', $json);
107
108
        $this->assertEquals('<input name="name:1" class="form-control" type="input" maxlength="255" value="" min="1" max="10" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false], $json);
109

110
        $formElement['checkType'] = SANITIZE_ALLOW_PATTERN;
111
        $formElement['checkPattern'] = '^[a-z]*$';
Carsten  Rose's avatar
Carsten Rose committed
112
        $result = $build->buildInput($formElement, 'name:1', '', $json);
113
114
        $this->assertEquals('<input name="name:1" class="form-control" type="input" maxlength="255" value="" pattern="^[a-z]*$" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false], $json);
115

116
        $formElement['checkType'] = SANITIZE_ALLOW_DIGIT;
117
        $formElement['checkPattern'] = '';
Carsten  Rose's avatar
Carsten Rose committed
118
        $result = $build->buildInput($formElement, 'name:1', '', $json);
119
120
        $this->assertEquals('<input name="name:1" class="form-control" type="input" maxlength="255" value="" pattern="^[\d]*$" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false], $json);
121

122
        $formElement['checkType'] = SANITIZE_ALLOW_EMAIL;
123
        $formElement['checkPattern'] = '';
Carsten  Rose's avatar
Carsten Rose committed
124
        $result = $build->buildInput($formElement, 'name:1', '', $json);
125
126
        $this->assertEquals('<input name="name:1" class="form-control" type="input" maxlength="255" value="" pattern="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false], $json);
127
128
129
130
131
132
133
134

        $formElement['checkType'] = '';
        $formElement['checkPattern'] = '';


        // Explizit size
        $formElement['size'] = 40;
        $formElement['maxLength'] = 40;
Carsten  Rose's avatar
Carsten Rose committed
135
        $result = $build->buildInput($formElement, 'name:1', '', $json);
136
137
        $this->assertEquals('<input name="name:1" class="form-control" type="input" size="40" maxlength="40" value="" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false], $json);
138
139
140

        // maxlength bigger than physical spec:
        $formElement['maxLength'] = 1000;
Carsten  Rose's avatar
Carsten Rose committed
141
        $result = $build->buildInput($formElement, 'name:1', '', $json);
142
143
        $this->assertEquals('<input name="name:1" class="form-control" type="input" size="40" maxlength="255" value="" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false], $json);
144
145
146
147

        // Explicit: further
        $formElement['tooltip'] = 'Nice Tooltip';
        $formElement['placeholder'] = 'Please type a name';
Carsten  Rose's avatar
Carsten Rose committed
148
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
149
150
        $this->assertEquals('<input name="name:1" class="form-control" type="input" size="40" maxlength="255" value="Hello World" placeholder="Please type a name" title="Nice Tooltip" data-hidden="no" data-disabled="no" data-required="no" ><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false], $json);
151
152
153

        // textarea
        $formElement['size'] = '40,10';
Carsten  Rose's avatar
Carsten Rose committed
154
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
155
156
        $this->assertEquals('<textarea name="name:1" class="form-control" cols="40" rows="10" placeholder="Please type a name" title="Nice Tooltip" data-hidden="no" data-disabled="no" data-required="no" >Hello World</textarea><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false], $json);
157
158

        $formElement['size'] = ' 40     , 10 ';
Carsten  Rose's avatar
Carsten Rose committed
159
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
160
161
        $this->assertEquals('<textarea name="name:1" class="form-control" cols="40" rows="10" placeholder="Please type a name" title="Nice Tooltip" data-hidden="no" data-disabled="no" data-required="no" >Hello World</textarea><div class="help-block with-errors"></div>', $result);
        $this->assertEquals([FE_MODE_HIDDEN => '', 'disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false], $json);
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    }

    private function setFormFormElement(array &$form, array &$formElement) {
        $form = [
            'id' => '1',
            'name' => 'form',
            'title' => 'Form Editor: {{SELECT id, " / ", title FROM Form WHERE id => {{recordId:S0}}}}',
            'noteInternal' => 'Please secure the form',
            'tableName' => 'Form',
            'permitNew' => 'always',
            'permitEdit' => 'always',
            'permitUrlParameter' => '',
            'render' => 'bootstrap',
            'multiMode' => 'none',
            'multiSql' => '',
            'multiDetailForm' => '',
            'multiDetailFormParameter' => '',
            'forwardMode' => 'auto',
            'forwardPage' => '',
            'bsLabelColumns' => '',
            'bsInputColumns' => '',
            'bsNoteColumns' => '',
            'parameter' => 'maxVisiblePill=>3',
            'deleted' => 'no'
        ];

        $formElement = [
            'id' => 123,
            'formId' => 1,
            'feIdContainer' => 0,
Carsten  Rose's avatar
Carsten Rose committed
192
            'dynamicUpdate' => 'no',
193
194
195
            'enabled' => 'yes',
            'name' => 'name',
            'label' => 'Name',
196
            FE_MODE => 'show',
197
            'class' => 'native',
198
            FE_TYPE => 'input',
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
            'value' => '',
            'sql1' => '',
            'parameter' => '',
            'debug' => 'no',
            'deleted' => 'no',

            'size' => '',
            'maxLength' => '',
            'tooltip' => '',
            'placeholder' => '',
            'checkType' => '',
            'checkPattern' => '',

            'tabindex' => 0
        ];

    }

    /**
218
     * @expectedException \qfq\UserFormException
219
220
221
222
223
     *
     */
    public function testBuildInputException() {
        $form = array();
        $formElement = array();
Carsten  Rose's avatar
Carsten Rose committed
224
        $json = array();
225
226
227
228
229

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

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

230
        $formElement['checkType'] = SANITIZE_ALLOW_MIN_MAX;
231
        $formElement['checkPattern'] = '';
Carsten  Rose's avatar
Carsten Rose committed
232
        $result = $build->buildInput($formElement, 'name:1', '', $json);
233
234
235
236
237
238
239
240
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
    }

    public function testGetKeyValueListFromSqlEnumSpec() {
        $form = array();
        $formElement = array();

        $this->setFormFormElement($form, $formElement);
        $formElement['name'] = 'deleted';

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

        $keys = array();
        $values = array();

        // Spec Enum
        $expect = ['yes', 'no'];
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // Spec Enum + emptyItemAtStart
        $expect = ['', 'yes', 'no'];
        $formElement['emptyItemAtStart'] = '';
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // Spec Enum + emptyItemAtEnd
        $expect = ['', 'yes', 'no', ''];
        $formElement['emptyItemAtEnd'] = '';
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // clean
        unset($formElement['emptyItemAtStart']);
        unset($formElement['emptyItemAtEnd']);

        // listItem: only value
        $expect = ['a', 'b', 'c'];
        $formElement['itemList'] = 'a,b,c';
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // listItem: key/value
        $expectKeys = ['A', 'B', 'C'];
        $expectValues = ['a', 'b', 'c'];
        $formElement['itemList'] = 'A:a,B:b,C:c';
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);

        // listItem: key/value + emptyItemAtEnd
        $formElement['emptyItemAtEnd'] = '';
        $expectKeys = ['A', 'B', 'C', ''];
        $expectValues = ['a', 'b', 'c', ''];
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);
        unset($formElement['emptyItemAtEnd']);

        // SQL
        $formElement['sql1'] = $this->db->sql('SELECT name FROM Form AS f ORDER BY f.id LIMIT 3');
        $expect = ['form', 'formElement', 'phpunit_person'];
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);

        // SQL (one column, no keyword) + emptyItemAtStart
        $formElement['emptyItemAtStart'] = '';
        $formElement['sql1'] = $this->db->sql('SELECT name FROM Form AS f ORDER BY f.id LIMIT 3');
        $expect = ['', 'form', 'formElement', 'phpunit_person'];
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expect, $keys);
        $this->assertEquals($expect, $values);
        unset($formElement['emptyItemAtStart']);

        // SQL (4 columns, none 'id' nor 'label') - Take the first two columns
        $expectKeys = ['10', '20', '30'];
        $expectValues = ['basic', 'permission', 'various'];
        $formElement['sql1'] = $this->db->sql('SELECT ord, name, created, modified  FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);

        // SQL (4 columns, none 'id', one 'label' ) - Take the first and the fourth two columns
        $expectKeys = ['10', '20', '30'];
        $expectValues = ['basic', 'permission', 'various'];
        $formElement['sql1'] = $this->db->sql('SELECT ord, created, modified, name AS label FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);

        // SQL (4 columns, none 'id', one 'label' ) - Take the first and the fourth two columns
        $expectKeys = ['1', '2', '3'];
        $expectValues = ['basic', 'permission', 'various'];
        $formElement['sql1'] = $this->db->sql('SELECT ord, created, modified, name AS label, id FROM FormElement AS fe ORDER BY fe.id LIMIT 3');
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
        $this->assertEquals($expectKeys, $keys);
        $this->assertEquals($expectValues, $values);
    }

    /**
337
     * @expectedException \qfq\UserFormException
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
     *
     */
    public function testGetKeyValueListFromSqlEnumSpecException() {
        $form = array();
        $formElement = array();

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

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

        $formElement['name'] = 'noteInternal';
        $build->getKeyValueListFromSqlEnumSpec($formElement, $keys, $values);
    }

    /**
     * @throws Exception
     */
    protected function setUp() {
        $_GET['form'] = 'phpunit_person';

        parent::setUp();

        $this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);
//        $this->executeSQLFile(__DIR__ . '/fixtures/TestFormEditor.sql', true);

363
        $GLOBALS["TSFE"] = new FakeTSFEBuildPlain();
364

Carsten  Rose's avatar
Carsten Rose committed
365
366
367
//        $this->form = new \qfq\QuickFormQuery(['bodytext' => "form=form\nr=3", 'uid' => 1234], true);
        $form = new \qfq\QuickFormQuery(['bodytext' => "form=form\nr=3", 'uid' => 1234], true);

368
        // this is necessary to initialize SIP
Carsten  Rose's avatar
Carsten Rose committed
369
370
//        $content = $this->form->process();
        $form->process();
371
372
    }
}
373

374
class FakeTSFEBuildPlain {
375
    public $id = 1;
376
377
    public $type = 1;
    public $sys_language_uid = 1;
378
379
}

380