FormExtHelperTest.php 8.1 KB

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