FormTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 Exception;
  22. use TestApp\Form\AppForm;
  23. use TestApp\Form\FormSchema;
  24. /**
  25. * Form test case.
  26. */
  27. class FormTest extends TestCase
  28. {
  29. /**
  30. * Test setSchema() and getSchema()
  31. */
  32. public function testSetGetSchema(): void
  33. {
  34. $form = new Form();
  35. $schema = $form->getSchema();
  36. $this->assertInstanceOf(Schema::class, $schema);
  37. $this->assertSame($schema, $form->getSchema(), 'Same instance each time');
  38. $schema = new Schema();
  39. $this->assertSame($form, $form->setSchema($schema));
  40. $this->assertSame($schema, $form->getSchema());
  41. $form = new AppForm();
  42. $this->assertInstanceOf(FormSchema::class, $form->getSchema());
  43. }
  44. /**
  45. * Test getValidator()
  46. */
  47. public function testGetValidator(): void
  48. {
  49. $form = new Form();
  50. $this->assertInstanceof(Validator::class, $form->getValidator());
  51. }
  52. /**
  53. * Test setValidator()
  54. */
  55. public function testSetValidator(): void
  56. {
  57. $form = new Form();
  58. $validator = new Validator();
  59. $form->setValidator('default', $validator);
  60. $this->assertSame($validator, $form->getValidator());
  61. }
  62. /**
  63. * Test validate method.
  64. */
  65. public function testValidate(): void
  66. {
  67. $form = new Form();
  68. $form->getValidator()
  69. ->add('email', 'format', ['rule' => 'email'])
  70. ->add('body', 'length', ['rule' => ['minLength', 12]]);
  71. $data = [
  72. 'email' => 'rong',
  73. 'body' => 'too short',
  74. ];
  75. $this->assertFalse($form->validate($data));
  76. $this->assertCount(2, $form->getErrors());
  77. $data = [
  78. 'email' => 'test@example.com',
  79. 'body' => 'Some content goes here',
  80. ];
  81. $this->assertTrue($form->validate($data));
  82. $this->assertCount(0, $form->getErrors());
  83. }
  84. /**
  85. * Test validate with custom validator
  86. */
  87. public function testValidateCustomValidator(): void
  88. {
  89. $form = new Form();
  90. $validator = clone $form->getValidator();
  91. $validator->add('email', 'format', ['rule' => 'email']);
  92. $form->setValidator('custom', $validator);
  93. $data = ['email' => 'wrong'];
  94. $this->assertFalse($form->validate($data, 'custom'));
  95. }
  96. /**
  97. * Test the get errors & get error methods.
  98. */
  99. public function testGetErrors(): void
  100. {
  101. $form = new Form();
  102. $form->getValidator()
  103. ->add('email', 'format', [
  104. 'message' => 'Must be a valid email',
  105. 'rule' => 'email',
  106. ])
  107. ->add('body', 'length', [
  108. 'message' => 'Must be so long',
  109. 'rule' => ['minLength', 12],
  110. ]);
  111. $data = [
  112. 'email' => 'rong',
  113. 'body' => 'too short',
  114. ];
  115. $form->validate($data);
  116. $errors = $form->getErrors();
  117. $this->assertCount(2, $errors);
  118. $this->assertSame('Must be a valid email', $errors['email']['format']);
  119. $this->assertSame('Must be so long', $errors['body']['length']);
  120. $error = $form->getError('email');
  121. $this->assertSame(['format' => 'Must be a valid email'], $error);
  122. $error = $form->getError('foo');
  123. $this->assertSame([], $error);
  124. }
  125. /**
  126. * Test setErrors()
  127. */
  128. public function testSetErrors(): void
  129. {
  130. $form = new Form();
  131. $expected = [
  132. 'field_name' => ['rule_name' => 'message'],
  133. ];
  134. $form->setErrors($expected);
  135. $this->assertSame($expected, $form->getErrors());
  136. }
  137. /**
  138. * Test _execute is skipped on validation failure.
  139. */
  140. public function testExecuteInvalid(): void
  141. {
  142. $form = new class extends Form {
  143. // phpcs:ignore CakePHP.NamingConventions.ValidFunctionName.PublicWithUnderscore
  144. public function _execute(array $data): bool
  145. {
  146. throw new Exception('Should not be called');
  147. }
  148. };
  149. $form->getValidator()
  150. ->add('email', 'format', ['rule' => 'email']);
  151. $data = [
  152. 'email' => 'rong',
  153. ];
  154. $this->assertFalse($form->execute($data));
  155. }
  156. /**
  157. * test execute() when data is valid.
  158. */
  159. public function testExecuteValid(): void
  160. {
  161. $form = new Form();
  162. $form->getValidator()
  163. ->add('email', 'format', ['rule' => 'email']);
  164. $data = [
  165. 'email' => 'test@example.com',
  166. ];
  167. $this->assertTrue($form->execute($data));
  168. }
  169. /**
  170. * test execute() when data is valid.
  171. */
  172. public function testExecuteSkipValidation(): void
  173. {
  174. $form = new Form();
  175. $form->getValidator()
  176. ->add('email', 'format', ['rule' => 'email']);
  177. $data = [
  178. 'email' => 'wrong',
  179. ];
  180. $this->assertTrue($form->execute($data, ['validate' => false]));
  181. }
  182. /**
  183. * Test set() with one param.
  184. */
  185. public function testSetOneParam(): void
  186. {
  187. $form = new Form();
  188. $data = ['test' => 'val', 'foo' => 'bar'];
  189. $form->set($data);
  190. $this->assertEquals($data, $form->getData());
  191. $update = ['test' => 'updated'];
  192. $form->set($update);
  193. $this->assertSame('updated', $form->getData()['test']);
  194. }
  195. /**
  196. * test set() with 2 params
  197. */
  198. public function testSetTwoParam(): void
  199. {
  200. $form = new Form();
  201. $form->set('testing', 'value');
  202. $this->assertEquals(['testing' => 'value'], $form->getData());
  203. }
  204. /**
  205. * test chainable set()
  206. */
  207. public function testSetChained(): void
  208. {
  209. $form = new Form();
  210. $result = $form->set('testing', 'value')
  211. ->set('foo', 'bar');
  212. $this->assertSame($form, $result);
  213. $this->assertEquals(['testing' => 'value', 'foo' => 'bar'], $form->getData());
  214. }
  215. /**
  216. * Test setting and getting form data.
  217. */
  218. public function testDataSetGet(): void
  219. {
  220. $form = new Form();
  221. $expected = ['title' => 'title', 'is_published' => true];
  222. $form->setData(['title' => 'title', 'is_published' => true]);
  223. $this->assertSame($expected, $form->getData());
  224. $this->assertSame('title', $form->getData('title'));
  225. $this->assertNull($form->getData('nonexistent'));
  226. }
  227. /**
  228. * test __debugInfo
  229. */
  230. public function testDebugInfo(): void
  231. {
  232. $form = new Form();
  233. $result = $form->__debugInfo();
  234. $this->assertArrayHasKey('_schema', $result);
  235. $this->assertArrayHasKey('_errors', $result);
  236. $this->assertArrayHasKey('_validator', $result);
  237. $this->assertArrayHasKey('_data', $result);
  238. }
  239. }