PageRenderTime 21ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php

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