formslib_test.php 39.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Unit tests for /lib/formslib.php.
 *
 * @package   core_form
 * @category  phpunit
 * @copyright 2011 Sam Hemelryk
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/form/radio.php');
require_once($CFG->libdir . '/form/select.php');
require_once($CFG->libdir . '/form/text.php');


35
class core_formslib_testcase extends advanced_testcase {
36
37
38
39
40
41
42
43
44
45
46

    public function test_require_rule() {
        global $CFG;

        $strictformsrequired = null;
        if (isset($CFG->strictformsrequired)) {
            $strictformsrequired = $CFG->strictformsrequired;
        }

        $rule = new MoodleQuickForm_Rule_Required();

47
        // First run the tests with strictformsrequired off.
48
        $CFG->strictformsrequired = false;
49
        // Passes.
50
51
52
53
        $this->assertTrue($rule->validate('Something'));
        $this->assertTrue($rule->validate("Something\nmore"));
        $this->assertTrue($rule->validate("\nmore"));
        $this->assertTrue($rule->validate(" more "));
54
55
        $this->assertTrue($rule->validate('ш'));
        $this->assertTrue($rule->validate("の"));
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        $this->assertTrue($rule->validate("0"));
        $this->assertTrue($rule->validate(0));
        $this->assertTrue($rule->validate(true));
        $this->assertTrue($rule->validate(' '));
        $this->assertTrue($rule->validate('      '));
        $this->assertTrue($rule->validate("\t"));
        $this->assertTrue($rule->validate("\n"));
        $this->assertTrue($rule->validate("\r"));
        $this->assertTrue($rule->validate("\r\n"));
        $this->assertTrue($rule->validate(" \t  \n  \r "));
        $this->assertTrue($rule->validate('<p></p>'));
        $this->assertTrue($rule->validate('<p> </p>'));
        $this->assertTrue($rule->validate('<p>x</p>'));
        $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
        $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
        $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
        $this->assertTrue($rule->validate('<hr />'));
        $this->assertTrue($rule->validate('<hr/>'));
        $this->assertTrue($rule->validate('<hr>'));
        $this->assertTrue($rule->validate('<hr></hr>'));
        $this->assertTrue($rule->validate('<br />'));
        $this->assertTrue($rule->validate('<br/>'));
        $this->assertTrue($rule->validate('<br>'));
        $this->assertTrue($rule->validate('&nbsp;'));
80
        // Fails.
81
82
83
84
        $this->assertFalse($rule->validate(''));
        $this->assertFalse($rule->validate(false));
        $this->assertFalse($rule->validate(null));

85
        // Now run the same tests with it on to make sure things work as expected.
86
        $CFG->strictformsrequired = true;
87
        // Passes.
88
89
90
91
92
        $this->assertTrue($rule->validate('Something'));
        $this->assertTrue($rule->validate("Something\nmore"));
        $this->assertTrue($rule->validate("\nmore"));
        $this->assertTrue($rule->validate(" more "));
        $this->assertTrue($rule->validate("0"));
93
94
        $this->assertTrue($rule->validate('ш'));
        $this->assertTrue($rule->validate("の"));
95
96
97
98
99
100
101
102
103
104
        $this->assertTrue($rule->validate(0));
        $this->assertTrue($rule->validate(true));
        $this->assertTrue($rule->validate('<p>x</p>'));
        $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
        $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
        $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
        $this->assertTrue($rule->validate('<hr />'));
        $this->assertTrue($rule->validate('<hr/>'));
        $this->assertTrue($rule->validate('<hr>'));
        $this->assertTrue($rule->validate('<hr></hr>'));
105
        // Fails.
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
        $this->assertFalse($rule->validate(' '));
        $this->assertFalse($rule->validate('      '));
        $this->assertFalse($rule->validate("\t"));
        $this->assertFalse($rule->validate("\n"));
        $this->assertFalse($rule->validate("\r"));
        $this->assertFalse($rule->validate("\r\n"));
        $this->assertFalse($rule->validate(" \t  \n  \r "));
        $this->assertFalse($rule->validate('<p></p>'));
        $this->assertFalse($rule->validate('<p> </p>'));
        $this->assertFalse($rule->validate('<br />'));
        $this->assertFalse($rule->validate('<br/>'));
        $this->assertFalse($rule->validate('<br>'));
        $this->assertFalse($rule->validate('&nbsp;'));
        $this->assertFalse($rule->validate(''));
        $this->assertFalse($rule->validate(false));
        $this->assertFalse($rule->validate(null));

        if (isset($strictformsrequired)) {
            $CFG->strictformsrequired = $strictformsrequired;
        }
    }

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    public function test_range_rule() {
        global $CFG;

        require_once('HTML/QuickForm/Rule/Range.php'); // Requires this pear stuff.

        $strictformsrequired = null;
        if (isset($CFG->strictformsrequired)) {
            $strictformsrequired = $CFG->strictformsrequired;
        }

        $rule = new HTML_QuickForm_Rule_Range();

        // First run the tests with strictformsrequired off.
        $CFG->strictformsrequired = false;
        // Passes.
        $rule->setName('minlength'); // Let's verify some min lengths.
        $this->assertTrue($rule->validate('12', 2));
        $this->assertTrue($rule->validate('123', 2));
        $this->assertTrue($rule->validate('áé', 2));
        $this->assertTrue($rule->validate('áéí', 2));
        $rule->setName('maxlength'); // Let's verify some max lengths.
        $this->assertTrue($rule->validate('1', 2));
        $this->assertTrue($rule->validate('12', 2));
        $this->assertTrue($rule->validate('á', 2));
        $this->assertTrue($rule->validate('áé', 2));
        $rule->setName('----'); // Let's verify some ranges.
        $this->assertTrue($rule->validate('', array(0, 2)));
        $this->assertTrue($rule->validate('1', array(0, 2)));
        $this->assertTrue($rule->validate('12', array(0, 2)));
        $this->assertTrue($rule->validate('á', array(0, 2)));
        $this->assertTrue($rule->validate('áé', array(0, 2)));

        // Fail.
        $rule->setName('minlength'); // Let's verify some min lengths.
        $this->assertFalse($rule->validate('', 2));
        $this->assertFalse($rule->validate('1', 2));
        $this->assertFalse($rule->validate('á', 2));
        $rule->setName('maxlength'); // Let's verify some max lengths.
        $this->assertFalse($rule->validate('123', 2));
        $this->assertFalse($rule->validate('áéí', 2));
        $rule->setName('----'); // Let's verify some ranges.
        $this->assertFalse($rule->validate('', array(1, 2)));
        $this->assertFalse($rule->validate('123', array(1, 2)));
        $this->assertFalse($rule->validate('áéí', array(1, 2)));

        // Now run the same tests with it on to make sure things work as expected.
        $CFG->strictformsrequired = true;
        // Passes.
        $rule->setName('minlength'); // Let's verify some min lengths.
        $this->assertTrue($rule->validate('12', 2));
        $this->assertTrue($rule->validate('123', 2));
        $this->assertTrue($rule->validate('áé', 2));
        $this->assertTrue($rule->validate('áéí', 2));
        $rule->setName('maxlength'); // Let's verify some min lengths.
        $this->assertTrue($rule->validate('1', 2));
        $this->assertTrue($rule->validate('12', 2));
        $this->assertTrue($rule->validate('á', 2));
        $this->assertTrue($rule->validate('áé', 2));
        $rule->setName('----'); // Let's verify some ranges.
        $this->assertTrue($rule->validate('', array(0, 2)));
        $this->assertTrue($rule->validate('1', array(0, 2)));
        $this->assertTrue($rule->validate('12', array(0, 2)));
        $this->assertTrue($rule->validate('á', array(0, 2)));
        $this->assertTrue($rule->validate('áé', array(0, 2)));

        // Fail.
        $rule->setName('minlength'); // Let's verify some min lengths.
        $this->assertFalse($rule->validate('', 2));
        $this->assertFalse($rule->validate('1', 2));
        $this->assertFalse($rule->validate('á', 2));
        $rule->setName('maxlength'); // Let's verify some min lengths.
        $this->assertFalse($rule->validate('123', 2));
        $this->assertFalse($rule->validate('áéí', 2));
        $rule->setName('----'); // Let's verify some ranges.
        $this->assertFalse($rule->validate('', array(1, 2)));
        $this->assertFalse($rule->validate('123', array(1, 2)));
        $this->assertFalse($rule->validate('áéí', array(1, 2)));

        if (isset($strictformsrequired)) {
            $CFG->strictformsrequired = $strictformsrequired;
        }
    }

211
212
213
214
    public function test_generate_id_select() {
        $el = new MoodleQuickForm_select('choose_one', 'Choose one',
            array(1 => 'One', '2' => 'Two'));
        $el->_generateId();
215
        $this->assertSame('id_choose_one', $el->getAttribute('id'));
216
217
218
219
220
    }

