FormExtHelperTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. App::uses('FormExtHelper', 'Tools.View/Helper');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. App::uses('View', 'View');
  5. class FormExtHelperTest extends MyCakeTestCase {
  6. public function setUp() {
  7. $this->Form = new FormExtHelper(new View(null));
  8. parent::setUp();
  9. }
  10. public function testObject() {
  11. $this->assertInstanceOf('FormExtHelper', $this->Form);
  12. }
  13. /**
  14. * FormExtHelperTest::testPostLink()
  15. *
  16. * @return void
  17. */
  18. public function testPostLink() {
  19. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  20. $this->assertTags($result, array(
  21. 'form' => array(
  22. 'method' => 'post', 'action' => '/posts/delete/1',
  23. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  24. ),
  25. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  26. '/form',
  27. 'a' => array('href' => '#', 'class' => 'post-link postLink', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  28. 'Delete',
  29. '/a'
  30. ));
  31. }
  32. /**
  33. * FormExtHelperTest::testPostLinkShim()
  34. *
  35. * @return void
  36. */
  37. public function testPostLinkShim() {
  38. $result = $this->Form->postLink('foo', '/bar', array('confirm' => 'Confirm me'));
  39. $this->assertTextContains('onclick="if (confirm(&quot;Confirm me&quot;)) {', $result);
  40. }
  41. /**
  42. * FormExtHelperTest::testDeleteLink()
  43. *
  44. * @return void
  45. */
  46. public function testDeleteLink() {
  47. $result = $this->Form->deleteLink('Delete', '/posts/delete/1');
  48. $this->assertTags($result, array(
  49. 'form' => array(
  50. 'method' => 'post', 'action' => '/posts/delete/1',
  51. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  52. ),
  53. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  54. '/form',
  55. 'a' => array('href' => '#', 'class' => 'delete-link deleteLink', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  56. 'Delete',
  57. '/a'
  58. ));
  59. }
  60. //Not needed right now as autoRequire has been disabled
  61. public function _testAutoRequire() {
  62. $this->Form->request->data['ContactExt']['id'] = 1;
  63. $this->Form->create('ContactExt');
  64. Configure::write('Validation.autoRequire', false);
  65. $result = $this->Form->input('ContactExt.imrequiredonupdate');
  66. $this->assertTags($result, array(
  67. 'div' => array(
  68. 'class' => 'input text'
  69. ),
  70. 'label' => array('for' => 'ContactExtImrequiredonupdate'),
  71. 'Imrequiredonupdate',
  72. '/label',
  73. 'input' => array('name' => 'data[ContactExt][imrequiredonupdate]', 'type' => 'text', 'id' => 'ContactExtImrequiredonupdate'),
  74. '/div'
  75. ));
  76. Configure::write('Validation.autoRequire', true);
  77. $result = $this->Form->input('ContactExt.imrequiredonupdate');
  78. $this->assertTags($result, array(
  79. 'div' => array(
  80. 'class' => 'input text required'
  81. ),
  82. 'label' => array('for' => 'ContactExtImrequiredonupdate'),
  83. 'Imrequiredonupdate',
  84. '/label',
  85. 'input' => array('name' => 'data[ContactExt][imrequiredonupdate]', 'type' => 'text', 'id' => 'ContactExtImrequiredonupdate'),
  86. '/div'
  87. ));
  88. }
  89. /**
  90. * Test that browserAutoRequire disables html5 frontend form validation
  91. *
  92. * @return void
  93. */
  94. public function testBrowserAutoRequire() {
  95. $this->Form->request->data['ContactExt']['id'] = 1;
  96. Configure::write('Validation.browserAutoRequire', false);
  97. $result = $this->Form->create('ContactExt');
  98. $this->assertTags($result, array(
  99. 'form' => array(
  100. 'action' => '/',
  101. 'novalidate' => 'novalidate',
  102. 'id' => 'ContactExtForm',
  103. 'method' => 'post',
  104. 'accept-charset' => 'utf-8',
  105. ),
  106. 'div' => array(
  107. 'style' => 'display:none;'
  108. ),
  109. 'input' => array(
  110. 'type' => 'hidden',
  111. 'name' => '_method',
  112. 'value' => 'PUT'
  113. ),
  114. '/div',
  115. ));
  116. Configure::write('Validation.browserAutoRequire', true);
  117. $result = $this->Form->create('ContactExt');
  118. $this->assertTags($result, array(
  119. 'form' => array(
  120. 'action' => '/',
  121. 'id' => 'ContactExtForm',
  122. 'method' => 'post',
  123. 'accept-charset' => 'utf-8',
  124. ),
  125. 'div' => array(
  126. 'style' => 'display:none;'
  127. ),
  128. 'input' => array(
  129. 'type' => 'hidden',
  130. 'name' => '_method',
  131. 'value' => 'PUT'
  132. ),
  133. '/div',
  134. ));
  135. }
  136. /**
  137. * TestNormalize method
  138. *
  139. * test that whitespaces are normalized for all inputs except textareas (which also understand new line characters)
  140. *
  141. * @return void
  142. */
  143. public function testNormalize() {
  144. $this->Form->request->data['Model']['field'] = "My\nvalue";
  145. $result = $this->Form->text('Model.field');
  146. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'My value', 'id' => 'ModelField')));
  147. $this->Form->request->data['Model']['field'] = "My\nvalue";
  148. $result = $this->Form->textarea('Model.field');
  149. $this->assertTags($result, array(
  150. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  151. "My\nvalue",
  152. '/textarea'
  153. ));
  154. $this->Form->request->data['Model']['field'] = "My\nvalue";
  155. $result = $this->Form->input('Model.field', array('type' => 'text'));
  156. $this->assertTags($result, array(
  157. 'div' => array('class' => 'input text'),
  158. 'label' => array('for' => 'ModelField'),
  159. 'Field',
  160. '/label',
  161. 'input' => array('name' => 'data[Model][field]', 'type' => 'text', 'value' => 'My value', 'id' => 'ModelField'),
  162. '/div'
  163. ));
  164. $this->Form->request->data['Model']['field'] = "My\nvalue";
  165. $result = $this->Form->input('Model.field', array('type' => 'textarea'));
  166. //debug($result);
  167. $this->assertTags($result, array(
  168. 'div' => array('class' => 'input textarea'),
  169. 'label' => array('for' => 'ModelField'),
  170. 'Field',
  171. '/label',
  172. 'textarea' => array('name' => 'data[Model][field]', 'cols' => '30', 'rows' => 6, 'id' => 'ModelField'),
  173. "My\nvalue",
  174. '/textarea',
  175. '/div'
  176. ));
  177. }
  178. }
  179. /**
  180. * Contact class
  181. *
  182. */
  183. class ContactExt extends CakeTestModel {
  184. /**
  185. * UseTable property
  186. *
  187. * @var bool
  188. */
  189. public $useTable = false;
  190. /**
  191. * Default schema
  192. *
  193. * @var array
  194. */
  195. protected $_schema = array(
  196. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  197. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  198. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  199. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  200. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  201. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  202. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  203. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  204. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  205. );
  206. /**
  207. * Validate property
  208. *
  209. * @var array
  210. */
  211. public $validate = array(
  212. 'non_existing' => array(),
  213. 'idontexist' => array(),
  214. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  215. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  216. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  217. 'imrequiredonboth' => array(
  218. 'required' => array('rule' => 'alphaNumeric'),
  219. ),
  220. 'string_required' => 'notEmpty',
  221. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  222. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  223. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  224. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  225. 'imalsonotrequired' => array(
  226. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  227. 'between' => array('rule' => array('between', 5, 30)),
  228. ),
  229. 'imalsonotrequired2' => array(
  230. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  231. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  232. ),
  233. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  234. 'iamrequiredalways' => array(
  235. 'email' => array('rule' => 'email'),
  236. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  237. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  238. ),
  239. );
  240. /**
  241. * Schema method
  242. *
  243. * @return void
  244. */
  245. public function setSchema($schema) {
  246. $this->_schema = $schema;
  247. }
  248. }