FormTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 & get error 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. $error = $form->getError('email');
  143. $this->assertSame(['format' => 'Must be a valid email'], $error);
  144. $error = $form->getError('foo');
  145. $this->assertSame([], $error);
  146. }
  147. /**
  148. * Test setErrors()
  149. */
  150. public function testSetErrors(): void
  151. {
  152. $form = new Form();
  153. $expected = [
  154. 'field_name' => ['rule_name' => 'message'],
  155. ];
  156. $form->setErrors($expected);
  157. $this->assertSame($expected, $form->getErrors());
  158. }
  159. /**
  160. * Test _execute is skipped on validation failure.
  161. */
  162. public function testExecuteInvalid(): void
  163. {
  164. $form = $this->getMockBuilder('Cake\Form\Form')
  165. ->onlyMethods(['_execute'])
  166. ->getMock();
  167. $form->getValidator()
  168. ->add('email', 'format', ['rule' => 'email']);
  169. $data = [
  170. 'email' => 'rong',
  171. ];
  172. $form->expects($this->never())
  173. ->method('_execute');
  174. $this->assertFalse($form->execute($data));
  175. }
  176. /**
  177. * test execute() when data is valid.
  178. */
  179. public function testExecuteValid(): void
  180. {
  181. $form = new Form();
  182. $form->getValidator()
  183. ->add('email', 'format', ['rule' => 'email']);
  184. $data = [
  185. 'email' => 'test@example.com',
  186. ];
  187. $this->assertTrue($form->execute($data));
  188. }
  189. /**
  190. * test execute() when data is valid.
  191. */
  192. public function testExecuteSkipValidation(): void
  193. {
  194. $form = new Form();
  195. $form->getValidator()
  196. ->add('email', 'format', ['rule' => 'email']);
  197. $data = [
  198. 'email' => 'wrong',
  199. ];
  200. $this->assertTrue($form->execute($data, ['validate' => false]));
  201. }
  202. /**
  203. * Test set() with one param.
  204. */
  205. public function testSetOneParam(): void
  206. {
  207. $form = new Form();
  208. $data = ['test' => 'val', 'foo' => 'bar'];
  209. $form->set($data);
  210. $this->assertEquals($data, $form->getData());
  211. $update = ['test' => 'updated'];
  212. $form->set($update);
  213. $this->assertSame('updated', $form->getData()['test']);
  214. }
  215. /**
  216. * test set() with 2 params
  217. */
  218. public function testSetTwoParam(): void
  219. {
  220. $form = new Form();
  221. $form->set('testing', 'value');
  222. $this->assertEquals(['testing' => 'value'], $form->getData());
  223. }
  224. /**
  225. * test chainable set()
  226. */
  227. public function testSetChained(): void
  228. {
  229. $form = new Form();
  230. $result = $form->set('testing', 'value')
  231. ->set('foo', 'bar');
  232. $this->assertSame($form, $result);
  233. $this->assertEquals(['testing' => 'value', 'foo' => 'bar'], $form->getData());
  234. }
  235. /**
  236. * Test setting and getting form data.
  237. */
  238. public function testDataSetGet(): void
  239. {
  240. $form = new Form();
  241. $expected = ['title' => 'title', 'is_published' => true];
  242. $form->setData(['title' => 'title', 'is_published' => true]);
  243. $this->assertSame($expected, $form->getData());
  244. $this->assertSame('title', $form->getData('title'));
  245. $this->assertNull($form->getData('nonexistent'));
  246. }
  247. /**
  248. * test __debugInfo
  249. */
  250. public function testDebugInfo(): void
  251. {
  252. $form = new Form();
  253. $result = $form->__debugInfo();
  254. $this->assertArrayHasKey('_schema', $result);
  255. $this->assertArrayHasKey('_errors', $result);
  256. $this->assertArrayHasKey('_validator', $result);
  257. $this->assertArrayHasKey('_data', $result);
  258. }
  259. }