FormTest.php 9.5 KB

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