FormTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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->getErrors());
  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->getErrors());
  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. $this->assertCount(1, $form->validator(), 'should have one rule');
  122. $data = [];
  123. $this->assertFalse($form->validate($data));
  124. $this->assertCount(1, $form->getErrors());
  125. });
  126. }
  127. /**
  128. * Test the errors methods.
  129. *
  130. * @return void
  131. */
  132. public function testErrors()
  133. {
  134. $this->deprecated(function () {
  135. $form = new Form();
  136. $form->getValidator()
  137. ->add('email', 'format', [
  138. 'message' => 'Must be a valid email',
  139. 'rule' => 'email',
  140. ])
  141. ->add('body', 'length', [
  142. 'message' => 'Must be so long',
  143. 'rule' => ['minLength', 12],
  144. ]);
  145. $data = [
  146. 'email' => 'rong',
  147. 'body' => 'too short',
  148. ];
  149. $form->validate($data);
  150. $errors = $form->errors();
  151. $this->assertCount(2, $errors);
  152. $this->assertEquals('Must be a valid email', $errors['email']['format']);
  153. $this->assertEquals('Must be so long', $errors['body']['length']);
  154. });
  155. }
  156. /**
  157. * Test the get errors methods.
  158. *
  159. * @return void
  160. */
  161. public function testGetErrors()
  162. {
  163. $form = new Form();
  164. $form->getValidator()
  165. ->add('email', 'format', [
  166. 'message' => 'Must be a valid email',
  167. 'rule' => 'email',
  168. ])
  169. ->add('body', 'length', [
  170. 'message' => 'Must be so long',
  171. 'rule' => ['minLength', 12],
  172. ]);
  173. $data = [
  174. 'email' => 'rong',
  175. 'body' => 'too short',
  176. ];
  177. $form->validate($data);
  178. $errors = $form->getErrors();
  179. $this->assertCount(2, $errors);
  180. $this->assertEquals('Must be a valid email', $errors['email']['format']);
  181. $this->assertEquals('Must be so long', $errors['body']['length']);
  182. }
  183. /**
  184. * Test setErrors()
  185. *
  186. * @return void
  187. */
  188. public function testSetErrors()
  189. {
  190. $form = new Form();
  191. $expected = [
  192. 'field_name' => ['rule_name' => 'message'],
  193. ];
  194. $form->setErrors($expected);
  195. $this->assertSame($expected, $form->getErrors());
  196. }
  197. /**
  198. * Test _execute is skipped on validation failure.
  199. *
  200. * @return void
  201. */
  202. public function testExecuteInvalid()
  203. {
  204. $form = $this->getMockBuilder('Cake\Form\Form')
  205. ->setMethods(['_execute'])
  206. ->getMock();
  207. $form->getValidator()
  208. ->add('email', 'format', ['rule' => 'email']);
  209. $data = [
  210. 'email' => 'rong',
  211. ];
  212. $form->expects($this->never())
  213. ->method('_execute');
  214. $this->assertFalse($form->execute($data));
  215. }
  216. /**
  217. * test execute() when data is valid.
  218. *
  219. * @return void
  220. */
  221. public function testExecuteValid()
  222. {
  223. $form = $this->getMockBuilder('Cake\Form\Form')
  224. ->setMethods(['_execute'])
  225. ->getMock();
  226. $form->getValidator()
  227. ->add('email', 'format', ['rule' => 'email']);
  228. $data = [
  229. 'email' => 'test@example.com',
  230. ];
  231. $form->expects($this->once())
  232. ->method('_execute')
  233. ->with($data)
  234. ->will($this->returnValue(true));
  235. $this->assertTrue($form->execute($data));
  236. }
  237. /**
  238. * Test setting and getting form data.
  239. *
  240. * @return void
  241. */
  242. public function testDataSetGet()
  243. {
  244. $form = new Form();
  245. $expected = ['title' => 'title', 'is_published' => true];
  246. $form->setData(['title' => 'title', 'is_published' => true]);
  247. $this->assertSame($expected, $form->getData());
  248. $this->assertEquals('title', $form->getData('title'));
  249. $this->assertNull($form->getData('non-existent'));
  250. }
  251. /**
  252. * test __debugInfo
  253. *
  254. * @return void
  255. */
  256. public function testDebugInfo()
  257. {
  258. $form = new Form();
  259. $result = $form->__debugInfo();
  260. $this->assertArrayHasKey('_schema', $result);
  261. $this->assertArrayHasKey('_errors', $result);
  262. $this->assertArrayHasKey('_validator', $result);
  263. $this->assertArrayHasKey('_data', $result);
  264. }
  265. }