PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/simpletest/testformslib.php

http://github.com/moodle/moodle
PHP | 222 lines | 160 code | 32 blank | 30 comment | 2 complexity | 309deb53193fde56dae034d786d1d2fc MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Unit tests for /lib/formslib.php.
  18. *
  19. * @package file
  20. * @copyright 2011 Sam Hemelryk
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. if (!defined('MOODLE_INTERNAL')) {
  24. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  25. }
  26. require_once($CFG->libdir . '/formslib.php');
  27. require_once($CFG->libdir . '/form/radio.php');
  28. require_once($CFG->libdir . '/form/select.php');
  29. require_once($CFG->libdir . '/form/text.php');
  30. class formslib_test extends UnitTestCase {
  31. public function test_require_rule() {
  32. global $CFG;
  33. $strictformsrequired = false;
  34. if (!empty($CFG->strictformsrequired)) {
  35. $strictformsrequired = $CFG->strictformsrequired;
  36. }
  37. $rule = new MoodleQuickForm_Rule_Required();
  38. // First run the tests with strictformsrequired off
  39. $CFG->strictformsrequired = false;
  40. // Passes
  41. $this->assertTrue($rule->validate('Something'));
  42. $this->assertTrue($rule->validate("Something\nmore"));
  43. $this->assertTrue($rule->validate("\nmore"));
  44. $this->assertTrue($rule->validate(" more "));
  45. $this->assertTrue($rule->validate("0"));
  46. $this->assertTrue($rule->validate(0));
  47. $this->assertTrue($rule->validate(true));
  48. $this->assertTrue($rule->validate(' '));
  49. $this->assertTrue($rule->validate(' '));
  50. $this->assertTrue($rule->validate("\t"));
  51. $this->assertTrue($rule->validate("\n"));
  52. $this->assertTrue($rule->validate("\r"));
  53. $this->assertTrue($rule->validate("\r\n"));
  54. $this->assertTrue($rule->validate(" \t \n \r "));
  55. $this->assertTrue($rule->validate('<p></p>'));
  56. $this->assertTrue($rule->validate('<p> </p>'));
  57. $this->assertTrue($rule->validate('<p>x</p>'));
  58. $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
  59. $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
  60. $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
  61. $this->assertTrue($rule->validate('<hr />'));
  62. $this->assertTrue($rule->validate('<hr/>'));
  63. $this->assertTrue($rule->validate('<hr>'));
  64. $this->assertTrue($rule->validate('<hr></hr>'));
  65. $this->assertTrue($rule->validate('<br />'));
  66. $this->assertTrue($rule->validate('<br/>'));
  67. $this->assertTrue($rule->validate('<br>'));
  68. $this->assertTrue($rule->validate('&nbsp;'));
  69. // Fails
  70. $this->assertFalse($rule->validate(''));
  71. $this->assertFalse($rule->validate(false));
  72. $this->assertFalse($rule->validate(null));
  73. // Now run the same tests with it on to make sure things work as expected
  74. $CFG->strictformsrequired = true;
  75. // Passes
  76. $this->assertTrue($rule->validate('Something'));
  77. $this->assertTrue($rule->validate("Something\nmore"));
  78. $this->assertTrue($rule->validate("\nmore"));
  79. $this->assertTrue($rule->validate(" more "));
  80. $this->assertTrue($rule->validate("0"));
  81. $this->assertTrue($rule->validate(0));
  82. $this->assertTrue($rule->validate(true));
  83. $this->assertTrue($rule->validate('<p>x</p>'));
  84. $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
  85. $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
  86. $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
  87. $this->assertTrue($rule->validate('<hr />'));
  88. $this->assertTrue($rule->validate('<hr/>'));
  89. $this->assertTrue($rule->validate('<hr>'));
  90. $this->assertTrue($rule->validate('<hr></hr>'));
  91. // Fails
  92. $this->assertFalse($rule->validate(' '));
  93. $this->assertFalse($rule->validate(' '));
  94. $this->assertFalse($rule->validate("\t"));
  95. $this->assertFalse($rule->validate("\n"));
  96. $this->assertFalse($rule->validate("\r"));
  97. $this->assertFalse($rule->validate("\r\n"));
  98. $this->assertFalse($rule->validate(" \t \n \r "));
  99. $this->assertFalse($rule->validate('<p></p>'));
  100. $this->assertFalse($rule->validate('<p> </p>'));
  101. $this->assertFalse($rule->validate('<br />'));
  102. $this->assertFalse($rule->validate('<br/>'));
  103. $this->assertFalse($rule->validate('<br>'));
  104. $this->assertFalse($rule->validate('&nbsp;'));
  105. $this->assertFalse($rule->validate(''));
  106. $this->assertFalse($rule->validate(false));
  107. $this->assertFalse($rule->validate(null));
  108. $CFG->strictformsrequired = $strictformsrequired;
  109. }
  110. public function test_generate_id_select() {
  111. $el = new MoodleQuickForm_select('choose_one', 'Choose one',
  112. array(1 => 'One', '2' => 'Two'));
  113. $el->_generateId();
  114. $this->assertEqual('id_choose_one', $el->getAttribute('id'));
  115. }
  116. public function test_generate_id_like_repeat() {
  117. $el = new MoodleQuickForm_text('text[7]', 'Type something');
  118. $el->_generateId();
  119. $this->assertEqual('id_text_7', $el->getAttribute('id'));
  120. }
  121. public function test_can_manually_set_id() {
  122. $el = new MoodleQuickForm_text('elementname', 'Type something',
  123. array('id' => 'customelementid'));
  124. $el->_generateId();
  125. $this->assertEqual('customelementid', $el->getAttribute('id'));
  126. }
  127. public function test_generate_id_radio() {
  128. $el = new MoodleQuickForm_radio('radio', 'Label', 'Choice label', 'choice_value');
  129. $el->_generateId();
  130. $this->assertEqual('id_radio_choice_value', $el->getAttribute('id'));
  131. }
  132. public function test_radio_can_manually_set_id() {
  133. $el = new MoodleQuickForm_radio('radio2', 'Label', 'Choice label', 'choice_value',
  134. array('id' => 'customelementid2'));
  135. $el->_generateId();
  136. $this->assertEqual('customelementid2', $el->getAttribute('id'));
  137. }
  138. public function test_generate_id_radio_like_repeat() {
  139. $el = new MoodleQuickForm_radio('repeatradio[2]', 'Label', 'Choice label', 'val');
  140. $el->_generateId();
  141. $this->assertEqual('id_repeatradio_2_val', $el->getAttribute('id'));
  142. }
  143. public function test_rendering() {
  144. $form = new formslib_test_form();
  145. ob_start();
  146. $form->display();
  147. $html = ob_get_clean();
  148. $this->assert(new ContainsTagWithAttributes('select', array(
  149. 'id' => 'id_choose_one', 'name' => 'choose_one')), $html);
  150. $this->assert(new ContainsTagWithAttributes('input', array(
  151. 'type' => 'text', 'id' => 'id_text_0', 'name' => 'text[0]')), $html);
  152. $this->assert(new ContainsTagWithAttributes('input', array(
  153. 'type' => 'text', 'id' => 'id_text_1', 'name' => 'text[1]')), $html);
  154. $this->assert(new ContainsTagWithAttributes('input', array(
  155. 'type' => 'radio', 'id' => 'id_radio_choice_value',
  156. 'name' => 'radio', 'value' => 'choice_value')), $html);
  157. $this->assert(new ContainsTagWithAttributes('input', array(
  158. 'type' => 'radio', 'id' => 'customelementid2', 'name' => 'radio2')), $html);
  159. $this->assert(new ContainsTagWithAttributes('input', array(
  160. 'type' => 'radio', 'id' => 'id_repeatradio_0_2',
  161. 'name' => 'repeatradio[0]', 'value' => '2')), $html);
  162. $this->assert(new ContainsTagWithAttributes('input', array(
  163. 'type' => 'radio', 'id' => 'id_repeatradio_2_1',
  164. 'name' => 'repeatradio[2]', 'value' => '1')), $html);
  165. $this->assert(new ContainsTagWithAttributes('input', array(
  166. 'type' => 'radio', 'id' => 'id_repeatradio_2_2',
  167. 'name' => 'repeatradio[2]', 'value' => '2')), $html);
  168. }
  169. }
  170. /**
  171. * Test form to be used by {@link formslib_test::test_rendering()}.
  172. */
  173. class formslib_test_form extends moodleform {
  174. public function definition() {
  175. $this->_form->addElement('select', 'choose_one', 'Choose one',
  176. array(1 => 'One', '2' => 'Two'));
  177. $repeatels = array(
  178. $this->_form->createElement('text', 'text', 'Type something')
  179. );
  180. $this->repeat_elements($repeatels, 2, array(), 'numtexts', 'addtexts');
  181. $this->_form->addElement('radio', 'radio', 'Label', 'Choice label', 'choice_value');
  182. $this->_form->addElement('radio', 'radio2', 'Label', 'Choice label', 'choice_value',
  183. array('id' => 'customelementid2'));
  184. $repeatels = array(
  185. $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'One', 1),
  186. $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'Two', 2),
  187. );
  188. $this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios');
  189. }
  190. }