FormTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Tests Kohana Form helper
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.form
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. * @author Kohana Team
  12. * @author Jeremy Bush <[email protected]>
  13. * @copyright (c) 2008-2012 Kohana Team
  14. * @license http://kohanaframework.org/license
  15. */
  16. class Kohana_FormTest extends Unittest_TestCase
  17. {
  18. /**
  19. * Defaults for this test
  20. * @var array
  21. */
  22. // @codingStandardsIgnoreStart
  23. protected $environmentDefault = array(
  24. 'Kohana::$base_url' => '/',
  25. 'HTTP_HOST' => 'kohanaframework.org',
  26. 'Kohana::$index_file' => '',
  27. );
  28. // @codingStandardsIgnoreEnd
  29. /**
  30. * Provides test data for test_open()
  31. *
  32. * @return array
  33. */
  34. public function provider_open()
  35. {
  36. return array(
  37. array(
  38. array('', NULL),
  39. array('action' => '')
  40. ),
  41. array(
  42. array(NULL, NULL),
  43. array('action' => '')
  44. ),
  45. array(
  46. array('foo', NULL),
  47. array('action' => '/foo')
  48. ),
  49. array(
  50. array('foo', array('method' => 'get')),
  51. array('action' => '/foo', 'method' => 'get')
  52. ),
  53. );
  54. }
  55. /**
  56. * Tests Form::open()
  57. *
  58. * @test
  59. * @dataProvider provider_open
  60. * @param boolean $input Input for Form::open
  61. * @param boolean $expected Output for Form::open
  62. */
  63. public function test_open($input, $expected)
  64. {
  65. list($action, $attributes) = $input;
  66. $tag = Form::open($action, $attributes);
  67. $matcher = array(
  68. 'tag' => 'form',
  69. // Default attributes
  70. 'attributes' => array(
  71. 'method' => 'post',
  72. 'accept-charset' => 'utf-8',
  73. ),
  74. );
  75. $matcher['attributes'] = $expected + $matcher['attributes'];
  76. $this->assertTag($matcher, $tag);
  77. }
  78. /**
  79. * Tests Form::close()
  80. *
  81. * @test
  82. */
  83. public function test_close()
  84. {
  85. $this->assertSame('</form>', Form::close());
  86. }
  87. /**
  88. * Provides test data for test_input()
  89. *
  90. * @return array
  91. */
  92. public function provider_input()
  93. {
  94. return array(
  95. // $value, $result
  96. array('input', 'foo', 'bar', NULL, 'input'),
  97. array('input', 'foo', NULL, NULL, 'input'),
  98. array('hidden', 'foo', 'bar', NULL, 'hidden'),
  99. array('password', 'foo', 'bar', NULL, 'password'),
  100. );
  101. }
  102. /**
  103. * Tests Form::input()
  104. *
  105. * @test
  106. * @dataProvider provider_input
  107. * @param boolean $input Input for Form::input
  108. * @param boolean $expected Output for Form::input
  109. */
  110. public function test_input($type, $name, $value, $attributes)
  111. {
  112. $matcher = array(
  113. 'tag' => 'input',
  114. 'attributes' => array('name' => $name, 'type' => $type)
  115. );
  116. // Form::input creates a text input
  117. if ($type === 'input')
  118. {
  119. $matcher['attributes']['type'] = 'text';
  120. }
  121. // NULL just means no value
  122. if ($value !== NULL)
  123. {
  124. $matcher['attributes']['value'] = $value;
  125. }
  126. // Add on any attributes
  127. if (is_array($attributes))
  128. {
  129. $matcher['attributes'] = $attributes + $matcher['attributes'];
  130. }
  131. $tag = Form::$type($name, $value, $attributes);
  132. $this->assertTag($matcher, $tag, $tag);
  133. }
  134. /**
  135. * Provides test data for test_file()
  136. *
  137. * @return array
  138. */
  139. public function provider_file()
  140. {
  141. return array(
  142. // $value, $result
  143. array('foo', NULL, '<input type="file" name="foo" />'),
  144. );
  145. }
  146. /**
  147. * Tests Form::file()
  148. *
  149. * @test
  150. * @dataProvider provider_file
  151. * @param boolean $input Input for Form::file
  152. * @param boolean $expected Output for Form::file
  153. */
  154. public function test_file($name, $attributes, $expected)
  155. {
  156. $this->assertSame($expected, Form::file($name, $attributes));
  157. }
  158. /**
  159. * Provides test data for test_check()
  160. *
  161. * @return array
  162. */
  163. public function provider_check()
  164. {
  165. return array(
  166. // $value, $result
  167. array('checkbox', 'foo', NULL, FALSE, NULL),
  168. array('checkbox', 'foo', NULL, TRUE, NULL),
  169. array('checkbox', 'foo', 'bar', TRUE, NULL),
  170. array('radio', 'foo', NULL, FALSE, NULL),
  171. array('radio', 'foo', NULL, TRUE, NULL),
  172. array('radio', 'foo', 'bar', TRUE, NULL),
  173. );
  174. }
  175. /**
  176. * Tests Form::check()
  177. *
  178. * @test
  179. * @dataProvider provider_check
  180. * @param boolean $input Input for Form::check
  181. * @param boolean $expected Output for Form::check
  182. */
  183. public function test_check($type, $name, $value, $checked, $attributes)
  184. {
  185. $matcher = array('tag' => 'input', 'attributes' => array('name' => $name, 'type' => $type));
  186. if ($value !== NULL)
  187. {
  188. $matcher['attributes']['value'] = $value;
  189. }
  190. if (is_array($attributes))
  191. {
  192. $matcher['attributes'] = $attributes + $matcher['attributes'];
  193. }
  194. if ($checked === TRUE)
  195. {
  196. $matcher['attributes']['checked'] = 'checked';
  197. }
  198. $tag = Form::$type($name, $value, $checked, $attributes);
  199. $this->assertTag($matcher, $tag, $tag);
  200. }
  201. /**
  202. * Provides test data for test_text()
  203. *
  204. * @return array
  205. */
  206. public function provider_text()
  207. {
  208. return array(
  209. // $value, $result
  210. array('textarea', 'foo', 'bar', NULL),
  211. array('textarea', 'foo', 'bar', array('rows' => 20, 'cols' => 20)),
  212. array('button', 'foo', 'bar', NULL),
  213. array('label', 'foo', 'bar', NULL),
  214. array('label', 'foo', NULL, NULL),
  215. );
  216. }
  217. /**
  218. * Tests Form::textarea()
  219. *
  220. * @test
  221. * @dataProvider provider_text
  222. * @param boolean $input Input for Form::textarea
  223. * @param boolean $expected Output for Form::textarea
  224. */
  225. public function test_text($type, $name, $body, $attributes)
  226. {
  227. $matcher = array(
  228. 'tag' => $type,
  229. 'attributes' => array(),
  230. 'content' => $body,
  231. );
  232. if ($type !== 'label')
  233. {
  234. $matcher['attributes'] = array('name' => $name);
  235. }
  236. else
  237. {
  238. $matcher['attributes'] = array('for' => $name);
  239. }
  240. if (is_array($attributes))
  241. {
  242. $matcher['attributes'] = $attributes + $matcher['attributes'];
  243. }
  244. $tag = Form::$type($name, $body, $attributes);
  245. $this->assertTag($matcher, $tag, $tag);
  246. }
  247. /**
  248. * Provides test data for test_select()
  249. *
  250. * @return array
  251. */
  252. public function provider_select()
  253. {
  254. return array(
  255. // $value, $result
  256. array('foo', NULL, NULL, "<select name=\"foo\"></select>"),
  257. array('foo', array('bar' => 'bar'), NULL, "<select name=\"foo\">\n<option value=\"bar\">bar</option>\n</select>"),
  258. array('foo', array('bar' => 'bar'), 'bar', "<select name=\"foo\">\n<option value=\"bar\" selected=\"selected\">bar</option>\n</select>"),
  259. array('foo', array('bar' => array('foo' => 'bar')), NULL, "<select name=\"foo\">\n<optgroup label=\"bar\">\n<option value=\"foo\">bar</option>\n</optgroup>\n</select>"),
  260. array('foo', array('bar' => array('foo' => 'bar')), 'foo', "<select name=\"foo\">\n<optgroup label=\"bar\">\n<option value=\"foo\" selected=\"selected\">bar</option>\n</optgroup>\n</select>"),
  261. // #2286
  262. array('foo', array('bar' => 'bar', 'unit' => 'test', 'foo' => 'foo'), array('bar', 'foo'), "<select name=\"foo\" multiple=\"multiple\">\n<option value=\"bar\" selected=\"selected\">bar</option>\n<option value=\"unit\">test</option>\n<option value=\"foo\" selected=\"selected\">foo</option>\n</select>"),
  263. );
  264. }
  265. /**
  266. * Tests Form::select()
  267. *
  268. * @test
  269. * @dataProvider provider_select
  270. * @param boolean $input Input for Form::select
  271. * @param boolean $expected Output for Form::select
  272. */
  273. public function test_select($name, $options, $selected, $expected)
  274. {
  275. // Much more efficient just to assertSame() rather than assertTag() on each element
  276. $this->assertSame($expected, Form::select($name, $options, $selected));
  277. }
  278. /**
  279. * Provides test data for test_submit()
  280. *
  281. * @return array
  282. */
  283. public function provider_submit()
  284. {
  285. return array(
  286. // $value, $result
  287. array('foo', 'Foobar!', '<input type="submit" name="foo" value="Foobar!" />'),
  288. );
  289. }
  290. /**
  291. * Tests Form::submit()
  292. *
  293. * @test
  294. * @dataProvider provider_submit
  295. * @param boolean $input Input for Form::submit
  296. * @param boolean $expected Output for Form::submit
  297. */
  298. public function test_submit($name, $value, $expected)
  299. {
  300. $matcher = array(
  301. 'tag' => 'input',
  302. 'attributes' => array('name' => $name, 'type' => 'submit', 'value' => $value)
  303. );
  304. $this->assertTag($matcher, Form::submit($name, $value));
  305. }
  306. /**
  307. * Provides test data for test_image()
  308. *
  309. * @return array
  310. */
  311. public function provider_image()
  312. {
  313. return array(
  314. // $value, $result
  315. array('foo', 'bar', array('src' => 'media/img/login.png'), '<input type="image" name="foo" value="bar" src="/media/img/login.png" />'),
  316. );
  317. }
  318. /**
  319. * Tests Form::image()
  320. *
  321. * @test
  322. * @dataProvider provider_image
  323. * @param boolean $name Input for Form::image
  324. * @param boolean $value Input for Form::image
  325. * @param boolean $attributes Input for Form::image
  326. * @param boolean $expected Output for Form::image
  327. */
  328. public function test_image($name, $value, $attributes, $expected)
  329. {
  330. $this->assertSame($expected, Form::image($name, $value, $attributes));
  331. }
  332. /**
  333. * Provides test data for test_label()
  334. *
  335. * @return array
  336. */
  337. function provider_label()
  338. {
  339. return array(
  340. // $value, $result
  341. // Single for provided
  342. array('email', NULL, NULL, '<label for="email">Email</label>'),
  343. array('email_address', NULL, NULL, '<label for="email_address">Email Address</label>'),
  344. array('email-address', NULL, NULL, '<label for="email-address">Email Address</label>'),
  345. // For and text values provided
  346. array('name', 'First name', NULL, '<label for="name">First name</label>'),
  347. // with attributes
  348. array('lastname', 'Last name', array('class' => 'text'), '<label class="text" for="lastname">Last name</label>'),
  349. array('lastname', 'Last name', array('class' => 'text', 'id'=>'txt_lastname'), '<label id="txt_lastname" class="text" for="lastname">Last name</label>'),
  350. );
  351. }
  352. /**
  353. * Tests Form::label()
  354. *
  355. * @test
  356. * @dataProvider provider_label
  357. * @param boolean $for Input for Form::label
  358. * @param boolean $text Input for Form::label
  359. * @param boolean $attributes Input for Form::label
  360. * @param boolean $expected Output for Form::label
  361. */
  362. function test_label($for, $text, $attributes, $expected)
  363. {
  364. $this->assertSame($expected, Form::label($for, $text, $attributes));
  365. }
  366. }