ArrayContextTest.php 10 KB

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