form.test.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. class FormTest extends PHPUnit_Framework_TestCase {
  3. /**
  4. * Setup the test environment.
  5. */
  6. public function setUp()
  7. {
  8. URL::$base = null;
  9. Config::set('application.url', 'http://localhost');
  10. Config::set('application.index', 'index.php');
  11. }
  12. /**
  13. * Destroy the test enviornment.
  14. */
  15. public function tearDown()
  16. {
  17. Config::set('application.url', '');
  18. Config::set('application.index', 'index.php');
  19. }
  20. /**
  21. * Test the compilation of opening a form
  22. *
  23. * @group laravel
  24. */
  25. public function testOpeningForm()
  26. {
  27. $form1 = Form::open('foobar', 'GET');
  28. $form2 = Form::open('foobar', 'POST');
  29. $form3 = Form::open('foobar', 'PUT', array('accept-charset' => 'UTF-16', 'class' => 'form'));
  30. $form4 = Form::open('foobar', 'DELETE', array('class' => 'form'));
  31. $this->assertEquals('<form method="GET" action="http://localhost/index.php/foobar" accept-charset="UTF-8">', $form1);
  32. $this->assertEquals('<form method="POST" action="http://localhost/index.php/foobar" accept-charset="UTF-8">', $form2);
  33. $this->assertEquals('<form accept-charset="UTF-16" class="form" method="POST" action="http://localhost/index.php/foobar"><input type="hidden" name="_method" value="PUT">', $form3);
  34. $this->assertEquals('<form class="form" method="POST" action="http://localhost/index.php/foobar" accept-charset="UTF-8"><input type="hidden" name="_method" value="DELETE">', $form4);
  35. }
  36. /**
  37. * Test the compilation of opening a secure form
  38. *
  39. * @group laravel
  40. */
  41. public function testOpeningFormSecure()
  42. {
  43. $form1 = Form::open_secure('foobar', 'GET');
  44. $form2 = Form::open_secure('foobar', 'POST');
  45. $form3 = Form::open_secure('foobar', 'PUT', array('accept-charset' => 'UTF-16', 'class' => 'form'));
  46. $form4 = Form::open_secure('foobar', 'DELETE', array('class' => 'form'));
  47. $this->assertEquals('<form method="GET" action="https://localhost/index.php/foobar" accept-charset="UTF-8">', $form1);
  48. $this->assertEquals('<form method="POST" action="https://localhost/index.php/foobar" accept-charset="UTF-8">', $form2);
  49. $this->assertEquals('<form accept-charset="UTF-16" class="form" method="POST" action="https://localhost/index.php/foobar"><input type="hidden" name="_method" value="PUT">', $form3);
  50. $this->assertEquals('<form class="form" method="POST" action="https://localhost/index.php/foobar" accept-charset="UTF-8"><input type="hidden" name="_method" value="DELETE">', $form4);
  51. }
  52. /**
  53. * Test the compilation of opening a form for files
  54. *
  55. * @group laravel
  56. */
  57. public function testOpeningFormForFile()
  58. {
  59. $form1 = Form::open_for_files('foobar', 'GET');
  60. $form2 = Form::open_for_files('foobar', 'POST');
  61. $form3 = Form::open_for_files('foobar', 'PUT', array('accept-charset' => 'UTF-16', 'class' => 'form'));
  62. $form4 = Form::open_for_files('foobar', 'DELETE', array('class' => 'form'));
  63. $this->assertEquals('<form enctype="multipart/form-data" method="GET" action="http://localhost/index.php/foobar" accept-charset="UTF-8">', $form1);
  64. $this->assertEquals('<form enctype="multipart/form-data" method="POST" action="http://localhost/index.php/foobar" accept-charset="UTF-8">', $form2);
  65. $this->assertEquals('<form accept-charset="UTF-16" class="form" enctype="multipart/form-data" method="POST" action="http://localhost/index.php/foobar"><input type="hidden" name="_method" value="PUT">', $form3);
  66. $this->assertEquals('<form class="form" enctype="multipart/form-data" method="POST" action="http://localhost/index.php/foobar" accept-charset="UTF-8"><input type="hidden" name="_method" value="DELETE">', $form4);
  67. }
  68. /**
  69. * Test the compilation of opening a secure form for files
  70. *
  71. * @group laravel
  72. */
  73. public function testOpeningFormSecureForFile()
  74. {
  75. $form1 = Form::open_secure_for_files('foobar', 'GET');
  76. $form2 = Form::open_secure_for_files('foobar', 'POST');
  77. $form3 = Form::open_secure_for_files('foobar', 'PUT', array('accept-charset' => 'UTF-16', 'class' => 'form'));
  78. $form4 = Form::open_secure_for_files('foobar', 'DELETE', array('class' => 'form'));
  79. $this->assertEquals('<form enctype="multipart/form-data" method="GET" action="https://localhost/index.php/foobar" accept-charset="UTF-8">', $form1);
  80. $this->assertEquals('<form enctype="multipart/form-data" method="POST" action="https://localhost/index.php/foobar" accept-charset="UTF-8">', $form2);
  81. $this->assertEquals('<form accept-charset="UTF-16" class="form" enctype="multipart/form-data" method="POST" action="https://localhost/index.php/foobar"><input type="hidden" name="_method" value="PUT">', $form3);
  82. $this->assertEquals('<form class="form" enctype="multipart/form-data" method="POST" action="https://localhost/index.php/foobar" accept-charset="UTF-8"><input type="hidden" name="_method" value="DELETE">', $form4);
  83. }
  84. /**
  85. * Test the compilation of closing a form
  86. *
  87. * @group laravel
  88. */
  89. public function testClosingForm()
  90. {
  91. $this->assertEquals('</form>', Form::close());
  92. }
  93. /**
  94. * Test the compilation of form label
  95. *
  96. * @group laravel
  97. */
  98. public function testFormLabel()
  99. {
  100. $form1 = Form::label('foo', 'Foobar');
  101. $form2 = Form::label('foo', 'Foobar', array('class' => 'control-label'));
  102. $form3 = Form::label('foo', 'Foobar <i>baz</i>', null, false);
  103. $this->assertEquals('<label for="foo">Foobar</label>', $form1);
  104. $this->assertEquals('<label for="foo" class="control-label">Foobar</label>', $form2);
  105. $this->assertEquals('<label for="foo">Foobar <i>baz</i></label>', $form3);
  106. }
  107. /**
  108. * Test the compilation of form input
  109. *
  110. * @group laravel
  111. */
  112. public function testFormInput()
  113. {
  114. $form1 = Form::input('text', 'foo');
  115. $form2 = Form::input('text', 'foo', 'foobar');
  116. $form3 = Form::input('date', 'foobar', null, array('class' => 'span2'));
  117. $this->assertEquals('<input type="text" name="foo" id="foo">', $form1);
  118. $this->assertEquals('<input type="text" name="foo" value="foobar" id="foo">', $form2);
  119. $this->assertEquals('<input class="span2" type="date" name="foobar">', $form3);
  120. }
  121. /**
  122. * Test the compilation of form text
  123. *
  124. * @group laravel
  125. */
  126. public function testFormText()
  127. {
  128. $form1 = Form::input('text', 'foo');
  129. $form2 = Form::text('foo');
  130. $form3 = Form::text('foo', 'foobar');
  131. $form4 = Form::text('foo', null, array('class' => 'span2'));
  132. $this->assertEquals('<input type="text" name="foo" id="foo">', $form1);
  133. $this->assertEquals($form1, $form2);
  134. $this->assertEquals('<input type="text" name="foo" value="foobar" id="foo">', $form3);
  135. $this->assertEquals('<input class="span2" type="text" name="foo" id="foo">', $form4);
  136. }
  137. /**
  138. * Test the compilation of form password
  139. *
  140. * @group laravel
  141. */
  142. public function testFormPassword()
  143. {
  144. $form1 = Form::input('password', 'foo');
  145. $form2 = Form::password('foo');
  146. $form3 = Form::password('foo', array('class' => 'span2'));
  147. $this->assertEquals('<input type="password" name="foo" id="foo">', $form1);
  148. $this->assertEquals($form1, $form2);
  149. $this->assertEquals('<input class="span2" type="password" name="foo" id="foo">', $form3);
  150. }
  151. /**
  152. * Test the compilation of form hidden
  153. *
  154. * @group laravel
  155. */
  156. public function testFormHidden()
  157. {
  158. $form1 = Form::input('hidden', 'foo');
  159. $form2 = Form::hidden('foo');
  160. $form3 = Form::hidden('foo', 'foobar');
  161. $form4 = Form::hidden('foo', null, array('class' => 'span2'));
  162. $this->assertEquals('<input type="hidden" name="foo" id="foo">', $form1);
  163. $this->assertEquals($form1, $form2);
  164. $this->assertEquals('<input type="hidden" name="foo" value="foobar" id="foo">', $form3);
  165. $this->assertEquals('<input class="span2" type="hidden" name="foo" id="foo">', $form4);
  166. }
  167. /**
  168. * Test the compilation of form search
  169. *
  170. * @group laravel
  171. */
  172. public function testFormSearch()
  173. {
  174. $form1 = Form::input('search', 'foo');
  175. $form2 = Form::search('foo');
  176. $form3 = Form::search('foo', 'foobar');
  177. $form4 = Form::search('foo', null, array('class' => 'span2'));
  178. $this->assertEquals('<input type="search" name="foo" id="foo">', $form1);
  179. $this->assertEquals($form1, $form2);
  180. $this->assertEquals('<input type="search" name="foo" value="foobar" id="foo">', $form3);
  181. $this->assertEquals('<input class="span2" type="search" name="foo" id="foo">', $form4);
  182. }
  183. /**
  184. * Test the compilation of form email
  185. *
  186. * @group laravel
  187. */
  188. public function testFormEmail()
  189. {
  190. $form1 = Form::input('email', 'foo');
  191. $form2 = Form::email('foo');
  192. $form3 = Form::email('foo', 'foobar');
  193. $form4 = Form::email('foo', null, array('class' => 'span2'));
  194. $this->assertEquals('<input type="email" name="foo" id="foo">', $form1);
  195. $this->assertEquals($form1, $form2);
  196. $this->assertEquals('<input type="email" name="foo" value="foobar" id="foo">', $form3);
  197. $this->assertEquals('<input class="span2" type="email" name="foo" id="foo">', $form4);
  198. }
  199. /**
  200. * Test the compilation of form telephone
  201. *
  202. * @group laravel
  203. */
  204. public function testFormTelephone()
  205. {
  206. $form1 = Form::input('tel', 'foo');
  207. $form2 = Form::telephone('foo');
  208. $form3 = Form::telephone('foo', 'foobar');
  209. $form4 = Form::telephone('foo', null, array('class' => 'span2'));
  210. $this->assertEquals('<input type="tel" name="foo" id="foo">', $form1);
  211. $this->assertEquals($form1, $form2);
  212. $this->assertEquals('<input type="tel" name="foo" value="foobar" id="foo">', $form3);
  213. $this->assertEquals('<input class="span2" type="tel" name="foo" id="foo">', $form4);
  214. }
  215. /**
  216. * Test the compilation of form url
  217. *
  218. * @group laravel
  219. */
  220. public function testFormUrl()
  221. {
  222. $form1 = Form::input('url', 'foo');
  223. $form2 = Form::url('foo');
  224. $form3 = Form::url('foo', 'foobar');
  225. $form4 = Form::url('foo', null, array('class' => 'span2'));
  226. $this->assertEquals('<input type="url" name="foo" id="foo">', $form1);
  227. $this->assertEquals($form1, $form2);
  228. $this->assertEquals('<input type="url" name="foo" value="foobar" id="foo">', $form3);
  229. $this->assertEquals('<input class="span2" type="url" name="foo" id="foo">', $form4);
  230. }
  231. /**
  232. * Test the compilation of form number
  233. *
  234. * @group laravel
  235. */
  236. public function testFormNumber()
  237. {
  238. $form1 = Form::input('number', 'foo');
  239. $form2 = Form::number('foo');
  240. $form3 = Form::number('foo', 'foobar');
  241. $form4 = Form::number('foo', null, array('class' => 'span2'));
  242. $this->assertEquals('<input type="number" name="foo" id="foo">', $form1);
  243. $this->assertEquals($form1, $form2);
  244. $this->assertEquals('<input type="number" name="foo" value="foobar" id="foo">', $form3);
  245. $this->assertEquals('<input class="span2" type="number" name="foo" id="foo">', $form4);
  246. }
  247. /**
  248. * Test the compilation of form date
  249. *
  250. * @group laravel
  251. */
  252. public function testFormDate()
  253. {
  254. $form1 = Form::input('date', 'foo');
  255. $form2 = Form::date('foo');
  256. $form3 = Form::date('foo', 'foobar');
  257. $form4 = Form::date('foo', null, array('class' => 'span2'));
  258. $this->assertEquals('<input type="date" name="foo" id="foo">', $form1);
  259. $this->assertEquals($form1, $form2);
  260. $this->assertEquals('<input type="date" name="foo" value="foobar" id="foo">', $form3);
  261. $this->assertEquals('<input class="span2" type="date" name="foo" id="foo">', $form4);
  262. }
  263. /**
  264. * Test the compilation of form file
  265. *
  266. * @group laravel
  267. */
  268. public function testFormFile()
  269. {
  270. $form1 = Form::input('file', 'foo');
  271. $form2 = Form::file('foo');
  272. $form3 = Form::file('foo', array('class' => 'span2'));
  273. $this->assertEquals('<input type="file" name="foo" id="foo">', $form1);
  274. $this->assertEquals($form1, $form2);
  275. $this->assertEquals('<input class="span2" type="file" name="foo" id="foo">', $form3);
  276. }
  277. /**
  278. * Test the compilation of form textarea
  279. *
  280. * @group laravel
  281. */
  282. public function testFormTextarea()
  283. {
  284. $form1 = Form::textarea('foo');
  285. $form2 = Form::textarea('foo', 'foobar');
  286. $form3 = Form::textarea('foo', null, array('class' => 'span2'));
  287. $this->assertEquals('<textarea name="foo" id="foo" rows="10" cols="50"></textarea>', $form1);
  288. $this->assertEquals('<textarea name="foo" id="foo" rows="10" cols="50">foobar</textarea>', $form2);
  289. $this->assertEquals('<textarea class="span2" name="foo" id="foo" rows="10" cols="50"></textarea>', $form3);
  290. }
  291. /**
  292. * Test the compilation of form select
  293. *
  294. * @group laravel
  295. */
  296. public function testFormSelect()
  297. {
  298. $select1 = array(
  299. 'foobar' => 'Foobar',
  300. 'hello' => 'Hello World',
  301. );
  302. $select2 = array(
  303. 'foo' => array(
  304. 'foobar' => 'Foobar',
  305. ),
  306. 'hello' => 'Hello World',
  307. );
  308. $form1 = Form::select('foo');
  309. $form2 = Form::select('foo', $select1, 'foobar');
  310. $form3 = Form::select('foo', $select1, null, array('class' => 'span2'));
  311. $form4 = Form::select('foo', $select2, 'foobar');
  312. $this->assertEquals('<select id="foo" name="foo"></select>', $form1);
  313. $this->assertEquals('<select id="foo" name="foo"><option value="foobar" selected="selected">Foobar</option><option value="hello">Hello World</option></select>', $form2);
  314. $this->assertEquals('<select class="span2" id="foo" name="foo"><option value="foobar">Foobar</option><option value="hello">Hello World</option></select>', $form3);
  315. $this->assertEquals('<select id="foo" name="foo"><optgroup label="foo"><option value="foobar" selected="selected">Foobar</option></optgroup><option value="hello">Hello World</option></select>', $form4);
  316. }
  317. /**
  318. * Test the compilation of form checkbox
  319. *
  320. * @group laravel
  321. */
  322. public function testFormCheckbox()
  323. {
  324. $form1 = Form::input('checkbox', 'foo');
  325. $form2 = Form::checkbox('foo');
  326. $form3 = Form::checkbox('foo', 'foobar', true);
  327. $form4 = Form::checkbox('foo', 'foobar', false, array('class' => 'span2'));
  328. $this->assertEquals('<input type="checkbox" name="foo" id="foo">', $form1);
  329. $this->assertEquals('<input id="foo" type="checkbox" name="foo" value="1">', $form2);
  330. $this->assertEquals('<input checked="checked" id="foo" type="checkbox" name="foo" value="foobar">', $form3);
  331. $this->assertEquals('<input class="span2" id="foo" type="checkbox" name="foo" value="foobar">', $form4);
  332. }
  333. /**
  334. * Test the compilation of form date
  335. *
  336. * @group laravel
  337. */
  338. public function testFormRadio()
  339. {
  340. $form1 = Form::input('radio', 'foo');
  341. $form2 = Form::radio('foo');
  342. $form3 = Form::radio('foo', 'foobar', true);
  343. $form4 = Form::radio('foo', 'foobar', false, array('class' => 'span2'));
  344. $this->assertEquals('<input type="radio" name="foo" id="foo">', $form1);
  345. $this->assertEquals('<input id="foo" type="radio" name="foo" value="foo">', $form2);
  346. $this->assertEquals('<input checked="checked" id="foo" type="radio" name="foo" value="foobar">', $form3);
  347. $this->assertEquals('<input class="span2" id="foo" type="radio" name="foo" value="foobar">', $form4);
  348. }
  349. /**
  350. * Test the compilation of form submit
  351. *
  352. * @group laravel
  353. */
  354. public function testFormSubmit()
  355. {
  356. $form1 = Form::submit('foo');
  357. $form2 = Form::submit('foo', array('class' => 'span2'));
  358. $this->assertEquals('<input type="submit" value="foo">', $form1);
  359. $this->assertEquals('<input class="span2" type="submit" value="foo">', $form2);
  360. }
  361. /**
  362. * Test the compilation of form reset
  363. *
  364. * @group laravel
  365. */
  366. public function testFormReset()
  367. {
  368. $form1 = Form::reset('foo');
  369. $form2 = Form::reset('foo', array('class' => 'span2'));
  370. $this->assertEquals('<input type="reset" value="foo">', $form1);
  371. $this->assertEquals('<input class="span2" type="reset" value="foo">', $form2);
  372. }
  373. /**
  374. * Test the compilation of form image
  375. *
  376. * @group laravel
  377. */
  378. public function testFormImage()
  379. {
  380. $form1 = Form::image('foo/bar', 'foo');
  381. $form2 = Form::image('foo/bar', 'foo', array('class' => 'span2'));
  382. $form3 = Form::image('http://google.com/foobar', 'foobar');
  383. $this->assertEquals('<input src="http://localhost/foo/bar" type="image" name="foo" id="foo">', $form1);
  384. $this->assertEquals('<input class="span2" src="http://localhost/foo/bar" type="image" name="foo" id="foo">', $form2);
  385. $this->assertEquals('<input src="http://google.com/foobar" type="image" name="foobar">', $form3);
  386. }
  387. /**
  388. * Test the compilation of form button
  389. *
  390. * @group laravel
  391. */
  392. public function testFormButton()
  393. {
  394. $form1 = Form::button('foo');
  395. $form2 = Form::button('foo', array('class' => 'span2'));
  396. $this->assertEquals('<button>foo</button>', $form1);
  397. $this->assertEquals('<button class="span2">foo</button>', $form2);
  398. }
  399. }