FormExtHelperTest.php 8.0 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, [
  21. 'form' => [
  22. 'method' => 'post', 'action' => '/posts/delete/1',
  23. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  24. ],
  25. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  26. '/form',
  27. 'a' => ['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', ['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, [
  49. 'form' => [
  50. 'method' => 'post', 'action' => '/posts/delete/1',
  51. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  52. ],
  53. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  54. '/form',
  55. 'a' => ['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, [
  67. 'div' => [
  68. 'class' => 'input text'
  69. ],
  70. 'label' => ['for' => 'ContactExtImrequiredonupdate'],
  71. 'Imrequiredonupdate',
  72. '/label',
  73. 'input' => ['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, [
  79. 'div' => [
  80. 'class' => 'input text required'
  81. ],
  82. 'label' => ['for' => 'ContactExtImrequiredonupdate'],
  83. 'Imrequiredonupdate',
  84. '/label',
  85. 'input' => ['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, [
  99. 'form' => [
  100. 'action' => '/',
  101. 'novalidate' => 'novalidate',
  102. 'id' => 'ContactExtForm',
  103. 'method' => 'post',
  104. 'accept-charset' => 'utf-8',
  105. ],
  106. 'div' => [
  107. 'style' => 'display:none;'
  108. ],
  109. 'input' => [
  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, [
  119. 'form' => [
  120. 'action' => '/',
  121. 'id' => 'ContactExtForm',
  122. 'method' => 'post',
  123. 'accept-charset' => 'utf-8',
  124. ],
  125. 'div' => [
  126. 'style' => 'display:none;'
  127. ],
  128. 'input' => [
  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, ['input' => ['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, [
  150. 'textarea' => ['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', ['type' => 'text']);
  156. $this->assertTags($result, [
  157. 'div' => ['class' => 'input text'],
  158. 'label' => ['for' => 'ModelField'],
  159. 'Field',
  160. '/label',
  161. 'input' => ['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', ['type' => 'textarea']);
  166. //debug($result);
  167. $this->assertTags($result, [
  168. 'div' => ['class' => 'input textarea'],
  169. 'label' => ['for' => 'ModelField'],
  170. 'Field',
  171. '/label',
  172. 'textarea' => ['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 = [
  196. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  197. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  198. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  199. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  200. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  201. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  202. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  203. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  204. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null]
  205. ];
  206. /**
  207. * Validate property
  208. *
  209. * @var array
  210. */
  211. public $validate = [
  212. 'non_existing' => [],
  213. 'idontexist' => [],
  214. 'imrequired' => ['rule' => ['between', 5, 30], 'allowEmpty' => false],
  215. 'imrequiredonupdate' => ['notBlank' => ['rule' => 'alphaNumeric', 'on' => 'update']],
  216. 'imrequiredoncreate' => ['required' => ['rule' => 'alphaNumeric', 'on' => 'create']],
  217. 'imrequiredonboth' => [
  218. 'required' => ['rule' => 'alphaNumeric'],
  219. ],
  220. 'string_required' => 'notBlank',
  221. 'imalsorequired' => ['rule' => 'alphaNumeric', 'allowEmpty' => false],
  222. 'imrequiredtoo' => ['rule' => 'notBlank'],
  223. 'required_one' => ['required' => ['rule' => ['notBlank']]],
  224. 'imnotrequired' => ['required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true],
  225. 'imalsonotrequired' => [
  226. 'alpha' => ['rule' => 'alphaNumeric', 'allowEmpty' => true],
  227. 'between' => ['rule' => ['between', 5, 30]],
  228. ],
  229. 'imalsonotrequired2' => [
  230. 'alpha' => ['rule' => 'alphaNumeric', 'allowEmpty' => true],
  231. 'between' => ['rule' => ['between', 5, 30], 'allowEmpty' => true],
  232. ],
  233. 'imnotrequiredeither' => ['required' => true, 'rule' => ['between', 5, 30], 'allowEmpty' => true],
  234. 'iamrequiredalways' => [
  235. 'email' => ['rule' => 'email'],
  236. 'rule_on_create' => ['rule' => ['maxLength', 50], 'on' => 'create'],
  237. 'rule_on_update' => ['rule' => ['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. }