FormContextTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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. $context = new FormContext($this->request, ['entity' => new Form()]);
  106. $result = $context->val('title', ['default' => 'default default']);
  107. $this->assertEquals('default default', $result);
  108. }
  109. /**
  110. * Test isRequired
  111. *
  112. * @return void
  113. */
  114. public function testIsRequired()
  115. {
  116. $form = new Form();
  117. $form->validator()
  118. ->requirePresence('name')
  119. ->add('email', 'format', ['rule' => 'email']);
  120. $context = new FormContext($this->request, [
  121. 'entity' => $form
  122. ]);
  123. $this->assertTrue($context->isRequired('name'));
  124. $this->assertTrue($context->isRequired('email'));
  125. $this->assertFalse($context->isRequired('body'));
  126. $this->assertFalse($context->isRequired('Prefix.body'));
  127. }
  128. /**
  129. * Test the type method.
  130. *
  131. * @return void
  132. */
  133. public function testType()
  134. {
  135. $form = new Form();
  136. $form->schema()
  137. ->addField('email', 'string')
  138. ->addField('user_id', 'integer');
  139. $context = new FormContext($this->request, [
  140. 'entity' => $form
  141. ]);
  142. $this->assertNull($context->type('undefined'));
  143. $this->assertEquals('integer', $context->type('user_id'));
  144. $this->assertEquals('string', $context->type('email'));
  145. $this->assertNull($context->type('Prefix.email'));
  146. }
  147. /**
  148. * Test fetching attributes.
  149. *
  150. * @return void
  151. */
  152. public function testAttributes()
  153. {
  154. $form = new Form();
  155. $form->schema()
  156. ->addField('email', [
  157. 'type' => 'string',
  158. 'length' => 10,
  159. ])
  160. ->addField('amount', [
  161. 'type' => 'decimal',
  162. 'length' => 5,
  163. 'precision' => 2,
  164. ]);
  165. $context = new FormContext($this->request, [
  166. 'entity' => $form
  167. ]);
  168. $this->assertEquals([], $context->attributes('id'));
  169. $this->assertEquals(['length' => 10, 'precision' => null], $context->attributes('email'));
  170. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('amount'));
  171. }
  172. /**
  173. * Test fetching errors.
  174. *
  175. * @return void
  176. */
  177. public function testError()
  178. {
  179. $nestedValidator = new Validator();
  180. $nestedValidator
  181. ->add('password', 'length', ['rule' => ['minLength', 8]])
  182. ->add('confirm', 'length', ['rule' => ['minLength', 8]]);
  183. $form = new Form();
  184. $form->validator()
  185. ->add('email', 'format', ['rule' => 'email'])
  186. ->add('name', 'length', ['rule' => ['minLength', 10]])
  187. ->addNested('pass', $nestedValidator);
  188. $form->validate([
  189. 'email' => 'derp',
  190. 'name' => 'derp',
  191. 'pass' => [
  192. 'password' => 'short',
  193. 'confirm' => 'long enough',
  194. ],
  195. ]);
  196. $context = new FormContext($this->request, ['entity' => $form]);
  197. $this->assertEquals([], $context->error('empty'));
  198. $this->assertEquals(['The provided value is invalid'], $context->error('email'));
  199. $this->assertEquals(['The provided value is invalid'], $context->error('name'));
  200. $this->assertEquals(['The provided value is invalid'], $context->error('pass.password'));
  201. $this->assertEquals([], $context->error('Alias.name'));
  202. $this->assertEquals([], $context->error('nope.nope'));
  203. $mock = $this->getMockBuilder('Cake\Validation\Validator')
  204. ->setMethods(['errors'])
  205. ->getMock();
  206. $mock->expects($this->once())
  207. ->method('errors')
  208. ->willReturn(['key' => 'should be an array, not a string']);
  209. $form->validator($mock);
  210. $form->validate([]);
  211. $context = new FormContext($this->request, ['entity' => $form]);
  212. $this->assertEquals(
  213. ['should be an array, not a string'],
  214. $context->error('key'),
  215. 'This test should not produce a PHP warning from array_values().'
  216. );
  217. }
  218. /**
  219. * Test checking errors.
  220. *
  221. * @return void
  222. */
  223. public function testHasError()
  224. {
  225. $nestedValidator = new Validator();
  226. $nestedValidator
  227. ->add('password', 'length', ['rule' => ['minLength', 8]])
  228. ->add('confirm', 'length', ['rule' => ['minLength', 8]]);
  229. $form = new Form();
  230. $form->validator()
  231. ->add('email', 'format', ['rule' => 'email'])
  232. ->add('name', 'length', ['rule' => ['minLength', 10]])
  233. ->addNested('pass', $nestedValidator);
  234. $form->validate([
  235. 'email' => 'derp',
  236. 'name' => 'derp',
  237. 'pass' => [
  238. 'password' => 'short',
  239. 'confirm' => 'long enough',
  240. ],
  241. ]);
  242. $context = new FormContext($this->request, ['entity' => $form]);
  243. $this->assertTrue($context->hasError('email'));
  244. $this->assertTrue($context->hasError('name'));
  245. $this->assertFalse($context->hasError('nope'));
  246. $this->assertFalse($context->hasError('nope.nope'));
  247. $this->assertTrue($context->hasError('pass.password'));
  248. }
  249. }