FormTest.php 7.0 KB

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