    public function test_generate_id_like_repeat() {
        $el = new MoodleQuickForm_text('text[7]', 'Type something');
        $el->_generateId();
221
        $this->assertSame('id_text_7', $el->getAttribute('id'));
222
223
224
225
226
227
    }

    public function test_can_manually_set_id() {
        $el = new MoodleQuickForm_text('elementname', 'Type something',
            array('id' => 'customelementid'));
        $el->_generateId();
228
        $this->assertSame('customelementid', $el->getAttribute('id'));
229
230
231
232
233
    }

    public function test_generate_id_radio() {
        $el = new MoodleQuickForm_radio('radio', 'Label', 'Choice label', 'choice_value');
        $el->_generateId();
234
        $this->assertSame('id_radio_choice_value', $el->getAttribute('id'));
235
236
237
238
239
240
    }

    public function test_radio_can_manually_set_id() {
        $el = new MoodleQuickForm_radio('radio2', 'Label', 'Choice label', 'choice_value',
            array('id' => 'customelementid2'));
        $el->_generateId();
241
        $this->assertSame('customelementid2', $el->getAttribute('id'));
242
243
244
245
246
    }

    public function test_generate_id_radio_like_repeat() {
        $el = new MoodleQuickForm_radio('repeatradio[2]', 'Label', 'Choice label', 'val');
        $el->_generateId();
247
        $this->assertSame('id_repeatradio_2_val', $el->getAttribute('id'));
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
    }

