ArrayContextTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 = $this->request->withParsedBody([
  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('common1', $context->val('users.99.tags'));
  171. $this->assertEquals('common2', $context->val('users.9.9tags'));
  172. $result = $context->val('title', ['default' => 'explicit default']);
  173. $this->assertEquals('explicit default', $result);
  174. }
  175. /**
  176. * Test isRequired
  177. *
  178. * @return void
  179. */
  180. public function testIsRequired()
  181. {
  182. $context = new ArrayContext($this->request, [
  183. 'required' => [
  184. 'Comments' => [
  185. 'required' => true,
  186. 'nope' => false,
  187. 'tags' => true
  188. ]
  189. ]
  190. ]);
  191. $this->assertTrue($context->isRequired('Comments.required'));
  192. $this->assertFalse($context->isRequired('Comments.nope'));
  193. $this->assertTrue($context->isRequired('Comments.0.tags'));
  194. $this->assertFalse($context->isRequired('Articles.id'));
  195. }
  196. /**
  197. * Test isRequired when the required key is omitted
  198. *
  199. * @return void
  200. */
  201. public function testIsRequiredUndefined()
  202. {
  203. $context = new ArrayContext($this->request, []);
  204. $this->assertFalse($context->isRequired('Comments.field'));
  205. }
  206. /**
  207. * Test the type method.
  208. *
  209. * @return void
  210. */
  211. public function testType()
  212. {
  213. $context = new ArrayContext($this->request, [
  214. 'schema' => [
  215. 'Comments' => [
  216. 'id' => ['type' => 'integer'],
  217. 'tags' => ['type' => 'string'],
  218. 'comment' => ['length' => 255]
  219. ]
  220. ]
  221. ]);
  222. $this->assertNull($context->type('Comments.undefined'));
  223. $this->assertEquals('integer', $context->type('Comments.id'));
  224. $this->assertEquals('string', $context->type('Comments.0.tags'));
  225. $this->assertNull($context->type('Comments.comment'));
  226. }
  227. /**
  228. * Test the type method when the data is missing.
  229. *
  230. * @return void
  231. */
  232. public function testIsTypeUndefined()
  233. {
  234. $context = new ArrayContext($this->request, []);
  235. $this->assertNull($context->type('Comments.undefined'));
  236. }
  237. /**
  238. * Test fetching attributes.
  239. *
  240. * @return void
  241. */
  242. public function testAttributes()
  243. {
  244. $context = new ArrayContext($this->request, [
  245. 'schema' => [
  246. 'Comments' => [
  247. 'id' => ['type' => 'integer'],
  248. 'comment' => ['type' => 'string', 'length' => 255],
  249. 'decimal' => ['type' => 'decimal', 'precision' => 2, 'length' => 5],
  250. 'floaty' => ['type' => 'float', 'precision' => 2, 'length' => 5],
  251. 'tags' => ['type' => 'string', 'length' => 25],
  252. ]
  253. ]
  254. ]);
  255. $this->assertEquals([], $context->attributes('Comments.id'));
  256. $this->assertEquals(['length' => 25], $context->attributes('Comments.0.tags'));
  257. $this->assertEquals(['length' => 255], $context->attributes('Comments.comment'));
  258. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.decimal'));
  259. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.floaty'));
  260. }
  261. /**
  262. * Test fetching errors.
  263. *
  264. * @return void
  265. */
  266. public function testError()
  267. {
  268. $context = new ArrayContext($this->request, []);
  269. $this->assertEquals([], $context->error('Comments.empty'));
  270. $context = new ArrayContext($this->request, [
  271. 'errors' => [
  272. 'Comments' => [
  273. 'comment' => ['Comment is required'],
  274. 'empty' => [],
  275. 'user_id' => 'A valid userid is required',
  276. ]
  277. ]
  278. ]);
  279. $this->assertEquals(['Comment is required'], $context->error('Comments.comment'));
  280. $this->assertEquals('A valid userid is required', $context->error('Comments.user_id'));
  281. $this->assertEquals([], $context->error('Comments.empty'));
  282. $this->assertNull($context->error('Comments.not_there'));
  283. }
  284. /**
  285. * Test checking errors.
  286. *
  287. * @return void
  288. */
  289. public function testHasError()
  290. {
  291. $context = new ArrayContext($this->request, [
  292. 'errors' => [
  293. 'Comments' => [
  294. 'comment' => ['Comment is required'],
  295. 'empty' => [],
  296. 'user_id' => 'A valid userid is required',
  297. ]
  298. ]
  299. ]);
  300. $this->assertFalse($context->hasError('Comments.not_there'));
  301. $this->assertFalse($context->hasError('Comments.empty'));
  302. $this->assertTrue($context->hasError('Comments.user_id'));
  303. $this->assertTrue($context->hasError('Comments.comment'));
  304. }
  305. }