CakeValidationSetTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. $data = array(
  70. 'title' => '',
  71. 'body' => 'a body'
  72. );
  73. $result = $Field->getRule('notEmpty');
  74. $this->assertInstanceOf('CakeValidationRule', $result);
  75. $this->assertEquals('notEmpty', $result->rule);
  76. $this->assertEquals(null, $result->required);
  77. $this->assertEquals(false, $result->allowEmpty);
  78. $this->assertEquals(null, $result->on);
  79. $this->assertEquals(true, $result->last);
  80. $this->assertEquals('Can not be empty', $result->message);
  81. }
  82. /**
  83. * testGetRules method
  84. *
  85. * @return void
  86. */
  87. public function testGetRules() {
  88. $rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
  89. $Field = new CakeValidationSet('title', $rules);
  90. $result = $Field->getRules();
  91. $this->assertEquals(array('notEmpty'), array_keys($result));
  92. $this->assertInstanceOf('CakeValidationRule', $result['notEmpty']);
  93. }
  94. /**
  95. * testSetRule method
  96. *
  97. * @return void
  98. */
  99. public function testSetRule() {
  100. $rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
  101. $Field = new CakeValidationSet('title', $rules);
  102. $Rule = new CakeValidationRule($rules['notEmpty']);
  103. $this->assertEquals($Rule, $Field->getRule('notEmpty'));
  104. $rules = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
  105. $Rule = new CakeValidationRule($rules['validEmail']);
  106. $Field->setRule('validEmail', $Rule);
  107. $result = $Field->getRules();
  108. $this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
  109. $rules = array('validEmail' => array('rule' => 'email', 'message' => 'Other message'));
  110. $Rule = new CakeValidationRule($rules['validEmail']);
  111. $Field->setRule('validEmail', $Rule);
  112. $result = $Field->getRules();
  113. $this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
  114. $result = $Field->getRule('validEmail');
  115. $this->assertInstanceOf('CakeValidationRule', $result);
  116. $this->assertEquals('email', $result->rule);
  117. $this->assertEquals(null, $result->required);
  118. $this->assertEquals(false, $result->allowEmpty);
  119. $this->assertEquals(null, $result->on);
  120. $this->assertEquals(true, $result->last);
  121. $this->assertEquals('Other message', $result->message);
  122. }
  123. /**
  124. * testSetRules method
  125. *
  126. * @return void
  127. */
  128. public function testSetRules() {
  129. $rule = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
  130. $Field = new CakeValidationSet('title', $rule);
  131. $RuleEmpty = new CakeValidationRule($rule['notEmpty']);
  132. $rule = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
  133. $RuleEmail = new CakeValidationRule($rule['validEmail']);
  134. $rules = array('validEmail' => $RuleEmail);
  135. $Field->setRules($rules, false);
  136. $result = $Field->getRules();
  137. $this->assertEquals(array('validEmail'), array_keys($result));
  138. $Field->setRules(array('validEmail' => $rule), false);
  139. $result = $Field->getRules();
  140. $this->assertEquals(array('validEmail'), array_keys($result));
  141. $this->assertTrue(array_pop($result) instanceof CakeValidationRule);
  142. $rules = array('notEmpty' => $RuleEmpty);
  143. $Field->setRules($rules, true);
  144. $result = $Field->getRules();
  145. $this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
  146. $rules = array('notEmpty' => array('rule' => 'notEmpty'));
  147. $Field->setRules($rules, true);
  148. $result = $Field->getRules();
  149. $this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
  150. $this->assertTrue(array_pop($result) instanceof CakeValidationRule);
  151. $this->assertTrue(array_pop($result) instanceof CakeValidationRule);
  152. }
  153. /**
  154. * Tests getting a rule from the set using array access
  155. *
  156. * @return void
  157. */
  158. public function testArrayAccessGet() {
  159. $Set = new CakeValidationSet('title', array(
  160. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  161. 'numeric' => array('rule' => 'numeric'),
  162. 'other' => array('rule' => array('other', 1)),
  163. ));
  164. $rule = $Set['notEmpty'];
  165. $this->assertInstanceOf('CakeValidationRule', $rule);
  166. $this->assertEquals('notEmpty', $rule->rule);
  167. $rule = $Set['numeric'];
  168. $this->assertInstanceOf('CakeValidationRule', $rule);
  169. $this->assertEquals('numeric', $rule->rule);
  170. $rule = $Set['other'];
  171. $this->assertInstanceOf('CakeValidationRule', $rule);
  172. $this->assertEquals(array('other', 1), $rule->rule);
  173. }
  174. /**
  175. * Tests checking a rule from the set using array access
  176. *
  177. * @return void
  178. */
  179. public function testArrayAccessExists() {
  180. $Set = new CakeValidationSet('title', array(
  181. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  182. 'numeric' => array('rule' => 'numeric'),
  183. 'other' => array('rule' => array('other', 1)),
  184. ));
  185. $this->assertTrue(isset($Set['notEmpty']));
  186. $this->assertTrue(isset($Set['numeric']));
  187. $this->assertTrue(isset($Set['other']));
  188. $this->assertFalse(isset($Set['fail']));
  189. }
  190. /**
  191. * Tests setting a rule in the set using array access
  192. *
  193. * @return void
  194. */
  195. public function testArrayAccessSet() {
  196. $Set = new CakeValidationSet('title', array(
  197. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  198. ));
  199. $this->assertFalse(isset($Set['other']));
  200. $Set['other'] = array('rule' => array('other', 1));
  201. $rule = $Set['other'];
  202. $this->assertInstanceOf('CakeValidationRule', $rule);
  203. $this->assertEquals(array('other', 1), $rule->rule);
  204. $this->assertFalse(isset($Set['numeric']));
  205. $Set['numeric'] = new CakeValidationRule(array('rule' => 'numeric'));
  206. $rule = $Set['numeric'];
  207. $this->assertInstanceOf('CakeValidationRule', $rule);
  208. $this->assertEquals('numeric', $rule->rule);
  209. }
  210. /**
  211. * Tests unseting a rule from the set using array access
  212. *
  213. * @return void
  214. */
  215. public function testArrayAccessUnset() {
  216. $Set = new CakeValidationSet('title', array(
  217. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  218. 'numeric' => array('rule' => 'numeric'),
  219. 'other' => array('rule' => array('other', 1)),
  220. ));
  221. unset($Set['notEmpty']);
  222. $this->assertFalse(isset($Set['notEmpty']));
  223. unset($Set['numeric']);
  224. $this->assertFalse(isset($Set['notEmpty']));
  225. unset($Set['other']);
  226. $this->assertFalse(isset($Set['notEmpty']));
  227. }
  228. /**
  229. * Tests it is possible to iterate a validation set object
  230. *
  231. * @return void
  232. */
  233. public function testIterator() {
  234. $Set = new CakeValidationSet('title', array(
  235. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  236. 'numeric' => array('rule' => 'numeric'),
  237. 'other' => array('rule' => array('other', 1)),
  238. ));
  239. $i = 0;
  240. foreach ($Set as $name => $rule) {
  241. if ($i === 0) {
  242. $this->assertEquals('notEmpty', $name);
  243. }
  244. if ($i === 1) {
  245. $this->assertEquals('numeric', $name);
  246. }
  247. if ($i === 2) {
  248. $this->assertEquals('other', $name);
  249. }
  250. $this->assertInstanceOf('CakeValidationRule', $rule);
  251. $i++;
  252. }
  253. $this->assertEquals(3, $i);
  254. }
  255. /**
  256. * Tests countable interface
  257. *
  258. * @return void
  259. */
  260. public function testCount() {
  261. $Set = new CakeValidationSet('title', array(
  262. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  263. 'numeric' => array('rule' => 'numeric'),
  264. 'other' => array('rule' => array('other', 1)),
  265. ));
  266. $this->assertCount(3, $Set);
  267. unset($Set['other']);
  268. $this->assertCount(2, $Set);
  269. }
  270. /**
  271. * Test removeRule method
  272. *
  273. * @return void
  274. */
  275. public function testRemoveRule() {
  276. $Set = new CakeValidationSet('title', array(
  277. 'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
  278. 'numeric' => array('rule' => 'numeric'),
  279. 'other' => array('rule' => array('other', 1)),
  280. ));
  281. $Set->removeRule('notEmpty');
  282. $this->assertFalse(isset($Set['notEmpty']));
  283. $Set->removeRule('numeric');
  284. $this->assertFalse(isset($Set['numeric']));
  285. $Set->removeRule('other');
  286. $this->assertFalse(isset($Set['other']));
  287. }
  288. }