FormExtHelperTest.php 8.0 KB

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