PageRenderTime 82ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/helper/FormHelperTest.php

https://github.com/theodo/symfony1.0-backports
PHP | 305 lines | 209 code | 55 blank | 41 comment | 1 complexity | a5258ddfaeac20c35ce07e29becb545b MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
  10. sfLoader::loadHelpers(array('Helper', 'Asset', 'Url', 'Tag', 'Form'));
  11. class myController
  12. {
  13. public function genUrl($parameters = array(), $absolute = false)
  14. {
  15. return 'module/action';
  16. }
  17. }
  18. class myUser
  19. {
  20. public function getCulture()
  21. {
  22. return 'en';
  23. }
  24. }
  25. class myRequest
  26. {
  27. public function getRelativeUrlRoot()
  28. {
  29. return '';
  30. }
  31. }
  32. class myResponse
  33. {
  34. public function addJavascript()
  35. {
  36. }
  37. }
  38. class sfContext
  39. {
  40. public $controller = null;
  41. public $user = null;
  42. public $request = null;
  43. public $response = null;
  44. static public $instance = null;
  45. public static function getInstance()
  46. {
  47. if (!isset(self::$instance))
  48. {
  49. self::$instance = new sfContext();
  50. }
  51. return self::$instance;
  52. }
  53. public function getController()
  54. {
  55. return $this->controller;
  56. }
  57. public function getUser()
  58. {
  59. return $this->user;
  60. }
  61. public function getRequest()
  62. {
  63. return $this->request;
  64. }
  65. public function getResponse()
  66. {
  67. return $this->response;
  68. }
  69. public function getModuleName()
  70. {
  71. return 'module';
  72. }
  73. public function getActionName()
  74. {
  75. return 'action';
  76. }
  77. }
  78. $t = new lime_test(88, new lime_output_color());
  79. $context = sfContext::getInstance();
  80. $context->controller = new myController();
  81. $context->user = new myUser();
  82. $context->request = new myRequest();
  83. $context->response = new myResponse();
  84. // options_for_select()
  85. $t->diag('options_for_select()');
  86. $t->is(options_for_select(array('item1', 'item2', 'item3')), "<option value=\"0\">item1</option>\n<option value=\"1\">item2</option>\n<option value=\"2\">item3</option>\n", 'options_for_select() takes an array of options as its first argument');
  87. $t->is(options_for_select(array(1 => 'item1', 2 => 'item2', 'foo' => 'item3')), "<option value=\"1\">item1</option>\n<option value=\"2\">item2</option>\n<option value=\"foo\">item3</option>\n", 'options_for_select() takes an array of options as its first argument');
  88. $t->is(options_for_select(array('item1', 'item2', 'item3'), '0'), "<option value=\"0\" selected=\"selected\">item1</option>\n<option value=\"1\">item2</option>\n<option value=\"2\">item3</option>\n", 'options_for_select() takes the selected index as its second argument');
  89. $t->is(options_for_select(array('item1', 'item2', 'item3'), '2'), "<option value=\"0\">item1</option>\n<option value=\"1\">item2</option>\n<option value=\"2\" selected=\"selected\">item3</option>\n", 'options_for_select() takes the selected index as its second argument');
  90. $t->is(options_for_select(array('item1', 'item2', 'item3'), array('1', '2')), "<option value=\"0\">item1</option>\n<option value=\"1\" selected=\"selected\">item2</option>\n<option value=\"2\" selected=\"selected\">item3</option>\n", 'options_for_select() takes the selected index as its second argument');
  91. $t->is(options_for_select(array('group1' => array('item1', 'item2'), 'bar' => 'item3')), "<optgroup label=\"group1\"><option value=\"0\">item1</option>\n<option value=\"1\">item2</option>\n</optgroup>\n<option value=\"bar\">item3</option>\n", 'options_for_select() can deal with optgroups');
  92. // unit testing for #3923
  93. $escaped = sfOutputEscaper::escape('htmlspecialchars', array('group1' => array('item1', 'item2'), 'bar' => 'item3'));
  94. $t->is(options_for_select($escaped), "<optgroup label=\"group1\"><option value=\"0\">item1</option>\n<option value=\"1\">item2</option>\n</optgroup>\n<option value=\"bar\">item3</option>\n", 'options_for_select() can deal with optgroups of escaped arrays');
  95. // options
  96. $t->is(options_for_select(array('item1'), '', array('include_custom' => 'test')), "<option value=\"\">test</option>\n<option value=\"0\">item1</option>\n", 'options_for_select() can take an "include_custom" option');
  97. $t->is(options_for_select(array('item1'), '', array('include_blank' => true)), "<option value=\"\"></option>\n<option value=\"0\">item1</option>\n", 'options_for_select() can take an "include_blank" option');
  98. // form_tag()
  99. $t->diag('form_tag()');
  100. $t->is(form_tag(), '<form method="post" action="module/action">', 'form_tag() creates a form tag');
  101. // options
  102. $t->is(form_tag('', array('class' => 'foo')), '<form class="foo" method="post" action="module/action">', 'form_tag() takes an array of attribute options');
  103. $t->is(form_tag('', array('method' => 'get')), '<form method="get" action="module/action">', 'form_tag() takes a "method" as an option');
  104. $t->is(form_tag('', array('multipart' => true)), '<form method="post" enctype="multipart/form-data" action="module/action">', 'form_tag() takes a "multipart" boolean option');
  105. // select_tag()
  106. $t->diag('select_tag()');
  107. $t->is(select_tag('name'), '<select name="name" id="name"></select>', 'select_tag() takes a name as its first argument');
  108. $option_for_select = options_for_select(array('item1'));
  109. $t->is(select_tag('name', $option_for_select), '<select name="name" id="name">'.$option_for_select.'</select>', 'select_tag() takes an HTML string of options as its second argument');
  110. $t->is(select_tag('name', array('item1')), '<select name="name" id="name">'.$option_for_select.'</select>', 'select_tag() takes an array of options as its second argument');
  111. // options
  112. $t->is(select_tag('name', $option_for_select, array('class' => 'foo')), '<select name="name" id="name" class="foo">'.$option_for_select.'</select>', 'select_tag() takes an array of attribute options as its third argument');
  113. $t->is(select_tag('name', $option_for_select, array('multiple' => true)), '<select name="name[]" id="name" multiple="multiple">'.$option_for_select.'</select>', 'select_tag() takes a "multiple" boolean option');
  114. $t->is(select_tag('name[]', $option_for_select, array('multiple' => true)), '<select name="name[]" id="name" multiple="multiple">'.$option_for_select.'</select>', 'select_tag() takes a "multiple" boolean option');
  115. $t->is(select_tag('name', $option_for_select, array('multiple' => false)), '<select name="name" id="name">'.$option_for_select.'</select>', 'select_tag() takes a "multiple" boolean option');
  116. $t->is(select_tag('name', $option_for_select, array('id' => 'bar')), '<select name="name" id="bar">'.$option_for_select.'</select>', 'select_tag() can take a "id" option');
  117. // select_country_tag()
  118. $t->diag('select_country_tag()');
  119. $t->like(select_country_tag('name'), '/'.preg_quote('<select name="name" id="name">').'/', 'select_country_tag() takes a name as its first argument');
  120. $t->cmp_ok(preg_match_all('/<option/', select_country_tag('name'), $matches), '>', 200, 'select_country_tag() takes a name as its first argument');
  121. $t->like(select_country_tag('name', 'FR'), '/'.preg_quote('<option value="FR" selected="selected">').'/', 'select_country_tag() takes an ISO code for the selected country as its second argument');
  122. // options
  123. $t->like(select_country_tag('name', null, array('class' => 'foo')), '/'.preg_quote('<select name="name" id="name" class="foo">').'/', 'select_country_tag() takes an array of options as its third argument');
  124. $t->is(preg_match_all('/<option/', select_country_tag('name', null, array('countries' => array('FR', 'GB'))), $matches), 2, 'select_country_tag() takes a "countries" option');
  125. // select_language_tag()
  126. $t->diag('select_language_tag()');
  127. $t->like(select_language_tag('name'), '/'.preg_quote('<select name="name" id="name">').'/', 'select_language_tag() takes a name as its first argument');
  128. $t->cmp_ok(preg_match_all('/<option/', select_language_tag('name'), $matches), '>', 200, 'select_language_tag() takes a name as its first argument');
  129. $t->like(select_language_tag('name', 'fr'), '/'.preg_quote('<option value="fr" selected="selected">').'/', 'select_language_tag() takes an ISO code for the selected language as its second argument');
  130. // option
  131. $t->like(select_language_tag('name', null, array('class' => 'foo')), '/'.preg_quote('<select name="name" id="name" class="foo">').'/', 'select_language_tag() takes an array of options as its third argument');
  132. $t->is(preg_match_all('/<option/', select_language_tag('name', null, array('languages' => array('fr', 'en'))), $matches), 2, 'select_language_tag() takes a "languages" option');
  133. // input_tag()
  134. $t->diag('input_tag()');
  135. $t->is(input_tag('name'), '<input type="text" name="name" id="name" value="" />', 'input_tag() takes a name as its first argument');
  136. $t->is(input_tag('name', 'foo'), '<input type="text" name="name" id="name" value="foo" />', 'input_tag() takes a value as its second argument');
  137. // options
  138. $t->is(input_tag('name', null, array('class' => 'foo')), '<input type="text" name="name" id="name" value="" class="foo" />', 'input_tag() takes an array of attribute options as its third argument');
  139. $t->is(input_tag('name', null, array('type' => 'password')), '<input type="password" name="name" id="name" value="" />', 'input_tag() can override the "type" attribute');
  140. $t->is(input_tag('name', null, array('id' => 'foo')), '<input type="text" name="name" id="foo" value="" />', 'input_tag() can override the "id" attribute');
  141. // input_hidden_tag()
  142. $t->diag('input_hidden_tag()');
  143. $t->is(input_hidden_tag('name'), '<input type="hidden" name="name" id="name" value="" />', 'input_hidden_tag() takes a name as its first argument');
  144. $t->is(input_hidden_tag('name', 'foo'), '<input type="hidden" name="name" id="name" value="foo" />', 'input_hidden_tag() takes a value as its second argument');
  145. $t->is(input_hidden_tag('name', null, array('class' => 'foo')), '<input type="hidden" name="name" id="name" value="" class="foo" />', 'input_hidden_tag() takes an array of attribute options as its third argument');
  146. // input_file_tag()
  147. $t->diag('input_file_tag()');
  148. $t->is(input_file_tag('name'), '<input type="file" name="name" id="name" value="" />', 'input_file_tag() takes a name as its first argument');
  149. $t->is(input_file_tag('name', array('class' => 'foo')), '<input type="file" name="name" id="name" value="" class="foo" />', 'input_hidden_tag() takes an array of attribute options as its second argument');
  150. // input_password_tag()
  151. $t->diag('input_password_tag()');
  152. $t->is(input_password_tag('name'), '<input type="password" name="name" id="name" value="" />', 'input_password_tag() takes a name as its first argument');
  153. $t->is(input_password_tag('name', 'foo'), '<input type="password" name="name" id="name" value="foo" />', 'input_password_tag() takes a value as its second argument');
  154. $t->is(input_password_tag('name', null, array('class' => 'foo')), '<input type="password" name="name" id="name" value="" class="foo" />', 'input_password_tag() takes an array of attribute options as its third argument');
  155. // textarea_tag()
  156. $t->diag('textarea_tag()');
  157. $t->is(textarea_tag('name'), '<textarea name="name" id="name"></textarea>', 'textarea_tag() takes a name as its first argument');
  158. $t->is(textarea_tag('name', 'content'), '<textarea name="name" id="name">content</textarea>', 'textarea_tag() takes a value as its second argument');
  159. $t->is(textarea_tag('name', '<p>foo</p>'), '<textarea name="name" id="name">&lt;p&gt;foo&lt;/p&gt;</textarea>', 'textarea_tag() escapes the content');
  160. $t->is(textarea_tag('name', '&lt;p&gt;foo&lt;/p&gt;'), '<textarea name="name" id="name">&lt;p&gt;foo&lt;/p&gt;</textarea>', 'textarea_tag() does not escape an already escaped content');
  161. // options
  162. $t->is(textarea_tag('name', null, array('class' => 'foo')), '<textarea name="name" id="name" class="foo"></textarea>', 'textarea_tag() takes an array of attribute options as its third argument');
  163. $t->is(textarea_tag('name', null, array('id' => 'foo')), '<textarea name="name" id="foo"></textarea>', 'textarea_tag() can override the "id" attribute');
  164. $t->is(textarea_tag('name', null, array('size' => '5x20')), '<textarea name="name" id="name" rows="20" cols="5"></textarea>', 'textarea_tag() can take a "size" attribute');
  165. require_once(sfConfig::get('sf_symfony_lib_dir').'/helper/sfRichTextEditor.class.php');
  166. require_once(sfConfig::get('sf_symfony_lib_dir').'/helper/sfRichTextEditorTinyMCE.class.php');
  167. sfConfig::set('sf_web_dir', dirname(__FILE__));
  168. sfConfig::set('sf_rich_text_js_dir', 'fixtures');
  169. $t->like(textarea_tag('name', 'content', array('rich' => 'TinyMCE')), '/tinyMCE\.init/', 'textarea_tag() can create a rich textarea tag based on tinyMCE');
  170. $t->like(textarea_tag('name', 'content', array('rich' => true)), '/tinyMCE\.init/', 'textarea_tag() can create a rich textarea tag based on tinyMCE');
  171. //regression for http://trac.symfony-project.com/ticket/3474
  172. $t->like(textarea_tag('name[]', 'content', array('rich' => true)), '/elements: "name",/', 'textarea_tag() can create a rich textarea tag based on tinyMCE using correct id');
  173. $t->like(textarea_tag('name[]', 'content', array('rich' => true, 'id' => 'test')), '/elements: "test",/', 'textarea_tag() can create a rich textarea tag based on tinyMCE using correct id');
  174. class sfRichTextEditorSample extends sfRichTextEditor
  175. {
  176. public function toHTML()
  177. {
  178. return 'fake editor';
  179. }
  180. }
  181. $t->like(textarea_tag('name', 'content', array('rich' => 'Sample')), '/fake editor/', 'textarea_tag() can create a rich textarea tag based on a custom editor');
  182. sfConfig::set('sf_rich_text_editor_class', 'Sample');
  183. $t->like(textarea_tag('name', 'content', array('rich' => true)), '/fake editor/', 'textarea_tag() can be configured to change the default editor by configuration');
  184. class sfRichTextEditorSampleBis
  185. {
  186. }
  187. try
  188. {
  189. textarea_tag('name', 'content', array('rich' => 'SampleBis'));
  190. $t->fail('textarea_tag() custom editor must extends sfRichTextEditor');
  191. }
  192. catch (sfConfigurationException $e)
  193. {
  194. $t->pass('textarea_tag() custom editor must extends sfRichTextEditor');
  195. }
  196. // checkbox_tag()
  197. $t->diag('checkbox_tag()');
  198. $t->is(checkbox_tag('name'), '<input type="checkbox" name="name" id="name" value="1" />', 'checkbox_tag() takes a name as its first argument');
  199. $t->is(checkbox_tag('name', 'foo'), '<input type="checkbox" name="name" id="name" value="foo" />', 'checkbox_tag() takes a value as its second argument');
  200. $t->is(checkbox_tag('name', null, true), '<input type="checkbox" name="name" id="name" value="" checked="checked" />', 'checkbox_tag() takes a boolean as its third argument');
  201. // options
  202. $t->is(checkbox_tag('name', null, false, array('class' => 'foo')), '<input type="checkbox" name="name" id="name" value="" class="foo" />', 'checkbox_tag() takes an array of attribute options as its fourth argument');
  203. $t->is(checkbox_tag('name', null, false, array('id' => 'foo')), '<input type="checkbox" name="name" id="foo" value="" />', 'checkbox_tag() can override the "id" attribute');
  204. // radiobutton_tag()
  205. $t->diag('radiobutton_tag()');
  206. $t->is(radiobutton_tag('name', 1), '<input type="radio" name="name" id="name_1" value="1" />', 'radiobutton_tag() takes a name as its first argument');
  207. $t->is(radiobutton_tag('name', 2), '<input type="radio" name="name" id="name_2" value="2" />', 'radiobutton_tag() takes a value as its second argument');
  208. $t->is(radiobutton_tag('name', null, true), '<input type="radio" name="name" id="name" value="" checked="checked" />', 'radiobutton_tag() takes a boolean as its third argument');
  209. // options
  210. $t->is(radiobutton_tag('name', null, false, array('class' => 'foo')), '<input type="radio" name="name" id="name" value="" class="foo" />', 'radiobutton_tag() takes an array of attribute options as its fourth argument');
  211. $t->is(radiobutton_tag('name', null, false, array('id' => 'foo')), '<input type="radio" name="name" id="foo" value="" />', 'radiobutton_tag() can override the "id" attribute');
  212. // input_date_range_tag()
  213. $t->diag('input_date_range_tag()');
  214. $t->unlike(input_date_range_tag('date', array('from' => time(), 'to' => time()), array('after' => 'foo')), '/after/', 'input_date_range_tag() output date fields for a date range');
  215. // input_date_tag()
  216. $t->diag('input_date_tag()');
  217. $t->todo('input_date_tag()');
  218. // submit_tag()
  219. $t->diag('submit_tag()');
  220. $t->is(submit_tag(), '<input type="submit" name="commit" value="Save changes" />', 'submit_tag() default value is "Save changes"');
  221. $t->is(submit_tag("save"), '<input type="submit" name="commit" value="save" />', 'submit_tag() takes a value as its first argument');
  222. // options
  223. $t->is(submit_tag('save', array('class' => 'foo')), '<input type="submit" name="commit" value="save" class="foo" />', 'submit_tag() takes an array of attribute options as its second argument');
  224. $t->is(submit_tag('save', array('name' => 'foo')), '<input type="submit" name="foo" value="save" />', 'submit_tag() can override the "name" attribute');
  225. // reset_tag()
  226. $t->diag('reset_tag()');
  227. $t->is(reset_tag(), '<input type="reset" name="reset" value="Reset" />', 'reset_tag() default value is "Reset"');
  228. $t->is(reset_tag("save"), '<input type="reset" name="reset" value="save" />', 'reset_tag() takes a value as its first argument');
  229. // options
  230. $t->is(reset_tag('save', array('class' => 'foo')), '<input type="reset" name="reset" value="save" class="foo" />', 'reset_tag() takes an array of attribute options as its second argument');
  231. $t->is(reset_tag('save', array('name' => 'foo')), '<input type="reset" name="foo" value="save" />', 'reset_tag() can override the "name" attribute');
  232. // submit_image_tag()
  233. $t->diag('submit_image_tag()');
  234. $t->is(submit_image_tag('submit'), '<input type="image" name="commit" src="/images/submit.png" alt="Submit" />', 'submit_image_tag() takes an image source as its first argument');
  235. $t->is(submit_image_tag('/img/submit.gif'), '<input type="image" name="commit" src="/img/submit.gif" alt="Submit" />', 'submit_image_tag() takes an image source as its first argument');
  236. // options
  237. $t->is(submit_image_tag('submit', array('class' => 'foo')), '<input type="image" name="commit" src="/images/submit.png" class="foo" alt="Submit" />', 'submit_image_tag() takes an array of attribute options as its second argument');
  238. $t->is(submit_image_tag('submit', array('alt' => 'foo')), '<input type="image" name="commit" src="/images/submit.png" alt="foo" />', 'reset_tag() can override the "alt" attribute');
  239. $t->is(submit_image_tag('submit', array('name' => 'foo')), '<input type="image" name="foo" src="/images/submit.png" alt="Submit" />', 'reset_tag() can override the "name" attribute');
  240. // label_for()
  241. $t->diag('label_for()');
  242. $t->todo('label_for()');
  243. // get_id_from_name()
  244. $t->diag('get_id_from_name()');
  245. $t->is(get_id_from_name('foo'), 'foo', 'get_id_from_name() returns the id if there is no [] in the id');
  246. $t->is(get_id_from_name('foo[]', 'name'), 'foo_name', 'get_id_from_name() removes all [] from ids');
  247. // _convert_options()
  248. $t->diag('_convert_options()');
  249. $t->is(_convert_options(array('class' => 'foo', 'multiple' => true)), array('class' => 'foo', 'multiple' => 'multiple'), '_convert_options() converts some options for XHTML compliance');
  250. $t->is(_convert_options(array('class' => 'foo', 'multiple' => false)), array('class' => 'foo'), '_convert_options() converts some options for XHTML compliance');