BuildFormPlainTest.php 16.8 KB
Newer Older
1
2
<?php

3
4
//use qfq\Store;

5
require_once(__DIR__ . '/../../qfq/BuildFormPlain.php');
6
require_once(__DIR__ . '/../../qfq/QuickFormQuery.php');
7
require_once(__DIR__ . '/AbstractDatabaseTest.php');
8
require_once(__DIR__ . '/../../qfq/store/Store.php');
9
10
11
12
13
14
15
16
17

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

18

19
20
21
22
23
24
25
    public function testGetProcessFilter() {

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

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

26
27
28
29
30
31
32
33
34
35
36
37
38
39
    /**
     *
     */
    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);

    }

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    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");
60
        $this->assertEquals('<label for="myLabel:123" class="control-label" >Hello World</label>', $result);
61
62
63
64
65
    }

    public function testBuildInput() {
        $form = array();
        $formElement = array();
Carsten  Rose's avatar
Carsten Rose committed
66
        $json = array();
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
97
98
99
100

        $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
//        ];

101

102

Carsten  Rose's avatar
Carsten Rose committed
103
        $result = $build->buildInput($formElement, 'name:1', '', $json);
104
105
        $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);
106
107

        // CheckType
108
        $formElement['checkType'] = SANITIZE_ALLOW_MIN_MAX;
109
        $formElement['checkPattern'] = '1|10';
Carsten  Rose's avatar
Carsten Rose committed
110
        $result = $build->buildInput($formElement, 'name:1', '', $json);
111
112
        $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);
113

114
        $formElement['checkType'] = SANITIZE_ALLOW_PATTERN;
115
        $formElement['checkPattern'] = '^[a-z]*$';
Carsten  Rose's avatar
Carsten Rose committed
116
        $result = $build->buildInput($formElement, 'name:1', '', $json);
117
118
        $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);
119

120
        $formElement['checkType'] = SANITIZE_ALLOW_DIGIT;
121
        $formElement['checkPattern'] = '';
Carsten  Rose's avatar
Carsten Rose committed
122
        $result = $build->buildInput($formElement, 'name:1', '', $json);
123
124
        $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);
125

126
        $formElement['checkType'] = SANITIZE_ALLOW_EMAIL;
127
        $formElement['checkPattern'] = '';
Carsten  Rose's avatar
Carsten Rose committed
128
        $result = $build->buildInput($formElement, 'name:1', '', $json);
129
130
        $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);
131
132
133
134
135
136
137
138

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


        // Explizit size
        $formElement['size'] = 40;
        $formElement['maxLength'] = 40;
Carsten  Rose's avatar
Carsten Rose committed
139
        $result = $build->buildInput($formElement, 'name:1', '', $json);
140
141
        $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);
142
143
144

        // maxlength bigger than physical spec:
        $formElement['maxLength'] = 1000;
Carsten  Rose's avatar
Carsten Rose committed
145
        $result = $build->buildInput($formElement, 'name:1', '', $json);
146
147
        $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);
148
149
150
151

        // Explicit: further
        $formElement['tooltip'] = 'Nice Tooltip';
        $formElement['placeholder'] = 'Please type a name';
Carsten  Rose's avatar
Carsten Rose committed
152
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
153
154
        $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);
155
156
157

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

        $formElement['size'] = ' 40     , 10 ';
Carsten  Rose's avatar
Carsten Rose committed
163
        $result = $build->buildInput($formElement, 'name:1', 'Hello World', $json);
164
165
        $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);
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
192
193
194
195
    }

    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
196
            'dynamicUpdate' => 'no',
197
198
199
            'enabled' => 'yes',
            'name' => 'name',
            'label' => 'Name',
200
            FE_MODE => 'show',
201
            'class' => 'native',
202
            FE_TYPE => 'input',
203
204
205
206
207
208
            'value' => '',
            'sql1' => '',
            'parameter' => '',
            'debug' => 'no',
            'deleted' => 'no',
            'size' => '',
209
            'maxLength' => '255',
210
211
212
213
214
215
216
217
218
219
220
            'tooltip' => '',
            'placeholder' => '',
            'checkType' => '',
            'checkPattern' => '',

            'tabindex' => 0
        ];

    }

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

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

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

233
        $formElement['checkType'] = SANITIZE_ALLOW_MIN_MAX;
234
        $formElement['checkPattern'] = '';
Carsten  Rose's avatar
Carsten Rose committed
235
        $result = $build->buildInput($formElement, 'name:1', '', $json);
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
337
338
339
    }

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

    /**
340
     * @expectedException \qfq\UserFormException
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();

363

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

367
368
369
370
371

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

372
        $GLOBALS["TSFE"] = new FakeTSFEBuildPlain();
373

Carsten  Rose's avatar
Carsten Rose committed
374
375
376
//        $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);

377
        // this is necessary to initialize SIP
Carsten  Rose's avatar
Carsten Rose committed
378
379
//        $content = $this->form->process();
        $form->process();
380
381
    }
}
382

383
class FakeTSFEBuildPlain {
384
    public $id = 1;
385
386
    public $type = 1;
    public $sys_language_uid = 1;
387
388
}

389