FormExtHelperTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. }
  9. public function tearDown() {
  10. }
  11. public function testObject() {
  12. $this->assertTrue(is_a($this->Form, 'FormExtHelper'));
  13. }
  14. public function testPostLink() {
  15. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  16. $this->assertTags($result, array(
  17. 'form' => array(
  18. 'method' => 'post', 'action' => '/posts/delete/1',
  19. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  20. ),
  21. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  22. '/form',
  23. 'a' => array('href' => '#', 'class' => 'postLink', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  24. 'Delete',
  25. '/a'
  26. ));
  27. }
  28. public function testDeleteLink() {
  29. $result = $this->Form->deleteLink('Delete', '/posts/delete/1');
  30. $this->assertTags($result, array(
  31. 'form' => array(
  32. 'method' => 'post', 'action' => '/posts/delete/1',
  33. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  34. ),
  35. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  36. '/form',
  37. 'a' => array('href' => '#', 'class' => 'deleteLink', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  38. 'Delete',
  39. '/a'
  40. ));
  41. }
  42. public function testAutoRequire() {
  43. $this->Form->request->data['ContactExt']['id'] = 1;
  44. $this->Form->create('ContactExt');
  45. Configure::write('Validation.autoRequire', false);
  46. Configure::write('Validation.browserAutoRequire', false);
  47. $result = $this->Form->input('ContactExt.imrequiredonupdate');
  48. $this->assertTags($result, array(
  49. 'div' => array(
  50. 'class' => 'input text'
  51. ),
  52. 'label' => array('for' => 'ContactExtImrequiredonupdate'),
  53. 'Imrequiredonupdate',
  54. '/label',
  55. 'input' => array('name' => 'data[ContactExt][imrequiredonupdate]', 'type' => 'text', 'id' => 'ContactExtImrequiredonupdate'),
  56. '/div'
  57. ));
  58. Configure::write('Validation.autoRequire', true);
  59. Configure::write('Validation.browserAutoRequire', true);
  60. $result = $this->Form->input('ContactExt.imrequiredonupdate');
  61. $this->assertTags($result, array(
  62. 'div' => array(
  63. 'class' => 'input text required'
  64. ),
  65. 'label' => array('for' => 'ContactExtImrequiredonupdate'),
  66. 'Imrequiredonupdate',
  67. '/label',
  68. 'input' => array('name' => 'data[ContactExt][imrequiredonupdate]', 'type' => 'text', 'id' => 'ContactExtImrequiredonupdate'),
  69. '/div'
  70. ));
  71. }
  72. /**
  73. * testNormalize method
  74. *
  75. * test that whitespaces are normalized for all inputs except textareas (which also understand new line characters)
  76. *
  77. * @return void
  78. */
  79. public function testNormalize() {
  80. $this->Form->request->data['Model']['field'] = "My\nvalue";
  81. $result = $this->Form->text('Model.field');
  82. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value'=>'My value', 'id' => 'ModelField')));
  83. $this->Form->request->data['Model']['field'] = "My\nvalue";
  84. $result = $this->Form->textarea('Model.field');
  85. $this->assertTags($result, array(
  86. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  87. "My\nvalue",
  88. '/textarea'
  89. ));
  90. $this->Form->request->data['Model']['field'] = "My\nvalue";
  91. $result = $this->Form->input('Model.field', array('type' => 'text'));
  92. $this->assertTags($result, array(
  93. 'div' => array('class' => 'input text'),
  94. 'label' => array('for' => 'ModelField'),
  95. 'Field',
  96. '/label',
  97. 'input' => array('name' => 'data[Model][field]', 'type' => 'text', 'value' => 'My value', 'id' => 'ModelField'),
  98. '/div'
  99. ));
  100. $this->Form->request->data['Model']['field'] = "My\nvalue";
  101. $result = $this->Form->input('Model.field', array('type' => 'textarea'));
  102. //debug($result);
  103. $this->assertTags($result, array(
  104. 'div' => array('class' => 'input textarea'),
  105. 'label' => array('for' => 'ModelField'),
  106. 'Field',
  107. '/label',
  108. 'textarea' => array('name' => 'data[Model][field]', 'cols' => '30', 'rows' => 6, 'id' => 'ModelField'),
  109. "My\nvalue",
  110. '/textarea',
  111. '/div'
  112. ));
  113. }
  114. }
  115. /**
  116. * Contact class
  117. *
  118. * @package cake
  119. * @package Cake.Test.Case.View.Helper
  120. */
  121. class ContactExt extends CakeTestModel {
  122. /**
  123. * useTable property
  124. *
  125. * @var bool false
  126. */
  127. public $useTable = false;
  128. /**
  129. * Default schema
  130. *
  131. * @var array
  132. */
  133. protected $_schema = array(
  134. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  135. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  136. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  137. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  138. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  139. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  140. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  141. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  142. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  143. );
  144. /**
  145. * validate property
  146. *
  147. * @var array
  148. */
  149. public $validate = array(
  150. 'non_existing' => array(),
  151. 'idontexist' => array(),
  152. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  153. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  154. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  155. 'imrequiredonboth' => array(
  156. 'required' => array('rule' => 'alphaNumeric'),
  157. ),
  158. 'string_required' => 'notEmpty',
  159. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  160. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  161. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  162. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  163. 'imalsonotrequired' => array(
  164. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  165. 'between' => array('rule' => array('between', 5, 30)),
  166. ),
  167. 'imalsonotrequired2' => array(
  168. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  169. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  170. ),
  171. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  172. 'iamrequiredalways' => array(
  173. 'email' => array('rule' => 'email'),
  174. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  175. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  176. ),
  177. );
  178. /**
  179. * schema method
  180. *
  181. * @return void
  182. */
  183. public function setSchema($schema) {
  184. $this->_schema = $schema;
  185. }
  186. }