FormContextTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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->data = [
  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->validator()
  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 fetching attributes.
  159. *
  160. * @return void
  161. */
  162. public function testAttributes()
  163. {
  164. $form = new Form();
  165. $form->schema()
  166. ->addField('email', [
  167. 'type' => 'string',
  168. 'length' => 10,
  169. ])
  170. ->addField('amount', [
  171. 'type' => 'decimal',
  172. 'length' => 5,
  173. 'precision' => 2,
  174. ]);
  175. $context = new FormContext($this->request, [
  176. 'entity' => $form
  177. ]);
  178. $this->assertEquals([], $context->attributes('id'));
  179. $this->assertEquals(['length' => 10, 'precision' => null], $context->attributes('email'));
  180. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('amount'));
  181. }
  182. /**
  183. * Test fetching errors.
  184. *
  185. * @return void
  186. */
  187. public function testError()
  188. {
  189. $nestedValidator = new Validator();
  190. $nestedValidator
  191. ->add('password', 'length', ['rule' => ['minLength', 8]])
  192. ->add('confirm', 'length', ['rule' => ['minLength', 8]]);
  193. $form = new Form();
  194. $form->validator()
  195. ->add('email', 'format', ['rule' => 'email'])
  196. ->add('name', 'length', ['rule' => ['minLength', 10]])
  197. ->addNested('pass', $nestedValidator);
  198. $form->validate([
  199. 'email' => 'derp',
  200. 'name' => 'derp',
  201. 'pass' => [
  202. 'password' => 'short',
  203. 'confirm' => 'long enough',
  204. ],
  205. ]);
  206. $context = new FormContext($this->request, ['entity' => $form]);
  207. $this->assertEquals([], $context->error('empty'));
  208. $this->assertEquals(['The provided value is invalid'], $context->error('email'));
  209. $this->assertEquals(['The provided value is invalid'], $context->error('name'));
  210. $this->assertEquals(['The provided value is invalid'], $context->error('pass.password'));
  211. $this->assertEquals([], $context->error('Alias.name'));
  212. $this->assertEquals([], $context->error('nope.nope'));
  213. $mock = $this->getMockBuilder('Cake\Validation\Validator')
  214. ->setMethods(['errors'])
  215. ->getMock();
  216. $mock->expects($this->once())
  217. ->method('errors')
  218. ->willReturn(['key' => 'should be an array, not a string']);
  219. $form->validator($mock);
  220. $form->validate([]);
  221. $context = new FormContext($this->request, ['entity' => $form]);
  222. $this->assertEquals(
  223. ['should be an array, not a string'],
  224. $context->error('key'),
  225. 'This test should not produce a PHP warning from array_values().'
  226. );
  227. }
  228. /**
  229. * Test checking errors.
  230. *
  231. * @return void
  232. */
  233. public function testHasError()
  234. {
  235. $nestedValidator = new Validator();
  236. $nestedValidator
  237. ->add('password', 'length', ['rule' => ['minLength', 8]])
  238. ->add('confirm', 'length', ['rule' => ['minLength', 8]]);
  239. $form = new Form();
  240. $form->validator()
  241. ->add('email', 'format', ['rule' => 'email'])
  242. ->add('name', 'length', ['rule' => ['minLength', 10]])
  243. ->addNested('pass', $nestedValidator);
  244. $form->validate([
  245. 'email' => 'derp',
  246. 'name' => 'derp',
  247. 'pass' => [
  248. 'password' => 'short',
  249. 'confirm' => 'long enough',
  250. ],
  251. ]);
  252. $context = new FormContext($this->request, ['entity' => $form]);
  253. $this->assertTrue($context->hasError('email'));
  254. $this->assertTrue($context->hasError('name'));
  255. $this->assertFalse($context->hasError('nope'));
  256. $this->assertFalse($context->hasError('nope.nope'));
  257. $this->assertTrue($context->hasError('pass.password'));
  258. }
  259. }