ArrayContextTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Form;
  16. use Cake\Network\Request;
  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 Request();
  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. * @return void
  156. */
  157. public function testValDefault()
  158. {
  159. $context = new ArrayContext($this->request, [
  160. 'defaults' => [
  161. 'title' => 'Default value',
  162. ]
  163. ]);
  164. $this->assertEquals('Default value', $context->val('title'));
  165. $result = $context->val('title', ['default' => 'explicit default']);
  166. $this->assertEquals('explicit default', $result);
  167. }
  168. /**
  169. * Test isRequired
  170. *
  171. * @return void
  172. */
  173. public function testIsRequired()
  174. {
  175. $context = new ArrayContext($this->request, [
  176. 'required' => [
  177. 'Comments' => [
  178. 'required' => true,
  179. 'nope' => false
  180. ]
  181. ]
  182. ]);
  183. $this->assertTrue($context->isRequired('Comments.required'));
  184. $this->assertFalse($context->isRequired('Comments.nope'));
  185. $this->assertFalse($context->isRequired('Articles.id'));
  186. }
  187. /**
  188. * Test isRequired when the required key is omitted
  189. *
  190. * @return void
  191. */
  192. public function testIsRequiredUndefined()
  193. {
  194. $context = new ArrayContext($this->request, []);
  195. $this->assertFalse($context->isRequired('Comments.field'));
  196. }
  197. /**
  198. * Test the type method.
  199. *
  200. * @return void
  201. */
  202. public function testType()
  203. {
  204. $context = new ArrayContext($this->request, [
  205. 'schema' => [
  206. 'Comments' => [
  207. 'id' => ['type' => 'integer'],
  208. 'comment' => ['length' => 255]
  209. ]
  210. ]
  211. ]);
  212. $this->assertNull($context->type('Comments.undefined'));
  213. $this->assertEquals('integer', $context->type('Comments.id'));
  214. $this->assertNull($context->type('Comments.comment'));
  215. }
  216. /**
  217. * Test the type method when the data is missing.
  218. *
  219. * @return void
  220. */
  221. public function testIsTypeUndefined()
  222. {
  223. $context = new ArrayContext($this->request, []);
  224. $this->assertNull($context->type('Comments.undefined'));
  225. }
  226. /**
  227. * Test fetching attributes.
  228. *
  229. * @return void
  230. */
  231. public function testAttributes()
  232. {
  233. $context = new ArrayContext($this->request, [
  234. 'schema' => [
  235. 'Comments' => [
  236. 'id' => ['type' => 'integer'],
  237. 'comment' => ['type' => 'string', 'length' => 255],
  238. 'decimal' => ['type' => 'decimal', 'precision' => 2, 'length' => 5],
  239. 'floaty' => ['type' => 'float', 'precision' => 2, 'length' => 5],
  240. ]
  241. ]
  242. ]);
  243. $this->assertEquals([], $context->attributes('Comments.id'));
  244. $this->assertEquals(['length' => 255], $context->attributes('Comments.comment'));
  245. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.decimal'));
  246. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.floaty'));
  247. }
  248. /**
  249. * Test fetching errors.
  250. *
  251. * @return void
  252. */
  253. public function testError()
  254. {
  255. $context = new ArrayContext($this->request, []);
  256. $this->assertEquals([], $context->error('Comments.empty'));
  257. $context = new ArrayContext($this->request, [
  258. 'errors' => [
  259. 'Comments' => [
  260. 'comment' => ['Comment is required'],
  261. 'empty' => [],
  262. 'user_id' => 'A valid userid is required',
  263. ]
  264. ]
  265. ]);
  266. $this->assertEquals(['Comment is required'], $context->error('Comments.comment'));
  267. $this->assertEquals('A valid userid is required', $context->error('Comments.user_id'));
  268. $this->assertEquals([], $context->error('Comments.empty'));
  269. $this->assertNull($context->error('Comments.not_there'));
  270. }
  271. /**
  272. * Test checking errors.
  273. *
  274. * @return void
  275. */
  276. public function testHasError()
  277. {
  278. $context = new ArrayContext($this->request, [
  279. 'errors' => [
  280. 'Comments' => [
  281. 'comment' => ['Comment is required'],
  282. 'empty' => [],
  283. 'user_id' => 'A valid userid is required',
  284. ]
  285. ]
  286. ]);
  287. $this->assertFalse($context->hasError('Comments.not_there'));
  288. $this->assertFalse($context->hasError('Comments.empty'));
  289. $this->assertTrue($context->hasError('Comments.user_id'));
  290. $this->assertTrue($context->hasError('Comments.comment'));
  291. }
  292. }