FormTest.php 7.9 KB

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