FormContextTest.php 7.9 KB

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