ValidatorTest.php 19 KB

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