/tests/TestCase/View/Form/FormContextTest.php

https://github.com/LubosRemplik/cakephp · PHP · 306 lines · 195 code · 27 blank · 84 comment · 0 complexity · 3cbd8c6722a32a4fc78207182e907035 MD5 · raw file

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