ArrayContextTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 CakePHP(tm) v 3.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 reading values from the request & defaults.
  34. */
  35. public function testValPresent() {
  36. $this->request->data = [
  37. 'Articles' => [
  38. 'title' => 'New title',
  39. 'body' => 'My copy',
  40. ]
  41. ];
  42. $context = new ArrayContext($this->request, [
  43. 'defaults' => [
  44. 'Articles' => [
  45. 'title' => 'Default value',
  46. 'published' => 0
  47. ]
  48. ]
  49. ]);
  50. $this->assertEquals('New title', $context->val('Articles.title'));
  51. $this->assertEquals('My copy', $context->val('Articles.body'));
  52. $this->assertEquals(0, $context->val('Articles.published'));
  53. $this->assertNull($context->val('Articles.nope'));
  54. }
  55. /**
  56. * Test getting values when the request and defaults are missing.
  57. *
  58. * @return void
  59. */
  60. public function testValMissing() {
  61. $context = new ArrayContext($this->request, []);
  62. $this->assertNull($context->val('Comments.field'));
  63. }
  64. /**
  65. * Test isRequired
  66. *
  67. * @return void
  68. */
  69. public function testIsRequired() {
  70. $context = new ArrayContext($this->request, [
  71. 'required' => [
  72. 'Comments' => [
  73. 'required' => true,
  74. 'nope' => false
  75. ]
  76. ]
  77. ]);
  78. $this->assertTrue($context->isRequired('Comments.required'));
  79. $this->assertFalse($context->isRequired('Comments.nope'));
  80. $this->assertFalse($context->isRequired('Articles.id'));
  81. }
  82. /**
  83. * Test isRequired when the required key is omitted
  84. *
  85. * @return void
  86. */
  87. public function testIsRequiredUndefined() {
  88. $context = new ArrayContext($this->request, []);
  89. $this->assertFalse($context->isRequired('Comments.field'));
  90. }
  91. /**
  92. * Test the type method.
  93. *
  94. * @return void
  95. */
  96. public function testType() {
  97. $context = new ArrayContext($this->request, [
  98. 'schema' => [
  99. 'Comments' => [
  100. 'id' => ['type' => 'integer'],
  101. 'comment' => ['length' => 255]
  102. ]
  103. ]
  104. ]);
  105. $this->assertNull($context->type('Comments.undefined'));
  106. $this->assertEquals('integer', $context->type('Comments.id'));
  107. $this->assertNull($context->type('Comments.comment'));
  108. }
  109. /**
  110. * Test the type method when the data is missing.
  111. *
  112. * @return void
  113. */
  114. public function testIsTypeUndefined() {
  115. $context = new ArrayContext($this->request, []);
  116. $this->assertNull($context->type('Comments.undefined'));
  117. }
  118. /**
  119. * Test fetching attributes.
  120. *
  121. * @return void
  122. */
  123. public function testAttributes() {
  124. $context = new ArrayContext($this->request, [
  125. 'schema' => [
  126. 'Comments' => [
  127. 'id' => ['type' => 'integer'],
  128. 'comment' => ['type' => 'string', 'length' => 255],
  129. 'decimal' => ['type' => 'decimal', 'precision' => 2, 'length' => 5],
  130. 'floaty' => ['type' => 'float', 'precision' => 2, 'length' => 5],
  131. ]
  132. ]
  133. ]);
  134. $this->assertEquals([], $context->attributes('Comments.id'));
  135. $this->assertEquals(['length' => 255], $context->attributes('Comments.comment'));
  136. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.decimal'));
  137. $this->assertEquals(['precision' => 2, 'length' => 5], $context->attributes('Comments.floaty'));
  138. }
  139. /**
  140. * Test fetching errors.
  141. *
  142. * @return void
  143. */
  144. public function testError() {
  145. $context = new ArrayContext($this->request, []);
  146. $this->assertEquals([], $context->error('Comments.empty'));
  147. $context = new ArrayContext($this->request, [
  148. 'errors' => [
  149. 'Comments' => [
  150. 'comment' => ['Comment is required'],
  151. 'empty' => [],
  152. 'user_id' => 'A valid userid is required',
  153. ]
  154. ]
  155. ]);
  156. $this->assertEquals(['Comment is required'], $context->error('Comments.comment'));
  157. $this->assertEquals('A valid userid is required', $context->error('Comments.user_id'));
  158. $this->assertEquals([], $context->error('Comments.empty'));
  159. $this->assertNull($context->error('Comments.not_there'));
  160. }
  161. /**
  162. * Test checking errors.
  163. *
  164. * @return void
  165. */
  166. public function testHasError() {
  167. $context = new ArrayContext($this->request, [
  168. 'errors' => [
  169. 'Comments' => [
  170. 'comment' => ['Comment is required'],
  171. 'empty' => [],
  172. 'user_id' => 'A valid userid is required',
  173. ]
  174. ]
  175. ]);
  176. $this->assertFalse($context->hasError('Comments.not_there'));
  177. $this->assertFalse($context->hasError('Comments.empty'));
  178. $this->assertTrue($context->hasError('Comments.user_id'));
  179. $this->assertTrue($context->hasError('Comments.comment'));
  180. }
  181. }