ArrayContextTest.php 10.0 KB

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