    public function test_rendering() {
        $form = new formslib_test_form();
        ob_start();
        $form->display();
        $html = ob_get_clean();

        $this->assertTag(array('tag'=>'select', 'id'=>'id_choose_one',
            'attributes'=>array('name'=>'choose_one')), $html);

        $this->assertTag(array('tag'=>'input', 'id'=>'id_text_0',
            'attributes'=>array('type'=>'text', 'name'=>'text[0]')), $html);

        $this->assertTag(array('tag'=>'input', 'id'=>'id_text_1',
            'attributes'=>array('type'=>'text', 'name'=>'text[1]')), $html);

        $this->assertTag(array('tag'=>'input', 'id'=>'id_radio_choice_value',
            'attributes'=>array('type'=>'radio', 'name'=>'radio', 'value'=>'choice_value')), $html);

        $this->assertTag(array('tag'=>'input', 'id'=>'customelementid2',
            'attributes'=>array('type'=>'radio', 'name'=>'radio2')), $html);

        $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_0_2',
            'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[0]', 'value'=>'2')), $html);

        $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_1',
            'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'1')), $html);

        $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_2',
            'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'2')), $html);
    }
280
281
282
283
284

    public function test_settype_debugging_text() {
        $mform = new formslib_settype_debugging_text();
        $this->assertDebuggingCalled("Did you remember to call setType() for 'texttest'? Defaulting to PARAM_RAW cleaning.");

285
        // Check form still there though.
286
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="texttest/');
287
288
289
290
291
292
293
        $mform->display();
    }

    public function test_settype_debugging_hidden() {
        $mform = new formslib_settype_debugging_hidden();
        $this->assertDebuggingCalled("Did you remember to call setType() for 'hiddentest'? Defaulting to PARAM_RAW cleaning.");

294
        // Check form still there though.
295
296
297
298
        $this->expectOutputRegex('/<input[^>]*name="hiddentest[^>]*type="hidden/');
        $mform->display();
    }

299
300
301
302
303
304
    public function test_settype_debugging_url() {
        $this->resetAfterTest(true);
        $this->setAdminUser();
        $mform = new formslib_settype_debugging_url();
        $this->assertDebuggingCalled("Did you remember to call setType() for 'urltest'? Defaulting to PARAM_RAW cleaning.");

305
        // Check form still there though.
306
        $this->expectOutputRegex('/<input[^>]*type="url[^>]*name="urltest"/');
307
308
309
        $mform->display();
    }

310
311
    public function test_settype_debugging_repeat() {
        $mform = new formslib_settype_debugging_repeat();
312
        $this->assertDebuggingCalled("Did you remember to call setType() for 'repeattest[0]'? Defaulting to PARAM_RAW cleaning.");
313

314
        // Check form still there though.
315
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="repeattest/');
316
317
318
319
320
321
322
        $mform->display();
    }

    public function test_settype_debugging_repeat_ok() {
        $mform = new formslib_settype_debugging_repeat_ok();
        // No debugging expected here.

323
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="repeattest/');
324
325
        $mform->display();
    }
326
327
328
329

    public function test_settype_debugging_group() {
        $mform = new formslib_settype_debugging_group();
        $this->assertDebuggingCalled("Did you remember to call setType() for 'groupel1'? Defaulting to PARAM_RAW cleaning.");
330
331
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="groupel1"/');
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="groupel2"/');
332
333
334
335
336
337
        $mform->display();
    }

    public function test_settype_debugging_namedgroup() {
        $mform = new formslib_settype_debugging_namedgroup();
        $this->assertDebuggingCalled("Did you remember to call setType() for 'namedgroup[groupel1]'? Defaulting to PARAM_RAW cleaning.");
338
339
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="namedgroup\[groupel1\]"/');
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="namedgroup\[groupel2\]"/');
340
341
342
343
344
345
        $mform->display();
    }

    public function test_settype_debugging_funky_name() {
        $mform = new formslib_settype_debugging_funky_name();
        $this->assertDebuggingCalled("Did you remember to call setType() for 'blah[foo][bar][1]'? Defaulting to PARAM_RAW cleaning.");
346
347
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[foo\]\[bar\]\[0\]"/');
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[foo\]\[bar\]\[1\]"/');
348
349
350
351
352
        $mform->display();
    }

    public function test_settype_debugging_type_inheritance() {
        $mform = new formslib_settype_debugging_type_inheritance();
353
354
355
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[foo\]\[bar\]\[0\]"/');
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[bar\]\[foo\]\[1\]"/');
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[any\]\[other\]\[2\]"/');
356
357
        $mform->display();
    }
