PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

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