FormTest.php 7.1 KB

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