358

359
360
361
    public function test_settype_debugging_type_group_in_repeat() {
        $mform = new formslib_settype_debugging_type_group_in_repeat();
        $this->assertDebuggingCalled("Did you remember to call setType() for 'test2[0]'? Defaulting to PARAM_RAW cleaning.");
362
363
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="test1\[0\]"/');
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="test2\[0\]"/');
364
365
366
367
368
369
        $mform->display();
    }

    public function test_settype_debugging_type_namedgroup_in_repeat() {
        $mform = new formslib_settype_debugging_type_namedgroup_in_repeat();
        $this->assertDebuggingCalled("Did you remember to call setType() for 'namedgroup[0][test2]'? Defaulting to PARAM_RAW cleaning.");
370
371
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="namedgroup\[0\]\[test1\]"/');
        $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="namedgroup\[0\]\[test2\]"/');
372
373
374
        $mform->display();
    }

375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
    public function test_type_cleaning() {
        $expectedtypes = array(
            'simpleel' => PARAM_INT,
            'groupel1' => PARAM_INT,
            'groupel2' => PARAM_FLOAT,
            'groupel3' => PARAM_INT,
            'namedgroup' => array(
                'sndgroupel1' => PARAM_INT,
                'sndgroupel2' => PARAM_FLOAT,
                'sndgroupel3' => PARAM_INT
            ),
            'namedgroupinherit' => array(
                'thdgroupel1' => PARAM_INT,
                'thdgroupel2' => PARAM_INT
            ),
            'repeatedel' => array(
                0 => PARAM_INT,
                1 => PARAM_INT
            ),
            'repeatedelinherit' => array(
                0 => PARAM_INT,
                1 => PARAM_INT
            ),
            'squaretest' => array(
                0 => PARAM_INT
            ),
            'nested' => array(
                0 => array(
                    'bob' => array(
                        123 => PARAM_INT,
                        'foo' => PARAM_FLOAT
                    ),
                    'xyz' => PARAM_RAW
                ),
                1 => PARAM_INT
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
            ),
            'repeatgroupel1' => array(
                0 => PARAM_INT,
                1 => PARAM_INT
            ),
            'repeatgroupel2' => array(
                0 => PARAM_INT,
                1 => PARAM_INT
            ),
            'repeatnamedgroup' => array(
                0 => array(
                    'repeatnamedgroupel1' => PARAM_INT,
                    'repeatnamedgroupel2' => PARAM_INT
                ),
                1 => array(
                    'repeatnamedgroupel1' => PARAM_INT,
                    'repeatnamedgroupel2' => PARAM_INT
                )
428
429
430
431
432
433
434
435
436
437
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
463
            )
        );
        $valuessubmitted = array(
            'simpleel' => '11.01',
            'groupel1' => '11.01',
            'groupel2' => '11.01',
            'groupel3' => '11.01',
            'namedgroup' => array(
                'sndgroupel1' => '11.01',
                'sndgroupel2' => '11.01',
                'sndgroupel3' => '11.01'
            ),
            'namedgroupinherit' => array(
                'thdgroupel1' => '11.01',
                'thdgroupel2' => '11.01'
            ),
            'repeatedel' => array(
                0 => '11.01',
                1 => '11.01'
            ),
            'repeatedelinherit' => array(
                0 => '11.01',
                1 => '11.01'
            ),
            'squaretest' => array(
                0 => '11.01'
            ),
            'nested' => array(
                0 => array(
                    'bob' => array(
                        123 => '11.01',
                        'foo' => '11.01'
                    ),
                    'xyz' => '11.01'
                ),
                1 => '11.01'
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
            ),
            'repeatgroupel1' => array(
                0 => '11.01',
                1 => '11.01'
            ),
            'repeatgroupel2' => array(
                0 => '11.01',
                1 => '11.01'
            ),
            'repeatnamedgroup' => array(
                0 => array(
                    'repeatnamedgroupel1' => '11.01',
                    'repeatnamedgroupel2' => '11.01'
                ),
                1 => array(
                    'repeatnamedgroupel1' => '11.01',
                    'repeatnamedgroupel2' => '11.01'
                )
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
            )
        );
        $expectedvalues = array(
            'simpleel' => 11,
            'groupel1' => 11,
            'groupel2' => 11.01,
            'groupel3' => 11,
            'namedgroup' => array(
                'sndgroupel1' => 11,
                'sndgroupel2' => 11.01,
                'sndgroupel3' => 11
            ),
            'namedgroupinherit' => array(
                'thdgroupel1' => 11,
                'thdgroupel2' => 11
            ),
498
            'repeatable' => 2,
499
500
501
502
            'repeatedel' => array(
                0 => 11,
                1 => 11
            ),
503
            'repeatableinherit' => 2,
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
            'repeatedelinherit' => array(
                0 => 11,
                1 => 11
            ),
            'squaretest' => array(
                0 => 11
            ),
            'nested' => array(
                0 => array(
                    'bob' => array(
                        123 => 11,
                        'foo' => 11.01
                    ),
                    'xyz' => '11.01'
                ),
                1 => 11
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
            ),
            'repeatablegroup' => 2,
            'repeatgroupel1' => array(
                0 => 11,
                1 => 11
            ),
            'repeatgroupel2' => array(
                0 => 11,
                1 => 11
            ),
            'repeatablenamedgroup' => 2,
            'repeatnamedgroup' => array(
                0 => array(
                    'repeatnamedgroupel1' => 11,
                    'repeatnamedgroupel2' => 11
                ),
                1 => array(
                    'repeatnamedgroupel1' => 11,
                    'repeatnamedgroupel2' => 11
                )
540
541
542
543
544
            )
        );

        $mform = new formslib_clean_value();
        $mform->get_form()->updateSubmission($valuessubmitted, null);
545
546
547
        foreach ($expectedtypes as $elementname => $expected) {
            $actual = $mform->get_form()->getCleanType($elementname, $valuessubmitted[$elementname]);
            $this->assertSame($expected, $actual, "Failed validating clean type of '$elementname'");
548
549
550
        }

        $data = $mform->get_data();
551
        $this->assertSame($expectedvalues, (array) $data);
552
    }
