ValidatorTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\Validation;
  16. use \Cake\Validation\ValidationSet;
  17. use \Cake\Validation\Validator;
  18. /**
  19. * Tests Validator class
  20. *
  21. */
  22. class ValidatorTest extends \Cake\TestSuite\TestCase {
  23. /**
  24. * Testing you can dynamically add rules to a field
  25. *
  26. * @return void
  27. */
  28. public function testAddingRulesToField() {
  29. $validator = new Validator;
  30. $validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
  31. $set = $validator->field('title');
  32. $this->assertInstanceOf('\Cake\Validation\ValidationSet', $set);
  33. $this->assertCount(1, $set);
  34. $validator->add('title', 'another', ['rule' => 'alphanumeric']);
  35. $this->assertCount(2, $set);
  36. $validator->add('body', 'another', ['rule' => 'crazy']);
  37. $this->assertCount(1, $validator->field('body'));
  38. $this->assertCount(2, $validator);
  39. }
  40. /**
  41. * Tests that calling field will create a default validation set for it
  42. *
  43. * @return void
  44. */
  45. public function testFieldDefault() {
  46. $validator = new Validator;
  47. $this->assertFalse($validator->hasField('foo'));
  48. $field = $validator->field('foo');
  49. $this->assertInstanceOf('\Cake\Validation\ValidationSet', $field);
  50. $this->assertCount(0, $field);
  51. $this->assertTrue($validator->hasField('foo'));
  52. }
  53. /**
  54. * Tests that field method can be used as a setter
  55. *
  56. * @return void
  57. */
  58. public function testFieldSetter() {
  59. $validator = new Validator;
  60. $validationSet = new ValidationSet;
  61. $validator->field('thing', $validationSet);
  62. $this->assertSame($validationSet, $validator->field('thing'));
  63. }
  64. /**
  65. * Tests the remove method
  66. *
  67. * @return void
  68. */
  69. public function testRemove() {
  70. $validator = new Validator;
  71. $validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
  72. $validator->add('title', 'foo', ['rule' => 'bar']);
  73. $this->assertCount(2, $validator->field('title'));
  74. $validator->remove('title');
  75. $this->assertCount(0, $validator->field('title'));
  76. $validator->remove('title');
  77. $validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
  78. $validator->add('title', 'foo', ['rule' => 'bar']);
  79. $this->assertCount(2, $validator->field('title'));
  80. $validator->remove('title', 'foo');
  81. $this->assertCount(1, $validator->field('title'));
  82. $this->assertNull($validator->field('title')->rule('foo'));
  83. }
  84. /**
  85. * Tests the validatePresence method
  86. *
  87. * @return void
  88. */
  89. public function testValidatePresence() {
  90. $validator = new Validator;
  91. $this->assertSame($validator, $validator->validatePresence('title'));
  92. $this->assertTrue($validator->field('title')->isPresenceRequired());
  93. $validator->validatePresence('title', false);
  94. $this->assertFalse($validator->field('title')->isPresenceRequired());
  95. $validator->validatePresence('title', 'create');
  96. $this->assertEquals('create', $validator->field('title')->isPresenceRequired());
  97. $validator->validatePresence('title', 'update');
  98. $this->assertEquals('update', $validator->field('title')->isPresenceRequired());
  99. }
  100. /**
  101. * Tests the isPresenceRequired method
  102. *
  103. * @return void
  104. */
  105. public function testIsPresenceRequired() {
  106. $validator = new Validator;
  107. $this->assertSame($validator, $validator->validatePresence('title'));
  108. $this->assertTrue($validator->isPresenceRequired('title', true));
  109. $this->assertTrue($validator->isPresenceRequired('title', false));
  110. $validator->validatePresence('title', false);
  111. $this->assertFalse($validator->isPresenceRequired('title', true));
  112. $this->assertFalse($validator->isPresenceRequired('title', false));
  113. $validator->validatePresence('title', 'create');
  114. $this->assertTrue($validator->isPresenceRequired('title', true));
  115. $this->assertFalse($validator->isPresenceRequired('title', false));
  116. $validator->validatePresence('title', 'update');
  117. $this->assertTrue($validator->isPresenceRequired('title', false));
  118. $this->assertFalse($validator->isPresenceRequired('title', true));
  119. }
  120. /**
  121. * Tests errors generated when a field presence is required
  122. *
  123. * @return void
  124. */
  125. public function testErrorsWithPresenceRequired() {
  126. $validator = new Validator;
  127. $validator->validatePresence('title');
  128. $errors = $validator->errors(['foo' => 'something']);
  129. $expected = ['title' => ['This field is required']];
  130. $this->assertEquals($expected, $errors);
  131. $this->assertEmpty($validator->errors(['title' => 'bar']));
  132. $validator->validatePresence('title', false);
  133. $this->assertEmpty($validator->errors(['foo' => 'bar']));
  134. }
  135. /**
  136. * Tests the allowEmpty method
  137. *
  138. * @return void
  139. */
  140. public function testAllowEmpty() {
  141. $validator = new Validator;
  142. $this->assertSame($validator, $validator->allowEmpty('title'));
  143. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  144. $validator->allowEmpty('title', false);
  145. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  146. $validator->allowEmpty('title', 'created');
  147. $this->assertEquals('created', $validator->field('title')->isEmptyAllowed());
  148. $validator->allowEmpty('title', 'updated');
  149. $this->assertEquals('updated', $validator->field('title')->isEmptyAllowed());
  150. }
  151. /**
  152. * Tests the isEmptyAllowed method
  153. *
  154. * @return void
  155. */
  156. public function testIsEmptyAllowed() {
  157. $validator = new Validator;
  158. $this->assertSame($validator, $validator->allowEmpty('title'));
  159. $this->assertTrue($validator->isEmptyAllowed('title', true));
  160. $this->assertTrue($validator->isEmptyAllowed('title', false));
  161. $validator->allowEmpty('title', false);
  162. $this->assertFalse($validator->isEmptyAllowed('title', true));
  163. $this->assertFalse($validator->isEmptyAllowed('title', false));
  164. $validator->allowEmpty('title', 'create');
  165. $this->assertTrue($validator->isEmptyAllowed('title', true));
  166. $this->assertFalse($validator->isEmptyAllowed('title', false));
  167. $validator->allowEmpty('title', 'update');
  168. $this->assertTrue($validator->isEmptyAllowed('title', false));
  169. $this->assertFalse($validator->isEmptyAllowed('title', true));
  170. }
  171. /**
  172. * Tests errors generated when a field is not allowed to be empty
  173. *
  174. * @return void
  175. */
  176. public function testErrorsWithEmptyNotAllowed() {
  177. $validator = new Validator;
  178. $validator->allowEmpty('title', false);
  179. $errors = $validator->errors(['title' => '']);
  180. $expected = ['title' => ['This field cannot be left empty']];
  181. $this->assertEquals($expected, $errors);
  182. $errors = $validator->errors(['title' => []]);
  183. $expected = ['title' => ['This field cannot be left empty']];
  184. $this->assertEquals($expected, $errors);
  185. $errors = $validator->errors(['title' => null]);
  186. $expected = ['title' => ['This field cannot be left empty']];
  187. $this->assertEquals($expected, $errors);
  188. $errors = $validator->errors(['title' => 0]);
  189. $this->assertEmpty($errors);
  190. $errors = $validator->errors(['title' => '0']);
  191. $this->assertEmpty($errors);
  192. $errors = $validator->errors(['title' => false]);
  193. $this->assertEmpty($errors);
  194. }
  195. /**
  196. * Tests errors generated when a field is allowed to be empty
  197. *
  198. * @return void
  199. */
  200. public function testErrorsWithEmptyAllowed() {
  201. $validator = new Validator;
  202. $validator->allowEmpty('title');
  203. $errors = $validator->errors(['title' => '']);
  204. $this->assertEmpty($errors);
  205. $errors = $validator->errors(['title' => []]);
  206. $this->assertEmpty($errors);
  207. $errors = $validator->errors(['title' => null]);
  208. $this->assertEmpty($errors);
  209. $errors = $validator->errors(['title' => 0]);
  210. $this->assertEmpty($errors);
  211. $errors = $validator->errors(['title' => '0']);
  212. $this->assertEmpty($errors);
  213. $errors = $validator->errors(['title' => false]);
  214. $this->assertEmpty($errors);
  215. }
  216. /**
  217. * Test the provider() method
  218. *
  219. * @return void
  220. */
  221. public function testProvider() {
  222. $validator = new Validator;
  223. $object = new \stdClass;
  224. $this->assertSame($validator, $validator->provider('foo', $object));
  225. $this->assertSame($object, $validator->provider('foo'));
  226. $this->assertNull($validator->provider('bar'));
  227. $another = new \stdClass;
  228. $this->assertSame($validator, $validator->provider('bar', $another));
  229. $this->assertSame($another, $validator->provider('bar'));
  230. $this->assertEquals(new \Cake\Validation\RulesProvider, $validator->provider('default'));
  231. }
  232. /**
  233. * Tests errors() method when using validators from the default provider, this proves
  234. * that it returns a default validation message and the custom one set in the rule
  235. *
  236. * @return void
  237. */
  238. public function testErrorsFromDefaultProvider() {
  239. $validator = new Validator;
  240. $validator
  241. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  242. ->add('email', 'notEmpty', ['rule' => 'notEmpty'])
  243. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  244. $errors = $validator->errors(['email' => 'not an email!']);
  245. $expected = [
  246. 'email' => [
  247. 'alpha' => 'The provided value is invalid',
  248. 'email' => 'Y u no write email?'
  249. ]
  250. ];
  251. $this->assertEquals($expected, $errors);
  252. }
  253. /**
  254. * Tests using validation methods from different providers and returning the error
  255. * as a string
  256. *
  257. * @return void
  258. */
  259. public function testErrorsFromCustomProvider() {
  260. $validator = new Validator;
  261. $validator
  262. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  263. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  264. $thing = $this->getMock('\stdClass', ['isCool']);
  265. $thing->expects($this->once())->method('isCool')
  266. ->will($this->returnCallback(function($data, $context) use ($thing) {
  267. $this->assertEquals('bar', $data);
  268. $expected = [
  269. 'default' => new \Cake\Validation\RulesProvider,
  270. 'thing' => $thing
  271. ];
  272. $expected = [
  273. 'newRecord' => true,
  274. 'providers' => $expected,
  275. 'data' => [
  276. 'email' => '!',
  277. 'title' => 'bar'
  278. ]
  279. ];
  280. $this->assertEquals($expected, $context);
  281. return "That ain't cool, yo";
  282. }));
  283. $validator->provider('thing', $thing);
  284. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  285. $expected = [
  286. 'email' => ['alpha' => 'The provided value is invalid'],
  287. 'title' => ['cool' => "That ain't cool, yo"]
  288. ];
  289. $this->assertEquals($expected, $errors);
  290. }
  291. /**
  292. * Tests that it is possible to pass extra arguments to the validation function
  293. * and it still gets the providers as last argument
  294. *
  295. * @return void
  296. */
  297. public function testMethodsWithExtraArguments() {
  298. $validator = new Validator;
  299. $validator->add('title', 'cool', [
  300. 'rule' => ['isCool', 'and', 'awesome'],
  301. 'provider' => 'thing'
  302. ]);
  303. $thing = $this->getMock('\stdClass', ['isCool']);
  304. $thing->expects($this->once())->method('isCool')
  305. ->will($this->returnCallback(function($data, $a, $b, $context) use ($thing) {
  306. $this->assertEquals('bar', $data);
  307. $this->assertEquals('and', $a);
  308. $this->assertEquals('awesome', $b);
  309. $expected = [
  310. 'default' => new \Cake\Validation\RulesProvider,
  311. 'thing' => $thing
  312. ];
  313. $expected = [
  314. 'newRecord' => true,
  315. 'providers' => $expected,
  316. 'data' => [
  317. 'email' => '!',
  318. 'title' => 'bar'
  319. ]
  320. ];
  321. $this->assertEquals($expected, $context);
  322. return "That ain't cool, yo";
  323. }));
  324. $validator->provider('thing', $thing);
  325. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  326. $expected = [
  327. 'title' => ['cool' => "That ain't cool, yo"]
  328. ];
  329. $this->assertEquals($expected, $errors);
  330. }
  331. /**
  332. * Tests that it is possible to use a closure as a rule
  333. *
  334. * @return void
  335. */
  336. public function testUsingClosureAsRule() {
  337. $validator = new Validator;
  338. $validator->add('name', 'myRule', [
  339. 'rule' => function($data, $provider) {
  340. $this->assertEquals('foo', $data);
  341. return 'You fail';
  342. }
  343. ]);
  344. $expected = ['name' => ['myRule' => 'You fail']];
  345. $this->assertEquals($expected, $validator->errors(['name' => 'foo']));
  346. }
  347. /**
  348. * Tests that setting last to a rule will stop validating the rest of the rules
  349. *
  350. * @return void
  351. */
  352. public function testErrorsWithLastRule() {
  353. $validator = new Validator;
  354. $validator
  355. ->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
  356. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  357. $errors = $validator->errors(['email' => 'not an email!']);
  358. $expected = [
  359. 'email' => [
  360. 'alpha' => 'The provided value is invalid'
  361. ]
  362. ];
  363. $this->assertEquals($expected, $errors);
  364. }
  365. /**
  366. * Tests it is possible to get validation sets for a field using an array interface
  367. *
  368. * @return void
  369. */
  370. public function testArrayAccessGet() {
  371. $validator = new Validator;
  372. $validator
  373. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  374. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  375. $this->assertSame($validator['email'], $validator->field('email'));
  376. $this->assertSame($validator['title'], $validator->field('title'));
  377. }
  378. /**
  379. * Tests it is possible to check for validation sets for a field using an array inteface
  380. *
  381. * @return void
  382. */
  383. public function testArrayAccessExists() {
  384. $validator = new Validator;
  385. $validator
  386. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  387. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  388. $this->assertTrue(isset($validator['email']));
  389. $this->assertTrue(isset($validator['title']));
  390. $this->assertFalse(isset($validator['foo']));
  391. }
  392. /**
  393. * Tests it is possible to set validation rules for a field using an array inteface
  394. *
  395. * @return void
  396. */
  397. public function testArrayAccessSet() {
  398. $validator = new Validator;
  399. $validator
  400. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  401. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  402. $validator['name'] = $validator->field('title');
  403. $this->assertSame($validator->field('title'), $validator->field('name'));
  404. $validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
  405. $this->assertEquals($validator->field('email'), $validator->field('email'));
  406. }
  407. /**
  408. * Tests it is possible to unset validation rules
  409. *
  410. * @return void
  411. */
  412. public function testArrayAccessUset() {
  413. $validator = new Validator;
  414. $validator
  415. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  416. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  417. $this->assertTrue(isset($validator['title']));
  418. unset($validator['title']);
  419. $this->assertFalse(isset($validator['title']));
  420. }
  421. /**
  422. * Tests the countable interface
  423. *
  424. * @return void
  425. */
  426. public function testCount() {
  427. $validator = new Validator;
  428. $validator
  429. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  430. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  431. $this->assertCount(2, $validator);
  432. }
  433. /**
  434. * Tests adding rules via alternative syntax
  435. *
  436. * @return void
  437. */
  438. public function testAddMulitple() {
  439. $validator = new Validator;
  440. $validator->add('title', [
  441. 'notEmpty' => [
  442. 'rule' => 'notEmpty'
  443. ],
  444. 'length' => [
  445. 'rule' => ['minLength', 10],
  446. 'message' => 'Titles need to be at least 10 characters long'
  447. ]
  448. ]);
  449. $set = $validator->field('title');
  450. $this->assertInstanceOf('\Cake\Validation\ValidationSet', $set);
  451. $this->assertCount(2, $set);
  452. }
  453. }