PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/symfony/lib/vendor/symfony/test/unit/helper/FormHelperTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 280 lines | 188 code | 49 blank | 43 comment | 0 complexity | b5eb8031f6c34ec0f1fd3229f65b7a7b MD5 | raw file
Possible License(s): CC-BY-SA-3.0, AGPL-3.0, BSD-3-Clause, AGPL-1.0, GPL-2.0, LGPL-2.1, LGPL-3.0
  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. require_once($_test_dir.'/unit/sfContextMock.class.php');
  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 ProjectConfiguration extends sfProjectConfiguration
  39. {
  40. }
  41. $configuration = new ProjectConfiguration(dirname(__FILE__).'/../../lib', new sfEventDispatcher());
  42. $t = new lime_test(96, new lime_output_color());
  43. $context = sfContext::getInstance(array(
  44. 'controller' => 'myController',
  45. 'user' => 'myUser',
  46. 'request' => 'myRequest',
  47. 'response' => 'myResponse',
  48. ));
  49. require_once(dirname(__FILE__).'/../../../lib/helper/HelperHelper.php');
  50. require_once(dirname(__FILE__).'/../../../lib/helper/UrlHelper.php');
  51. require_once(dirname(__FILE__).'/../../../lib/helper/TagHelper.php');
  52. require_once(dirname(__FILE__).'/../../../lib/helper/FormHelper.php');
  53. require_once(dirname(__FILE__).'/../../../lib/helper/AssetHelper.php');
  54. // options_for_select()
  55. $t->diag('options_for_select()');
  56. $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');
  57. $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');
  58. $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');
  59. $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');
  60. $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');
  61. $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');
  62. // unit testing for #3923
  63. $escaped = sfOutputEscaper::escape('htmlspecialchars', array('group1' => array('item1', 'item2'), 'bar' => 'item3'));
  64. $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');
  65. // options
  66. $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');
  67. $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');
  68. // form_tag()
  69. $t->diag('form_tag()');
  70. $t->is(form_tag(), '<form method="post" action="module/action">', 'form_tag() creates a form tag');
  71. // options
  72. $t->is(form_tag('', array('class' => 'foo')), '<form class="foo" method="post" action="module/action">', 'form_tag() takes an array of attribute options');
  73. $t->is(form_tag('', array('method' => 'get')), '<form method="get" action="module/action">', 'form_tag() takes a "method" as an option');
  74. $t->is(form_tag('', array('multipart' => true)), '<form method="post" enctype="multipart/form-data" action="module/action">', 'form_tag() takes a "multipart" boolean option');
  75. $t->is(form_tag('', array('method' => 'put')), '<form method="post" action="module/action"><input type="hidden" name="sf_method" value="put" />', 'form_tag() creates a hidden field for methods different from GET or POST');
  76. // select_tag()
  77. $t->diag('select_tag()');
  78. $t->is(select_tag('name'), '<select name="name" id="name"></select>', 'select_tag() takes a name as its first argument');
  79. $option_for_select = options_for_select(array('item1'));
  80. $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');
  81. $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');
  82. // options
  83. $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');
  84. $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');
  85. $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');
  86. $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');
  87. $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');
  88. // select_country_tag()
  89. $t->diag('select_country_tag()');
  90. $t->like(select_country_tag('name'), '/'.preg_quote('<select name="name" id="name">').'/', 'select_country_tag() takes a name as its first argument');
  91. $t->cmp_ok(preg_match_all('/<option/', select_country_tag('name'), $matches), '>', 200, 'select_country_tag() takes a name as its first argument');
  92. $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');
  93. // options
  94. $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');
  95. $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');
  96. // select_language_tag()
  97. $t->diag('select_language_tag()');
  98. $t->like(select_language_tag('name'), '/'.preg_quote('<select name="name" id="name">').'/', 'select_language_tag() takes a name as its first argument');
  99. $t->cmp_ok(preg_match_all('/<option/', select_language_tag('name'), $matches), '>', 200, 'select_language_tag() takes a name as its first argument');
  100. $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');
  101. // option
  102. $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');
  103. $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');
  104. // select_currency_tag()
  105. $t->diag('select_currency_tag()');
  106. $t->like(select_currency_tag('name'), '/'.preg_quote('<select name="name" id="name">').'/', 'select_currency_tag() takes a name as its first argument');
  107. $t->cmp_ok(preg_match_all('/<option/', select_currency_tag('name'), $matches), '>', 200, 'select_currency_tag() takes a name as its first argument');
  108. $t->like(select_currency_tag('name', 'EUR'), '/'.preg_quote('<option value="EUR" selected="selected">').'/', 'select_currency_tag() takes the selected currency as its second argument');
  109. // option
  110. $t->like(select_currency_tag('name', null, array('class' => 'foo')), '/'.preg_quote('<select name="name" id="name" class="foo">').'/', 'select_currency_tag() takes an array of options as its third argument');
  111. $t->is(preg_match_all('/<option/', select_currency_tag('name', null, array('currencies' => array('EUR', 'USD'))), $matches), 2, 'select_currency_tag() takes a "currencies" option');
  112. $t->like(select_currency_tag('name', 'USD', array('currencies' => array('USD'), 'display' => 'symbol')), '/'.preg_quote('<option value="USD" selected="selected">US$<').'/' , 'select_currency_tag() takes a "display" option');
  113. $t->like(select_currency_tag('name', 'USD', array('currencies' => array('USD'), 'display' => 'code')), '/'.preg_quote('<option value="USD" selected="selected">USD<').'/' , 'select_currency_tag() takes a "display" option');
  114. // input_tag()
  115. $t->diag('input_tag()');
  116. $t->is(input_tag('name'), '<input type="text" name="name" id="name" value="" />', 'input_tag() takes a name as its first argument');
  117. $t->is(input_tag('name', 'foo'), '<input type="text" name="name" id="name" value="foo" />', 'input_tag() takes a value as its second argument');
  118. // options
  119. $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');
  120. $t->is(input_tag('name', null, array('type' => 'password')), '<input type="password" name="name" id="name" value="" />', 'input_tag() can override the "type" attribute');
  121. $t->is(input_tag('name', null, array('id' => 'foo')), '<input type="text" name="name" id="foo" value="" />', 'input_tag() can override the "id" attribute');
  122. // input_hidden_tag()
  123. $t->diag('input_hidden_tag()');
  124. $t->is(input_hidden_tag('name'), '<input type="hidden" name="name" id="name" value="" />', 'input_hidden_tag() takes a name as its first argument');
  125. $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');
  126. $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');
  127. // input_file_tag()
  128. $t->diag('input_file_tag()');
  129. $t->is(input_file_tag('name'), '<input type="file" name="name" id="name" value="" />', 'input_file_tag() takes a name as its first argument');
  130. $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');
  131. // input_password_tag()
  132. $t->diag('input_password_tag()');
  133. $t->is(input_password_tag('name'), '<input type="password" name="name" id="name" value="" />', 'input_password_tag() takes a name as its first argument');
  134. $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');
  135. $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');
  136. // textarea_tag()
  137. $t->diag('textarea_tag()');
  138. $t->is(textarea_tag('name'), '<textarea name="name" id="name"></textarea>', 'textarea_tag() takes a name as its first argument');
  139. $t->is(textarea_tag('name', 'content'), '<textarea name="name" id="name">content</textarea>', 'textarea_tag() takes a value as its second argument');
  140. $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');
  141. $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');
  142. // options
  143. $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');
  144. $t->is(textarea_tag('name', null, array('id' => 'foo')), '<textarea name="name" id="foo"></textarea>', 'textarea_tag() can override the "id" attribute');
  145. $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');
  146. require_once(sfConfig::get('sf_symfony_lib_dir').'/helper/sfRichTextEditor.class.php');
  147. require_once(sfConfig::get('sf_symfony_lib_dir').'/helper/sfRichTextEditorTinyMCE.class.php');
  148. sfConfig::set('sf_web_dir', dirname(__FILE__));
  149. sfConfig::set('sf_rich_text_js_dir', 'fixtures');
  150. $t->like(textarea_tag('name', 'content', array('rich' => 'TinyMCE')), '/tinyMCE\.init/', 'textarea_tag() can create a rich textarea tag based on tinyMCE');
  151. $t->like(textarea_tag('name', 'content', array('rich' => true)), '/tinyMCE\.init/', 'textarea_tag() can create a rich textarea tag based on tinyMCE');
  152. //regression for http://trac.symfony-project.com/ticket/3474
  153. $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');
  154. $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');
  155. class sfRichTextEditorSample extends sfRichTextEditor
  156. {
  157. public function toHTML()
  158. {
  159. return 'fake editor';
  160. }
  161. }
  162. $t->like(textarea_tag('name', 'content', array('rich' => 'Sample')), '/fake editor/', 'textarea_tag() can create a rich textarea tag based on a custom editor');
  163. sfConfig::set('sf_rich_text_editor_class', 'Sample');
  164. $t->like(textarea_tag('name', 'content', array('rich' => true)), '/fake editor/', 'textarea_tag() can be configured to change the default editor by configuration');
  165. class sfRichTextEditorSampleBis
  166. {
  167. }
  168. try
  169. {
  170. textarea_tag('name', 'content', array('rich' => 'SampleBis'));
  171. $t->fail('textarea_tag() custom editor must extends sfRichTextEditor');
  172. }
  173. catch (sfConfigurationException $e)
  174. {
  175. $t->pass('textarea_tag() custom editor must extends sfRichTextEditor');
  176. }
  177. // checkbox_tag()
  178. $t->diag('checkbox_tag()');
  179. $t->is(checkbox_tag('name'), '<input type="checkbox" name="name" id="name" value="1" />', 'checkbox_tag() takes a name as its first argument');
  180. $t->is(checkbox_tag('name', 'foo'), '<input type="checkbox" name="name" id="name" value="foo" />', 'checkbox_tag() takes a value as its second argument');
  181. $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');
  182. // options
  183. $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');
  184. $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');
  185. // radiobutton_tag()
  186. $t->diag('radiobutton_tag()');
  187. $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');
  188. $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');
  189. $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');
  190. // options
  191. $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');
  192. $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');
  193. // input_date_range_tag()
  194. $t->diag('input_date_range_tag()');
  195. $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');
  196. // input_date_tag()
  197. $t->diag('input_date_tag()');
  198. $t->todo('input_date_tag()');
  199. // submit_tag()
  200. $t->diag('submit_tag()');
  201. $t->is(submit_tag(), '<input type="submit" name="commit" value="Save changes" />', 'submit_tag() default value is "Save changes"');
  202. $t->is(submit_tag("save"), '<input type="submit" name="commit" value="save" />', 'submit_tag() takes a value as its first argument');
  203. // options
  204. $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');
  205. $t->is(submit_tag('save', array('name' => 'foo')), '<input type="submit" name="foo" value="save" />', 'submit_tag() can override the "name" attribute');
  206. // reset_tag()
  207. $t->diag('reset_tag()');
  208. $t->is(reset_tag(), '<input type="reset" name="reset" value="Reset" />', 'reset_tag() default value is "Reset"');
  209. $t->is(reset_tag("save"), '<input type="reset" name="reset" value="save" />', 'reset_tag() takes a value as its first argument');
  210. // options
  211. $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');
  212. $t->is(reset_tag('save', array('name' => 'foo')), '<input type="reset" name="foo" value="save" />', 'reset_tag() can override the "name" attribute');
  213. // submit_image_tag()
  214. $t->diag('submit_image_tag()');
  215. $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');
  216. $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');
  217. // options
  218. $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');
  219. $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');
  220. $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');
  221. // label_for()
  222. $t->diag('label_for()');
  223. $t->todo('label_for()');
  224. // get_id_from_name()
  225. $t->diag('get_id_from_name()');
  226. $t->is(get_id_from_name('foo'), 'foo', 'get_id_from_name() returns the id if there is no [] in the id');
  227. $t->is(get_id_from_name('foo[]', 'name'), 'foo_name', 'get_id_from_name() removes all [] from ids');
  228. // _convert_options()
  229. $t->diag('_convert_options()');
  230. $t->is(_convert_options(array('class' => 'foo', 'multiple' => true)), array('class' => 'foo', 'multiple' => 'multiple'), '_convert_options() converts some options for XHTML compliance');
  231. $t->is(_convert_options(array('class' => 'foo', 'multiple' => false)), array('class' => 'foo'), '_convert_options() converts some options for XHTML compliance');