553
554
555
556
557

    /**
     * MDL-52873
     */
    public function test_multiple_modgrade_fields() {
558
        global $CFG;
559
560
        $this->resetAfterTest(true);

561
        $CFG->theme = 'clean';
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
        $form = new formslib_multiple_modgrade_form();
        ob_start();
        $form->display();
        $html = ob_get_clean();

        $this->assertTag(array('id' => 'fgroup_id_grade1'), $html);
        $this->assertTag(array('id' => 'id_grade1_modgrade_type'), $html);
        $this->assertTag(array('id' => 'id_grade1_modgrade_point'), $html);
        $this->assertTag(array('id' => 'id_grade1_modgrade_scale'), $html);

        $this->assertTag(array('id' => 'fgroup_id_grade2'), $html);
        $this->assertTag(array('id' => 'id_grade2_modgrade_type'), $html);
        $this->assertTag(array('id' => 'id_grade2_modgrade_point'), $html);
        $this->assertTag(array('id' => 'id_grade2_modgrade_scale'), $html);

        $this->assertTag(array('id' => 'fgroup_id_grade_3'), $html);
        $this->assertTag(array('id' => 'id_grade_3_modgrade_type'), $html);
        $this->assertTag(array('id' => 'id_grade_3_modgrade_point'), $html);
        $this->assertTag(array('id' => 'id_grade_3_modgrade_scale'), $html);
    }
582
583
584
585
586

    /**
     * Test persistant freeze elements have different id's.
     */
    public function test_persistantrreeze_element() {
587
        global $CFG;
588
        $this->resetAfterTest(true);
589
        $CFG->theme = 'clean';
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612

        $form = new formslib_persistantrreeze_element();
        ob_start();
        $form->display();
        $html = ob_get_clean();

        // Test advcheckbox id's.
        $this->assertTag(array('id' => 'id_advcheckboxpersistant'), $html);
        $this->assertTag(array('id' => 'id_advcheckboxpersistant_persistant'), $html);
        $this->assertTag(array('id' => 'id_advcheckboxnotpersistant'), $html);
        $this->assertNotTag(array('id' => 'id_advcheckboxnotpersistant_persistant'), $html);
        $this->assertTag(array('id' => 'id_advcheckboxfrozen'), $html);
        $this->assertTag(array('id' => 'id_advcheckboxfrozen_persistant'), $html);

        // Check text element id's.
        $this->assertTag(array('id' => 'id_textpersistant'), $html);
        $this->assertTag(array('id' => 'id_textpersistant_persistant'), $html);
        $this->assertTag(array('id' => 'id_textnotpersistant'), $html);
        $this->assertNotTag(array('id' => 'id_textnotpersistant_persistant'), $html);
        $this->assertTag(array('id' => 'id_textfrozen'), $html);
        $this->assertNotTag(array('id' => 'id_textfrozen_persistant'), $html);

    }
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631

    /**
     * Ensure a validation can run at least once per object. See MDL-56259.
     */
    public function test_multiple_validation() {
        $this->resetAfterTest(true);

        // It should be valid.
        formslib_multiple_validation_form::mock_submit(['somenumber' => '10']);
        $form = new formslib_multiple_validation_form();
        $this->assertTrue($form->is_validated());
        $this->assertEquals(10, $form->get_data()->somenumber);

        // It should not validate.
        formslib_multiple_validation_form::mock_submit(['somenumber' => '-5']);
        $form = new formslib_multiple_validation_form();
        $this->assertFalse($form->is_validated());
        $this->assertNull($form->get_data());
    }
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648

    /**
     * MDL-56233 - Tests mocking a form inside a namespace.
     */
    public function test_mock_submit() {
        require_once(__DIR__.'/fixtures/namespaced_form.php');
        \local_unittests\namespaced_form\exampleform::mock_submit(['title' => 'Mocked Value']);
        $form = new \local_unittests\namespaced_form\exampleform();

        // Here is the problem, this is the expected hidden field name.
        $expected = '_qf__local_unittests_namespaced_form_exampleform';
        self::assertArrayHasKey($expected, $_POST);

        // This should work now, before it would fail.
        self::assertTrue($form->is_submitted());
        self::assertSame('Mocked Value', $form->get_data()->title);
    }
