ArrayContextTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 isRequired
  154. *
  155. * @return void
  156. */
  157. public function testIsRequired()
  158. {
  159. $context = new ArrayContext($this->request, [
  160. 'required' => [
  161. 'Comments' => [
  162. 'required' => true,
  163. 'nope' => false
  164. ]
  165. ]
  166. ]);
  167. $this->assertTrue($context->isRequired('Comments.required'));
  168. $this->assertFalse($context->isRequired('Comments.nope'));
  169. $this->assertFalse($context->isRequired('Articles.id'));
  170. }
  171. /**
  172. * Test isRequired when the required key is omitted
  173. *
  174. * @return void
  175. */
  176. public function testIsRequiredUndefined()
  177. {
  178. $context = new ArrayContext($this->request, []);
  179. $this->assertFalse($context->isRequired('Comments.field'));
  180. }
  181. /**
  182. * Test the type method.
  183. *
  184. * @return void
  185. */
  186. public function testType()
  187. {
  188. $context = new ArrayContext($this->request, [
  189. 'schema' => [
  190. 'Comments' => [
  191. 'id' => ['type' => 'integer'],
  192. 'comment' => ['length' => 255]
  193. ]
  194. ]
  195. ]);
  196. $this->assertNull($context->type('Comments.undefined'));
  197. $this->assertEquals('integer', $context->type('Comments.id'));
  198. $this->assertNull($context->type('Comments.comment'));
  199. }
  200. /**
  201. * Test the type method when the data is missing.
  202. *
  203. * @return void
  204. */
  205. public function testIsTypeUndefined()
  206. {
  207. $context = new ArrayContext($this->request, []);
  208. $this->assertNull($context->type('Comments.undefined'));
  209. }
  210. /**
  211. * Test fetching attributes.
  212. *
  213. * @return void
  214. */
  215. public function testAttributes()
  216. {
  217. $context = new ArrayContext($this->request, [
  218. 'schema' => [
  219. 'Comments' => [
  220. 'id' => ['type' => 'integer'],
  221. 'comment' => ['type' => 'string', 'length' => 255],
  222. 'decimal' => ['type' => 'decimal', 'precision' => 2, 'length' => 5],
  223. 'floaty' => ['type' => 'float', 'precision' => 2, 'length' => 5],
  224. ]
  225. ]
  226. ]);
  227. $this->assertEquals([], $context->attributes('Comments.id'));
  228. $this->assertEquals(['length' => 255], $context->attributes('Comments.comment'));
  229. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.decimal'));
  230. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.floaty'));
  231. }
  232. /**
  233. * Test fetching errors.
  234. *
  235. * @return void
  236. */
  237. public function testError()
  238. {
  239. $context = new ArrayContext($this->request, []);
  240. $this->assertEquals([], $context->error('Comments.empty'));
  241. $context = new ArrayContext($this->request, [
  242. 'errors' => [
  243. 'Comments' => [
  244. 'comment' => ['Comment is required'],
  245. 'empty' => [],
  246. 'user_id' => 'A valid userid is required',
  247. ]
  248. ]
  249. ]);
  250. $this->assertEquals(['Comment is required'], $context->error('Comments.comment'));
  251. $this->assertEquals('A valid userid is required', $context->error('Comments.user_id'));
  252. $this->assertEquals([], $context->error('Comments.empty'));
  253. $this->assertNull($context->error('Comments.not_there'));
  254. }
  255. /**
  256. * Test checking errors.
  257. *
  258. * @return void
  259. */
  260. public function testHasError()
  261. {
  262. $context = new ArrayContext($this->request, [
  263. 'errors' => [
  264. 'Comments' => [
  265. 'comment' => ['Comment is required'],
  266. 'empty' => [],
  267. 'user_id' => 'A valid userid is required',
  268. ]
  269. ]
  270. ]);
  271. $this->assertFalse($context->hasError('Comments.not_there'));
  272. $this->assertFalse($context->hasError('Comments.empty'));
  273. $this->assertTrue($context->hasError('Comments.user_id'));
  274. $this->assertTrue($context->hasError('Comments.comment'));
  275. }
  276. }