FormContextTest.php 9.1 KB

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