FormTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. * @return void
  33. */
  34. public function testSchema()
  35. {
  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. * Test setSchema() and getSchema()
  48. *
  49. * @return void
  50. */
  51. public function testSetGetSchema()
  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. * @return void
  67. */
  68. public function testGetValidator()
  69. {
  70. $form = $this->getMockBuilder(Form::class)
  71. ->setMethods(['buildValidator'])
  72. ->getMock();
  73. $form->expects($this->once())
  74. ->method('buildValidator');
  75. $this->assertInstanceof(Validator::class, $form->getValidator());
  76. }
  77. /**
  78. * Test setValidator()
  79. *
  80. * @return void
  81. */
  82. public function testSetValidator()
  83. {
  84. $form = new Form();
  85. $validator = new Validator();
  86. $form->setValidator('default', $validator);
  87. $this->assertSame($validator, $form->getValidator());
  88. }
  89. /**
  90. * Test validate method.
  91. *
  92. * @return void
  93. */
  94. public function testValidate()
  95. {
  96. $form = new Form();
  97. $form->getValidator()
  98. ->add('email', 'format', ['rule' => 'email'])
  99. ->add('body', 'length', ['rule' => ['minLength', 12]]);
  100. $data = [
  101. 'email' => 'rong',
  102. 'body' => 'too short',
  103. ];
  104. $this->assertFalse($form->validate($data));
  105. $this->assertCount(2, $form->getErrors());
  106. $data = [
  107. 'email' => 'test@example.com',
  108. 'body' => 'Some content goes here',
  109. ];
  110. $this->assertTrue($form->validate($data));
  111. $this->assertCount(0, $form->getErrors());
  112. }
  113. /**
  114. * Test the get errors methods.
  115. *
  116. * @return void
  117. */
  118. public function testGetErrors()
  119. {
  120. $form = new Form();
  121. $form->getValidator()
  122. ->add('email', 'format', [
  123. 'message' => 'Must be a valid email',
  124. 'rule' => 'email',
  125. ])
  126. ->add('body', 'length', [
  127. 'message' => 'Must be so long',
  128. 'rule' => ['minLength', 12],
  129. ]);
  130. $data = [
  131. 'email' => 'rong',
  132. 'body' => 'too short',
  133. ];
  134. $form->validate($data);
  135. $errors = $form->getErrors();
  136. $this->assertCount(2, $errors);
  137. $this->assertSame('Must be a valid email', $errors['email']['format']);
  138. $this->assertSame('Must be so long', $errors['body']['length']);
  139. }
  140. /**
  141. * Test setErrors()
  142. *
  143. * @return void
  144. */
  145. public function testSetErrors()
  146. {
  147. $form = new Form();
  148. $expected = [
  149. 'field_name' => ['rule_name' => 'message'],
  150. ];
  151. $form->setErrors($expected);
  152. $this->assertSame($expected, $form->getErrors());
  153. }
  154. /**
  155. * Test _execute is skipped on validation failure.
  156. *
  157. * @return void
  158. */
  159. public function testExecuteInvalid()
  160. {
  161. $form = $this->getMockBuilder('Cake\Form\Form')
  162. ->setMethods(['_execute'])
  163. ->getMock();
  164. $form->getValidator()
  165. ->add('email', 'format', ['rule' => 'email']);
  166. $data = [
  167. 'email' => 'rong',
  168. ];
  169. $form->expects($this->never())
  170. ->method('_execute');
  171. $this->assertFalse($form->execute($data));
  172. }
  173. /**
  174. * test execute() when data is valid.
  175. *
  176. * @return void
  177. */
  178. public function testExecuteValid()
  179. {
  180. $form = new Form();
  181. $form->getValidator()
  182. ->add('email', 'format', ['rule' => 'email']);
  183. $data = [
  184. 'email' => 'test@example.com',
  185. ];
  186. $this->assertTrue($form->execute($data));
  187. }
  188. /**
  189. * Test setting and getting form data.
  190. *
  191. * @return void
  192. */
  193. public function testDataSetGet()
  194. {
  195. $form = new Form();
  196. $expected = ['title' => 'title', 'is_published' => true];
  197. $form->setData(['title' => 'title', 'is_published' => true]);
  198. $this->assertSame($expected, $form->getData());
  199. $this->assertSame('title', $form->getData('title'));
  200. $this->assertNull($form->getData('non-existent'));
  201. }
  202. /**
  203. * test __debugInfo
  204. *
  205. * @return void
  206. */
  207. public function testDebugInfo()
  208. {
  209. $form = new Form();
  210. $result = $form->__debugInfo();
  211. $this->assertArrayHasKey('_schema', $result);
  212. $this->assertArrayHasKey('_errors', $result);
  213. $this->assertArrayHasKey('_validator', $result);
  214. $this->assertArrayHasKey('_data', $result);
  215. }
  216. }