Permalink
Browse files

MDL-38897 formslib: add unit tests for setType debugging

  • Loading branch information...
1 parent 48056d5 commit b7d20148a1a7860806ce0c5b3e7440506b5feaab @danpoltawski danpoltawski committed Apr 4, 2013
Showing with 80 additions and 1 deletion.
  1. +80 −1 lib/tests/formslib_test.php
@@ -32,7 +32,7 @@
require_once($CFG->libdir . '/form/text.php');
-class formslib_testcase extends basic_testcase {
+class formslib_testcase extends advanced_testcase {
public function test_require_rule() {
global $CFG;
@@ -190,6 +190,41 @@ public function test_rendering() {
$this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_2',
'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'2')), $html);
}
+
+ 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.");
+
+ // Check form still there though
+ $this->expectOutputRegex('/<input[^>]*name="texttest[^>]*type="text/');
+ $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.");
+
+ // Check form still there though
+ $this->expectOutputRegex('/<input[^>]*name="hiddentest[^>]*type="hidden/');
+ $mform->display();
+ }
+
+ public function test_settype_debugging_repeat() {
+ $mform = new formslib_settype_debugging_repeat();
+ $this->assertDebuggingCalled("Did you remember to call setType() for 'repeattest'? Defaulting to PARAM_RAW cleaning.");
+
+ // Check form still there though
+ $this->expectOutputRegex('/<input[^>]*name="repeattest[^>]*type="text/');
+ $mform->display();
+ }
+
+ public function test_settype_debugging_repeat_ok() {
+ $mform = new formslib_settype_debugging_repeat_ok();
+ // No debugging expected here.
+
+ $this->expectOutputRegex('/<input[^>]*name="repeattest[^>]*type="text/');
+ $mform->display();
+ }
}
@@ -223,3 +258,47 @@ public function definition() {
$this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios');
}
}
+
+// Used to test debugging is called when text added without setType.
+class formslib_settype_debugging_text extends moodleform {
+ public function definition() {
+ $mform = $this->_form;
+
+ $mform->addElement('text', 'texttest', 'test123', 'testing123');
+ }
+}
+
+// Used to test debugging is called when hidden added without setType.
+class formslib_settype_debugging_hidden extends moodleform {
+ public function definition() {
+ $mform = $this->_form;
+
+ $mform->addElement('hidden', 'hiddentest', '1');
+ }
+}
+
+// Used to test debugging is called when repeated text added without setType.
+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');
+ }
+}
+
+// Used to no debugging is called when correctly tset
+class formslib_settype_debugging_repeat_ok extends moodleform {
+ public function definition() {
+ $mform = $this->_form;
+
+ $repeatels = array(
+ $mform->createElement('text', 'repeattest', 'Type something')
+ );
+
+ $this->repeat_elements($repeatels, 2, array('repeattest' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
+ }
+}

0 comments on commit b7d2014

Please sign in to comment.