ArrayContextTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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\Http\ServerRequest;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\View\Form\ArrayContext;
  19. /**
  20. * Array context test case.
  21. */
  22. class ArrayContextTest extends TestCase
  23. {
  24. /**
  25. * setup method.
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $this->request = new ServerRequest();
  33. }
  34. public function testGetRequiredMessage()
  35. {
  36. $context = new ArrayContext($this->request, [
  37. 'required' => [
  38. 'Comments' => [
  39. 'required' => 'My custom message',
  40. 'nope' => false,
  41. 'tags' => true,
  42. ],
  43. ],
  44. ]);
  45. $this->assertSame('My custom message', $context->getRequiredMessage('Comments.required'));
  46. $this->assertSame('This field is required', $context->getRequiredMessage('Comments.tags'));
  47. $this->assertSame(null, $context->getRequiredMessage('Comments.nope'));
  48. }
  49. /**
  50. * Test getting the primary key.
  51. *
  52. * @return void
  53. */
  54. public function testPrimaryKey()
  55. {
  56. $context = new ArrayContext($this->request, []);
  57. $this->assertEquals([], $context->primaryKey());
  58. $context = new ArrayContext($this->request, [
  59. 'schema' => [
  60. '_constraints' => 'mistake',
  61. ],
  62. ]);
  63. $this->assertEquals([], $context->primaryKey());
  64. $data = [
  65. 'schema' => [
  66. '_constraints' => [
  67. 'primary' => ['type' => 'primary', 'columns' => ['id']],
  68. ],
  69. ],
  70. ];
  71. $context = new ArrayContext($this->request, $data);
  72. $expected = ['id'];
  73. $this->assertEquals($expected, $context->primaryKey());
  74. }
  75. /**
  76. * Test isPrimaryKey.
  77. *
  78. * @return void
  79. */
  80. public function testIsPrimaryKey()
  81. {
  82. $context = new ArrayContext($this->request, []);
  83. $this->assertFalse($context->isPrimaryKey('id'));
  84. $context = new ArrayContext($this->request, [
  85. 'schema' => [
  86. '_constraints' => 'mistake',
  87. ],
  88. ]);
  89. $this->assertFalse($context->isPrimaryKey('mistake'));
  90. $data = [
  91. 'schema' => [
  92. '_constraints' => [
  93. 'primary' => ['type' => 'primary', 'columns' => ['id']],
  94. ],
  95. ],
  96. ];
  97. $context = new ArrayContext($this->request, $data);
  98. $this->assertTrue($context->isPrimaryKey('id'));
  99. $this->assertFalse($context->isPrimaryKey('name'));
  100. $data = [
  101. 'schema' => [
  102. '_constraints' => [
  103. 'primary' => ['type' => 'primary', 'columns' => ['id', 'name']],
  104. ],
  105. ],
  106. ];
  107. $context = new ArrayContext($this->request, $data);
  108. $this->assertTrue($context->isPrimaryKey('id'));
  109. $this->assertTrue($context->isPrimaryKey('name'));
  110. }
  111. /**
  112. * Test the isCreate method.
  113. *
  114. * @return void
  115. */
  116. public function testIsCreate()
  117. {
  118. $context = new ArrayContext($this->request, []);
  119. $this->assertTrue($context->isCreate());
  120. $data = [
  121. 'schema' => [
  122. '_constraints' => [
  123. 'primary' => ['type' => 'primary', 'columns' => ['id']],
  124. ],
  125. ],
  126. ];
  127. $context = new ArrayContext($this->request, $data);
  128. $this->assertTrue($context->isCreate());
  129. $data['defaults'] = ['id' => 2];
  130. $context = new ArrayContext($this->request, $data);
  131. $this->assertFalse($context->isCreate());
  132. }
  133. /**
  134. * Test reading values from the request & defaults.
  135. */
  136. public function testValPresent()
  137. {
  138. $this->request = $this->request->withParsedBody([
  139. 'Articles' => [
  140. 'title' => 'New title',
  141. 'body' => 'My copy',
  142. ],
  143. ]);
  144. $context = new ArrayContext($this->request, [
  145. 'defaults' => [
  146. 'Articles' => [
  147. 'title' => 'Default value',
  148. 'published' => 0,
  149. ],
  150. ],
  151. ]);
  152. $this->assertEquals('New title', $context->val('Articles.title'));
  153. $this->assertEquals('My copy', $context->val('Articles.body'));
  154. $this->assertEquals(0, $context->val('Articles.published'));
  155. $this->assertNull($context->val('Articles.nope'));
  156. }
  157. /**
  158. * Test getting values when the request and defaults are missing.
  159. *
  160. * @return void
  161. */
  162. public function testValMissing()
  163. {
  164. $context = new ArrayContext($this->request, []);
  165. $this->assertNull($context->val('Comments.field'));
  166. }
  167. /**
  168. * Test getting default value
  169. *
  170. * Tests includes making sure numeric elements are stripped but not keys beginning with numeric
  171. * value
  172. *
  173. * @return void
  174. */
  175. public function testValDefault()
  176. {
  177. $context = new ArrayContext($this->request, [
  178. 'defaults' => [
  179. 'title' => 'Default value',
  180. 'users' => ['tags' => 'common1', '9tags' => 'common2'],
  181. ],
  182. ]);
  183. $this->assertEquals('Default value', $context->val('title'));
  184. $this->assertEquals('common1', $context->val('users.0.tags'));
  185. $this->assertEquals('common1', $context->val('users.99.tags'));
  186. $this->assertEquals('common2', $context->val('users.9.9tags'));
  187. $result = $context->val('title', ['default' => 'explicit default']);
  188. $this->assertEquals('explicit default', $result);
  189. }
  190. /**
  191. * Test isRequired
  192. *
  193. * @return void
  194. */
  195. public function testIsRequired()
  196. {
  197. $context = new ArrayContext($this->request, [
  198. 'required' => [
  199. 'Comments' => [
  200. 'required' => true,
  201. 'nope' => false,
  202. 'tags' => true,
  203. ],
  204. ],
  205. ]);
  206. $this->assertTrue($context->isRequired('Comments.required'));
  207. $this->assertFalse($context->isRequired('Comments.nope'));
  208. $this->assertTrue($context->isRequired('Comments.0.tags'));
  209. $this->assertFalse($context->isRequired('Articles.id'));
  210. }
  211. /**
  212. * Test isRequired when the required key is omitted
  213. *
  214. * @return void
  215. */
  216. public function testIsRequiredUndefined()
  217. {
  218. $context = new ArrayContext($this->request, []);
  219. $this->assertFalse($context->isRequired('Comments.field'));
  220. }
  221. /**
  222. * Test the type method.
  223. *
  224. * @return void
  225. */
  226. public function testType()
  227. {
  228. $context = new ArrayContext($this->request, [
  229. 'schema' => [
  230. 'Comments' => [
  231. 'id' => ['type' => 'integer'],
  232. 'tags' => ['type' => 'string'],
  233. 'comment' => ['length' => 255],
  234. ],
  235. ],
  236. ]);
  237. $this->assertNull($context->type('Comments.undefined'));
  238. $this->assertEquals('integer', $context->type('Comments.id'));
  239. $this->assertEquals('string', $context->type('Comments.0.tags'));
  240. $this->assertNull($context->type('Comments.comment'));
  241. }
  242. /**
  243. * Test the type method when the data is missing.
  244. *
  245. * @return void
  246. */
  247. public function testIsTypeUndefined()
  248. {
  249. $context = new ArrayContext($this->request, []);
  250. $this->assertNull($context->type('Comments.undefined'));
  251. }
  252. /**
  253. * Test fetching attributes.
  254. *
  255. * @return void
  256. */
  257. public function testAttributes()
  258. {
  259. $context = new ArrayContext($this->request, [
  260. 'schema' => [
  261. 'Comments' => [
  262. 'id' => ['type' => 'integer'],
  263. 'comment' => ['type' => 'string', 'length' => 255],
  264. 'decimal' => ['type' => 'decimal', 'precision' => 2, 'length' => 5],
  265. 'floaty' => ['type' => 'float', 'precision' => 2, 'length' => 5],
  266. 'tags' => ['type' => 'string', 'length' => 25],
  267. ],
  268. ],
  269. ]);
  270. $this->assertEquals([], $context->attributes('Comments.id'));
  271. $this->assertEquals(['length' => 25], $context->attributes('Comments.0.tags'));
  272. $this->assertEquals(['length' => 255], $context->attributes('Comments.comment'));
  273. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.decimal'));
  274. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.floaty'));
  275. }
  276. /**
  277. * Test fetching errors.
  278. *
  279. * @return void
  280. */
  281. public function testError()
  282. {
  283. $context = new ArrayContext($this->request, []);
  284. $this->assertEquals([], $context->error('Comments.empty'));
  285. $context = new ArrayContext($this->request, [
  286. 'errors' => [
  287. 'Comments' => [
  288. 'comment' => ['Comment is required'],
  289. 'empty' => [],
  290. 'user_id' => 'A valid userid is required',
  291. ],
  292. ],
  293. ]);
  294. $this->assertEquals(['Comment is required'], $context->error('Comments.comment'));
  295. $this->assertEquals(['A valid userid is required'], $context->error('Comments.user_id'));
  296. $this->assertEquals([], $context->error('Comments.empty'));
  297. $this->assertEquals([], $context->error('Comments.not_there'));
  298. }
  299. /**
  300. * Test checking errors.
  301. *
  302. * @return void
  303. */
  304. public function testHasError()
  305. {
  306. $context = new ArrayContext($this->request, [
  307. 'errors' => [
  308. 'Comments' => [
  309. 'comment' => ['Comment is required'],
  310. 'empty' => [],
  311. 'user_id' => 'A valid userid is required',
  312. ],
  313. ],
  314. ]);
  315. $this->assertFalse($context->hasError('Comments.not_there'));
  316. $this->assertFalse($context->hasError('Comments.empty'));
  317. $this->assertTrue($context->hasError('Comments.user_id'));
  318. $this->assertTrue($context->hasError('Comments.comment'));
  319. }
  320. }