PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/test/Mad/View/Helper/FormTest.php

http://github.com/maintainable/framework
PHP | 553 lines | 435 code | 95 blank | 23 comment | 2 complexity | 08796460d2717ae41d906b7963bc8f87 MD5 | raw file
  1. <?php
  2. /**
  3. * @category Mad
  4. * @package Mad_View
  5. * @subpackage UnitTests
  6. * @copyright (c) 2007-2009 Maintainable Software, LLC
  7. * @license http://opensource.org/licenses/bsd-license.php BSD
  8. */
  9. /**
  10. * Set environment
  11. */
  12. if (!defined('MAD_ENV')) define('MAD_ENV', 'test');
  13. if (!defined('MAD_ROOT')) {
  14. require_once dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/config/environment.php';
  15. }
  16. class Mad_View_Helper_FormTest_MockUrlHelper extends Mad_View_Helper_Base
  17. {
  18. public function urlFor($options)
  19. {
  20. return 'http://www.example.com';
  21. }
  22. }
  23. /**
  24. * @group view
  25. * @category Mad
  26. * @package Mad_View
  27. * @subpackage UnitTests
  28. * @copyright (c) 2007-2009 Maintainable Software, LLC
  29. * @license http://opensource.org/licenses/bsd-license.php BSD
  30. */
  31. class Mad_View_Helper_FormTest extends Mad_Test_Functional
  32. {
  33. public function setUp()
  34. {
  35. $this->view = new Mad_View_Base();
  36. $this->view->addHelper(new Mad_View_Helper_Form($this->view));
  37. $this->view->addHelper(new Mad_View_Helper_FormTag($this->view));
  38. $this->view->addHelper(new Mad_View_Helper_Tag($this->view));
  39. $this->view->addHelper(new Mad_View_Helper_FormTest_MockUrlHelper($this->view));
  40. $this->post = (object)array('title', 'authorName', 'body',
  41. 'secret', 'writtenOn', 'cost');
  42. $this->post->title = 'Hello World';
  43. $this->post->authorName = '';
  44. $this->post->body = 'Back to the hill and over it again!';
  45. $this->post->secret = 1;
  46. $this->post->writtenOn = mktime(2004, 6, 15);
  47. $this->post->id = 123;
  48. $this->post->id_before_type_cast = 123;
  49. $this->view->post = $this->post;
  50. }
  51. public function testTextField()
  52. {
  53. $this->assertEquals(
  54. '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />',
  55. $this->view->textField('post', 'title'));
  56. $this->assertEquals(
  57. '<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />',
  58. $this->view->passwordField('post', 'title'));
  59. $this->assertEquals(
  60. '<input id="person_name" name="person[name]" size="30" type="password" />',
  61. $this->view->passwordField("person", "name"));
  62. }
  63. public function testTextFieldWithEscapes()
  64. {
  65. $this->post->title = '<b>Hello World</b>';
  66. $this->assertEquals(
  67. '<input id="post_title" name="post[title]" size="30" type="text" value="&lt;b&gt;Hello World&lt;/b&gt;" />',
  68. $this->view->textField('post', 'title'));
  69. }
  70. public function testTextFieldWithOptions()
  71. {
  72. $expected = '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />';
  73. $this->assertEquals($expected, $this->view->textField('post', 'title', array('size' => 35)));
  74. }
  75. public function testTextFieldAssumingSize()
  76. {
  77. $expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />';
  78. $this->assertEquals($expected, $this->view->textField('post', 'title', array('maxlength' => 35)));
  79. }
  80. public function testTextFieldDoesntChangeParamValues()
  81. {
  82. $objectName = 'post[]';
  83. $expected = '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />';
  84. $this->assertEquals($expected, $this->view->textField($objectName, 'title'));
  85. $this->assertEquals($objectName, 'post[]');
  86. }
  87. public function testCheckBox()
  88. {
  89. $this->assertEquals(
  90. '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
  91. $this->view->checkBox('post', 'secret'));
  92. $this->post->secret = 0;
  93. $this->assertEquals(
  94. '<input name="post[secret]" type="hidden" value="0" /><input id="post_secret" name="post[secret]" type="checkbox" value="1" />',
  95. $this->view->checkBox('post', 'secret'));
  96. $this->assertEquals(
  97. '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
  98. $this->view->checkBox('post', 'secret', array('checked' => 'checked')));
  99. $this->post->secret = true;
  100. $this->assertEquals(
  101. '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
  102. $this->view->checkBox('post', 'secret'));
  103. }
  104. public function testCheckBoxWithExplicitCheckedAndUncheckedValues()
  105. {
  106. $this->post->secret = 'on';
  107. $this->assertEquals(
  108. '<input name="post[secret]" type="hidden" value="off" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" />',
  109. $this->view->checkBox('post', 'secret', array(), 'on', 'off'));
  110. }
  111. public function testRadioButton()
  112. {
  113. $this->assertEquals(
  114. '<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
  115. $this->view->radioButton('post', 'title', 'Hello World'));
  116. $this->assertEquals(
  117. '<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
  118. $this->view->radioButton('post', 'title', 'Goodbye World'));
  119. }
  120. public function testRadioButtonIsCheckedWithIntegers()
  121. {
  122. $this->assertEquals(
  123. '<input checked="checked" id="post_secret_1" name="post[secret]" type="radio" value="1" />',
  124. $this->view->radioButton('post', 'secret', '1'));
  125. }
  126. public function testRadioButtonRespectsPassedInId()
  127. {
  128. $this->assertEquals(
  129. '<input checked="checked" id="foo" name="post[secret]" type="radio" value="1" />',
  130. $this->view->radioButton('post', 'secret', '1', array('id' => 'foo')));
  131. }
  132. public function testTextArea()
  133. {
  134. $this->assertEquals(
  135. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
  136. $this->view->textArea('post', 'body'));
  137. }
  138. public function testTextAreaWithEscapes()
  139. {
  140. $this->post->body = "Back to <i>the</i> hill and over it again!";
  141. $this->assertEquals(
  142. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to &lt;i&gt;the&lt;/i&gt; hill and over it again!</textarea>',
  143. $this->view->textArea('post', 'body'));
  144. }
  145. public function testTextAreaWithAlternateValue()
  146. {
  147. $this->assertEquals(
  148. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Testing alternate values.</textarea>',
  149. $this->view->textArea('post', 'body', array('value' => 'Testing alternate values.')));
  150. }
  151. public function testTextAreaWithSizeOption()
  152. {
  153. $this->assertEquals(
  154. '<textarea cols="183" id="post_body" name="post[body]" rows="820">Back to the hill and over it again!</textarea>',
  155. $this->view->textArea('post', 'body', array('size' => '183x820')));
  156. }
  157. public function testExplicitName()
  158. {
  159. $this->assertEquals(
  160. '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />',
  161. $this->view->textField("post", "title", array("name" => "dont guess")));
  162. $this->assertEquals(
  163. '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
  164. $this->view->textArea("post", "body", array("name" => "really!")));
  165. $this->assertEquals(
  166. '<input name="i mean it" type="hidden" value="0" /><input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" />',
  167. $this->view->checkBox("post", "secret", array("name" => "i mean it")));
  168. }
  169. public function testExplicitId()
  170. {
  171. $this->assertEquals(
  172. '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />',
  173. $this->view->textField("post", "title", array("id" => "dont guess")));
  174. $this->assertEquals(
  175. '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
  176. $this->view->textArea("post", "body", array("id" => "really!")));
  177. $this->assertEquals(
  178. '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" />',
  179. $this->view->checkBox("post", "secret", array("id" => "i mean it")));
  180. }
  181. public function testAutoIndex()
  182. {
  183. $pid = $this->post->id;
  184. $this->assertEquals(
  185. "<input id=\"post_{$pid}_title\" name=\"post[{$pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />",
  186. $this->view->textField("post[]", "title"));
  187. $this->assertEquals(
  188. "<textarea cols=\"40\" id=\"post_{$pid}_body\" name=\"post[{$pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
  189. $this->view->textArea("post[]", "body"));
  190. $this->assertEquals(
  191. "<input name=\"post[{$pid}][secret]\" type=\"hidden\" value=\"0\" /><input checked=\"checked\" id=\"post_{$pid}_secret\" name=\"post[{$pid}][secret]\" type=\"checkbox\" value=\"1\" />",
  192. $this->view->checkBox('post[]', 'secret'));
  193. $this->assertEquals(
  194. "<input checked=\"checked\" id=\"post_{$pid}_title_hello_world\" name=\"post[{$pid}][title]\" type=\"radio\" value=\"Hello World\" />",
  195. $this->view->radioButton('post[]', 'title', 'Hello World'));
  196. $this->assertEquals(
  197. "<input id=\"post_{$pid}_title_goodbye_world\" name=\"post[{$pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
  198. $this->view->radioButton('post[]', 'title', 'Goodbye World'));
  199. }
  200. public function testFormFor()
  201. {
  202. ob_start();
  203. $form = $this->view->formFor('post', $this->post, array('html' => array('id' => 'create-post')));
  204. echo $form->textField('title');
  205. echo $form->textArea('body');
  206. echo $form->checkBox('secret');
  207. echo $form->submit('Create post');
  208. $form->end();
  209. $expected =
  210. '<form action="http://www.example.com" id="create-post" method="post">' .
  211. '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
  212. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
  213. '<input name="post[secret]" type="hidden" value="0" />' .
  214. '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />' .
  215. '<input id="post_submit" name="commit" type="submit" value="Create post" />' .
  216. "</form>";
  217. $this->assertEquals($expected, ob_get_clean());
  218. }
  219. public function testFormForWithMethod()
  220. {
  221. ob_start();
  222. $form = $this->view->formFor('post', $this->post, array('html' => array('id' => 'create-post',
  223. 'method' => 'put')));
  224. echo $form->textField('title');
  225. echo $form->textArea('body');
  226. echo $form->checkBox('secret');
  227. $form->end();
  228. $expected =
  229. '<form action="http://www.example.com" id="create-post" method="post">' .
  230. '<div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div>' .
  231. '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
  232. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
  233. '<input name="post[secret]" type="hidden" value="0" />' .
  234. '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />' .
  235. "</form>";
  236. $this->assertEquals($expected, ob_get_clean());
  237. }
  238. public function testFormForWithoutObject()
  239. {
  240. ob_start();
  241. $form = $this->view->formFor('post', array('html' => array('id' => 'create-post')));
  242. echo $form->textField('title');
  243. echo $form->textArea('body');
  244. echo $form->checkBox('secret');
  245. $form->end();
  246. $expected =
  247. '<form action="http://www.example.com" id="create-post" method="post">' .
  248. '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
  249. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
  250. '<input name="post[secret]" type="hidden" value="0" />' .
  251. '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />' .
  252. "</form>";
  253. $this->assertEquals($expected, ob_get_clean());
  254. }
  255. public function testFormForWithIndex()
  256. {
  257. ob_start();
  258. $form = $this->view->formFor('post[]', $this->post);
  259. echo $form->textField('title');
  260. echo $form->textArea('body');
  261. echo $form->checkBox('secret');
  262. $form->end();
  263. $expected =
  264. '<form action="http://www.example.com" method="post">' .
  265. '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />' .
  266. '<textarea cols="40" id="post_123_body" name="post[123][body]" rows="20">Back to the hill and over it again!</textarea>' .
  267. '<input name="post[123][secret]" type="hidden" value="0" />' .
  268. '<input checked="checked" id="post_123_secret" name="post[123][secret]" type="checkbox" value="1" />' .
  269. '</form>';
  270. $this->assertEquals($expected, ob_get_clean());
  271. }
  272. public function testFieldsFor()
  273. {
  274. ob_start();
  275. $fields = $this->view->fieldsFor('post', $this->post);
  276. echo $fields->textField('title');
  277. echo $fields->textArea('body');
  278. echo $fields->checkBox('secret');
  279. $fields->end();
  280. $expected =
  281. '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
  282. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
  283. '<input name="post[secret]" type="hidden" value="0" />' .
  284. '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />';
  285. $this->assertEquals($expected, ob_get_clean());
  286. }
  287. public function testNestedFieldsFor()
  288. {
  289. ob_start();
  290. $form = $this->view->formFor('post', $this->post);
  291. $fields = $form->fieldsFor('comment', $this->post);
  292. echo $fields->textField('title');
  293. $fields->end();
  294. $form->end();
  295. $expected =
  296. '<form action="http://www.example.com" method="post">' .
  297. '<input id="post_comment_title" name="post[comment][title]" size="30" type="text" value="Hello World" />' .
  298. '</form>';
  299. $this->assertEquals($expected, ob_get_clean());
  300. }
  301. public function testFieldsForWithoutObject()
  302. {
  303. ob_start();
  304. $fields = $this->view->fieldsFor('post');
  305. echo $fields->textField('title');
  306. echo $fields->textArea('body');
  307. echo $fields->checkBox('secret');
  308. $fields->end();
  309. $expected =
  310. '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
  311. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
  312. '<input name="post[secret]" type="hidden" value="0" />' .
  313. '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />';
  314. $this->assertEquals($expected, ob_get_clean());
  315. }
  316. public function testFieldsForobjectWithBracketedName()
  317. {
  318. ob_start();
  319. $fields = $this->view->fieldsFor('author[post]', $this->post);
  320. echo $fields->textField('title');
  321. $fields->end();
  322. $this->assertEquals(
  323. '<input id="author_post_title" name="author[post][title]" size="30" type="text" value="Hello World" />',
  324. ob_get_clean());
  325. }
  326. public function testFormbuilderDoesNotHaveFormForMethod()
  327. {
  328. $methods = get_class_methods('Mad_View_Helper_Form_Builder');
  329. $this->assertTrue(empty($methods['formFor']));
  330. }
  331. public function testFormForAndFieldsFor()
  332. {
  333. ob_start();
  334. $postForm = $this->view->formFor('post', $this->post, array('html' => array('id' => 'create-post')));
  335. echo $postForm->textField('title');
  336. echo $postForm->textArea('body');
  337. $parentFields = $this->view->fieldsFor('parent_post', $this->post);
  338. echo $parentFields->checkBox('secret');
  339. $parentFields->end();
  340. $postForm->end();
  341. $expected =
  342. '<form action="http://www.example.com" id="create-post" method="post">' .
  343. '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
  344. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
  345. '<input name="parent_post[secret]" type="hidden" value="0" />' .
  346. '<input checked="checked" id="parent_post_secret" name="parent_post[secret]" type="checkbox" value="1" />' .
  347. '</form>';
  348. $this->assertEquals($expected, ob_get_clean());
  349. }
  350. public function testFormForWithCustomBuilder()
  351. {
  352. ob_start();
  353. $form = $this->view->formFor('post', $this->post, array('builder' => 'Mad_View_Helper_FormTest_BuilderMock'));
  354. echo $form->textField('bar');
  355. echo $form->foo();
  356. $form->end();
  357. $expected =
  358. '<form action="http://www.example.com" method="post">' .
  359. '<input id="post_bar" name="post[bar]" size="30" type="text" />' .
  360. '<foo /></form>';
  361. $this->assertEquals($expected, ob_get_clean());
  362. }
  363. public function testDefaultFormBuilder()
  364. {
  365. $oldDefaultFormBuilder = Mad_View_Base::$defaultFormBuilder;
  366. Mad_View_Base::$defaultFormBuilder = 'Mad_View_Helper_FormTest_BuilderMock';
  367. try {
  368. ob_start();
  369. $form = $this->view->formFor('post', $this->post);
  370. echo $form->textField('bar');
  371. echo $form->foo();
  372. $form->end();
  373. $expected =
  374. '<form action="http://www.example.com" method="post">' .
  375. '<input id="post_bar" name="post[bar]" size="30" type="text" />' .
  376. '<foo /></form>';
  377. $this->assertEquals($expected, ob_get_clean());
  378. } catch (Exception $e) {}
  379. Mad_View_Base::$defaultFormBuilder = $oldDefaultFormBuilder;
  380. }
  381. // @todo test_default_form_builder_with_active_record_helpers
  382. // @todo test_remote_form_for_with_labelled_builder
  383. public function testFieldsForWithCustomBuilder()
  384. {
  385. ob_start();
  386. $fields = $this->view->fieldsFor('post', $this->post, array('builder' => 'Mad_View_Helper_FormTest_BuilderMock'));
  387. echo $fields->textField('bar');
  388. echo $fields->foo();
  389. $fields->end();
  390. $this->assertEquals(
  391. '<input id="post_bar" name="post[bar]" size="30" type="text" /><foo />',
  392. ob_get_clean());
  393. }
  394. public function testFormForWithHtmlOptionsAddsOptionsToFormTag()
  395. {
  396. ob_start();
  397. $form = $this->view->formFor('post', $this->post, array('html' => array('id' => 'some_form',
  398. 'class' => 'some_class')));
  399. $form->end();
  400. $this->assertEquals(
  401. '<form action="http://www.example.com" class="some_class" id="some_form" method="post"></form>',
  402. ob_get_clean());
  403. }
  404. public function testFormForWithHiddenFieldMadOnly()
  405. {
  406. ob_start();
  407. $form = $this->view->formFor('post', $this->post);
  408. echo $form->hiddenField('title');
  409. $form->end();
  410. $expected =
  411. '<form action="http://www.example.com" method="post">' .
  412. '<input id="post_title" name="post[title]" type="hidden" value="Hello World" />' .
  413. '</form>';
  414. $this->assertEquals($expected, ob_get_clean());
  415. }
  416. public function testFormForWithFileFieldMadOnly()
  417. {
  418. ob_start();
  419. $form = $this->view->formFor('post', $this->post);
  420. echo $form->fileField('title');
  421. $form->end();
  422. $expected =
  423. '<form action="http://www.example.com" method="post">' .
  424. '<input id="post_title" name="post[title]" size="30" type="file" />' .
  425. '</form>';
  426. $this->assertEquals($expected, ob_get_clean());
  427. }
  428. public function testLabel()
  429. {
  430. $expected = '<label for="post_secret">Secret</label>';
  431. $this->assertEquals($expected, $this->view->label('post', 'secret'));
  432. }
  433. public function testLabelWithSeperator()
  434. {
  435. $expected = '<label for="post_secret_elixir">Secret Elixir</label>';
  436. $this->assertEquals($expected, $this->view->label('post', 'secret_elixir'));
  437. }
  438. public function testLabelWithValue()
  439. {
  440. $expected = '<label for="post_secret">Value</label>';
  441. $this->assertEquals($expected, $this->view->label('post', 'secret', array('value' => 'Value')));
  442. }
  443. public function testLabelWithFor()
  444. {
  445. $expected = '<label for="for">Secret</label>';
  446. $this->assertEquals($expected, $this->view->label('post', 'secret', array('for' => 'for')));
  447. }
  448. // @todo test_form_for_with_string_url_option
  449. // @todo test_form_for_with_hash_url_option
  450. // @todo test_remote_form_for_with_html_options_adds_options_to_form_tag
  451. }
  452. class Mad_View_Helper_FormTest_BuilderMock extends Mad_View_Helper_Form_Builder
  453. {
  454. public function foo()
  455. {
  456. return '<foo />';
  457. }
  458. }