ArrayContextTest.php 11 KB

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