ValidatorTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 custom error messages generated when a field presence is required
  137. *
  138. * @return void
  139. */
  140. public function testCustomErrorsWithPresenceRequired() {
  141. $validator = new Validator;
  142. $validator->validatePresence('title', true, 'Custom message');
  143. $errors = $validator->errors(['foo' => 'something']);
  144. $expected = ['title' => ['Custom message']];
  145. $this->assertEquals($expected, $errors);
  146. }
  147. /**
  148. * Tests the allowEmpty method
  149. *
  150. * @return void
  151. */
  152. public function testAllowEmpty() {
  153. $validator = new Validator;
  154. $this->assertSame($validator, $validator->allowEmpty('title'));
  155. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  156. $validator->allowEmpty('title', false);
  157. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  158. $validator->allowEmpty('title', 'created');
  159. $this->assertEquals('created', $validator->field('title')->isEmptyAllowed());
  160. $validator->allowEmpty('title', 'updated');
  161. $this->assertEquals('updated', $validator->field('title')->isEmptyAllowed());
  162. }
  163. /**
  164. * Tests the isEmptyAllowed method
  165. *
  166. * @return void
  167. */
  168. public function testIsEmptyAllowed() {
  169. $validator = new Validator;
  170. $this->assertSame($validator, $validator->allowEmpty('title'));
  171. $this->assertTrue($validator->isEmptyAllowed('title', true));
  172. $this->assertTrue($validator->isEmptyAllowed('title', false));
  173. $validator->allowEmpty('title', false);
  174. $this->assertFalse($validator->isEmptyAllowed('title', true));
  175. $this->assertFalse($validator->isEmptyAllowed('title', false));
  176. $validator->allowEmpty('title', 'create');
  177. $this->assertTrue($validator->isEmptyAllowed('title', true));
  178. $this->assertFalse($validator->isEmptyAllowed('title', false));
  179. $validator->allowEmpty('title', 'update');
  180. $this->assertTrue($validator->isEmptyAllowed('title', false));
  181. $this->assertFalse($validator->isEmptyAllowed('title', true));
  182. }
  183. /**
  184. * Tests errors generated when a field is not allowed to be empty
  185. *
  186. * @return void
  187. */
  188. public function testErrorsWithEmptyNotAllowed() {
  189. $validator = new Validator;
  190. $validator->allowEmpty('title', false);
  191. $errors = $validator->errors(['title' => '']);
  192. $expected = ['title' => ['This field cannot be left empty']];
  193. $this->assertEquals($expected, $errors);
  194. $errors = $validator->errors(['title' => []]);
  195. $expected = ['title' => ['This field cannot be left empty']];
  196. $this->assertEquals($expected, $errors);
  197. $errors = $validator->errors(['title' => null]);
  198. $expected = ['title' => ['This field cannot be left empty']];
  199. $this->assertEquals($expected, $errors);
  200. $errors = $validator->errors(['title' => 0]);
  201. $this->assertEmpty($errors);
  202. $errors = $validator->errors(['title' => '0']);
  203. $this->assertEmpty($errors);
  204. $errors = $validator->errors(['title' => false]);
  205. $this->assertEmpty($errors);
  206. }
  207. /**
  208. * Tests custom error mesages generated when a field is not allowed to be empty
  209. *
  210. * @return void
  211. */
  212. public function testCustomErrorsWithEmptyNotAllowed() {
  213. $validator = new Validator;
  214. $validator->allowEmpty('title', false, 'Custom message');
  215. $errors = $validator->errors(['title' => '']);
  216. $expected = ['title' => ['Custom message']];
  217. $this->assertEquals($expected, $errors);
  218. }
  219. /**
  220. * Tests errors generated when a field is allowed to be empty
  221. *
  222. * @return void
  223. */
  224. public function testErrorsWithEmptyAllowed() {
  225. $validator = new Validator;
  226. $validator->allowEmpty('title');
  227. $errors = $validator->errors(['title' => '']);
  228. $this->assertEmpty($errors);
  229. $errors = $validator->errors(['title' => []]);
  230. $this->assertEmpty($errors);
  231. $errors = $validator->errors(['title' => null]);
  232. $this->assertEmpty($errors);
  233. $errors = $validator->errors(['title' => 0]);
  234. $this->assertEmpty($errors);
  235. $errors = $validator->errors(['title' => '0']);
  236. $this->assertEmpty($errors);
  237. $errors = $validator->errors(['title' => false]);
  238. $this->assertEmpty($errors);
  239. }
  240. /**
  241. * Test the provider() method
  242. *
  243. * @return void
  244. */
  245. public function testProvider() {
  246. $validator = new Validator;
  247. $object = new \stdClass;
  248. $this->assertSame($validator, $validator->provider('foo', $object));
  249. $this->assertSame($object, $validator->provider('foo'));
  250. $this->assertNull($validator->provider('bar'));
  251. $another = new \stdClass;
  252. $this->assertSame($validator, $validator->provider('bar', $another));
  253. $this->assertSame($another, $validator->provider('bar'));
  254. $this->assertEquals(new \Cake\Validation\RulesProvider, $validator->provider('default'));
  255. }
  256. /**
  257. * Tests errors() method when using validators from the default provider, this proves
  258. * that it returns a default validation message and the custom one set in the rule
  259. *
  260. * @return void
  261. */
  262. public function testErrorsFromDefaultProvider() {
  263. $validator = new Validator;
  264. $validator
  265. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  266. ->add('email', 'notEmpty', ['rule' => 'notEmpty'])
  267. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  268. $errors = $validator->errors(['email' => 'not an email!']);
  269. $expected = [
  270. 'email' => [
  271. 'alpha' => 'The provided value is invalid',
  272. 'email' => 'Y u no write email?'
  273. ]
  274. ];
  275. $this->assertEquals($expected, $errors);
  276. }
  277. /**
  278. * Tests using validation methods from different providers and returning the error
  279. * as a string
  280. *
  281. * @return void
  282. */
  283. public function testErrorsFromCustomProvider() {
  284. $validator = new Validator;
  285. $validator
  286. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  287. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  288. $thing = $this->getMock('\stdClass', ['isCool']);
  289. $thing->expects($this->once())->method('isCool')
  290. ->will($this->returnCallback(function($data, $context) use ($thing) {
  291. $this->assertEquals('bar', $data);
  292. $expected = [
  293. 'default' => new \Cake\Validation\RulesProvider,
  294. 'thing' => $thing
  295. ];
  296. $expected = [
  297. 'newRecord' => true,
  298. 'providers' => $expected,
  299. 'data' => [
  300. 'email' => '!',
  301. 'title' => 'bar'
  302. ]
  303. ];
  304. $this->assertEquals($expected, $context);
  305. return "That ain't cool, yo";
  306. }));
  307. $validator->provider('thing', $thing);
  308. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  309. $expected = [
  310. 'email' => ['alpha' => 'The provided value is invalid'],
  311. 'title' => ['cool' => "That ain't cool, yo"]
  312. ];
  313. $this->assertEquals($expected, $errors);
  314. }
  315. /**
  316. * Tests that it is possible to pass extra arguments to the validation function
  317. * and it still gets the providers as last argument
  318. *
  319. * @return void
  320. */
  321. public function testMethodsWithExtraArguments() {
  322. $validator = new Validator;
  323. $validator->add('title', 'cool', [
  324. 'rule' => ['isCool', 'and', 'awesome'],
  325. 'provider' => 'thing'
  326. ]);
  327. $thing = $this->getMock('\stdClass', ['isCool']);
  328. $thing->expects($this->once())->method('isCool')
  329. ->will($this->returnCallback(function($data, $a, $b, $context) use ($thing) {
  330. $this->assertEquals('bar', $data);
  331. $this->assertEquals('and', $a);
  332. $this->assertEquals('awesome', $b);
  333. $expected = [
  334. 'default' => new \Cake\Validation\RulesProvider,
  335. 'thing' => $thing
  336. ];
  337. $expected = [
  338. 'newRecord' => true,
  339. 'providers' => $expected,
  340. 'data' => [
  341. 'email' => '!',
  342. 'title' => 'bar'
  343. ]
  344. ];
  345. $this->assertEquals($expected, $context);
  346. return "That ain't cool, yo";
  347. }));
  348. $validator->provider('thing', $thing);
  349. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  350. $expected = [
  351. 'title' => ['cool' => "That ain't cool, yo"]
  352. ];
  353. $this->assertEquals($expected, $errors);
  354. }
  355. /**
  356. * Tests that it is possible to use a closure as a rule
  357. *
  358. * @return void
  359. */
  360. public function testUsingClosureAsRule() {
  361. $validator = new Validator;
  362. $validator->add('name', 'myRule', [
  363. 'rule' => function($data, $provider) {
  364. $this->assertEquals('foo', $data);
  365. return 'You fail';
  366. }
  367. ]);
  368. $expected = ['name' => ['myRule' => 'You fail']];
  369. $this->assertEquals($expected, $validator->errors(['name' => 'foo']));
  370. }
  371. /**
  372. * Tests that setting last to a rule will stop validating the rest of the rules
  373. *
  374. * @return void
  375. */
  376. public function testErrorsWithLastRule() {
  377. $validator = new Validator;
  378. $validator
  379. ->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
  380. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  381. $errors = $validator->errors(['email' => 'not an email!']);
  382. $expected = [
  383. 'email' => [
  384. 'alpha' => 'The provided value is invalid'
  385. ]
  386. ];
  387. $this->assertEquals($expected, $errors);
  388. }
  389. /**
  390. * Tests it is possible to get validation sets for a field using an array interface
  391. *
  392. * @return void
  393. */
  394. public function testArrayAccessGet() {
  395. $validator = new Validator;
  396. $validator
  397. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  398. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  399. $this->assertSame($validator['email'], $validator->field('email'));
  400. $this->assertSame($validator['title'], $validator->field('title'));
  401. }
  402. /**
  403. * Tests it is possible to check for validation sets for a field using an array inteface
  404. *
  405. * @return void
  406. */
  407. public function testArrayAccessExists() {
  408. $validator = new Validator;
  409. $validator
  410. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  411. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  412. $this->assertTrue(isset($validator['email']));
  413. $this->assertTrue(isset($validator['title']));
  414. $this->assertFalse(isset($validator['foo']));
  415. }
  416. /**
  417. * Tests it is possible to set validation rules for a field using an array inteface
  418. *
  419. * @return void
  420. */
  421. public function testArrayAccessSet() {
  422. $validator = new Validator;
  423. $validator
  424. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  425. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  426. $validator['name'] = $validator->field('title');
  427. $this->assertSame($validator->field('title'), $validator->field('name'));
  428. $validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
  429. $this->assertEquals($validator->field('email'), $validator->field('email'));
  430. }
  431. /**
  432. * Tests it is possible to unset validation rules
  433. *
  434. * @return void
  435. */
  436. public function testArrayAccessUset() {
  437. $validator = new Validator;
  438. $validator
  439. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  440. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  441. $this->assertTrue(isset($validator['title']));
  442. unset($validator['title']);
  443. $this->assertFalse(isset($validator['title']));
  444. }
  445. /**
  446. * Tests the countable interface
  447. *
  448. * @return void
  449. */
  450. public function testCount() {
  451. $validator = new Validator;
  452. $validator
  453. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  454. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  455. $this->assertCount(2, $validator);
  456. }
  457. /**
  458. * Tests adding rules via alternative syntax
  459. *
  460. * @return void
  461. */
  462. public function testAddMulitple() {
  463. $validator = new Validator;
  464. $validator->add('title', [
  465. 'notEmpty' => [
  466. 'rule' => 'notEmpty'
  467. ],
  468. 'length' => [
  469. 'rule' => ['minLength', 10],
  470. 'message' => 'Titles need to be at least 10 characters long'
  471. ]
  472. ]);
  473. $set = $validator->field('title');
  474. $this->assertInstanceOf('\Cake\Validation\ValidationSet', $set);
  475. $this->assertCount(2, $set);
  476. }
  477. }