FormContextTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\Network\Request;
  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()
  32. {
  33. parent::setUp();
  34. $this->request = new Request();
  35. }
  36. /**
  37. * Test getting the primary key.
  38. *
  39. * @return void
  40. */
  41. public function testPrimaryKey()
  42. {
  43. $context = new FormContext($this->request, ['entity' => new Form()]);
  44. $this->assertEquals([], $context->primaryKey());
  45. }
  46. /**
  47. * Test isPrimaryKey.
  48. *
  49. * @return void
  50. */
  51. public function testIsPrimaryKey()
  52. {
  53. $context = new FormContext($this->request, ['entity' => new Form()]);
  54. $this->assertFalse($context->isPrimaryKey('id'));
  55. }
  56. /**
  57. * Test the isCreate method.
  58. *
  59. * @return void
  60. */
  61. public function testIsCreate()
  62. {
  63. $context = new FormContext($this->request, ['entity' => new Form()]);
  64. $this->assertTrue($context->isCreate());
  65. }
  66. /**
  67. * Test reading values from the request & defaults.
  68. */
  69. public function testValPresent()
  70. {
  71. $this->request->data = [
  72. 'Articles' => [
  73. 'title' => 'New title',
  74. 'body' => 'My copy',
  75. ]
  76. ];
  77. $context = new FormContext($this->request, ['entity' => new Form()]);
  78. $this->assertEquals('New title', $context->val('Articles.title'));
  79. $this->assertEquals('My copy', $context->val('Articles.body'));
  80. $this->assertNull($context->val('Articles.nope'));
  81. }
  82. /**
  83. * Test getting values when the request and defaults are missing.
  84. *
  85. * @return void
  86. */
  87. public function testValMissing()
  88. {
  89. $context = new FormContext($this->request, ['entity' => new Form()]);
  90. $this->assertNull($context->val('Comments.field'));
  91. }
  92. /**
  93. * Test isRequired
  94. *
  95. * @return void
  96. */
  97. public function testIsRequired()
  98. {
  99. $form = new Form();
  100. $form->validator()
  101. ->requirePresence('name')
  102. ->add('email', 'format', ['rule' => 'email']);
  103. $context = new FormContext($this->request, [
  104. 'entity' => $form
  105. ]);
  106. $this->assertTrue($context->isRequired('name'));
  107. $this->assertTrue($context->isRequired('email'));
  108. $this->assertFalse($context->isRequired('body'));
  109. $this->assertFalse($context->isRequired('Prefix.body'));
  110. }
  111. /**
  112. * Test the type method.
  113. *
  114. * @return void
  115. */
  116. public function testType()
  117. {
  118. $form = new Form();
  119. $form->schema()
  120. ->addField('email', 'string')
  121. ->addField('user_id', 'integer');
  122. $context = new FormContext($this->request, [
  123. 'entity' => $form
  124. ]);
  125. $this->assertNull($context->type('undefined'));
  126. $this->assertEquals('integer', $context->type('user_id'));
  127. $this->assertEquals('string', $context->type('email'));
  128. $this->assertNull($context->type('Prefix.email'));
  129. }
  130. /**
  131. * Test fetching attributes.
  132. *
  133. * @return void
  134. */
  135. public function testAttributes()
  136. {
  137. $form = new Form();
  138. $form->schema()
  139. ->addField('email', [
  140. 'type' => 'string',
  141. 'length' => 10,
  142. ])
  143. ->addField('amount', [
  144. 'type' => 'decimal',
  145. 'length' => 5,
  146. 'precision' => 2,
  147. ]);
  148. $context = new FormContext($this->request, [
  149. 'entity' => $form
  150. ]);
  151. $this->assertEquals([], $context->attributes('id'));
  152. $this->assertEquals(['length' => 10, 'precision' => null], $context->attributes('email'));
  153. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('amount'));
  154. }
  155. /**
  156. * Test fetching errors.
  157. *
  158. * @return void
  159. */
  160. public function testError()
  161. {
  162. $nestedValidator = new Validator();
  163. $nestedValidator
  164. ->add('password', 'length', ['rule' => ['minLength', 8]])
  165. ->add('confirm', 'length', ['rule' => ['minLength', 8]]);
  166. $form = new Form();
  167. $form->validator()
  168. ->add('email', 'format', ['rule' => 'email'])
  169. ->add('name', 'length', ['rule' => ['minLength', 10]])
  170. ->addNested('pass', $nestedValidator);
  171. $form->validate([
  172. 'email' => 'derp',
  173. 'name' => 'derp',
  174. 'pass' => [
  175. 'password' => 'short',
  176. 'confirm' => 'long enough',
  177. ],
  178. ]);
  179. $context = new FormContext($this->request, ['entity' => $form]);
  180. $this->assertEquals([], $context->error('empty'));
  181. $this->assertEquals(['The provided value is invalid'], $context->error('email'));
  182. $this->assertEquals(['The provided value is invalid'], $context->error('name'));
  183. $this->assertEquals(['The provided value is invalid'], $context->error('pass.password'));
  184. $this->assertEquals([], $context->error('Alias.name'));
  185. $this->assertEquals([], $context->error('nope.nope'));
  186. }
  187. /**
  188. * Test checking errors.
  189. *
  190. * @return void
  191. */
  192. public function testHasError()
  193. {
  194. $form = new Form();
  195. $form->validator()
  196. ->add('email', 'format', ['rule' => 'email'])
  197. ->add('name', 'length', ['rule' => ['minLength', 10]]);
  198. $form->validate([
  199. 'email' => 'derp',
  200. 'name' => 'derp'
  201. ]);
  202. $context = new FormContext($this->request, ['entity' => $form]);
  203. $this->assertTrue($context->hasError('email'));
  204. $this->assertTrue($context->hasError('name'));
  205. $this->assertFalse($context->hasError('nope'));
  206. $this->assertFalse($context->hasError('nope.nope'));
  207. }
  208. }