FormContextTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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\View\Form;
  17. use Cake\Form\Form;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\Validation\Validator;
  20. use Cake\View\Form\FormContext;
  21. /**
  22. * Form context test case.
  23. */
  24. class FormContextTest extends TestCase
  25. {
  26. /**
  27. * setup method.
  28. */
  29. public function setUp(): void
  30. {
  31. parent::setUp();
  32. }
  33. /**
  34. * tests getRequiredMessage
  35. */
  36. public function testGetRequiredMessage(): void
  37. {
  38. $validator = new Validator();
  39. $validator->notEmptyString('title', "Don't forget a title!");
  40. $form = new Form();
  41. $form->setValidator(Form::DEFAULT_VALIDATOR, $validator);
  42. $context = new FormContext([
  43. 'entity' => $form,
  44. ]);
  45. $this->assertNull($context->getRequiredMessage('body'));
  46. $this->assertSame("Don't forget a title!", $context->getRequiredMessage('title'));
  47. }
  48. /**
  49. * Test getting the primary key.
  50. */
  51. public function testPrimaryKey(): void
  52. {
  53. $context = new FormContext(['entity' => new Form()]);
  54. $this->assertEquals([], $context->getPrimaryKey());
  55. }
  56. /**
  57. * Test isPrimaryKey.
  58. */
  59. public function testIsPrimaryKey(): void
  60. {
  61. $context = new FormContext(['entity' => new Form()]);
  62. $this->assertFalse($context->isPrimaryKey('id'));
  63. }
  64. /**
  65. * Test the isCreate method.
  66. */
  67. public function testIsCreate(): void
  68. {
  69. $context = new FormContext(['entity' => new Form()]);
  70. $this->assertTrue($context->isCreate());
  71. }
  72. /**
  73. * Test reading values from form data.
  74. */
  75. public function testValPresent(): void
  76. {
  77. $form = new Form();
  78. $form->setData(['title' => 'set title']);
  79. $context = new FormContext(['entity' => $form]);
  80. $this->assertSame('set title', $context->val('title'));
  81. }
  82. /**
  83. * Test getting values when data and defaults are missing.
  84. */
  85. public function testValMissing(): void
  86. {
  87. $context = new FormContext(['entity' => new Form()]);
  88. $this->assertNull($context->val('Comments.field'));
  89. }
  90. /**
  91. * Test getting default value
  92. */
  93. public function testValDefault(): void
  94. {
  95. $form = new Form();
  96. $form->getSchema()->addField('name', ['default' => 'schema default']);
  97. $context = new FormContext(['entity' => $form]);
  98. $result = $context->val('title');
  99. $this->assertNull($result);
  100. $result = $context->val('title', ['default' => 'default default']);
  101. $this->assertSame('default default', $result);
  102. $result = $context->val('name');
  103. $this->assertSame('schema default', $result);
  104. $result = $context->val('name', ['default' => 'custom default']);
  105. $this->assertSame('custom default', $result);
  106. $result = $context->val('name', ['schemaDefault' => false]);
  107. $this->assertNull($result);
  108. }
  109. /**
  110. * Test isRequired
  111. */
  112. public function testIsRequired(): void
  113. {
  114. $form = new Form();
  115. $form->getValidator()
  116. ->requirePresence('name')
  117. ->add('email', 'format', ['rule' => 'email']);
  118. $context = new FormContext([
  119. 'entity' => $form,
  120. ]);
  121. $this->assertTrue($context->isRequired('name'));
  122. $this->assertTrue($context->isRequired('email'));
  123. $this->assertNull($context->isRequired('body'));
  124. $this->assertNull($context->isRequired('Prefix.body'));
  125. // Non-default validator name.
  126. $form = new Form();
  127. $form->setValidator('custom', new Validator());
  128. $form->getValidator('custom')
  129. ->notEmptyString('title');
  130. $form->validate([
  131. 'title' => '',
  132. ], 'custom');
  133. $context = new FormContext(['entity' => $form, 'validator' => 'custom']);
  134. $this->assertTrue($context->isRequired('title'));
  135. }
  136. /**
  137. * Test the type method.
  138. */
  139. public function testType(): void
  140. {
  141. $form = new Form();
  142. $form->getSchema()
  143. ->addField('email', 'string')
  144. ->addField('user_id', 'integer');
  145. $context = new FormContext([
  146. 'entity' => $form,
  147. ]);
  148. $this->assertNull($context->type('undefined'));
  149. $this->assertSame('integer', $context->type('user_id'));
  150. $this->assertSame('string', $context->type('email'));
  151. $this->assertNull($context->type('Prefix.email'));
  152. }
  153. /**
  154. * Test the fieldNames method.
  155. */
  156. public function testFieldNames(): void
  157. {
  158. $form = new Form();
  159. $context = new FormContext([
  160. 'entity' => $form,
  161. ]);
  162. $expected = [];
  163. $result = $context->fieldNames();
  164. $this->assertEquals($expected, $result);
  165. $form->getSchema()
  166. ->addField('email', 'string')
  167. ->addField('password', 'string');
  168. $context = new FormContext([
  169. 'entity' => $form,
  170. ]);
  171. $expected = ['email', 'password'];
  172. $result = $context->fieldNames();
  173. $this->assertEquals($expected, $result);
  174. }
  175. /**
  176. * Test fetching attributes.
  177. */
  178. public function testAttributes(): void
  179. {
  180. $form = new Form();
  181. $form->getSchema()
  182. ->addField('email', [
  183. 'type' => 'string',
  184. 'length' => 10,
  185. ])
  186. ->addField('amount', [
  187. 'type' => 'decimal',
  188. 'length' => 5,
  189. 'precision' => 2,
  190. ]);
  191. $context = new FormContext([
  192. 'entity' => $form,
  193. ]);
  194. $this->assertEquals([], $context->attributes('id'));
  195. $this->assertEquals(
  196. ['length' => 10, 'precision' => null, 'default' => null],
  197. $context->attributes('email')
  198. );
  199. $this->assertEquals(
  200. ['precision' => 2, 'length' => 5, 'default' => null],
  201. $context->attributes('amount')
  202. );
  203. }
  204. /**
  205. * Test fetching errors.
  206. */
  207. public function testError(): void
  208. {
  209. $nestedValidator = new Validator();
  210. $nestedValidator
  211. ->add('password', 'length', ['rule' => ['minLength', 8]])
  212. ->add('confirm', 'length', ['rule' => ['minLength', 8]]);
  213. $form = new Form();
  214. $form->getValidator()
  215. ->add('email', 'format', ['rule' => 'email'])
  216. ->add('name', 'length', ['rule' => ['minLength', 10]])
  217. ->addNested('pass', $nestedValidator);
  218. $form->validate([
  219. 'email' => 'derp',
  220. 'name' => 'derp',
  221. 'pass' => [
  222. 'password' => 'short',
  223. 'confirm' => 'long enough',
  224. ],
  225. ]);
  226. $context = new FormContext(['entity' => $form]);
  227. $this->assertEquals([], $context->error('empty'));
  228. $this->assertEquals(['format' => 'The provided value is invalid'], $context->error('email'));
  229. $this->assertEquals(['length' => 'The provided value is invalid'], $context->error('name'));
  230. $this->assertEquals(['length' => 'The provided value is invalid'], $context->error('pass.password'));
  231. $this->assertEquals([], $context->error('Alias.name'));
  232. $this->assertEquals([], $context->error('nope.nope'));
  233. $validator = new Validator();
  234. $validator->requirePresence('key', true, 'should be an array, not a string');
  235. $form->setValidator('default', $validator);
  236. $form->validate([]);
  237. $context = new FormContext(['entity' => $form]);
  238. $this->assertEquals(
  239. ['_required' => 'should be an array, not a string'],
  240. $context->error('key'),
  241. 'This test should not produce a PHP warning from array_values().'
  242. );
  243. }
  244. /**
  245. * Test checking errors.
  246. */
  247. public function testHasError(): void
  248. {
  249. $nestedValidator = new Validator();
  250. $nestedValidator
  251. ->add('password', 'length', ['rule' => ['minLength', 8]])
  252. ->add('confirm', 'length', ['rule' => ['minLength', 8]]);
  253. $form = new Form();
  254. $form->getValidator()
  255. ->add('email', 'format', ['rule' => 'email'])
  256. ->add('name', 'length', ['rule' => ['minLength', 10]])
  257. ->addNested('pass', $nestedValidator);
  258. $form->validate([
  259. 'email' => 'derp',
  260. 'name' => 'derp',
  261. 'pass' => [
  262. 'password' => 'short',
  263. 'confirm' => 'long enough',
  264. ],
  265. ]);
  266. $context = new FormContext(['entity' => $form]);
  267. $this->assertTrue($context->hasError('email'));
  268. $this->assertTrue($context->hasError('name'));
  269. $this->assertFalse($context->hasError('nope'));
  270. $this->assertFalse($context->hasError('nope.nope'));
  271. $this->assertTrue($context->hasError('pass.password'));
  272. }
  273. }