FormContextTest.php 6.0 KB

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