FormTest.php 8.4 KB

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