FormTest.php 6.8 KB

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