Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Unit tests for /lib/formslib.php.
  19   *
  20   * @package   core
  21   * @category  test
  22   * @copyright 2011 Sam Hemelryk
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core;
  27  
  28  use HTML_QuickForm_Rule_Range;
  29  use moodleform;
  30  use MoodleQuickForm_radio;
  31  use MoodleQuickForm_Rule_Required;
  32  use MoodleQuickForm_select;
  33  use MoodleQuickForm_text;
  34  
  35  defined('MOODLE_INTERNAL') || die();
  36  
  37  global $CFG;
  38  require_once($CFG->libdir . '/formslib.php');
  39  require_once($CFG->libdir . '/form/radio.php');
  40  require_once($CFG->libdir . '/form/select.php');
  41  require_once($CFG->libdir . '/form/text.php');
  42  
  43  /**
  44   * Unit tests for /lib/formslib.php.
  45   *
  46   * @package   core
  47   * @category  test
  48   * @copyright 2011 Sam Hemelryk
  49   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  50   */
  51  class formslib_test extends \advanced_testcase {
  52  
  53      public function test_require_rule() {
  54          global $CFG;
  55  
  56          $strictformsrequired = null;
  57          if (isset($CFG->strictformsrequired)) {
  58              $strictformsrequired = $CFG->strictformsrequired;
  59          }
  60  
  61          $rule = new MoodleQuickForm_Rule_Required();
  62  
  63          // First run the tests with strictformsrequired off.
  64          $CFG->strictformsrequired = false;
  65          // Passes.
  66          $this->assertTrue($rule->validate('Something'));
  67          $this->assertTrue($rule->validate("Something\nmore"));
  68          $this->assertTrue($rule->validate("\nmore"));
  69          $this->assertTrue($rule->validate(" more "));
  70          $this->assertTrue($rule->validate('ш'));
  71          $this->assertTrue($rule->validate("の"));
  72          $this->assertTrue($rule->validate("0"));
  73          $this->assertTrue($rule->validate(0));
  74          $this->assertTrue($rule->validate(true));
  75          $this->assertTrue($rule->validate(' '));
  76          $this->assertTrue($rule->validate('      '));
  77          $this->assertTrue($rule->validate("\t"));
  78          $this->assertTrue($rule->validate("\n"));
  79          $this->assertTrue($rule->validate("\r"));
  80          $this->assertTrue($rule->validate("\r\n"));
  81          $this->assertTrue($rule->validate(" \t  \n  \r "));
  82          $this->assertTrue($rule->validate('<p></p>'));
  83          $this->assertTrue($rule->validate('<p> </p>'));
  84          $this->assertTrue($rule->validate('<p>x</p>'));
  85          $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
  86          $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
  87          $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
  88          $this->assertTrue($rule->validate('<hr />'));
  89          $this->assertTrue($rule->validate('<hr/>'));
  90          $this->assertTrue($rule->validate('<hr>'));
  91          $this->assertTrue($rule->validate('<hr></hr>'));
  92          $this->assertTrue($rule->validate('<br />'));
  93          $this->assertTrue($rule->validate('<br/>'));
  94          $this->assertTrue($rule->validate('<br>'));
  95          $this->assertTrue($rule->validate('&nbsp;'));
  96          // Fails.
  97          $this->assertFalse($rule->validate(''));
  98          $this->assertFalse($rule->validate(false));
  99          $this->assertFalse($rule->validate(null));
 100  
 101          // Now run the same tests with it on to make sure things work as expected.
 102          $CFG->strictformsrequired = true;
 103          // Passes.
 104          $this->assertTrue($rule->validate('Something'));
 105          $this->assertTrue($rule->validate("Something\nmore"));
 106          $this->assertTrue($rule->validate("\nmore"));
 107          $this->assertTrue($rule->validate(" more "));
 108          $this->assertTrue($rule->validate("0"));
 109          $this->assertTrue($rule->validate('ш'));
 110          $this->assertTrue($rule->validate("の"));
 111          $this->assertTrue($rule->validate(0));
 112          $this->assertTrue($rule->validate(true));
 113          $this->assertTrue($rule->validate('<p>x</p>'));
 114          $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
 115          $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
 116          $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
 117          $this->assertTrue($rule->validate('<hr />'));
 118          $this->assertTrue($rule->validate('<hr/>'));
 119          $this->assertTrue($rule->validate('<hr>'));
 120          $this->assertTrue($rule->validate('<hr></hr>'));
 121          // Fails.
 122          $this->assertFalse($rule->validate(' '));
 123          $this->assertFalse($rule->validate('      '));
 124          $this->assertFalse($rule->validate("\t"));
 125          $this->assertFalse($rule->validate("\n"));
 126          $this->assertFalse($rule->validate("\r"));
 127          $this->assertFalse($rule->validate("\r\n"));
 128          $this->assertFalse($rule->validate(" \t  \n  \r "));
 129          $this->assertFalse($rule->validate('<p></p>'));
 130          $this->assertFalse($rule->validate('<p> </p>'));
 131          $this->assertFalse($rule->validate('<br />'));
 132          $this->assertFalse($rule->validate('<br/>'));
 133          $this->assertFalse($rule->validate('<br>'));
 134          $this->assertFalse($rule->validate('&nbsp;'));
 135          $this->assertFalse($rule->validate(''));
 136          $this->assertFalse($rule->validate(false));
 137          $this->assertFalse($rule->validate(null));
 138  
 139          if (isset($strictformsrequired)) {
 140              $CFG->strictformsrequired = $strictformsrequired;
 141          }
 142      }
 143  
 144      public function test_range_rule() {
 145          global $CFG;
 146  
 147          require_once('HTML/QuickForm/Rule/Range.php'); // Requires this pear stuff.
 148  
 149          $strictformsrequired = null;
 150          if (isset($CFG->strictformsrequired)) {
 151              $strictformsrequired = $CFG->strictformsrequired;
 152          }
 153  
 154          $rule = new HTML_QuickForm_Rule_Range();
 155  
 156          // First run the tests with strictformsrequired off.
 157          $CFG->strictformsrequired = false;
 158          // Passes.
 159          $rule->setName('minlength'); // Let's verify some min lengths.
 160          $this->assertTrue($rule->validate('12', 2));
 161          $this->assertTrue($rule->validate('123', 2));
 162          $this->assertTrue($rule->validate('áé', 2));
 163          $this->assertTrue($rule->validate('áéí', 2));
 164          $rule->setName('maxlength'); // Let's verify some max lengths.
 165          $this->assertTrue($rule->validate('1', 2));
 166          $this->assertTrue($rule->validate('12', 2));
 167          $this->assertTrue($rule->validate('á', 2));
 168          $this->assertTrue($rule->validate('áé', 2));
 169          $rule->setName('----'); // Let's verify some ranges.
 170          $this->assertTrue($rule->validate('', array(0, 2)));
 171          $this->assertTrue($rule->validate('1', array(0, 2)));
 172          $this->assertTrue($rule->validate('12', array(0, 2)));
 173          $this->assertTrue($rule->validate('á', array(0, 2)));
 174          $this->assertTrue($rule->validate('áé', array(0, 2)));
 175  
 176          // Fail.
 177          $rule->setName('minlength'); // Let's verify some min lengths.
 178          $this->assertFalse($rule->validate('', 2));
 179          $this->assertFalse($rule->validate('1', 2));
 180          $this->assertFalse($rule->validate('á', 2));
 181          $rule->setName('maxlength'); // Let's verify some max lengths.
 182          $this->assertFalse($rule->validate('123', 2));
 183          $this->assertFalse($rule->validate('áéí', 2));
 184          $rule->setName('----'); // Let's verify some ranges.
 185          $this->assertFalse($rule->validate('', array(1, 2)));
 186          $this->assertFalse($rule->validate('123', array(1, 2)));
 187          $this->assertFalse($rule->validate('áéí', array(1, 2)));
 188  
 189          // Now run the same tests with it on to make sure things work as expected.
 190          $CFG->strictformsrequired = true;
 191          // Passes.
 192          $rule->setName('minlength'); // Let's verify some min lengths.
 193          $this->assertTrue($rule->validate('12', 2));
 194          $this->assertTrue($rule->validate('123', 2));
 195          $this->assertTrue($rule->validate('áé', 2));
 196          $this->assertTrue($rule->validate('áéí', 2));
 197          $rule->setName('maxlength'); // Let's verify some min lengths.
 198          $this->assertTrue($rule->validate('1', 2));
 199          $this->assertTrue($rule->validate('12', 2));
 200          $this->assertTrue($rule->validate('á', 2));
 201          $this->assertTrue($rule->validate('áé', 2));
 202          $rule->setName('----'); // Let's verify some ranges.
 203          $this->assertTrue($rule->validate('', array(0, 2)));
 204          $this->assertTrue($rule->validate('1', array(0, 2)));
 205          $this->assertTrue($rule->validate('12', array(0, 2)));
 206          $this->assertTrue($rule->validate('á', array(0, 2)));
 207          $this->assertTrue($rule->validate('áé', array(0, 2)));
 208  
 209          // Fail.
 210          $rule->setName('minlength'); // Let's verify some min lengths.
 211          $this->assertFalse($rule->validate('', 2));
 212          $this->assertFalse($rule->validate('1', 2));
 213          $this->assertFalse($rule->validate('á', 2));
 214          $rule->setName('maxlength'); // Let's verify some min lengths.
 215          $this->assertFalse($rule->validate('123', 2));
 216          $this->assertFalse($rule->validate('áéí', 2));
 217          $rule->setName('----'); // Let's verify some ranges.
 218          $this->assertFalse($rule->validate('', array(1, 2)));
 219          $this->assertFalse($rule->validate('123', array(1, 2)));
 220          $this->assertFalse($rule->validate('áéí', array(1, 2)));
 221  
 222          if (isset($strictformsrequired)) {
 223              $CFG->strictformsrequired = $strictformsrequired;
 224          }
 225      }
 226  
 227      public function test_generate_id_select() {
 228          $el = new MoodleQuickForm_select('choose_one', 'Choose one',
 229              array(1 => 'One', '2' => 'Two'));
 230          $el->_generateId();
 231          $this->assertSame('id_choose_one', $el->getAttribute('id'));
 232      }
 233  
 234      public function test_generate_id_like_repeat() {
 235          $el = new MoodleQuickForm_text('text[7]', 'Type something');
 236          $el->_generateId();
 237          $this->assertSame('id_text_7', $el->getAttribute('id'));
 238      }
 239  
 240      public function test_can_manually_set_id() {
 241          $el = new MoodleQuickForm_text('elementname', 'Type something',
 242              array('id' => 'customelementid'));
 243          $el->_generateId();
 244          $this->assertSame('customelementid', $el->getAttribute('id'));
 245      }
 246  
 247      public function test_generate_id_radio() {
 248          $el = new MoodleQuickForm_radio('radio', 'Label', 'Choice label', 'choice_value');
 249          $el->_generateId();
 250          $this->assertSame('id_radio_choice_value', $el->getAttribute('id'));
 251      }
 252  
 253      public function test_radio_can_manually_set_id() {
 254          $el = new MoodleQuickForm_radio('radio2', 'Label', 'Choice label', 'choice_value',
 255              array('id' => 'customelementid2'));
 256          $el->_generateId();
 257          $this->assertSame('customelementid2', $el->getAttribute('id'));
 258      }
 259  
 260      public function test_generate_id_radio_like_repeat() {
 261          $el = new MoodleQuickForm_radio('repeatradio[2]', 'Label', 'Choice label', 'val');
 262          $el->_generateId();
 263          $this->assertSame('id_repeatradio_2_val', $el->getAttribute('id'));
 264      }
 265  
 266      public function test_rendering() {
 267          $form = new formslib_test_form();
 268          ob_start();
 269          $form->display();
 270          $html = ob_get_clean();
 271  
 272          $this->assertTag(array('tag'=>'select', 'id'=>'id_choose_one',
 273              'attributes'=>array('name'=>'choose_one')), $html);
 274  
 275          $this->assertTag(array('tag'=>'input', 'id'=>'id_text_0',
 276              'attributes'=>array('type'=>'text', 'name'=>'text[0]')), $html);
 277  
 278          $this->assertTag(array('tag'=>'input', 'id'=>'id_text_1',
 279              'attributes'=>array('type'=>'text', 'name'=>'text[1]')), $html);
 280  
 281          $this->assertTag(array('tag'=>'input', 'id'=>'id_radio_choice_value',
 282              'attributes'=>array('type'=>'radio', 'name'=>'radio', 'value'=>'choice_value')), $html);
 283  
 284          $this->assertTag(array('tag'=>'input', 'id'=>'customelementid2',
 285              'attributes'=>array('type'=>'radio', 'name'=>'radio2')), $html);
 286  
 287          $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_0_2',
 288              'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[0]', 'value'=>'2')), $html);
 289  
 290          $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_1',
 291              'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'1')), $html);
 292  
 293          $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_2',
 294              'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'2')), $html);
 295      }
 296  
 297      public function test_settype_debugging_text() {
 298          $mform = new formslib_settype_debugging_text();
 299          $this->assertDebuggingCalled("Did you remember to call setType() for 'texttest'? Defaulting to PARAM_RAW cleaning.");
 300  
 301          // Check form still there though.
 302          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="texttest/');
 303          $mform->display();
 304      }
 305  
 306      public function test_settype_debugging_hidden() {
 307          $mform = new formslib_settype_debugging_hidden();
 308          $this->assertDebuggingCalled("Did you remember to call setType() for 'hiddentest'? Defaulting to PARAM_RAW cleaning.");
 309  
 310          // Check form still there though.
 311          $this->expectOutputRegex('/<input[^>]*name="hiddentest[^>]*type="hidden/');
 312          $mform->display();
 313      }
 314  
 315      public function test_settype_debugging_url() {
 316          $this->resetAfterTest(true);
 317          $this->setAdminUser();
 318          $mform = new formslib_settype_debugging_url();
 319          $this->assertDebuggingCalled("Did you remember to call setType() for 'urltest'? Defaulting to PARAM_RAW cleaning.");
 320  
 321          // Check form still there though.
 322          $this->expectOutputRegex('/<input[^>]*type="url[^>]*name="urltest"/');
 323          $mform->display();
 324      }
 325  
 326      public function test_settype_debugging_repeat() {
 327          $mform = new formslib_settype_debugging_repeat();
 328          $this->assertDebuggingCalled("Did you remember to call setType() for 'repeattest[0]'? Defaulting to PARAM_RAW cleaning.");
 329  
 330          // Check form still there though.
 331          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="repeattest/');
 332          $mform->display();
 333      }
 334  
 335      public function test_settype_debugging_repeat_ok() {
 336          $mform = new formslib_settype_debugging_repeat_ok();
 337          // No debugging expected here.
 338  
 339          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="repeattest/');
 340          $mform->display();
 341      }
 342  
 343      public function test_settype_debugging_group() {
 344          $mform = new formslib_settype_debugging_group();
 345          $this->assertDebuggingCalled("Did you remember to call setType() for 'groupel1'? Defaulting to PARAM_RAW cleaning.");
 346          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="groupel1"/');
 347          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="groupel2"/');
 348          $mform->display();
 349      }
 350  
 351      public function test_settype_debugging_namedgroup() {
 352          $mform = new formslib_settype_debugging_namedgroup();
 353          $this->assertDebuggingCalled("Did you remember to call setType() for 'namedgroup[groupel1]'? Defaulting to PARAM_RAW cleaning.");
 354          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="namedgroup\[groupel1\]"/');
 355          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="namedgroup\[groupel2\]"/');
 356          $mform->display();
 357      }
 358  
 359      public function test_settype_debugging_funky_name() {
 360          $mform = new formslib_settype_debugging_funky_name();
 361          $this->assertDebuggingCalled("Did you remember to call setType() for 'blah[foo][bar][1]'? Defaulting to PARAM_RAW cleaning.");
 362          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[foo\]\[bar\]\[0\]"/');
 363          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[foo\]\[bar\]\[1\]"/');
 364          $mform->display();
 365      }
 366  
 367      public function test_settype_debugging_type_inheritance() {
 368          $mform = new formslib_settype_debugging_type_inheritance();
 369          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[foo\]\[bar\]\[0\]"/');
 370          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[bar\]\[foo\]\[1\]"/');
 371          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[any\]\[other\]\[2\]"/');
 372          $mform->display();
 373      }
 374  
 375      public function test_settype_debugging_type_group_in_repeat() {
 376          $mform = new formslib_settype_debugging_type_group_in_repeat();
 377          $this->assertDebuggingCalled("Did you remember to call setType() for 'test2[0]'? Defaulting to PARAM_RAW cleaning.");
 378          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="test1\[0\]"/');
 379          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="test2\[0\]"/');
 380          $mform->display();
 381      }
 382  
 383      public function test_settype_debugging_type_namedgroup_in_repeat() {
 384          $mform = new formslib_settype_debugging_type_namedgroup_in_repeat();
 385          $this->assertDebuggingCalled("Did you remember to call setType() for 'namedgroup[0][test2]'? Defaulting to PARAM_RAW cleaning.");
 386          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="namedgroup\[0\]\[test1\]"/');
 387          $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="namedgroup\[0\]\[test2\]"/');
 388          $mform->display();
 389      }
 390  
 391      public function test_type_cleaning() {
 392          $expectedtypes = array(
 393              'simpleel' => PARAM_INT,
 394              'groupel1' => PARAM_INT,
 395              'groupel2' => PARAM_FLOAT,
 396              'groupel3' => PARAM_INT,
 397              'namedgroup' => array(
 398                  'sndgroupel1' => PARAM_INT,
 399                  'sndgroupel2' => PARAM_FLOAT,
 400                  'sndgroupel3' => PARAM_INT
 401              ),
 402              'namedgroupinherit' => array(
 403                  'thdgroupel1' => PARAM_INT,
 404                  'thdgroupel2' => PARAM_INT
 405              ),
 406              'repeatedel' => array(
 407                  0 => PARAM_INT,
 408                  1 => PARAM_INT
 409              ),
 410              'repeatedelinherit' => array(
 411                  0 => PARAM_INT,
 412                  1 => PARAM_INT
 413              ),
 414              'squaretest' => array(
 415                  0 => PARAM_INT
 416              ),
 417              'nested' => array(
 418                  0 => array(
 419                      'bob' => array(
 420                          123 => PARAM_INT,
 421                          'foo' => PARAM_FLOAT
 422                      ),
 423                      'xyz' => PARAM_RAW
 424                  ),
 425                  1 => PARAM_INT
 426              ),
 427              'repeatgroupel1' => array(
 428                  0 => PARAM_INT,
 429                  1 => PARAM_INT
 430              ),
 431              'repeatgroupel2' => array(
 432                  0 => PARAM_INT,
 433                  1 => PARAM_INT
 434              ),
 435              'repeatnamedgroup' => array(
 436                  0 => array(
 437                      'repeatnamedgroupel1' => PARAM_INT,
 438                      'repeatnamedgroupel2' => PARAM_INT
 439                  ),
 440                  1 => array(
 441                      'repeatnamedgroupel1' => PARAM_INT,
 442                      'repeatnamedgroupel2' => PARAM_INT
 443                  )
 444              )
 445          );
 446          $valuessubmitted = array(
 447              'simpleel' => '11.01',
 448              'groupel1' => '11.01',
 449              'groupel2' => '11.01',
 450              'groupel3' => '11.01',
 451              'namedgroup' => array(
 452                  'sndgroupel1' => '11.01',
 453                  'sndgroupel2' => '11.01',
 454                  'sndgroupel3' => '11.01'
 455              ),
 456              'namedgroupinherit' => array(
 457                  'thdgroupel1' => '11.01',
 458                  'thdgroupel2' => '11.01'
 459              ),
 460              'repeatedel' => array(
 461                  0 => '11.01',
 462                  1 => '11.01'
 463              ),
 464              'repeatedelinherit' => array(
 465                  0 => '11.01',
 466                  1 => '11.01'
 467              ),
 468              'squaretest' => array(
 469                  0 => '11.01'
 470              ),
 471              'nested' => array(
 472                  0 => array(
 473                      'bob' => array(
 474                          123 => '11.01',
 475                          'foo' => '11.01'
 476                      ),
 477                      'xyz' => '11.01'
 478                  ),
 479                  1 => '11.01'
 480              ),
 481              'repeatgroupel1' => array(
 482                  0 => '11.01',
 483                  1 => '11.01'
 484              ),
 485              'repeatgroupel2' => array(
 486                  0 => '11.01',
 487                  1 => '11.01'
 488              ),
 489              'repeatnamedgroup' => array(
 490                  0 => array(
 491                      'repeatnamedgroupel1' => '11.01',
 492                      'repeatnamedgroupel2' => '11.01'
 493                  ),
 494                  1 => array(
 495                      'repeatnamedgroupel1' => '11.01',
 496                      'repeatnamedgroupel2' => '11.01'
 497                  )
 498              )
 499          );
 500          $expectedvalues = array(
 501              'simpleel' => 11,
 502              'groupel1' => 11,
 503              'groupel2' => 11.01,
 504              'groupel3' => 11,
 505              'namedgroup' => array(
 506                  'sndgroupel1' => 11,
 507                  'sndgroupel2' => 11.01,
 508                  'sndgroupel3' => 11
 509              ),
 510              'namedgroupinherit' => array(
 511                  'thdgroupel1' => 11,
 512                  'thdgroupel2' => 11
 513              ),
 514              'repeatable' => 2,
 515              'repeatedel' => array(
 516                  0 => 11,
 517                  1 => 11
 518              ),
 519              'repeatableinherit' => 2,
 520              'repeatedelinherit' => array(
 521                  0 => 11,
 522                  1 => 11
 523              ),
 524              'squaretest' => array(
 525                  0 => 11
 526              ),
 527              'nested' => array(
 528                  0 => array(
 529                      'bob' => array(
 530                          123 => 11,
 531                          'foo' => 11.01
 532                      ),
 533                      'xyz' => '11.01'
 534                  ),
 535                  1 => 11
 536              ),
 537              'repeatablegroup' => 2,
 538              'repeatgroupel1' => array(
 539                  0 => 11,
 540                  1 => 11
 541              ),
 542              'repeatgroupel2' => array(
 543                  0 => 11,
 544                  1 => 11
 545              ),
 546              'repeatablenamedgroup' => 2,
 547              'repeatnamedgroup' => array(
 548                  0 => array(
 549                      'repeatnamedgroupel1' => 11,
 550                      'repeatnamedgroupel2' => 11
 551                  ),
 552                  1 => array(
 553                      'repeatnamedgroupel1' => 11,
 554                      'repeatnamedgroupel2' => 11
 555                  )
 556              )
 557          );
 558  
 559          $mform = new formslib_clean_value();
 560          $mform->get_form()->updateSubmission($valuessubmitted, null);
 561          foreach ($expectedtypes as $elementname => $expected) {
 562              $actual = $mform->get_form()->getCleanType($elementname, $valuessubmitted[$elementname]);
 563              $this->assertSame($expected, $actual, "Failed validating clean type of '$elementname'");
 564          }
 565  
 566          $data = $mform->get_data();
 567          $this->assertSame($expectedvalues, (array) $data);
 568      }
 569  
 570      /**
 571       * MDL-52873
 572       */
 573      public function test_multiple_modgrade_fields() {
 574          global $CFG;
 575          $this->resetAfterTest(true);
 576  
 577          $CFG->theme = 'classic';
 578          $form = new formslib_multiple_modgrade_form();
 579          ob_start();
 580          $form->display();
 581          $html = ob_get_clean();
 582  
 583          $this->assertTag(array('id' => 'fitem_fgroup_id_grade1'), $html);
 584          $this->assertTag(array('id' => 'id_grade1_modgrade_type'), $html);
 585          $this->assertTag(array('id' => 'id_grade1_modgrade_point'), $html);
 586          $this->assertTag(array('id' => 'id_grade1_modgrade_scale'), $html);
 587  
 588          $this->assertTag(array('id' => 'fitem_fgroup_id_grade2'), $html);
 589          $this->assertTag(array('id' => 'id_grade2_modgrade_type'), $html);
 590          $this->assertTag(array('id' => 'id_grade2_modgrade_point'), $html);
 591          $this->assertTag(array('id' => 'id_grade2_modgrade_scale'), $html);
 592  
 593          $this->assertTag(array('id' => 'fitem_fgroup_id_grade_3'), $html);
 594          $this->assertTag(array('id' => 'id_grade_3_modgrade_type'), $html);
 595          $this->assertTag(array('id' => 'id_grade_3_modgrade_point'), $html);
 596          $this->assertTag(array('id' => 'id_grade_3_modgrade_scale'), $html);
 597      }
 598  
 599      /**
 600       * Test persistant freeze elements have different id's.
 601       */
 602      public function test_persistantrreeze_element() {
 603          global $CFG;
 604          $this->resetAfterTest(true);
 605          $CFG->theme = 'classic';
 606  
 607          $form = new formslib_persistantrreeze_element();
 608          ob_start();
 609          $form->display();
 610          $html = ob_get_clean();
 611  
 612          // Test advcheckbox id's.
 613          $this->assertTag(array('id' => 'id_advcheckboxpersistant'), $html);
 614          $this->assertTag(array('id' => 'id_advcheckboxnotpersistant'), $html);
 615          $this->assertNotTag(array('id' => 'id_advcheckboxnotpersistant_persistant'), $html);
 616          $this->assertTag(array('id' => 'id_advcheckboxfrozen'), $html);
 617  
 618          // Check text element id's.
 619          $this->assertTag(array('id' => 'id_textpersistant'), $html);
 620          $this->assertTag(array('id' => 'id_textnotpersistant'), $html);
 621          $this->assertNotTag(array('id' => 'id_textnotpersistant_persistant'), $html);
 622          $this->assertTag(array('id' => 'id_textfrozen'), $html);
 623          $this->assertNotTag(array('id' => 'id_textfrozen_persistant'), $html);
 624  
 625      }
 626  
 627      /**
 628       * Ensure a validation can run at least once per object. See MDL-56259.
 629       */
 630      public function test_multiple_validation() {
 631          $this->resetAfterTest(true);
 632  
 633          // It should be valid.
 634          formslib_multiple_validation_form::mock_submit(['somenumber' => '10']);
 635          $form = new formslib_multiple_validation_form();
 636          $this->assertTrue($form->is_validated());
 637          $this->assertEquals(10, $form->get_data()->somenumber);
 638  
 639          // It should not validate.
 640          formslib_multiple_validation_form::mock_submit(['somenumber' => '-5']);
 641          $form = new formslib_multiple_validation_form();
 642          $this->assertFalse($form->is_validated());
 643          $this->assertNull($form->get_data());
 644      }
 645  
 646      /**
 647       * MDL-56233 - Tests mocking a form inside a namespace.
 648       */
 649      public function test_mock_submit() {
 650          require_once (__DIR__.'/fixtures/namespaced_form.php');
 651          \local_unittests\namespaced_form\exampleform::mock_submit(['title' => 'Mocked Value']);
 652          $form = new \local_unittests\namespaced_form\exampleform();
 653  
 654          // Here is the problem, this is the expected hidden field name.
 655          $expected = '_qf__local_unittests_namespaced_form_exampleform';
 656          self::assertArrayHasKey($expected, $_POST);
 657  
 658          // This should work now, before it would fail.
 659          self::assertTrue($form->is_submitted());
 660          self::assertSame('Mocked Value', $form->get_data()->title);
 661      }
 662  }
 663  
 664  
 665  /**
 666   * Test form to be used by {@link formslib_test::test_rendering()}.
 667   */
 668  class formslib_test_form extends moodleform {
 669      public function definition() {
 670          $this->_form->addElement('select', 'choose_one', 'Choose one',
 671              array(1 => 'One', '2' => 'Two'));
 672  
 673          $repeatels = array(
 674              $this->_form->createElement('text', 'text', 'Type something')
 675          );
 676          // TODO: The repeat_elements() is far from perfect. Everything should be
 677          // repeated auto-magically by default with options only defining exceptions.
 678          // Surely this is caused because we are storing some element information OUT
 679          // from the element (type...) at form level. Anyway, the method should do its
 680          // work better, no matter of that.
 681          $this->repeat_elements($repeatels, 2, array('text' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
 682  
 683          $this->_form->addElement('radio', 'radio', 'Label', 'Choice label', 'choice_value');
 684  
 685          $this->_form->addElement('radio', 'radio2', 'Label', 'Choice label', 'choice_value',
 686              array('id' => 'customelementid2'));
 687  
 688          $repeatels = array(
 689              $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'One', 1),
 690              $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'Two', 2),
 691          );
 692          $this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios');
 693      }
 694  }
 695  
 696  /**
 697   * Used to test debugging is called when text added without setType.
 698   */
 699  class formslib_settype_debugging_text extends moodleform {
 700      public function definition() {
 701          $mform = $this->_form;
 702  
 703          $mform->addElement('text', 'texttest', 'test123', 'testing123');
 704      }
 705  }
 706  
 707  /**
 708   * Used to test debugging is called when hidden added without setType.
 709   */
 710  class formslib_settype_debugging_hidden extends moodleform {
 711      public function definition() {
 712          $mform = $this->_form;
 713  
 714          $mform->addElement('hidden', 'hiddentest', '1');
 715      }
 716  }
 717  
 718  /**
 719   * Used to test debugging is called when hidden added without setType.
 720   */
 721  class formslib_settype_debugging_url extends moodleform {
 722      public function definition() {
 723          $mform = $this->_form;
 724  
 725          $mform->addElement('url', 'urltest', 'urltest');
 726      }
 727  }
 728  
 729  /**
 730   * Used to test debugging is called when repeated text added without setType.
 731   */
 732  class formslib_settype_debugging_repeat extends moodleform {
 733      public function definition() {
 734          $mform = $this->_form;
 735  
 736          $repeatels = array(
 737              $mform->createElement('text', 'repeattest', 'Type something')
 738          );
 739  
 740          $this->repeat_elements($repeatels, 1, array(), 'numtexts', 'addtexts');
 741      }
 742  }
 743  
 744  /**
 745   * Used to no debugging is called when correctly test.
 746   */
 747  class formslib_settype_debugging_repeat_ok extends moodleform {
 748      public function definition() {
 749          $mform = $this->_form;
 750  
 751          $repeatels = array(
 752              $mform->createElement('text', 'repeattest', 'Type something')
 753          );
 754  
 755          $this->repeat_elements($repeatels, 2, array('repeattest' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
 756      }
 757  }
 758  
 759  /**
 760   * Used to test if debugging is called when a group contains elements without type.
 761   */
 762  class formslib_settype_debugging_group extends moodleform {
 763      public function definition() {
 764          $mform = $this->_form;
 765          $group = array(
 766              $mform->createElement('text', 'groupel1', 'groupel1'),
 767              $mform->createElement('text', 'groupel2', 'groupel2')
 768          );
 769          $mform->addGroup($group);
 770          $mform->setType('groupel2', PARAM_INT);
 771      }
 772  }
 773  
 774  /**
 775   * Used to test if debugging is called when a named group contains elements without type.
 776   */
 777  class formslib_settype_debugging_namedgroup extends moodleform {
 778      public function definition() {
 779          $mform = $this->_form;
 780          $group = array(
 781              $mform->createElement('text', 'groupel1', 'groupel1'),
 782              $mform->createElement('text', 'groupel2', 'groupel2')
 783          );
 784          $mform->addGroup($group, 'namedgroup');
 785          $mform->setType('namedgroup[groupel2]', PARAM_INT);
 786      }
 787  }
 788  
 789  /**
 790   * Used to test if debugging is called when has a funky name.
 791   */
 792  class formslib_settype_debugging_funky_name extends moodleform {
 793      public function definition() {
 794          $mform = $this->_form;
 795          $mform->addElement('text', 'blah[foo][bar][0]', 'test', 'test');
 796          $mform->addElement('text', 'blah[foo][bar][1]', 'test', 'test');
 797          $mform->setType('blah[foo][bar][0]', PARAM_INT);
 798      }
 799  }
 800  
 801  /**
 802   * Used to test that debugging is not called with type inheritance.
 803   */
 804  class formslib_settype_debugging_type_inheritance extends moodleform {
 805      public function definition() {
 806          $mform = $this->_form;
 807          $mform->addElement('text', 'blah[foo][bar][0]', 'test1', 'test');
 808          $mform->addElement('text', 'blah[bar][foo][1]', 'test2', 'test');
 809          $mform->addElement('text', 'blah[any][other][2]', 'test3', 'test');
 810          $mform->setType('blah[foo][bar]', PARAM_INT);
 811          $mform->setType('blah[bar]', PARAM_FLOAT);
 812          $mform->setType('blah', PARAM_TEXT);
 813      }
 814  }
 815  
 816  /**
 817   * Used to test the debugging when using groups in repeated elements.
 818   */
 819  class formslib_settype_debugging_type_group_in_repeat extends moodleform {
 820      public function definition() {
 821          $mform = $this->_form;
 822          $groupelements = array(
 823              $mform->createElement('text', 'test1', 'test1', 'test'),
 824              $mform->createElement('text', 'test2', 'test2', 'test')
 825          );
 826          $group = $mform->createElement('group', null, 'group1', $groupelements, null, false);
 827          $this->repeat_elements(array($group), 1, array('test1' => array('type' => PARAM_INT)), 'hidden', 'button');
 828      }
 829  }
 830  
 831  /**
 832   * Used to test the debugging when using named groups in repeated elements.
 833   */
 834  class formslib_settype_debugging_type_namedgroup_in_repeat extends moodleform {
 835      public function definition() {
 836          $mform = $this->_form;
 837          $groupelements = array(
 838              $mform->createElement('text', 'test1', 'test1', 'test'),
 839              $mform->createElement('text', 'test2', 'test2', 'test')
 840          );
 841          $group = $mform->createElement('group', 'namedgroup', 'group1', $groupelements, null, true);
 842          $this->repeat_elements(array($group), 1, array('namedgroup[test1]' => array('type' => PARAM_INT)), 'hidden', 'button');
 843      }
 844  }
 845  
 846  /**
 847   * Used to check value cleaning.
 848   */
 849  class formslib_clean_value extends moodleform {
 850      public function get_form() {
 851          return $this->_form;
 852      }
 853      public function definition() {
 854          $mform = $this->_form;
 855  
 856          // Add a simple int.
 857          $mform->addElement('text', 'simpleel', 'simpleel');
 858          $mform->setType('simpleel', PARAM_INT);
 859  
 860          // Add a non-named group.
 861          $group = array(
 862              $mform->createElement('text', 'groupel1', 'groupel1'),
 863              $mform->createElement('text', 'groupel2', 'groupel2'),
 864              $mform->createElement('text', 'groupel3', 'groupel3')
 865          );
 866          $mform->setType('groupel1', PARAM_INT);
 867          $mform->setType('groupel2', PARAM_FLOAT);
 868          $mform->setType('groupel3', PARAM_INT);
 869          $mform->addGroup($group);
 870  
 871          // Add a named group.
 872          $group = array(
 873              $mform->createElement('text', 'sndgroupel1', 'sndgroupel1'),
 874              $mform->createElement('text', 'sndgroupel2', 'sndgroupel2'),
 875              $mform->createElement('text', 'sndgroupel3', 'sndgroupel3')
 876          );
 877          $mform->addGroup($group, 'namedgroup');
 878          $mform->setType('namedgroup[sndgroupel1]', PARAM_INT);
 879          $mform->setType('namedgroup[sndgroupel2]', PARAM_FLOAT);
 880          $mform->setType('namedgroup[sndgroupel3]', PARAM_INT);
 881  
 882          // Add a named group, with inheritance.
 883          $group = array(
 884              $mform->createElement('text', 'thdgroupel1', 'thdgroupel1'),
 885              $mform->createElement('text', 'thdgroupel2', 'thdgroupel2')
 886          );
 887          $mform->addGroup($group, 'namedgroupinherit');
 888          $mform->setType('namedgroupinherit', PARAM_INT);
 889  
 890          // Add a repetition.
 891          $repeat = $mform->createElement('text', 'repeatedel', 'repeatedel');
 892          $this->repeat_elements(array($repeat), 2, array('repeatedel' => array('type' => PARAM_INT)), 'repeatable', 'add', 0);
 893  
 894          // Add a repetition, with inheritance.
 895          $repeat = $mform->createElement('text', 'repeatedelinherit', 'repeatedelinherit');
 896          $this->repeat_elements(array($repeat), 2, array(), 'repeatableinherit', 'add', 0);
 897          $mform->setType('repeatedelinherit', PARAM_INT);
 898  
 899          // Add an arbitrary named element.
 900          $mform->addElement('text', 'squaretest[0]', 'squaretest[0]');
 901          $mform->setType('squaretest[0]', PARAM_INT);
 902  
 903          // Add an arbitrary nested array named element.
 904          $mform->addElement('text', 'nested[0][bob][123]', 'nested[0][bob][123]');
 905          $mform->setType('nested[0][bob][123]', PARAM_INT);
 906  
 907          // Add inheritance test cases.
 908          $mform->setType('nested', PARAM_INT);
 909          $mform->setType('nested[0]', PARAM_RAW);
 910          $mform->setType('nested[0][bob]', PARAM_FLOAT);
 911          $mform->addElement('text', 'nested[1]', 'nested[1]');
 912          $mform->addElement('text', 'nested[0][xyz]', 'nested[0][xyz]');
 913          $mform->addElement('text', 'nested[0][bob][foo]', 'nested[0][bob][foo]');
 914  
 915          // Add group in repeated element (with extra inheritance).
 916          $groupelements = array(
 917              $mform->createElement('text', 'repeatgroupel1', 'repeatgroupel1'),
 918              $mform->createElement('text', 'repeatgroupel2', 'repeatgroupel2')
 919          );
 920          $group = $mform->createElement('group', 'repeatgroup', 'repeatgroup', $groupelements, null, false);
 921          $this->repeat_elements(array($group), 2, array('repeatgroupel1' => array('type' => PARAM_INT),
 922              'repeatgroupel2' => array('type' => PARAM_INT)), 'repeatablegroup', 'add', 0);
 923  
 924          // Add named group in repeated element.
 925          $groupelements = array(
 926              $mform->createElement('text', 'repeatnamedgroupel1', 'repeatnamedgroupel1'),
 927              $mform->createElement('text', 'repeatnamedgroupel2', 'repeatnamedgroupel2')
 928          );
 929          $group = $mform->createElement('group', 'repeatnamedgroup', 'repeatnamedgroup', $groupelements, null, true);
 930          $this->repeat_elements(array($group), 2, array('repeatnamedgroup[repeatnamedgroupel1]' => array('type' => PARAM_INT),
 931              'repeatnamedgroup[repeatnamedgroupel2]' => array('type' => PARAM_INT)), 'repeatablenamedgroup', 'add', 0);
 932      }
 933  }
 934  
 935  /**
 936   * Used to test that modgrade fields get unique id attributes.
 937   */
 938  class formslib_multiple_modgrade_form extends moodleform {
 939      public function definition() {
 940          $mform = $this->_form;
 941          $mform->addElement('modgrade', 'grade1', 'Grade 1');
 942          $mform->addElement('modgrade', 'grade2', 'Grade 2');
 943          $mform->addElement('modgrade', 'grade[3]', 'Grade 3');
 944      }
 945  }
 946  
 947  /**
 948   * Used to test frozen elements get unique id attributes.
 949   */
 950  class formslib_persistantrreeze_element extends moodleform {
 951      public function definition() {
 952          $mform = $this->_form;
 953  
 954          // Create advanced checkbox.
 955          // Persistant.
 956          $advcheckboxpersistant = $mform->addElement('advcheckbox', 'advcheckboxpersistant', 'advcheckbox');
 957          $mform->setType('advcheckboxpersistant', PARAM_BOOL);
 958          $advcheckboxpersistant->setChecked(true);
 959          $advcheckboxpersistant->freeze();
 960          $advcheckboxpersistant->setPersistantFreeze(true);
 961          // Frozen.
 962          $advcheckboxfrozen = $mform->addElement('advcheckbox', 'advcheckboxfrozen', 'advcheckbox');
 963          $mform->setType('advcheckboxfrozen', PARAM_BOOL);
 964          $advcheckboxfrozen->setChecked(true);
 965          $advcheckboxfrozen->freeze();
 966          // Neither persistant nor Frozen.
 967          $mform->addElement('advcheckbox', 'advcheckboxnotpersistant', 'advcheckbox');
 968          $mform->setType('advcheckboxnotpersistant', PARAM_BOOL);
 969  
 970          // Create text fields.
 971          // Persistant.
 972          $elpersistant = $mform->addElement('text', 'textpersistant', 'test', 'test');
 973          $mform->setType('textpersistant', PARAM_TEXT);
 974          $elpersistant->freeze();
 975          $elpersistant->setPersistantFreeze(true);
 976          // Frozen.
 977          $elfrozen = $mform->addElement('text', 'textfrozen', 'test', 'test');
 978          $mform->setType('textfrozen', PARAM_TEXT);
 979          $elfrozen->freeze();
 980          // Neither persistant nor Frozen.
 981          $mform->addElement('text', 'textnotpersistant', 'test', 'test');
 982          $mform->setType('textnotpersistant', PARAM_TEXT);
 983      }
 984  }
 985  
 986  /**
 987   * Used to test that you can validate a form more than once. See MDL-56250.
 988   * @package    core_form
 989   * @author     Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
 990   * @copyright  2016 Catalyst IT
 991   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 992   */
 993  class formslib_multiple_validation_form extends moodleform {
 994      /**
 995       * Simple definition, one text field which can have a number.
 996       */
 997      public function definition() {
 998          $mform = $this->_form;
 999          $mform->addElement('text', 'somenumber');
1000          $mform->setType('somenumber', PARAM_INT);
1001      }
1002  
1003      /**
1004       * The number cannot be negative.
1005       * @param array $data An array of form data
1006       * @param array $files An array of form files
1007       * @return array Error messages
1008       */
1009      public function validation($data, $files) {
1010          $errors = parent::validation($data, $files);
1011          if ($data['somenumber'] < 0) {
1012              $errors['somenumber'] = 'The number cannot be negative.';
1013          }
1014          return $errors;
1015      }
1016  }