FormExtHelperTest.php 7.0 KB

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