|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00018 00027 if (!defined('MOODLE_INTERNAL')) { 00028 die('Direct access to this script is forbidden.'); 00029 } 00030 require_once($CFG->libdir . '/formslib.php'); 00031 require_once($CFG->libdir . '/form/radio.php'); 00032 require_once($CFG->libdir . '/form/select.php'); 00033 require_once($CFG->libdir . '/form/text.php'); 00034 00035 class formslib_test extends UnitTestCase { 00036 00037 public function test_require_rule() { 00038 global $CFG; 00039 00040 $strictformsrequired = false; 00041 if (!empty($CFG->strictformsrequired)) { 00042 $strictformsrequired = $CFG->strictformsrequired; 00043 } 00044 00045 $rule = new MoodleQuickForm_Rule_Required(); 00046 00047 // First run the tests with strictformsrequired off 00048 $CFG->strictformsrequired = false; 00049 // Passes 00050 $this->assertTrue($rule->validate('Something')); 00051 $this->assertTrue($rule->validate("Something\nmore")); 00052 $this->assertTrue($rule->validate("\nmore")); 00053 $this->assertTrue($rule->validate(" more ")); 00054 $this->assertTrue($rule->validate("0")); 00055 $this->assertTrue($rule->validate(0)); 00056 $this->assertTrue($rule->validate(true)); 00057 $this->assertTrue($rule->validate(' ')); 00058 $this->assertTrue($rule->validate(' ')); 00059 $this->assertTrue($rule->validate("\t")); 00060 $this->assertTrue($rule->validate("\n")); 00061 $this->assertTrue($rule->validate("\r")); 00062 $this->assertTrue($rule->validate("\r\n")); 00063 $this->assertTrue($rule->validate(" \t \n \r ")); 00064 $this->assertTrue($rule->validate('<p></p>')); 00065 $this->assertTrue($rule->validate('<p> </p>')); 00066 $this->assertTrue($rule->validate('<p>x</p>')); 00067 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />')); 00068 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>')); 00069 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>')); 00070 $this->assertTrue($rule->validate('<hr />')); 00071 $this->assertTrue($rule->validate('<hr/>')); 00072 $this->assertTrue($rule->validate('<hr>')); 00073 $this->assertTrue($rule->validate('<hr></hr>')); 00074 $this->assertTrue($rule->validate('<br />')); 00075 $this->assertTrue($rule->validate('<br/>')); 00076 $this->assertTrue($rule->validate('<br>')); 00077 $this->assertTrue($rule->validate(' ')); 00078 // Fails 00079 $this->assertFalse($rule->validate('')); 00080 $this->assertFalse($rule->validate(false)); 00081 $this->assertFalse($rule->validate(null)); 00082 00083 // Now run the same tests with it on to make sure things work as expected 00084 $CFG->strictformsrequired = true; 00085 // Passes 00086 $this->assertTrue($rule->validate('Something')); 00087 $this->assertTrue($rule->validate("Something\nmore")); 00088 $this->assertTrue($rule->validate("\nmore")); 00089 $this->assertTrue($rule->validate(" more ")); 00090 $this->assertTrue($rule->validate("0")); 00091 $this->assertTrue($rule->validate(0)); 00092 $this->assertTrue($rule->validate(true)); 00093 $this->assertTrue($rule->validate('<p>x</p>')); 00094 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />')); 00095 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>')); 00096 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>')); 00097 $this->assertTrue($rule->validate('<hr />')); 00098 $this->assertTrue($rule->validate('<hr/>')); 00099 $this->assertTrue($rule->validate('<hr>')); 00100 $this->assertTrue($rule->validate('<hr></hr>')); 00101 // Fails 00102 $this->assertFalse($rule->validate(' ')); 00103 $this->assertFalse($rule->validate(' ')); 00104 $this->assertFalse($rule->validate("\t")); 00105 $this->assertFalse($rule->validate("\n")); 00106 $this->assertFalse($rule->validate("\r")); 00107 $this->assertFalse($rule->validate("\r\n")); 00108 $this->assertFalse($rule->validate(" \t \n \r ")); 00109 $this->assertFalse($rule->validate('<p></p>')); 00110 $this->assertFalse($rule->validate('<p> </p>')); 00111 $this->assertFalse($rule->validate('<br />')); 00112 $this->assertFalse($rule->validate('<br/>')); 00113 $this->assertFalse($rule->validate('<br>')); 00114 $this->assertFalse($rule->validate(' ')); 00115 $this->assertFalse($rule->validate('')); 00116 $this->assertFalse($rule->validate(false)); 00117 $this->assertFalse($rule->validate(null)); 00118 00119 $CFG->strictformsrequired = $strictformsrequired; 00120 } 00121 00122 public function test_generate_id_select() { 00123 $el = new MoodleQuickForm_select('choose_one', 'Choose one', 00124 array(1 => 'One', '2' => 'Two')); 00125 $el->_generateId(); 00126 $this->assertEqual('id_choose_one', $el->getAttribute('id')); 00127 } 00128 00129 public function test_generate_id_like_repeat() { 00130 $el = new MoodleQuickForm_text('text[7]', 'Type something'); 00131 $el->_generateId(); 00132 $this->assertEqual('id_text_7', $el->getAttribute('id')); 00133 } 00134 00135 public function test_can_manually_set_id() { 00136 $el = new MoodleQuickForm_text('elementname', 'Type something', 00137 array('id' => 'customelementid')); 00138 $el->_generateId(); 00139 $this->assertEqual('customelementid', $el->getAttribute('id')); 00140 } 00141 00142 public function test_generate_id_radio() { 00143 $el = new MoodleQuickForm_radio('radio', 'Label', 'Choice label', 'choice_value'); 00144 $el->_generateId(); 00145 $this->assertEqual('id_radio_choice_value', $el->getAttribute('id')); 00146 } 00147 00148 public function test_radio_can_manually_set_id() { 00149 $el = new MoodleQuickForm_radio('radio2', 'Label', 'Choice label', 'choice_value', 00150 array('id' => 'customelementid2')); 00151 $el->_generateId(); 00152 $this->assertEqual('customelementid2', $el->getAttribute('id')); 00153 } 00154 00155 public function test_generate_id_radio_like_repeat() { 00156 $el = new MoodleQuickForm_radio('repeatradio[2]', 'Label', 'Choice label', 'val'); 00157 $el->_generateId(); 00158 $this->assertEqual('id_repeatradio_2_val', $el->getAttribute('id')); 00159 } 00160 00161 public function test_rendering() { 00162 $form = new formslib_test_form(); 00163 ob_start(); 00164 $form->display(); 00165 $html = ob_get_clean(); 00166 00167 $this->assert(new ContainsTagWithAttributes('select', array( 00168 'id' => 'id_choose_one', 'name' => 'choose_one')), $html); 00169 00170 $this->assert(new ContainsTagWithAttributes('input', array( 00171 'type' => 'text', 'id' => 'id_text_0', 'name' => 'text[0]')), $html); 00172 00173 $this->assert(new ContainsTagWithAttributes('input', array( 00174 'type' => 'text', 'id' => 'id_text_1', 'name' => 'text[1]')), $html); 00175 00176 $this->assert(new ContainsTagWithAttributes('input', array( 00177 'type' => 'radio', 'id' => 'id_radio_choice_value', 00178 'name' => 'radio', 'value' => 'choice_value')), $html); 00179 00180 $this->assert(new ContainsTagWithAttributes('input', array( 00181 'type' => 'radio', 'id' => 'customelementid2', 'name' => 'radio2')), $html); 00182 00183 $this->assert(new ContainsTagWithAttributes('input', array( 00184 'type' => 'radio', 'id' => 'id_repeatradio_0_2', 00185 'name' => 'repeatradio[0]', 'value' => '2')), $html); 00186 00187 $this->assert(new ContainsTagWithAttributes('input', array( 00188 'type' => 'radio', 'id' => 'id_repeatradio_2_1', 00189 'name' => 'repeatradio[2]', 'value' => '1')), $html); 00190 00191 $this->assert(new ContainsTagWithAttributes('input', array( 00192 'type' => 'radio', 'id' => 'id_repeatradio_2_2', 00193 'name' => 'repeatradio[2]', 'value' => '2')), $html); 00194 } 00195 } 00196 00197 00201 class formslib_test_form extends moodleform { 00202 public function definition() { 00203 $this->_form->addElement('select', 'choose_one', 'Choose one', 00204 array(1 => 'One', '2' => 'Two')); 00205 00206 $repeatels = array( 00207 $this->_form->createElement('text', 'text', 'Type something') 00208 ); 00209 $this->repeat_elements($repeatels, 2, array(), 'numtexts', 'addtexts'); 00210 00211 $this->_form->addElement('radio', 'radio', 'Label', 'Choice label', 'choice_value'); 00212 00213 $this->_form->addElement('radio', 'radio2', 'Label', 'Choice label', 'choice_value', 00214 array('id' => 'customelementid2')); 00215 00216 $repeatels = array( 00217 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'One', 1), 00218 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'Two', 2), 00219 ); 00220 $this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios'); 00221 } 00222 }