FormContextTest.php 9.2 KB

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