FormTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /**
  20. * Form test case.
  21. */
  22. class FormTest extends TestCase
  23. {
  24. /**
  25. * Test schema()
  26. *
  27. * @return void
  28. */
  29. public function testSchema()
  30. {
  31. $form = new Form();
  32. $schema = $form->schema();
  33. $this->assertInstanceOf('Cake\Form\Schema', $schema);
  34. $this->assertSame($schema, $form->schema(), 'Same instance each time');
  35. $schema = $this->getMockBuilder('Cake\Form\Schema')->getMock();
  36. $this->assertSame($schema, $form->schema($schema));
  37. $this->assertSame($schema, $form->schema());
  38. }
  39. /**
  40. * Test validator()
  41. *
  42. * @return void
  43. * @group deprecated
  44. */
  45. public function testValidator()
  46. {
  47. $this->deprecated(function () {
  48. $form = new Form();
  49. $validator = $form->validator();
  50. $this->assertInstanceOf('Cake\Validation\Validator', $validator);
  51. $this->assertSame($validator, $form->validator(), 'Same instance each time');
  52. $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
  53. $this->assertSame($validator, $form->validator($validator));
  54. $this->assertSame($validator, $form->validator());
  55. });
  56. }
  57. /**
  58. * Test getValidator()
  59. *
  60. * @return void
  61. */
  62. public function testGetValidator()
  63. {
  64. $form = $this->getMockBuilder(Form::class)
  65. ->setMethods(['buildValidator'])
  66. ->getMock();
  67. $form->expects($this->once())
  68. ->method('buildValidator');
  69. $this->assertInstanceof(Validator::class, $form->getValidator());
  70. }
  71. /**
  72. * Test setValidator()
  73. *
  74. * @return void
  75. */
  76. public function testSetValidator()
  77. {
  78. $form = new Form();
  79. $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
  80. $form->setValidator('default', $validator);
  81. $this->assertSame($validator, $form->getValidator());
  82. }
  83. /**
  84. * Test validate method.
  85. *
  86. * @return void
  87. */
  88. public function testValidate()
  89. {
  90. $form = new Form();
  91. $form->getValidator()
  92. ->add('email', 'format', ['rule' => 'email'])
  93. ->add('body', 'length', ['rule' => ['minLength', 12]]);
  94. $data = [
  95. 'email' => 'rong',
  96. 'body' => 'too short'
  97. ];
  98. $this->assertFalse($form->validate($data));
  99. $this->assertCount(2, $form->errors());
  100. $data = [
  101. 'email' => 'test@example.com',
  102. 'body' => 'Some content goes here'
  103. ];
  104. $this->assertTrue($form->validate($data));
  105. $this->assertCount(0, $form->errors());
  106. }
  107. /**
  108. * Test the errors methods.
  109. *
  110. * @return void
  111. */
  112. public function testErrors()
  113. {
  114. $form = new Form();
  115. $form->getValidator()
  116. ->add('email', 'format', [
  117. 'message' => 'Must be a valid email',
  118. 'rule' => 'email'
  119. ])
  120. ->add('body', 'length', [
  121. 'message' => 'Must be so long',
  122. 'rule' => ['minLength', 12],
  123. ]);
  124. $data = [
  125. 'email' => 'rong',
  126. 'body' => 'too short'
  127. ];
  128. $form->validate($data);
  129. $errors = $form->errors();
  130. $this->assertCount(2, $errors);
  131. $this->assertEquals('Must be a valid email', $errors['email']['format']);
  132. $this->assertEquals('Must be so long', $errors['body']['length']);
  133. }
  134. /**
  135. * Test setErrors()
  136. *
  137. * @return void
  138. */
  139. public function testSetErrors()
  140. {
  141. $form = new Form();
  142. $expected = [
  143. 'field_name' => ['rule_name' => 'message']
  144. ];
  145. $form->setErrors($expected);
  146. $this->assertSame($expected, $form->errors());
  147. }
  148. /**
  149. * Test _execute is skipped on validation failure.
  150. *
  151. * @return void
  152. */
  153. public function testExecuteInvalid()
  154. {
  155. $form = $this->getMockBuilder('Cake\Form\Form')
  156. ->setMethods(['_execute'])
  157. ->getMock();
  158. $form->getValidator()
  159. ->add('email', 'format', ['rule' => 'email']);
  160. $data = [
  161. 'email' => 'rong'
  162. ];
  163. $form->expects($this->never())
  164. ->method('_execute');
  165. $this->assertFalse($form->execute($data));
  166. }
  167. /**
  168. * test execute() when data is valid.
  169. *
  170. * @return void
  171. */
  172. public function testExecuteValid()
  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' => 'test@example.com'
  181. ];
  182. $form->expects($this->once())
  183. ->method('_execute')
  184. ->with($data)
  185. ->will($this->returnValue(true));
  186. $this->assertTrue($form->execute($data));
  187. }
  188. /**
  189. * test __debugInfo
  190. *
  191. * @return void
  192. */
  193. public function testDebugInfo()
  194. {
  195. $form = new Form();
  196. $result = $form->__debugInfo();
  197. $this->assertArrayHasKey('_schema', $result);
  198. $this->assertArrayHasKey('_errors', $result);
  199. $this->assertArrayHasKey('_validator', $result);
  200. }
  201. }