FormTest.php 9.5 KB

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