form.test.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. $this->assertEquals('<label for="foo">Foobar</label>', $form1);
  103. $this->assertEquals('<label for="foo" class="control-label">Foobar</label>', $form2);
  104. }
  105. /**
  106. * Test the compilation of form input
  107. *
  108. * @group laravel
  109. */
  110. public function testFormInput()
  111. {
  112. $form1 = Form::input('text', 'foo');
  113. $form2 = Form::input('text', 'foo', 'foobar');
  114. $form3 = Form::input('date', 'foobar', null, array('class' => 'span2'));
  115. $this->assertEquals('<input type="text" name="foo" id="foo">', $form1);
  116. $this->assertEquals('<input type="text" name="foo" value="foobar" id="foo">', $form2);
  117. $this->assertEquals('<input class="span2" type="date" name="foobar">', $form3);
  118. }
  119. /**
  120. * Test the compilation of form text
  121. *
  122. * @group laravel
  123. */
  124. public function testFormText()
  125. {
  126. $form1 = Form::input('text', 'foo');
  127. $form2 = Form::text('foo');
  128. $form3 = Form::text('foo', 'foobar');
  129. $form4 = Form::text('foo', null, array('class' => 'span2'));
  130. $this->assertEquals('<input type="text" name="foo" id="foo">', $form1);
  131. $this->assertEquals($form1, $form2);
  132. $this->assertEquals('<input type="text" name="foo" value="foobar" id="foo">', $form3);
  133. $this->assertEquals('<input class="span2" type="text" name="foo" id="foo">', $form4);
  134. }
  135. /**
  136. * Test the compilation of form password
  137. *
  138. * @group laravel
  139. */
  140. public function testFormPassword()
  141. {
  142. $form1 = Form::input('password', 'foo');
  143. $form2 = Form::password('foo');
  144. $form3 = Form::password('foo', array('class' => 'span2'));
  145. $this->assertEquals('<input type="password" name="foo" id="foo">', $form1);
  146. $this->assertEquals($form1, $form2);
  147. $this->assertEquals('<input class="span2" type="password" name="foo" id="foo">', $form3);
  148. }
  149. /**
  150. * Test the compilation of form hidden
  151. *
  152. * @group laravel
  153. */
  154. public function testFormHidden()
  155. {
  156. $form1 = Form::input('hidden', 'foo');
  157. $form2 = Form::hidden('foo');
  158. $form3 = Form::hidden('foo', 'foobar');
  159. $form4 = Form::hidden('foo', null, array('class' => 'span2'));
  160. $this->assertEquals('<input type="hidden" name="foo" id="foo">', $form1);
  161. $this->assertEquals($form1, $form2);
  162. $this->assertEquals('<input type="hidden" name="foo" value="foobar" id="foo">', $form3);
  163. $this->assertEquals('<input class="span2" type="hidden" name="foo" id="foo">', $form4);
  164. }
  165. /**
  166. * Test the compilation of form search
  167. *
  168. * @group laravel
  169. */
  170. public function testFormSearch()
  171. {
  172. $form1 = Form::input('search', 'foo');
  173. $form2 = Form::search('foo');
  174. $form3 = Form::search('foo', 'foobar');
  175. $form4 = Form::search('foo', null, array('class' => 'span2'));
  176. $this->assertEquals('<input type="search" name="foo" id="foo">', $form1);
  177. $this->assertEquals($form1, $form2);
  178. $this->assertEquals('<input type="search" name="foo" value="foobar" id="foo">', $form3);
  179. $this->assertEquals('<input class="span2" type="search" name="foo" id="foo">', $form4);
  180. }
  181. /**
  182. * Test the compilation of form email
  183. *
  184. * @group laravel
  185. */
  186. public function testFormEmail()
  187. {
  188. $form1 = Form::input('email', 'foo');
  189. $form2 = Form::email('foo');
  190. $form3 = Form::email('foo', 'foobar');
  191. $form4 = Form::email('foo', null, array('class' => 'span2'));
  192. $this->assertEquals('<input type="email" name="foo" id="foo">', $form1);
  193. $this->assertEquals($form1, $form2);
  194. $this->assertEquals('<input type="email" name="foo" value="foobar" id="foo">', $form3);
  195. $this->assertEquals('<input class="span2" type="email" name="foo" id="foo">', $form4);
  196. }
  197. /**
  198. * Test the compilation of form telephone
  199. *
  200. * @group laravel
  201. */
  202. public function testFormTelephone()
  203. {
  204. $form1 = Form::input('tel', 'foo');
  205. $form2 = Form::telephone('foo');
  206. $form3 = Form::telephone('foo', 'foobar');
  207. $form4 = Form::telephone('foo', null, array('class' => 'span2'));
  208. $this->assertEquals('<input type="tel" name="foo" id="foo">', $form1);
  209. $this->assertEquals($form1, $form2);
  210. $this->assertEquals('<input type="tel" name="foo" value="foobar" id="foo">', $form3);
  211. $this->assertEquals('<input class="span2" type="tel" name="foo" id="foo">', $form4);
  212. }
  213. /**
  214. * Test the compilation of form url
  215. *
  216. * @group laravel
  217. */
  218. public function testFormUrl()
  219. {
  220. $form1 = Form::input('url', 'foo');
  221. $form2 = Form::url('foo');
  222. $form3 = Form::url('foo', 'foobar');
  223. $form4 = Form::url('foo', null, array('class' => 'span2'));
  224. $this->assertEquals('<input type="url" name="foo" id="foo">', $form1);
  225. $this->assertEquals($form1, $form2);
  226. $this->assertEquals('<input type="url" name="foo" value="foobar" id="foo">', $form3);
  227. $this->assertEquals('<input class="span2" type="url" name="foo" id="foo">', $form4);
  228. }
  229. /**
  230. * Test the compilation of form number
  231. *
  232. * @group laravel
  233. */
  234. public function testFormNumber()
  235. {
  236. $form1 = Form::input('number', 'foo');
  237. $form2 = Form::number('foo');
  238. $form3 = Form::number('foo', 'foobar');
  239. $form4 = Form::number('foo', null, array('class' => 'span2'));
  240. $this->assertEquals('<input type="number" name="foo" id="foo">', $form1);
  241. $this->assertEquals($form1, $form2);
  242. $this->assertEquals('<input type="number" name="foo" value="foobar" id="foo">', $form3);
  243. $this->assertEquals('<input class="span2" type="number" name="foo" id="foo">', $form4);
  244. }
  245. /**
  246. * Test the compilation of form date
  247. *
  248. * @group laravel
  249. */
  250. public function testFormDate()
  251. {
  252. $form1 = Form::input('date', 'foo');
  253. $form2 = Form::date('foo');
  254. $form3 = Form::date('foo', 'foobar');
  255. $form4 = Form::date('foo', null, array('class' => 'span2'));
  256. $this->assertEquals('<input type="date" name="foo" id="foo">', $form1);
  257. $this->assertEquals($form1, $form2);
  258. $this->assertEquals('<input type="date" name="foo" value="foobar" id="foo">', $form3);
  259. $this->assertEquals('<input class="span2" type="date" name="foo" id="foo">', $form4);
  260. }
  261. /**
  262. * Test the compilation of form file
  263. *
  264. * @group laravel
  265. */
  266. public function testFormFile()
  267. {
  268. $form1 = Form::input('file', 'foo');
  269. $form2 = Form::file('foo');
  270. $form3 = Form::file('foo', array('class' => 'span2'));
  271. $this->assertEquals('<input type="file" name="foo" id="foo">', $form1);
  272. $this->assertEquals($form1, $form2);
  273. $this->assertEquals('<input class="span2" type="file" name="foo" id="foo">', $form3);
  274. }
  275. /**
  276. * Test the compilation of form textarea
  277. *
  278. * @group laravel
  279. */
  280. public function testFormTextarea()
  281. {
  282. $form1 = Form::textarea('foo');
  283. $form2 = Form::textarea('foo', 'foobar');
  284. $form3 = Form::textarea('foo', null, array('class' => 'span2'));
  285. $this->assertEquals('<textarea name="foo" id="foo" rows="10" cols="50"></textarea>', $form1);
  286. $this->assertEquals('<textarea name="foo" id="foo" rows="10" cols="50">foobar</textarea>', $form2);
  287. $this->assertEquals('<textarea class="span2" name="foo" id="foo" rows="10" cols="50"></textarea>', $form3);
  288. }
  289. /**
  290. * Test the compilation of form select
  291. *
  292. * @group laravel
  293. */
  294. public function testFormSelect()
  295. {
  296. $select1 = array(
  297. 'foobar' => 'Foobar',
  298. 'hello' => 'Hello World',
  299. );
  300. $select2 = array(
  301. 'foo' => array(
  302. 'foobar' => 'Foobar',
  303. ),
  304. 'hello' => 'Hello World',
  305. );
  306. $form1 = Form::select('foo');
  307. $form2 = Form::select('foo', $select1, 'foobar');
  308. $form3 = Form::select('foo', $select1, null, array('class' => 'span2'));
  309. $form4 = Form::select('foo', $select2, 'foobar');
  310. $this->assertEquals('<select id="foo" name="foo"></select>', $form1);
  311. $this->assertEquals('<select id="foo" name="foo"><option value="foobar" selected="selected">Foobar</option><option value="hello">Hello World</option></select>', $form2);
  312. $this->assertEquals('<select class="span2" id="foo" name="foo"><option value="foobar">Foobar</option><option value="hello">Hello World</option></select>', $form3);
  313. $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);
  314. }
  315. /**
  316. * Test the compilation of form checkbox
  317. *
  318. * @group laravel
  319. */
  320. public function testFormCheckbox()
  321. {
  322. $form1 = Form::input('checkbox', 'foo');
  323. $form2 = Form::checkbox('foo');
  324. $form3 = Form::checkbox('foo', 'foobar', true);
  325. $form4 = Form::checkbox('foo', 'foobar', false, array('class' => 'span2'));
  326. $this->assertEquals('<input type="checkbox" name="foo" id="foo">', $form1);
  327. $this->assertEquals('<input id="foo" type="checkbox" name="foo" value="1">', $form2);
  328. $this->assertEquals('<input checked="checked" id="foo" type="checkbox" name="foo" value="foobar">', $form3);
  329. $this->assertEquals('<input class="span2" id="foo" type="checkbox" name="foo" value="foobar">', $form4);
  330. }
  331. /**
  332. * Test the compilation of form date
  333. *
  334. * @group laravel
  335. */
  336. public function testFormRadio()
  337. {
  338. $form1 = Form::input('radio', 'foo');
  339. $form2 = Form::radio('foo');
  340. $form3 = Form::radio('foo', 'foobar', true);
  341. $form4 = Form::radio('foo', 'foobar', false, array('class' => 'span2'));
  342. $this->assertEquals('<input type="radio" name="foo" id="foo">', $form1);
  343. $this->assertEquals('<input id="foo" type="radio" name="foo" value="foo">', $form2);
  344. $this->assertEquals('<input checked="checked" id="foo" type="radio" name="foo" value="foobar">', $form3);
  345. $this->assertEquals('<input class="span2" id="foo" type="radio" name="foo" value="foobar">', $form4);
  346. }
  347. /**
  348. * Test the compilation of form submit
  349. *
  350. * @group laravel
  351. */
  352. public function testFormSubmit()
  353. {
  354. $form1 = Form::submit('foo');
  355. $form2 = Form::submit('foo', array('class' => 'span2'));
  356. $this->assertEquals('<input type="submit" value="foo">', $form1);
  357. $this->assertEquals('<input class="span2" type="submit" value="foo">', $form2);
  358. }
  359. /**
  360. * Test the compilation of form reset
  361. *
  362. * @group laravel
  363. */
  364. public function testFormReset()
  365. {
  366. $form1 = Form::reset('foo');
  367. $form2 = Form::reset('foo', array('class' => 'span2'));
  368. $this->assertEquals('<input type="reset" value="foo">', $form1);
  369. $this->assertEquals('<input class="span2" type="reset" value="foo">', $form2);
  370. }
  371. /**
  372. * Test the compilation of form image
  373. *
  374. * @group laravel
  375. */
  376. public function testFormImage()
  377. {
  378. $form1 = Form::image('foo/bar', 'foo');
  379. $form2 = Form::image('foo/bar', 'foo', array('class' => 'span2'));
  380. $form3 = Form::image('http://google.com/foobar', 'foobar');
  381. $this->assertEquals('<input src="http://localhost/foo/bar" type="image" name="foo" id="foo">', $form1);
  382. $this->assertEquals('<input class="span2" src="http://localhost/foo/bar" type="image" name="foo" id="foo">', $form2);
  383. $this->assertEquals('<input src="http://google.com/foobar" type="image" name="foobar">', $form3);
  384. }
  385. /**
  386. * Test the compilation of form button
  387. *
  388. * @group laravel
  389. */
  390. public function testFormButton()
  391. {
  392. $form1 = Form::button('foo');
  393. $form2 = Form::button('foo', array('class' => 'span2'));
  394. $this->assertEquals('<button>foo</button>', $form1);
  395. $this->assertEquals('<button class="span2">foo</button>', $form2);
  396. }
  397. }