CakeValidationSetTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * CakeValidationSetTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Model.Validator
  16. * @since CakePHP(tm) v 2.2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakeValidationSet', 'Model/Validator');
  20. /**
  21. * CakeValidationSetTest
  22. *
  23. * @package Cake.Test.Case.Model.Validator
  24. */
  25. class CakeValidationSetTest extends CakeTestCase {
  26. /**
  27. * testValidate method
  28. *
  29. * @return void
  30. */
  31. public function testValidate() {
  32. $Field = new CakeValidationSet('title', 'notEmpty');
  33. $data = array(
  34. 'title' => '',
  35. 'body' => 'a body'
  36. );
  37. $result = $Field->validate($data);
  38. $expected = array('This field cannot be left blank');
  39. $this->assertEquals($expected, $result);
  40. $Field = new CakeValidationSet('body', 'notEmpty');
  41. $result = $Field->validate($data);
  42. $this->assertEmpty($result);
  43. $Field = new CakeValidationSet('nothere', array(
  44. 'notEmpty' => array(
  45. 'rule' => 'notEmpty',
  46. 'required' => true
  47. )
  48. ));
  49. $result = $Field->validate($data);
  50. $expected = array('notEmpty');
  51. $this->assertEquals($expected, $result);
  52. $Field = new CakeValidationSet('body', array(
  53. 'inList' => array(
  54. 'rule' => array('inList', array('test'))
  55. )
  56. ));
  57. $result = $Field->validate($data);
  58. $expected = array('inList');
  59. $this->assertEquals($expected, $result);
  60. }
  61. /**
  62. * testGetRule method
  63. *
  64. * @return void
  65. */
  66. public function testGetRule() {
  67. $rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
  68. $Field = new CakeValidationSet('title', $rules);
  69. $result = $Field->getRule('notEmpty');
  70. $this->assertInstanceOf('CakeValidationRule', $result);
  71. $this->assertEquals('notEmpty', $result->rule);
  72. $this->assertEquals(null, $result->required);
  73. $this->assertEquals(false, $result->allowEmpty);
  74. $this->assertEquals(null, $result->on);
  75. $this->assertEquals(true, $result->last);
  76. $this->assertEquals('Can not be empty', $result->message);
  77. }
  78. /**
  79. * testGetRules method
  80. *
  81. * @return void
  82. */
  83. public function testGetRules() {
  84. $rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
  85. $Field = new CakeValidationSet('title', $rules);
  86. $result = $Field->getRules();
  87. $this->assertEquals(array('notEmpty'), array_keys($result));
  88. $this->assertInstanceOf('CakeValidationRule', $result['notEmpty']);
  89. }
  90. /**
  91. * testSetRule method
  92. *
  93. * @return void
  94. */
  95. public function testSetRule() {
  96. $rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
  97. $Field = new CakeValidationSet('title', $rules);
  98. $Rule = new CakeValidationRule($rules['notEmpty']);
  99. $this->assertEquals($Rule, $Field->getRule('notEmpty'));
  100. $rules = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
  101. $Rule = new CakeValidationRule($rules['validEmail']);
  102. $Field->setRule('validEmail', $Rule);
  103. $result = $Field->getRules();
  104. $this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
  105. $rules = array('validEmail' => array('rule' => 'email', 'message' => 'Other message'));
  106. $Rule = new CakeValidationRule($rules['validEmail']);
  107. $Field->setRule('validEmail', $Rule);
  108. $result = $Field->getRules();
  109. $this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
  110. $result = $Field->getRule('validEmail');
  111. $this->assertInstanceOf('CakeValidationRule', $result);
  112. $this->assertEquals('email', $result->rule);
  113. $this->assertEquals(null, $result->required);
  114. $this->assertEquals(false, $result->allowEmpty);
  115. $this->assertEquals(null, $result->on);
  116. $this->assertEquals(true, $result->last);
  117. $this->assertEquals('Other message', $result->message);
  118. }
  119. /**
  120. * testSetRules method
  121. *
  122. * @return void
  123. */
  124. public function testSetRules() {
  125. $rule = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
  126. $Field = new CakeValidationSet('title', $rule);
  127. $RuleEmpty = new CakeValidationRule($rule['notEmpty']);
  128. $rule = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
  129. $RuleEmail = new CakeValidationRule($rule['validEmail']);
  130. $rules = array('validEmail' => $RuleEmail);
  131. $Field->setRules($rules, false);
  132. $result = $Field->getRules();
  133. $this->assertEquals(array('validEmail'), array_keys($result));
  134. $Field->setRules(array('validEmail' => $rule), false);
  135. $result = $Field->getRules();
  136. $this->assertEquals(array('validEmail'), array_keys($result));
  137. $this->assertTrue(array_pop($result) instanceof CakeValidationRule);
  138. $rules = array('notEmpty' => $RuleEmpty);
  139. $Field->setRules($rules, true);
  140. $result = $Field->getRules();
  141. $this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
  142. $rules = array('notEmpty' => array('rule' => 'notEmpty'));
  143. $Field->setRules($rules, true);
  144. $result = $Field->getRules();
  145. $this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
  146. $this->assertTrue(array_pop($result) instanceof CakeValidationRule);
  147. $this->assertTrue(array_pop($result) instanceof CakeValidationRule);
  148. }
  149. /**
  150. * Tests getting a rule from the set using array access
  151. *
  152. * @return void
  153. */
  154. public function testArrayAccessGet() {
  155. $Set = new CakeValidationSet('title', array(
  156. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  157. 'numeric' => array('rule' => 'numeric'),
  158. 'other' => array('rule' => array('other', 1)),
  159. ));
  160. $rule = $Set['notEmpty'];
  161. $this->assertInstanceOf('CakeValidationRule', $rule);
  162. $this->assertEquals('notEmpty', $rule->rule);
  163. $rule = $Set['numeric'];
  164. $this->assertInstanceOf('CakeValidationRule', $rule);
  165. $this->assertEquals('numeric', $rule->rule);
  166. $rule = $Set['other'];
  167. $this->assertInstanceOf('CakeValidationRule', $rule);
  168. $this->assertEquals(array('other', 1), $rule->rule);
  169. }
  170. /**
  171. * Tests checking a rule from the set using array access
  172. *
  173. * @return void
  174. */
  175. public function testArrayAccessExists() {
  176. $Set = new CakeValidationSet('title', array(
  177. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  178. 'numeric' => array('rule' => 'numeric'),
  179. 'other' => array('rule' => array('other', 1)),
  180. ));
  181. $this->assertTrue(isset($Set['notEmpty']));
  182. $this->assertTrue(isset($Set['numeric']));
  183. $this->assertTrue(isset($Set['other']));
  184. $this->assertFalse(isset($Set['fail']));
  185. }
  186. /**
  187. * Tests setting a rule in the set using array access
  188. *
  189. * @return void
  190. */
  191. public function testArrayAccessSet() {
  192. $Set = new CakeValidationSet('title', array(
  193. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  194. ));
  195. $this->assertFalse(isset($Set['other']));
  196. $Set['other'] = array('rule' => array('other', 1));
  197. $rule = $Set['other'];
  198. $this->assertInstanceOf('CakeValidationRule', $rule);
  199. $this->assertEquals(array('other', 1), $rule->rule);
  200. $this->assertFalse(isset($Set['numeric']));
  201. $Set['numeric'] = new CakeValidationRule(array('rule' => 'numeric'));
  202. $rule = $Set['numeric'];
  203. $this->assertInstanceOf('CakeValidationRule', $rule);
  204. $this->assertEquals('numeric', $rule->rule);
  205. }
  206. /**
  207. * Tests unseting a rule from the set using array access
  208. *
  209. * @return void
  210. */
  211. public function testArrayAccessUnset() {
  212. $Set = new CakeValidationSet('title', array(
  213. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  214. 'numeric' => array('rule' => 'numeric'),
  215. 'other' => array('rule' => array('other', 1)),
  216. ));
  217. unset($Set['notEmpty']);
  218. $this->assertFalse(isset($Set['notEmpty']));
  219. unset($Set['numeric']);
  220. $this->assertFalse(isset($Set['notEmpty']));
  221. unset($Set['other']);
  222. $this->assertFalse(isset($Set['notEmpty']));
  223. }
  224. /**
  225. * Tests it is possible to iterate a validation set object
  226. *
  227. * @return void
  228. */
  229. public function testIterator() {
  230. $Set = new CakeValidationSet('title', array(
  231. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  232. 'numeric' => array('rule' => 'numeric'),
  233. 'other' => array('rule' => array('other', 1)),
  234. ));
  235. $i = 0;
  236. foreach ($Set as $name => $rule) {
  237. if ($i === 0) {
  238. $this->assertEquals('notEmpty', $name);
  239. }
  240. if ($i === 1) {
  241. $this->assertEquals('numeric', $name);
  242. }
  243. if ($i === 2) {
  244. $this->assertEquals('other', $name);
  245. }
  246. $this->assertInstanceOf('CakeValidationRule', $rule);
  247. $i++;
  248. }
  249. $this->assertEquals(3, $i);
  250. }
  251. /**
  252. * Tests countable interface
  253. *
  254. * @return void
  255. */
  256. public function testCount() {
  257. $Set = new CakeValidationSet('title', array(
  258. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  259. 'numeric' => array('rule' => 'numeric'),
  260. 'other' => array('rule' => array('other', 1)),
  261. ));
  262. $this->assertCount(3, $Set);
  263. unset($Set['other']);
  264. $this->assertCount(2, $Set);
  265. }
  266. /**
  267. * Test removeRule method
  268. *
  269. * @return void
  270. */
  271. public function testRemoveRule() {
  272. $Set = new CakeValidationSet('title', array(
  273. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  274. 'numeric' => array('rule' => 'numeric'),
  275. 'other' => array('rule' => array('other', 1)),
  276. ));
  277. $Set->removeRule('notEmpty');
  278. $this->assertFalse(isset($Set['notEmpty']));
  279. $Set->removeRule('numeric');
  280. $this->assertFalse(isset($Set['numeric']));
  281. $Set->removeRule('other');
  282. $this->assertFalse(isset($Set['other']));
  283. }
  284. }