ArrayContextTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * setup method.
  25. *
  26. * @return void
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. $this->request = new Request();
  31. }
  32. /**
  33. * Test getting the primary key.
  34. *
  35. * @return void
  36. */
  37. public function testPrimaryKey() {
  38. $context = new ArrayContext($this->request, []);
  39. $this->assertEquals([], $context->primaryKey());
  40. $context = new ArrayContext($this->request, [
  41. 'schema' => [
  42. '_constraints' => 'mistake',
  43. ]
  44. ]);
  45. $this->assertEquals([], $context->primaryKey());
  46. $data = [
  47. 'schema' => [
  48. '_constraints' => [
  49. 'primary' => ['type' => 'primary', 'columns' => ['id']]
  50. ]
  51. ],
  52. ];
  53. $context = new ArrayContext($this->request, $data);
  54. $expected = ['id'];
  55. $this->assertEquals($expected, $context->primaryKey());
  56. }
  57. /**
  58. * Test isPrimaryKey.
  59. *
  60. * @return void
  61. */
  62. public function testIsPrimaryKey() {
  63. $context = new ArrayContext($this->request, []);
  64. $this->assertFalse($context->isPrimaryKey('id'));
  65. $context = new ArrayContext($this->request, [
  66. 'schema' => [
  67. '_constraints' => 'mistake',
  68. ]
  69. ]);
  70. $this->assertFalse($context->isPrimaryKey('mistake'));
  71. $data = [
  72. 'schema' => [
  73. '_constraints' => [
  74. 'primary' => ['type' => 'primary', 'columns' => ['id']]
  75. ]
  76. ],
  77. ];
  78. $context = new ArrayContext($this->request, $data);
  79. $this->assertTrue($context->isPrimaryKey('id'));
  80. $this->assertFalse($context->isPrimaryKey('name'));
  81. $data = [
  82. 'schema' => [
  83. '_constraints' => [
  84. 'primary' => ['type' => 'primary', 'columns' => ['id', 'name']]
  85. ]
  86. ],
  87. ];
  88. $context = new ArrayContext($this->request, $data);
  89. $this->assertTrue($context->isPrimaryKey('id'));
  90. $this->assertTrue($context->isPrimaryKey('name'));
  91. }
  92. /**
  93. * Test the isCreate method.
  94. *
  95. * @return void
  96. */
  97. public function testIsCreate() {
  98. $context = new ArrayContext($this->request, []);
  99. $this->assertTrue($context->isCreate());
  100. $data = [
  101. 'schema' => [
  102. '_constraints' => [
  103. 'primary' => ['type' => 'primary', 'columns' => ['id']]
  104. ]
  105. ],
  106. ];
  107. $context = new ArrayContext($this->request, $data);
  108. $this->assertTrue($context->isCreate());
  109. $data['defaults'] = ['id' => 2];
  110. $context = new ArrayContext($this->request, $data);
  111. $this->assertFalse($context->isCreate());
  112. }
  113. /**
  114. * Test reading values from the request & defaults.
  115. */
  116. public function testValPresent() {
  117. $this->request->data = [
  118. 'Articles' => [
  119. 'title' => 'New title',
  120. 'body' => 'My copy',
  121. ]
  122. ];
  123. $context = new ArrayContext($this->request, [
  124. 'defaults' => [
  125. 'Articles' => [
  126. 'title' => 'Default value',
  127. 'published' => 0
  128. ]
  129. ]
  130. ]);
  131. $this->assertEquals('New title', $context->val('Articles.title'));
  132. $this->assertEquals('My copy', $context->val('Articles.body'));
  133. $this->assertEquals(0, $context->val('Articles.published'));
  134. $this->assertNull($context->val('Articles.nope'));
  135. }
  136. /**
  137. * Test getting values when the request and defaults are missing.
  138. *
  139. * @return void
  140. */
  141. public function testValMissing() {
  142. $context = new ArrayContext($this->request, []);
  143. $this->assertNull($context->val('Comments.field'));
  144. }
  145. /**
  146. * Test isRequired
  147. *
  148. * @return void
  149. */
  150. public function testIsRequired() {
  151. $context = new ArrayContext($this->request, [
  152. 'required' => [
  153. 'Comments' => [
  154. 'required' => true,
  155. 'nope' => false
  156. ]
  157. ]
  158. ]);
  159. $this->assertTrue($context->isRequired('Comments.required'));
  160. $this->assertFalse($context->isRequired('Comments.nope'));
  161. $this->assertFalse($context->isRequired('Articles.id'));
  162. }
  163. /**
  164. * Test isRequired when the required key is omitted
  165. *
  166. * @return void
  167. */
  168. public function testIsRequiredUndefined() {
  169. $context = new ArrayContext($this->request, []);
  170. $this->assertFalse($context->isRequired('Comments.field'));
  171. }
  172. /**
  173. * Test the type method.
  174. *
  175. * @return void
  176. */
  177. public function testType() {
  178. $context = new ArrayContext($this->request, [
  179. 'schema' => [
  180. 'Comments' => [
  181. 'id' => ['type' => 'integer'],
  182. 'comment' => ['length' => 255]
  183. ]
  184. ]
  185. ]);
  186. $this->assertNull($context->type('Comments.undefined'));
  187. $this->assertEquals('integer', $context->type('Comments.id'));
  188. $this->assertNull($context->type('Comments.comment'));
  189. }
  190. /**
  191. * Test the type method when the data is missing.
  192. *
  193. * @return void
  194. */
  195. public function testIsTypeUndefined() {
  196. $context = new ArrayContext($this->request, []);
  197. $this->assertNull($context->type('Comments.undefined'));
  198. }
  199. /**
  200. * Test fetching attributes.
  201. *
  202. * @return void
  203. */
  204. public function testAttributes() {
  205. $context = new ArrayContext($this->request, [
  206. 'schema' => [
  207. 'Comments' => [
  208. 'id' => ['type' => 'integer'],
  209. 'comment' => ['type' => 'string', 'length' => 255],
  210. 'decimal' => ['type' => 'decimal', 'precision' => 2, 'length' => 5],
  211. 'floaty' => ['type' => 'float', 'precision' => 2, 'length' => 5],
  212. ]
  213. ]
  214. ]);
  215. $this->assertEquals([], $context->attributes('Comments.id'));
  216. $this->assertEquals(['length' => 255], $context->attributes('Comments.comment'));
  217. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.decimal'));
  218. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.floaty'));
  219. }
  220. /**
  221. * Test fetching errors.
  222. *
  223. * @return void
  224. */
  225. public function testError() {
  226. $context = new ArrayContext($this->request, []);
  227. $this->assertEquals([], $context->error('Comments.empty'));
  228. $context = new ArrayContext($this->request, [
  229. 'errors' => [
  230. 'Comments' => [
  231. 'comment' => ['Comment is required'],
  232. 'empty' => [],
  233. 'user_id' => 'A valid userid is required',
  234. ]
  235. ]
  236. ]);
  237. $this->assertEquals(['Comment is required'], $context->error('Comments.comment'));
  238. $this->assertEquals('A valid userid is required', $context->error('Comments.user_id'));
  239. $this->assertEquals([], $context->error('Comments.empty'));
  240. $this->assertNull($context->error('Comments.not_there'));
  241. }
  242. /**
  243. * Test checking errors.
  244. *
  245. * @return void
  246. */
  247. public function testHasError() {
  248. $context = new ArrayContext($this->request, [
  249. 'errors' => [
  250. 'Comments' => [
  251. 'comment' => ['Comment is required'],
  252. 'empty' => [],
  253. 'user_id' => 'A valid userid is required',
  254. ]
  255. ]
  256. ]);
  257. $this->assertFalse($context->hasError('Comments.not_there'));
  258. $this->assertFalse($context->hasError('Comments.empty'));
  259. $this->assertTrue($context->hasError('Comments.user_id'));
  260. $this->assertTrue($context->hasError('Comments.comment'));
  261. }
  262. }