FormTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Form;
  16. use Cake\Form\Form;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\Validation\Validator;
  19. use TestApp\Form\AppForm;
  20. use TestApp\Form\FormSchema;
  21. /**
  22. * Form test case.
  23. */
  24. class FormTest extends TestCase
  25. {
  26. /**
  27. * Test schema()
  28. *
  29. * @return void
  30. */
  31. public function testSchema()
  32. {
  33. $form = new Form();
  34. $schema = $form->schema();
  35. $this->assertInstanceOf('Cake\Form\Schema', $schema);
  36. $this->assertSame($schema, $form->schema(), 'Same instance each time');
  37. $schema = $this->getMockBuilder('Cake\Form\Schema')->getMock();
  38. $this->assertSame($schema, $form->schema($schema));
  39. $this->assertSame($schema, $form->schema());
  40. $form = new AppForm();
  41. $this->assertInstanceOf(FormSchema::class, $form->schema());
  42. }
  43. /**
  44. * Test getValidator()
  45. *
  46. * @return void
  47. */
  48. public function testGetValidator()
  49. {
  50. $form = $this->getMockBuilder(Form::class)
  51. ->setMethods(['buildValidator'])
  52. ->getMock();
  53. $form->expects($this->once())
  54. ->method('buildValidator');
  55. $this->assertInstanceof(Validator::class, $form->getValidator());
  56. }
  57. /**
  58. * Test setValidator()
  59. *
  60. * @return void
  61. */
  62. public function testSetValidator()
  63. {
  64. $form = new Form();
  65. $validator = $this->getMockBuilder('Cake\Validation\Validator')->getMock();
  66. $form->setValidator('default', $validator);
  67. $this->assertSame($validator, $form->getValidator());
  68. }
  69. /**
  70. * Test validate method.
  71. *
  72. * @return void
  73. */
  74. public function testValidate()
  75. {
  76. $form = new Form();
  77. $form->getValidator()
  78. ->add('email', 'format', ['rule' => 'email'])
  79. ->add('body', 'length', ['rule' => ['minLength', 12]]);
  80. $data = [
  81. 'email' => 'rong',
  82. 'body' => 'too short'
  83. ];
  84. $this->assertFalse($form->validate($data));
  85. $this->assertCount(2, $form->errors());
  86. $data = [
  87. 'email' => 'test@example.com',
  88. 'body' => 'Some content goes here'
  89. ];
  90. $this->assertTrue($form->validate($data));
  91. $this->assertCount(0, $form->errors());
  92. }
  93. /**
  94. * Test the errors methods.
  95. *
  96. * @return void
  97. */
  98. public function testErrors()
  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->errors();
  116. $this->assertCount(2, $errors);
  117. $this->assertEquals('Must be a valid email', $errors['email']['format']);
  118. $this->assertEquals('Must be so long', $errors['body']['length']);
  119. }
  120. /**
  121. * Test setErrors()
  122. *
  123. * @return void
  124. */
  125. public function testSetErrors()
  126. {
  127. $form = new Form();
  128. $expected = [
  129. 'field_name' => ['rule_name' => 'message']
  130. ];
  131. $form->setErrors($expected);
  132. $this->assertSame($expected, $form->errors());
  133. }
  134. /**
  135. * Test _execute is skipped on validation failure.
  136. *
  137. * @return void
  138. */
  139. public function testExecuteInvalid()
  140. {
  141. $form = $this->getMockBuilder('Cake\Form\Form')
  142. ->setMethods(['_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. * @return void
  157. */
  158. public function testExecuteValid()
  159. {
  160. $form = $this->getMockBuilder('Cake\Form\Form')
  161. ->setMethods(['_execute'])
  162. ->getMock();
  163. $form->getValidator()
  164. ->add('email', 'format', ['rule' => 'email']);
  165. $data = [
  166. 'email' => 'test@example.com'
  167. ];
  168. $form->expects($this->once())
  169. ->method('_execute')
  170. ->with($data)
  171. ->will($this->returnValue(true));
  172. $this->assertTrue($form->execute($data));
  173. }
  174. /**
  175. * Test setting and getting form data.
  176. *
  177. * @return void
  178. */
  179. public function testDataSetGet()
  180. {
  181. $form = new Form();
  182. $expected = ['title' => 'title', 'is_published' => true];
  183. $form->setData(['title' => 'title', 'is_published' => true]);
  184. $this->assertSame($expected, $form->getData());
  185. $this->assertEquals('title', $form->getData('title'));
  186. $this->assertNull($form->getData('non-existent'));
  187. }
  188. /**
  189. * test __debugInfo
  190. *
  191. * @return void
  192. */
  193. public function testDebugInfo()
  194. {
  195. $form = new Form();
  196. $result = $form->__debugInfo();
  197. $this->assertArrayHasKey('_schema', $result);
  198. $this->assertArrayHasKey('_errors', $result);
  199. $this->assertArrayHasKey('_validator', $result);
  200. $this->assertArrayHasKey('_data', $result);
  201. }
  202. }