FormContextTest.php 9.6 KB

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