649
650
651
652
653
654
655
656
657
658
659
660
661
662
}


/**
 * Test form to be used by {@link formslib_test::test_rendering()}.
 */
class formslib_test_form extends moodleform {
    public function definition() {
        $this->_form->addElement('select', 'choose_one', 'Choose one',
            array(1 => 'One', '2' => 'Two'));

        $repeatels = array(
            $this->_form->createElement('text', 'text', 'Type something')
        );
663
664
665
666
667
668
        // TODO: The repeat_elements() is far from perfect. Everything should be
        // repeated auto-magically by default with options only defining exceptions.
        // Surely this is caused because we are storing some element information OUT
        // from the element (type...) at form level. Anyway, the method should do its
        // work better, no matter of that.
        $this->repeat_elements($repeatels, 2, array('text' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
669
670
671
672
673
674
675
676
677
678
679
680

        $this->_form->addElement('radio', 'radio', 'Label', 'Choice label', 'choice_value');

        $this->_form->addElement('radio', 'radio2', 'Label', 'Choice label', 'choice_value',
            array('id' => 'customelementid2'));

        $repeatels = array(
            $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'One', 1),
            $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'Two', 2),
        );
        $this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios');
    }
681
}
682

683
684
685
/**
 * Used to test debugging is called when text added without setType.
 */
686
687
688
689
690
691
692
693
class formslib_settype_debugging_text extends moodleform {
    public function definition() {
        $mform = $this->_form;

        $mform->addElement('text', 'texttest', 'test123', 'testing123');
    }
}

694
695
696
/**
 * Used to test debugging is called when hidden added without setType.
 */
697
698
699
700
701
702
703
704
class formslib_settype_debugging_hidden extends moodleform {
    public function definition() {
        $mform = $this->_form;

        $mform->addElement('hidden', 'hiddentest', '1');
    }
}

705
706
707
/**
 * Used to test debugging is called when hidden added without setType.
 */
708
709
710
711
712
713
714
715
class formslib_settype_debugging_url extends moodleform {
    public function definition() {
        $mform = $this->_form;

        $mform->addElement('url', 'urltest', 'urltest');
    }
}

716
717
718
/**
 * Used to test debugging is called when repeated text added without setType.
 */
719
720
721
722
723
724
725
726
727
728
729
730
class formslib_settype_debugging_repeat extends moodleform {
    public function definition() {
        $mform = $this->_form;

        $repeatels = array(
            $mform->createElement('text', 'repeattest', 'Type something')
        );

        $this->repeat_elements($repeatels, 1, array(), 'numtexts', 'addtexts');
    }
}

731
732
733
/**
 * Used to no debugging is called when correctly test.
 */
