FormTest.php 7.3 KB

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