FormTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Form;
  16. use Cake\Form\Form;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\Validation\Validator;
  19. use TestApp\Form\AppForm;
  20. use TestApp\Form\FormSchema;
  21. use TestApp\Form\ValidateForm;
  22. /**
  23. * Form test case.
  24. */
  25. class FormTest extends TestCase
  26. {
  27. /**
  28. * Test schema()
  29. *
  30. * @return void
  31. */
  32. public function testSchema()
  33. {
  34. $form = new Form();
  35. $schema = $form->schema();
  36. $this->assertInstanceOf('Cake\Form\Schema', $schema);
  37. $this->assertSame($schema, $form->schema(), 'Same instance each time');
  38. $schema = $this->getMockBuilder('Cake\Form\Schema')->getMock();
  39. $this->assertSame($schema, $form->schema($schema));
  40. $this->assertSame($schema, $form->schema());
  41. $form = new AppForm();
  42. $this->assertInstanceOf(FormSchema::class, $form->schema());
  43. }
  44. /**
  45. * Test validator()
  46. *
  47. * @return void
  48. * @group deprecated
  49. */
  50. public function testValidator()
  51. {
  52. $this->deprecated(function () {
  53. $form = new Form();
  54. $validator = $form->validator();
  55. $this->assertInstanceOf('Cake\Validation\Validator', $validator);
  56. $this->assertSame($validator, $form->validator(), 'Same instance each time');
  57. $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
  58. $this->assertSame($validator, $form->validator($validator));
  59. $this->assertSame($validator, $form->validator());
  60. });
  61. }
  62. /**
  63. * Test getValidator()
  64. *
  65. * @return void
  66. */
  67. public function testGetValidator()
  68. {
  69. $form = $this->getMockBuilder(Form::class)
  70. ->setMethods(['buildValidator'])
  71. ->getMock();
  72. $form->expects($this->once())
  73. ->method('buildValidator');
  74. $this->assertInstanceof(Validator::class, $form->getValidator());
  75. }
  76. /**
  77. * Test setValidator()
  78. *
  79. * @return void
  80. */
  81. public function testSetValidator()
  82. {
  83. $form = new Form();
  84. $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
  85. $form->setValidator('default', $validator);
  86. $this->assertSame($validator, $form->getValidator());
  87. }
  88. /**
  89. * Test validate method.
  90. *
  91. * @return void
  92. */
  93. public function testValidate()
  94. {
  95. $form = new Form();
  96. $form->getValidator()
  97. ->add('email', 'format', ['rule' => 'email'])
  98. ->add('body', 'length', ['rule' => ['minLength', 12]]);
  99. $data = [
  100. 'email' => 'rong',
  101. 'body' => 'too short'
  102. ];
  103. $this->assertFalse($form->validate($data));
  104. $this->assertCount(2, $form->errors());
  105. $data = [
  106. 'email' => 'test@example.com',
  107. 'body' => 'Some content goes here'
  108. ];
  109. $this->assertTrue($form->validate($data));
  110. $this->assertCount(0, $form->errors());
  111. }
  112. /**
  113. * tests validate using deprecated validate() method
  114. *
  115. * @return void
  116. */
  117. public function testValidateDeprected()
  118. {
  119. $this->deprecated(function() {
  120. $form = new ValidateForm();
  121. $data = [];
  122. $this->assertFalse($form->validate($data));
  123. $this->assertCount(1, $form->errors());
  124. });
  125. }
  126. /**
  127. * Test the errors methods.
  128. *
  129. * @return void
  130. */
  131. public function testErrors()
  132. {
  133. $form = new Form();
  134. $form->getValidator()
  135. ->add('email', 'format', [
  136. 'message' => 'Must be a valid email',
  137. 'rule' => 'email'
  138. ])
  139. ->add('body', 'length', [
  140. 'message' => 'Must be so long',
  141. 'rule' => ['minLength', 12],
  142. ]);
  143. $data = [
  144. 'email' => 'rong',
  145. 'body' => 'too short'
  146. ];
  147. $form->validate($data);
  148. $errors = $form->errors();
  149. $this->assertCount(2, $errors);
  150. $this->assertEquals('Must be a valid email', $errors['email']['format']);
  151. $this->assertEquals('Must be so long', $errors['body']['length']);
  152. }
  153. /**
  154. * Test setErrors()
  155. *
  156. * @return void
  157. */
  158. public function testSetErrors()
  159. {
  160. $form = new Form();
  161. $expected = [
  162. 'field_name' => ['rule_name' => 'message']
  163. ];
  164. $form->setErrors($expected);
  165. $this->assertSame($expected, $form->errors());
  166. }
  167. /**
  168. * Test _execute is skipped on validation failure.
  169. *
  170. * @return void
  171. */
  172. public function testExecuteInvalid()
  173. {
  174. $form = $this->getMockBuilder('Cake\Form\Form')
  175. ->setMethods(['_execute'])
  176. ->getMock();
  177. $form->getValidator()
  178. ->add('email', 'format', ['rule' => 'email']);
  179. $data = [
  180. 'email' => 'rong'
  181. ];
  182. $form->expects($this->never())
  183. ->method('_execute');
  184. $this->assertFalse($form->execute($data));
  185. }
  186. /**
  187. * test execute() when data is valid.
  188. *
  189. * @return void
  190. */
  191. public function testExecuteValid()
  192. {
  193. $form = $this->getMockBuilder('Cake\Form\Form')
  194. ->setMethods(['_execute'])
  195. ->getMock();
  196. $form->getValidator()
  197. ->add('email', 'format', ['rule' => 'email']);
  198. $data = [
  199. 'email' => 'test@example.com'
  200. ];
  201. $form->expects($this->once())
  202. ->method('_execute')
  203. ->with($data)
  204. ->will($this->returnValue(true));
  205. $this->assertTrue($form->execute($data));
  206. }
  207. /**
  208. * test __debugInfo
  209. *
  210. * @return void
  211. */
  212. public function testDebugInfo()
  213. {
  214. $form = new Form();
  215. $result = $form->__debugInfo();
  216. $this->assertArrayHasKey('_schema', $result);
  217. $this->assertArrayHasKey('_errors', $result);
  218. $this->assertArrayHasKey('_validator', $result);
  219. }
  220. }