734
735
736
737
738
739
740
741
class formslib_settype_debugging_repeat_ok extends moodleform {
    public function definition() {
        $mform = $this->_form;

        $repeatels = array(
            $mform->createElement('text', 'repeattest', 'Type something')
        );

742
        $this->repeat_elements($repeatels, 2, array('repeattest' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
743
744
    }
}
745

746
747
748
/**
 * Used to test if debugging is called when a group contains elements without type.
 */
749
750
751
752
753
754
755
756
757
758
759
760
class formslib_settype_debugging_group extends moodleform {
    public function definition() {
        $mform = $this->_form;
        $group = array(
            $mform->createElement('text', 'groupel1', 'groupel1'),
            $mform->createElement('text', 'groupel2', 'groupel2')
        );
        $mform->addGroup($group);
        $mform->setType('groupel2', PARAM_INT);
    }
}

761
762
763
/**
 * Used to test if debugging is called when a named group contains elements without type.
 */
764
765
766
767
768
769
770
771
772
773
774
775
class formslib_settype_debugging_namedgroup extends moodleform {
    public function definition() {
        $mform = $this->_form;
        $group = array(
            $mform->createElement('text', 'groupel1', 'groupel1'),
            $mform->createElement('text', 'groupel2', 'groupel2')
        );
        $mform->addGroup($group, 'namedgroup');
        $mform->setType('namedgroup[groupel2]', PARAM_INT);
    }
}

776
777
778
/**
 * Used to test if debugging is called when has a funky name.
 */
779
780
781
782
783
784
785
786
787
class formslib_settype_debugging_funky_name extends moodleform {
    public function definition() {
        $mform = $this->_form;
        $mform->addElement('text', 'blah[foo][bar][0]', 'test', 'test');
        $mform->addElement('text', 'blah[foo][bar][1]', 'test', 'test');
        $mform->setType('blah[foo][bar][0]', PARAM_INT);
    }
}

788
789
790
/**
 * Used to test that debugging is not called with type inheritance.
 */
791
792
793
794
795
796
797
798
799
800
801
class formslib_settype_debugging_type_inheritance extends moodleform {
    public function definition() {
        $mform = $this->_form;
        $mform->addElement('text', 'blah[foo][bar][0]', 'test1', 'test');
        $mform->addElement('text', 'blah[bar][foo][1]', 'test2', 'test');
        $mform->addElement('text', 'blah[any][other][2]', 'test3', 'test');
        $mform->setType('blah[foo][bar]', PARAM_INT);
        $mform->setType('blah[bar]', PARAM_FLOAT);
        $mform->setType('blah', PARAM_TEXT);
    }
}
802

803
804
805
/**
 * Used to test the debugging when using groups in repeated elements.
 */
806
807
808
809
810
811
812
813
814
815
816
817
class formslib_settype_debugging_type_group_in_repeat extends moodleform {
    public function definition() {
        $mform = $this->_form;
        $groupelements = array(
            $mform->createElement('text', 'test1', 'test1', 'test'),
            $mform->createElement('text', 'test2', 'test2', 'test')
        );
        $group = $mform->createElement('group', null, 'group1', $groupelements, null, false);
        $this->repeat_elements(array($group), 1, array('test1' => array('type' => PARAM_INT)), 'hidden', 'button');
    }
}

818
819
820
/**
 * Used to test the debugging when using named groups in repeated elements.
 */
821
822
823
824
825
826
827
828
829
830
831
832
class formslib_settype_debugging_type_namedgroup_in_repeat extends moodleform {
    public function definition() {
        $mform = $this->_form;
        $groupelements = array(
            $mform->createElement('text', 'test1', 'test1', 'test'),
            $mform->createElement('text', 'test2', 'test2', 'test')
        );
        $group = $mform->createElement('group', 'namedgroup', 'group1', $groupelements, null, true);
        $this->repeat_elements(array($group), 1, array('namedgroup[test1]' => array('type' => PARAM_INT)), 'hidden', 'button');
    }
}

833
834
835
/**
 * Used to check value cleaning.
 */
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
class formslib_clean_value extends moodleform {
    public function get_form() {
        return $this->_form;
    }
    public function definition() {
        $mform = $this->_form;

        // Add a simple int.
        $mform->addElement('text', 'simpleel', 'simpleel');
        $mform->setType('simpleel', PARAM_INT);

        // Add a non-named group.
        $group = array(
            $mform->createElement('text', 'groupel1', 'groupel1'),
            $mform->createElement('text', 'groupel2', 'groupel2'),
            $mform->createElement('text', 'groupel3', 'groupel3')
        );
        $mform->setType('groupel1', PARAM_INT);
        $mform->setType('groupel2', PARAM_FLOAT);
        $mform->setType('groupel3', PARAM_INT);
        $mform->addGroup($group);

        // Add a named group.
        $group = array(
            $mform->createElement('text', 'sndgroupel1', 'sndgroupel1'),
            $mform->createElement('text', 'sndgroupel2', 'sndgroupel2'),
            $mform->createElement('text', 'sndgroupel3', 'sndgroupel3')
        );
        $mform->addGroup($group, 'namedgroup');
        $mform->setType('namedgroup[sndgroupel1]', PARAM_INT);
        $mform->setType('namedgroup[sndgroupel2]', PARAM_FLOAT);
        $mform->setType('namedgroup[sndgroupel3]', PARAM_INT);

        // Add a named group, with inheritance.
        $group = array(
            $mform->createElement('text', 'thdgroupel1', 'thdgroupel1'),
            $mform->createElement('text', 'thdgroupel2', 'thdgroupel2')
        );
        $mform->addGroup($group, 'namedgroupinherit');
        $mform->setType('namedgroupinherit', PARAM_INT);

        // Add a repetition.
        $repeat = $mform->createElement('text', 'repeatedel', 'repeatedel');
        $this->repeat_elements(array($repeat), 2, array('repeatedel' => array('type' => PARAM_INT)), 'repeatable', 'add', 0);

        // Add a repetition, with inheritance.
        $repeat = $mform->createElement('text', 'repeatedelinherit', 'repeatedelinherit');
        $this->repeat_elements(array($repeat), 2, array(), 'repeatableinherit', 'add', 0);
        $mform->setType('repeatedelinherit', PARAM_INT);

        // Add an arbitrary named element.
        $mform->addElement('text', 'squaretest[0]', 'squaretest[0]');
        $mform->setType('squaretest[0]', PARAM_INT);

        // Add an arbitrary nested array named element.
        $mform->addElement('text', 'nested[0][bob][123]', 'nested[0][bob][123]');
        $mform->setType('nested[0][bob][123]', PARAM_INT);

        // Add inheritance test cases.
        $mform->setType('nested', PARAM_INT);
        $mform->setType('nested[0]', PARAM_RAW);
        $mform->setType('nested[0][bob]', PARAM_FLOAT);
        $mform->addElement('text', 'nested[1]', 'nested[1]');
        $mform->addElement('text', 'nested[0][xyz]', 'nested[0][xyz]');
        $mform->addElement('text', 'nested[0][bob][foo]', 'nested[0][bob][foo]');
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918

        // Add group in repeated element (with extra inheritance).
        $groupelements = array(
            $mform->createElement('text', 'repeatgroupel1', 'repeatgroupel1'),
            $mform->createElement('text', 'repeatgroupel2', 'repeatgroupel2')
        );
        $group = $mform->createElement('group', 'repeatgroup', 'repeatgroup', $groupelements, null, false);
        $this->repeat_elements(array($group), 2, array('repeatgroupel1' => array('type' => PARAM_INT),
            'repeatgroupel2' => array('type' => PARAM_INT)), 'repeatablegroup', 'add', 0);

        // Add named group in repeated element.
        $groupelements = array(
            $mform->createElement('text', 'repeatnamedgroupel1', 'repeatnamedgroupel1'),
            $mform->createElement('text', 'repeatnamedgroupel2', 'repeatnamedgroupel2')
        );
        $group = $mform->createElement('group', 'repeatnamedgroup', 'repeatnamedgroup', $groupelements, null, true);
        $this->repeat_elements(array($group), 2, array('repeatnamedgroup[repeatnamedgroupel1]' => array('type' => PARAM_INT),
            'repeatnamedgroup[repeatnamedgroupel2]' => array('type' => PARAM_INT)), 'repeatablenamedgroup', 'add', 0);
919
920
    }
}
921
922
923
924
925
926
927
928
929
930
931
932

/**
 * Used to test that modgrade fields get unique id attributes.
 */
class formslib_multiple_modgrade_form extends moodleform {
    public function definition() {
        $mform = $this->_form;
        $mform->addElement('modgrade', 'grade1', 'Grade 1');
        $mform->addElement('modgrade', 'grade2', 'Grade 2');
        $mform->addElement('modgrade', 'grade[3]', 'Grade 3');
    }
}
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970

/**
 * Used to test frozen elements get unique id attributes.
 */
class formslib_persistantrreeze_element extends moodleform {
    public function definition() {
        $mform = $this->_form;

        // Create advanced checkbox.
        // Persistant.
        $advcheckboxpersistant = $mform->addElement('advcheckbox', 'advcheckboxpersistant', 'advcheckbox');
        $mform->setType('advcheckboxpersistant', PARAM_BOOL);
        $advcheckboxpersistant->setChecked(true);
        $advcheckboxpersistant->freeze();
        $advcheckboxpersistant->setPersistantFreeze(true);
        // Frozen.
        $advcheckboxfrozen = $mform->addElement('advcheckbox', 'advcheckboxfrozen', 'advcheckbox');
        $mform->setType('advcheckboxfrozen', PARAM_BOOL);
        $advcheckboxfrozen->setChecked(true);
        $advcheckboxfrozen->freeze();
        // Neither persistant nor Frozen.
        $mform->addElement('advcheckbox', 'advcheckboxnotpersistant', 'advcheckbox');
        $mform->setType('advcheckboxnotpersistant', PARAM_BOOL);

        // Create text fields.
        // Persistant.
        $elpersistant = $mform->addElement('text', 'textpersistant', 'test', 'test');
        $mform->setType('textpersistant', PARAM_TEXT);
        $elpersistant->freeze();
        $elpersistant->setPersistantFreeze(true);
        // Frozen.
        $elfrozen = $mform->addElement('text', 'textfrozen', 'test', 'test');
        $mform->setType('textfrozen', PARAM_TEXT);
        $elfrozen->freeze();
        // Neither persistant nor Frozen.
        $mform->addElement('text', 'textnotpersistant', 'test', 'test');
        $mform->setType('textnotpersistant', PARAM_TEXT);
    }
971
}
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

/**
 * Used to test that you can validate a form more than once. See MDL-56250.
 * @package    core_form
 * @author     Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
 * @copyright  2016 Catalyst IT
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class formslib_multiple_validation_form extends moodleform {
    /**
     * Simple definition, one text field which can have a number.
     */
    public function definition() {
        $mform = $this->_form;
        $mform->addElement('text', 'somenumber');
        $mform->setType('somenumber', PARAM_INT);
    }

    /**
     * The number cannot be negative.
     * @param array $data An array of form data
     * @param array $files An array of form files
     * @return array Error messages
     */
    public function validation($data, $files) {
        $errors = parent::validation($data, $files);
        if ($data['somenumber'] < 0) {
            $errors['somenumber'] = 'The number cannot be negative.';
        }