ValidatorTest.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  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. /**
  26. * Testing you can dynamically add rules to a field
  27. *
  28. * @return void
  29. */
  30. public function testAddingRulesToField()
  31. {
  32. $validator = new Validator;
  33. $validator->add('title', 'not-blank', ['rule' => 'notBlank']);
  34. $set = $validator->field('title');
  35. $this->assertInstanceOf('Cake\Validation\ValidationSet', $set);
  36. $this->assertCount(1, $set);
  37. $validator->add('title', 'another', ['rule' => 'alphanumeric']);
  38. $this->assertCount(2, $set);
  39. $validator->add('body', 'another', ['rule' => 'crazy']);
  40. $this->assertCount(1, $validator->field('body'));
  41. $this->assertCount(2, $validator);
  42. }
  43. /**
  44. * Testing addNested field rules
  45. *
  46. * @return void
  47. */
  48. public function testAddNestedSingle()
  49. {
  50. $validator = new Validator();
  51. $inner = new Validator();
  52. $inner->add('username', 'not-blank', ['rule' => 'notBlank']);
  53. $this->assertSame($validator, $validator->addNested('user', $inner));
  54. $this->assertCount(1, $validator->field('user'));
  55. }
  56. /**
  57. * Testing addNested connects providers
  58. *
  59. * @return void
  60. */
  61. public function testAddNestedSingleProviders()
  62. {
  63. $validator = new Validator();
  64. $validator->provider('test', $this);
  65. $inner = new Validator();
  66. $inner->add('username', 'not-blank', ['rule' => function () use ($inner, $validator) {
  67. $this->assertSame($validator->providers(), $inner->providers(), 'Providers should match');
  68. return false;
  69. }]);
  70. $validator->addNested('user', $inner);
  71. $result = $validator->errors(['user' => ['username' => 'example']]);
  72. $this->assertNotEmpty($result, 'Validation should fail');
  73. }
  74. /**
  75. * Testing addNestedMany field rules
  76. *
  77. * @return void
  78. */
  79. public function testAddNestedMany()
  80. {
  81. $validator = new Validator();
  82. $inner = new Validator();
  83. $inner->add('comment', 'not-blank', ['rule' => 'notBlank']);
  84. $this->assertSame($validator, $validator->addNestedMany('comments', $inner));
  85. $this->assertCount(1, $validator->field('comments'));
  86. }
  87. /**
  88. * Testing addNestedMany connects providers
  89. *
  90. * @return void
  91. */
  92. public function testAddNestedManyProviders()
  93. {
  94. $validator = new Validator();
  95. $validator->provider('test', $this);
  96. $inner = new Validator();
  97. $inner->add('comment', 'not-blank', ['rule' => function () use ($inner, $validator) {
  98. $this->assertSame($validator->providers(), $inner->providers(), 'Providers should match');
  99. return false;
  100. }]);
  101. $validator->addNestedMany('comments', $inner);
  102. $result = $validator->errors(['comments' => [['comment' => 'example']]]);
  103. $this->assertNotEmpty($result, 'Validation should fail');
  104. }
  105. /**
  106. * Tests that calling field will create a default validation set for it
  107. *
  108. * @return void
  109. */
  110. public function testFieldDefault()
  111. {
  112. $validator = new Validator;
  113. $this->assertFalse($validator->hasField('foo'));
  114. $field = $validator->field('foo');
  115. $this->assertInstanceOf('Cake\Validation\ValidationSet', $field);
  116. $this->assertCount(0, $field);
  117. $this->assertTrue($validator->hasField('foo'));
  118. }
  119. /**
  120. * Tests that field method can be used as a setter
  121. *
  122. * @return void
  123. */
  124. public function testFieldSetter()
  125. {
  126. $validator = new Validator;
  127. $validationSet = new ValidationSet;
  128. $validator->field('thing', $validationSet);
  129. $this->assertSame($validationSet, $validator->field('thing'));
  130. }
  131. /**
  132. * Tests the remove method
  133. *
  134. * @return void
  135. */
  136. public function testRemove()
  137. {
  138. $validator = new Validator;
  139. $validator->add('title', 'not-blank', ['rule' => 'notBlank']);
  140. $validator->add('title', 'foo', ['rule' => 'bar']);
  141. $this->assertCount(2, $validator->field('title'));
  142. $validator->remove('title');
  143. $this->assertCount(0, $validator->field('title'));
  144. $validator->remove('title');
  145. $validator->add('title', 'not-blank', ['rule' => 'notBlank']);
  146. $validator->add('title', 'foo', ['rule' => 'bar']);
  147. $this->assertCount(2, $validator->field('title'));
  148. $validator->remove('title', 'foo');
  149. $this->assertCount(1, $validator->field('title'));
  150. $this->assertNull($validator->field('title')->rule('foo'));
  151. }
  152. /**
  153. * Tests the requirePresence method
  154. *
  155. * @return void
  156. */
  157. public function testRequirePresence()
  158. {
  159. $validator = new Validator;
  160. $this->assertSame($validator, $validator->requirePresence('title'));
  161. $this->assertTrue($validator->field('title')->isPresenceRequired());
  162. $validator->requirePresence('title', false);
  163. $this->assertFalse($validator->field('title')->isPresenceRequired());
  164. $validator->requirePresence('title', 'create');
  165. $this->assertEquals('create', $validator->field('title')->isPresenceRequired());
  166. $validator->requirePresence('title', 'update');
  167. $this->assertEquals('update', $validator->field('title')->isPresenceRequired());
  168. }
  169. /**
  170. * Tests the requirePresence method
  171. *
  172. * @return void
  173. */
  174. public function testRequirePresenceAsArray()
  175. {
  176. $validator = new Validator;
  177. $validator->requirePresence(['title', 'created']);
  178. $this->assertTrue($validator->field('title')->isPresenceRequired());
  179. $this->assertTrue($validator->field('created')->isPresenceRequired());
  180. $validator->requirePresence([
  181. 'title' => [
  182. 'mode' => false
  183. ],
  184. 'content' => [
  185. 'mode' => 'update'
  186. ],
  187. 'subject'
  188. ], true);
  189. $this->assertFalse($validator->field('title')->isPresenceRequired());
  190. $this->assertEquals('update', $validator->field('content')->isPresenceRequired());
  191. $this->assertTrue($validator->field('subject')->isPresenceRequired());
  192. }
  193. /**
  194. * Tests the requirePresence failure case
  195. *
  196. * @expectedException InvalidArgumentException
  197. * @return void
  198. */
  199. public function testRequirePresenceAsArrayFailure()
  200. {
  201. $validator = new Validator();
  202. $validator->requirePresence(['title' => 'derp', 'created' => false]);
  203. }
  204. /**
  205. * Tests the requirePresence method when passing a callback
  206. *
  207. * @return void
  208. */
  209. public function testRequirePresenceCallback()
  210. {
  211. $validator = new Validator;
  212. $require = true;
  213. $validator->requirePresence('title', function ($context) use (&$require) {
  214. $this->assertEquals([], $context['data']);
  215. $this->assertEquals([], $context['providers']);
  216. $this->assertEquals('title', $context['field']);
  217. $this->assertTrue($context['newRecord']);
  218. return $require;
  219. });
  220. $this->assertTrue($validator->isPresenceRequired('title', true));
  221. $require = false;
  222. $this->assertFalse($validator->isPresenceRequired('title', true));
  223. }
  224. /**
  225. * Tests the isPresenceRequired method
  226. *
  227. * @return void
  228. */
  229. public function testIsPresenceRequired()
  230. {
  231. $validator = new Validator;
  232. $this->assertSame($validator, $validator->requirePresence('title'));
  233. $this->assertTrue($validator->isPresenceRequired('title', true));
  234. $this->assertTrue($validator->isPresenceRequired('title', false));
  235. $validator->requirePresence('title', false);
  236. $this->assertFalse($validator->isPresenceRequired('title', true));
  237. $this->assertFalse($validator->isPresenceRequired('title', false));
  238. $validator->requirePresence('title', 'create');
  239. $this->assertTrue($validator->isPresenceRequired('title', true));
  240. $this->assertFalse($validator->isPresenceRequired('title', false));
  241. $validator->requirePresence('title', 'update');
  242. $this->assertTrue($validator->isPresenceRequired('title', false));
  243. $this->assertFalse($validator->isPresenceRequired('title', true));
  244. }
  245. /**
  246. * Tests errors generated when a field presence is required
  247. *
  248. * @return void
  249. */
  250. public function testErrorsWithPresenceRequired()
  251. {
  252. $validator = new Validator;
  253. $validator->requirePresence('title');
  254. $errors = $validator->errors(['foo' => 'something']);
  255. $expected = ['title' => ['_required' => 'This field is required']];
  256. $this->assertEquals($expected, $errors);
  257. $this->assertEmpty($validator->errors(['title' => 'bar']));
  258. $validator->requirePresence('title', false);
  259. $this->assertEmpty($validator->errors(['foo' => 'bar']));
  260. }
  261. /**
  262. * Test that errors() can work with nested data.
  263. *
  264. * @return void
  265. */
  266. public function testErrorsWithNestedFields()
  267. {
  268. $validator = new Validator();
  269. $user = new Validator();
  270. $user->add('username', 'letter', ['rule' => 'alphanumeric']);
  271. $comments = new Validator();
  272. $comments->add('comment', 'letter', ['rule' => 'alphanumeric']);
  273. $validator->addNested('user', $user);
  274. $validator->addNestedMany('comments', $comments);
  275. $data = [
  276. 'user' => [
  277. 'username' => 'is wrong'
  278. ],
  279. 'comments' => [
  280. ['comment' => 'is wrong']
  281. ]
  282. ];
  283. $errors = $validator->errors($data);
  284. $expected = [
  285. 'user' => [
  286. 'username' => ['letter' => 'The provided value is invalid']
  287. ],
  288. 'comments' => [
  289. 0 => ['comment' => ['letter' => 'The provided value is invalid']]
  290. ]
  291. ];
  292. $this->assertEquals($expected, $errors);
  293. }
  294. /**
  295. * Test nested fields with many, but invalid data.
  296. *
  297. * @return void
  298. */
  299. public function testErrorsWithNestedSingleInvalidType()
  300. {
  301. $validator = new Validator();
  302. $user = new Validator();
  303. $user->add('user', 'letter', ['rule' => 'alphanumeric']);
  304. $validator->addNested('user', $user);
  305. $data = [
  306. 'user' => 'a string',
  307. ];
  308. $errors = $validator->errors($data);
  309. $expected = [
  310. 'user' => ['_nested' => 'The provided value is invalid'],
  311. ];
  312. $this->assertEquals($expected, $errors);
  313. }
  314. /**
  315. * Test nested fields with many, but invalid data.
  316. *
  317. * @return void
  318. */
  319. public function testErrorsWithNestedManyInvalidType()
  320. {
  321. $validator = new Validator();
  322. $comments = new Validator();
  323. $comments->add('comment', 'letter', ['rule' => 'alphanumeric']);
  324. $validator->addNestedMany('comments', $comments);
  325. $data = [
  326. 'comments' => 'a string',
  327. ];
  328. $errors = $validator->errors($data);
  329. $expected = [
  330. 'comments' => ['_nested' => 'The provided value is invalid'],
  331. ];
  332. $this->assertEquals($expected, $errors);
  333. }
  334. /**
  335. * Test nested fields with many, but invalid data.
  336. *
  337. * @return void
  338. */
  339. public function testErrorsWithNestedManySomeInvalid()
  340. {
  341. $validator = new Validator();
  342. $comments = new Validator();
  343. $comments->add('comment', 'letter', ['rule' => 'alphanumeric']);
  344. $validator->addNestedMany('comments', $comments);
  345. $data = [
  346. 'comments' => [
  347. 'a string',
  348. ['comment' => 'letters'],
  349. ['comment' => 'more invalid']
  350. ]
  351. ];
  352. $errors = $validator->errors($data);
  353. $expected = [
  354. 'comments' => [
  355. '_nested' => 'The provided value is invalid',
  356. ],
  357. ];
  358. $this->assertEquals($expected, $errors);
  359. }
  360. /**
  361. * Tests custom error messages generated when a field presence is required
  362. *
  363. * @return void
  364. */
  365. public function testCustomErrorsWithPresenceRequired()
  366. {
  367. $validator = new Validator;
  368. $validator->requirePresence('title', true, 'Custom message');
  369. $errors = $validator->errors(['foo' => 'something']);
  370. $expected = ['title' => ['_required' => 'Custom message']];
  371. $this->assertEquals($expected, $errors);
  372. }
  373. /**
  374. * Tests custom error messages generated when a field presence is required
  375. *
  376. * @return void
  377. */
  378. public function testCustomErrorsWithPresenceRequiredAsArray()
  379. {
  380. $validator = new Validator;
  381. $validator->requirePresence(['title', 'content'], true, 'Custom message');
  382. $errors = $validator->errors(['foo' => 'something']);
  383. $expected = [
  384. 'title' => ['_required' => 'Custom message'],
  385. 'content' => ['_required' => 'Custom message']
  386. ];
  387. $this->assertEquals($expected, $errors);
  388. $validator->requirePresence([
  389. 'title' => [
  390. 'message' => 'Test message'
  391. ],
  392. 'content'
  393. ], true, 'Custom message');
  394. $errors = $validator->errors(['foo' => 'something']);
  395. $expected = [
  396. 'title' => ['_required' => 'Test message'],
  397. 'content' => ['_required' => 'Custom message']
  398. ];
  399. $this->assertEquals($expected, $errors);
  400. }
  401. /**
  402. * Tests the allowEmpty method
  403. *
  404. * @return void
  405. */
  406. public function testAllowEmpty()
  407. {
  408. $validator = new Validator;
  409. $this->assertSame($validator, $validator->allowEmpty('title'));
  410. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  411. $validator->allowEmpty('title', 'create');
  412. $this->assertEquals('create', $validator->field('title')->isEmptyAllowed());
  413. $validator->allowEmpty('title', 'update');
  414. $this->assertEquals('update', $validator->field('title')->isEmptyAllowed());
  415. }
  416. /**
  417. * Tests the allowEmpty method with date/time fields.
  418. *
  419. * @return void
  420. */
  421. public function testAllowEmptyDateTime()
  422. {
  423. $validator = new Validator;
  424. $validator->allowEmpty('created')
  425. ->add('created', 'date', ['rule' => 'date']);
  426. $data = [
  427. 'created' => [
  428. 'year' => '',
  429. 'month' => '',
  430. 'day' => ''
  431. ]
  432. ];
  433. $result = $validator->errors($data);
  434. $this->assertEmpty($result, 'No errors on empty date');
  435. $data = [
  436. 'created' => [
  437. 'year' => '',
  438. 'month' => '',
  439. 'day' => '',
  440. 'hour' => '',
  441. 'minute' => '',
  442. 'second' => '',
  443. 'meridian' => '',
  444. ]
  445. ];
  446. $result = $validator->errors($data);
  447. $this->assertEmpty($result, 'No errors on empty datetime');
  448. $data = [
  449. 'created' => [
  450. 'hour' => '',
  451. 'minute' => '',
  452. 'meridian' => '',
  453. ]
  454. ];
  455. $result = $validator->errors($data);
  456. $this->assertEmpty($result, 'No errors on empty time');
  457. }
  458. /**
  459. * Tests the allowEmpty method with file fields.
  460. *
  461. * @return void
  462. */
  463. public function testAllowEmptyFileFields()
  464. {
  465. $validator = new Validator;
  466. $validator->allowEmpty('picture')
  467. ->add('picture', 'file', ['rule' => 'uploadedFile']);
  468. $data = [
  469. 'picture' => [
  470. 'name' => '',
  471. 'type' => '',
  472. 'tmp_name' => '',
  473. 'error' => UPLOAD_ERR_NO_FILE,
  474. ]
  475. ];
  476. $result = $validator->errors($data);
  477. $this->assertEmpty($result, 'No errors on empty date');
  478. $data = [
  479. 'picture' => [
  480. 'name' => 'fake.png',
  481. 'type' => '',
  482. 'tmp_name' => '',
  483. 'error' => UPLOAD_ERR_OK,
  484. ]
  485. ];
  486. $result = $validator->errors($data);
  487. $this->assertNotEmpty($result, 'Invalid file should be caught still.');
  488. }
  489. /**
  490. * Test the notEmpty() method.
  491. *
  492. * @return void
  493. */
  494. public function testNotEmpty()
  495. {
  496. $validator = new Validator;
  497. $validator->notEmpty('title');
  498. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  499. $validator->allowEmpty('title');
  500. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  501. }
  502. /**
  503. * Test the notEmpty() method.
  504. *
  505. * @return void
  506. */
  507. public function testNotEmptyModes()
  508. {
  509. $validator = new Validator;
  510. $validator->notEmpty('title', 'Need a title', 'create');
  511. $this->assertFalse($validator->isEmptyAllowed('title', true));
  512. $this->assertTrue($validator->isEmptyAllowed('title', false));
  513. $validator->notEmpty('title', 'Need a title', 'update');
  514. $this->assertTrue($validator->isEmptyAllowed('title', true));
  515. $this->assertFalse($validator->isEmptyAllowed('title', false));
  516. $validator->notEmpty('title', 'Need a title');
  517. $this->assertFalse($validator->isEmptyAllowed('title', true));
  518. $this->assertFalse($validator->isEmptyAllowed('title', false));
  519. $validator->notEmpty('title');
  520. $this->assertFalse($validator->isEmptyAllowed('title', true));
  521. $this->assertFalse($validator->isEmptyAllowed('title', false));
  522. }
  523. /**
  524. * Test interactions between notEmpty() and isAllowed().
  525. *
  526. * @return void
  527. */
  528. public function testNotEmptyAndIsAllowed()
  529. {
  530. $validator = new Validator;
  531. $validator->allowEmpty('title')
  532. ->notEmpty('title', 'Need it', 'update');
  533. $this->assertTrue($validator->isEmptyAllowed('title', true));
  534. $this->assertFalse($validator->isEmptyAllowed('title', false));
  535. $validator->allowEmpty('title')
  536. ->notEmpty('title');
  537. $this->assertFalse($validator->isEmptyAllowed('title', true));
  538. $this->assertFalse($validator->isEmptyAllowed('title', false));
  539. $validator->notEmpty('title')
  540. ->allowEmpty('title', 'create');
  541. $this->assertTrue($validator->isEmptyAllowed('title', true));
  542. $this->assertFalse($validator->isEmptyAllowed('title', false));
  543. }
  544. /**
  545. * Tests the allowEmpty method when passing a callback
  546. *
  547. * @return void
  548. */
  549. public function testAllowEmptyCallback()
  550. {
  551. $validator = new Validator;
  552. $allow = true;
  553. $validator->allowEmpty('title', function ($context) use (&$allow) {
  554. $this->assertEquals([], $context['data']);
  555. $this->assertEquals([], $context['providers']);
  556. $this->assertTrue($context['newRecord']);
  557. return $allow;
  558. });
  559. $this->assertTrue($validator->isEmptyAllowed('title', true));
  560. $allow = false;
  561. $this->assertFalse($validator->isEmptyAllowed('title', true));
  562. }
  563. /**
  564. * Tests the notEmpty method when passing a callback
  565. *
  566. * @return void
  567. */
  568. public function testNotEmptyCallback()
  569. {
  570. $validator = new Validator;
  571. $prevent = true;
  572. $validator->notEmpty('title', 'error message', function ($context) use (&$prevent) {
  573. $this->assertEquals([], $context['data']);
  574. $this->assertEquals([], $context['providers']);
  575. $this->assertFalse($context['newRecord']);
  576. return $prevent;
  577. });
  578. $this->assertFalse($validator->isEmptyAllowed('title', false));
  579. $prevent = false;
  580. $this->assertTrue($validator->isEmptyAllowed('title', false));
  581. }
  582. /**
  583. * Tests the isEmptyAllowed method
  584. *
  585. * @return void
  586. */
  587. public function testIsEmptyAllowed()
  588. {
  589. $validator = new Validator;
  590. $this->assertSame($validator, $validator->allowEmpty('title'));
  591. $this->assertTrue($validator->isEmptyAllowed('title', true));
  592. $this->assertTrue($validator->isEmptyAllowed('title', false));
  593. $validator->notEmpty('title');
  594. $this->assertFalse($validator->isEmptyAllowed('title', true));
  595. $this->assertFalse($validator->isEmptyAllowed('title', false));
  596. $validator->allowEmpty('title', 'create');
  597. $this->assertTrue($validator->isEmptyAllowed('title', true));
  598. $this->assertFalse($validator->isEmptyAllowed('title', false));
  599. $validator->allowEmpty('title', 'update');
  600. $this->assertTrue($validator->isEmptyAllowed('title', false));
  601. $this->assertFalse($validator->isEmptyAllowed('title', true));
  602. }
  603. /**
  604. * Tests errors generated when a field is not allowed to be empty
  605. *
  606. * @return void
  607. */
  608. public function testErrorsWithEmptyNotAllowed()
  609. {
  610. $validator = new Validator;
  611. $validator->notEmpty('title');
  612. $errors = $validator->errors(['title' => '']);
  613. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  614. $this->assertEquals($expected, $errors);
  615. $errors = $validator->errors(['title' => []]);
  616. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  617. $this->assertEquals($expected, $errors);
  618. $errors = $validator->errors(['title' => null]);
  619. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  620. $this->assertEquals($expected, $errors);
  621. $errors = $validator->errors(['title' => 0]);
  622. $this->assertEmpty($errors);
  623. $errors = $validator->errors(['title' => '0']);
  624. $this->assertEmpty($errors);
  625. $errors = $validator->errors(['title' => false]);
  626. $this->assertEmpty($errors);
  627. }
  628. /**
  629. * Tests custom error mesages generated when a field is not allowed to be empty
  630. *
  631. * @return void
  632. */
  633. public function testCustomErrorsWithEmptyNotAllowed()
  634. {
  635. $validator = new Validator;
  636. $validator->notEmpty('title', 'Custom message');
  637. $errors = $validator->errors(['title' => '']);
  638. $expected = ['title' => ['_empty' => 'Custom message']];
  639. $this->assertEquals($expected, $errors);
  640. }
  641. /**
  642. * Tests errors generated when a field is allowed to be empty
  643. *
  644. * @return void
  645. */
  646. public function testErrorsWithEmptyAllowed()
  647. {
  648. $validator = new Validator;
  649. $validator->allowEmpty('title');
  650. $errors = $validator->errors(['title' => '']);
  651. $this->assertEmpty($errors);
  652. $errors = $validator->errors(['title' => []]);
  653. $this->assertEmpty($errors);
  654. $errors = $validator->errors(['title' => null]);
  655. $this->assertEmpty($errors);
  656. $errors = $validator->errors(['title' => 0]);
  657. $this->assertEmpty($errors);
  658. $errors = $validator->errors(['title' => 0.0]);
  659. $this->assertEmpty($errors);
  660. $errors = $validator->errors(['title' => '0']);
  661. $this->assertEmpty($errors);
  662. $errors = $validator->errors(['title' => false]);
  663. $this->assertEmpty($errors);
  664. }
  665. /**
  666. * Test the provider() method
  667. *
  668. * @return void
  669. */
  670. public function testProvider()
  671. {
  672. $validator = new Validator;
  673. $object = new \stdClass;
  674. $this->assertSame($validator, $validator->provider('foo', $object));
  675. $this->assertSame($object, $validator->provider('foo'));
  676. $this->assertNull($validator->provider('bar'));
  677. $another = new \stdClass;
  678. $this->assertSame($validator, $validator->provider('bar', $another));
  679. $this->assertSame($another, $validator->provider('bar'));
  680. $this->assertEquals(new \Cake\Validation\RulesProvider, $validator->provider('default'));
  681. }
  682. /**
  683. * Tests errors() method when using validators from the default provider, this proves
  684. * that it returns a default validation message and the custom one set in the rule
  685. *
  686. * @return void
  687. */
  688. public function testErrorsFromDefaultProvider()
  689. {
  690. $validator = new Validator;
  691. $validator
  692. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  693. ->add('email', 'notBlank', ['rule' => 'notBlank'])
  694. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  695. $errors = $validator->errors(['email' => 'not an email!']);
  696. $expected = [
  697. 'email' => [
  698. 'alpha' => 'The provided value is invalid',
  699. 'email' => 'Y u no write email?'
  700. ]
  701. ];
  702. $this->assertEquals($expected, $errors);
  703. }
  704. /**
  705. * Tests using validation methods from different providers and returning the error
  706. * as a string
  707. *
  708. * @return void
  709. */
  710. public function testErrorsFromCustomProvider()
  711. {
  712. $validator = new Validator;
  713. $validator
  714. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  715. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  716. $thing = $this->getMock('\stdClass', ['isCool']);
  717. $thing->expects($this->once())->method('isCool')
  718. ->will($this->returnCallback(function ($data, $context) use ($thing) {
  719. $this->assertEquals('bar', $data);
  720. $expected = [
  721. 'default' => new \Cake\Validation\RulesProvider,
  722. 'thing' => $thing
  723. ];
  724. $expected = [
  725. 'newRecord' => true,
  726. 'providers' => $expected,
  727. 'data' => [
  728. 'email' => '!',
  729. 'title' => 'bar'
  730. ],
  731. 'field' => 'title'
  732. ];
  733. $this->assertEquals($expected, $context);
  734. return "That ain't cool, yo";
  735. }));
  736. $validator->provider('thing', $thing);
  737. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  738. $expected = [
  739. 'email' => ['alpha' => 'The provided value is invalid'],
  740. 'title' => ['cool' => "That ain't cool, yo"]
  741. ];
  742. $this->assertEquals($expected, $errors);
  743. }
  744. /**
  745. * Tests that it is possible to pass extra arguments to the validation function
  746. * and it still gets the providers as last argument
  747. *
  748. * @return void
  749. */
  750. public function testMethodsWithExtraArguments()
  751. {
  752. $validator = new Validator;
  753. $validator->add('title', 'cool', [
  754. 'rule' => ['isCool', 'and', 'awesome'],
  755. 'provider' => 'thing'
  756. ]);
  757. $thing = $this->getMock('\stdClass', ['isCool']);
  758. $thing->expects($this->once())->method('isCool')
  759. ->will($this->returnCallback(function ($data, $a, $b, $context) use ($thing) {
  760. $this->assertEquals('bar', $data);
  761. $this->assertEquals('and', $a);
  762. $this->assertEquals('awesome', $b);
  763. $expected = [
  764. 'default' => new \Cake\Validation\RulesProvider,
  765. 'thing' => $thing
  766. ];
  767. $expected = [
  768. 'newRecord' => true,
  769. 'providers' => $expected,
  770. 'data' => [
  771. 'email' => '!',
  772. 'title' => 'bar'
  773. ],
  774. 'field' => 'title'
  775. ];
  776. $this->assertEquals($expected, $context);
  777. return "That ain't cool, yo";
  778. }));
  779. $validator->provider('thing', $thing);
  780. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  781. $expected = [
  782. 'title' => ['cool' => "That ain't cool, yo"]
  783. ];
  784. $this->assertEquals($expected, $errors);
  785. }
  786. /**
  787. * Tests that it is possible to use a closure as a rule
  788. *
  789. * @return void
  790. */
  791. public function testUsingClosureAsRule()
  792. {
  793. $validator = new Validator;
  794. $validator->add('name', 'myRule', [
  795. 'rule' => function ($data, $provider) {
  796. $this->assertEquals('foo', $data);
  797. return 'You fail';
  798. }
  799. ]);
  800. $expected = ['name' => ['myRule' => 'You fail']];
  801. $this->assertEquals($expected, $validator->errors(['name' => 'foo']));
  802. }
  803. /**
  804. * Tests that setting last to a rule will stop validating the rest of the rules
  805. *
  806. * @return void
  807. */
  808. public function testErrorsWithLastRule()
  809. {
  810. $validator = new Validator;
  811. $validator
  812. ->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
  813. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  814. $errors = $validator->errors(['email' => 'not an email!']);
  815. $expected = [
  816. 'email' => [
  817. 'alpha' => 'The provided value is invalid'
  818. ]
  819. ];
  820. $this->assertEquals($expected, $errors);
  821. }
  822. /**
  823. * Tests it is possible to get validation sets for a field using an array interface
  824. *
  825. * @return void
  826. */
  827. public function testArrayAccessGet()
  828. {
  829. $validator = new Validator;
  830. $validator
  831. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  832. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  833. $this->assertSame($validator['email'], $validator->field('email'));
  834. $this->assertSame($validator['title'], $validator->field('title'));
  835. }
  836. /**
  837. * Tests it is possible to check for validation sets for a field using an array inteface
  838. *
  839. * @return void
  840. */
  841. public function testArrayAccessExists()
  842. {
  843. $validator = new Validator;
  844. $validator
  845. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  846. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  847. $this->assertTrue(isset($validator['email']));
  848. $this->assertTrue(isset($validator['title']));
  849. $this->assertFalse(isset($validator['foo']));
  850. }
  851. /**
  852. * Tests it is possible to set validation rules for a field using an array inteface
  853. *
  854. * @return void
  855. */
  856. public function testArrayAccessSet()
  857. {
  858. $validator = new Validator;
  859. $validator
  860. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  861. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  862. $validator['name'] = $validator->field('title');
  863. $this->assertSame($validator->field('title'), $validator->field('name'));
  864. $validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
  865. $this->assertEquals($validator->field('email'), $validator->field('email'));
  866. }
  867. /**
  868. * Tests it is possible to unset validation rules
  869. *
  870. * @return void
  871. */
  872. public function testArrayAccessUset()
  873. {
  874. $validator = new Validator;
  875. $validator
  876. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  877. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  878. $this->assertTrue(isset($validator['title']));
  879. unset($validator['title']);
  880. $this->assertFalse(isset($validator['title']));
  881. }
  882. /**
  883. * Tests the countable interface
  884. *
  885. * @return void
  886. */
  887. public function testCount()
  888. {
  889. $validator = new Validator;
  890. $validator
  891. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  892. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  893. $this->assertCount(2, $validator);
  894. }
  895. /**
  896. * Tests adding rules via alternative syntax
  897. *
  898. * @return void
  899. */
  900. public function testAddMulitple()
  901. {
  902. $validator = new Validator;
  903. $validator->add('title', [
  904. 'notBlank' => [
  905. 'rule' => 'notBlank'
  906. ],
  907. 'length' => [
  908. 'rule' => ['minLength', 10],
  909. 'message' => 'Titles need to be at least 10 characters long'
  910. ]
  911. ]);
  912. $set = $validator->field('title');
  913. $this->assertInstanceOf('Cake\Validation\ValidationSet', $set);
  914. $this->assertCount(2, $set);
  915. }
  916. /**
  917. * Integration test for compareWith validator.
  918. *
  919. * @return void
  920. */
  921. public function testCompareWithIntegration()
  922. {
  923. $validator = new Validator;
  924. $validator->add('password', [
  925. 'compare' => [
  926. 'rule' => ['compareWith', 'password_compare']
  927. ],
  928. ]);
  929. $data = [
  930. 'password' => 'test',
  931. 'password_compare' => 'not the same'
  932. ];
  933. $this->assertNotEmpty($validator->errors($data), 'Validation should fail.');
  934. }
  935. /**
  936. * Test debugInfo helper method.
  937. *
  938. * @return void
  939. */
  940. public function testDebugInfo()
  941. {
  942. $validator = new Validator();
  943. $validator->provider('test', $this);
  944. $validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
  945. $validator->requirePresence('body');
  946. $validator->allowEmpty('published');
  947. $result = $validator->__debugInfo();
  948. $expected = [
  949. '_providers' => ['test'],
  950. '_fields' => [
  951. 'title' => [
  952. 'isPresenceRequired' => false,
  953. 'isEmptyAllowed' => false,
  954. 'rules' => ['not-empty'],
  955. ],
  956. 'body' => [
  957. 'isPresenceRequired' => true,
  958. 'isEmptyAllowed' => false,
  959. 'rules' => [],
  960. ],
  961. 'published' => [
  962. 'isPresenceRequired' => false,
  963. 'isEmptyAllowed' => true,
  964. 'rules' => [],
  965. ],
  966. ],
  967. '_presenceMessages' => [],
  968. '_allowEmptyMessages' => [],
  969. '_useI18n' => true,
  970. ];
  971. $this->assertEquals($expected, $result);
  972. }
  973. /**
  974. * Tests that the 'create' and 'update' modes are preserved when using
  975. * nested validators
  976. *
  977. * @return void
  978. */
  979. public function testNestedValidatorCreate()
  980. {
  981. $validator = new Validator();
  982. $inner = new Validator();
  983. $inner->add('username', 'email', ['rule' => 'email', 'on' => 'create']);
  984. $validator->addNested('user', $inner);
  985. $this->assertNotEmpty($validator->errors(['user' => ['username' => 'example']], true));
  986. $this->assertEmpty($validator->errors(['user' => ['username' => 'a']], false));
  987. }
  988. /**
  989. * Tests that the 'create' and 'update' modes are preserved when using
  990. * nested validators
  991. *
  992. * @return void
  993. */
  994. public function testNestedManyValidatorCreate()
  995. {
  996. $validator = new Validator();
  997. $inner = new Validator();
  998. $inner->add('username', 'email', ['rule' => 'email', 'on' => 'create']);
  999. $validator->addNestedMany('user', $inner);
  1000. $this->assertNotEmpty($validator->errors(['user' => [['username' => 'example']]], true));
  1001. $this->assertEmpty($validator->errors(['user' => [['username' => 'a']]], false));
  1002. }
  1003. /**
  1004. * Tests the notBlank proxy method
  1005. *
  1006. * @return void
  1007. */
  1008. public function testNotBlank()
  1009. {
  1010. $validator = new Validator();
  1011. $this->assertProxyMethod($validator, 'notBlank');
  1012. $this->assertNotEmpty($validator->errors(['username' => ' ']));
  1013. }
  1014. /**
  1015. * Tests the alphanumeric proxy method
  1016. *
  1017. * @return void
  1018. */
  1019. public function testAlphanumeric()
  1020. {
  1021. $validator = new Validator();
  1022. $this->assertProxyMethod($validator, 'alphaNumeric');
  1023. $this->assertNotEmpty($validator->errors(['username' => '$']));
  1024. }
  1025. /**
  1026. * Tests the lengthBetween proxy method
  1027. *
  1028. * @return void
  1029. */
  1030. public function testLengthBetween()
  1031. {
  1032. $validator = new Validator();
  1033. $this->assertProxyMethod($validator, 'lengthBetween', [5, 7], [5, 7]);
  1034. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1035. }
  1036. /**
  1037. * Tests the lengthBetween proxy method
  1038. *
  1039. * @expectedException InvalidArgumentException
  1040. * @return void
  1041. */
  1042. public function testLengthBetweenFailure()
  1043. {
  1044. $validator = new Validator();
  1045. $validator->lengthBetween('username', [7]);
  1046. }
  1047. /**
  1048. * Tests the creditCard proxy method
  1049. *
  1050. * @return void
  1051. */
  1052. public function testCreditCard()
  1053. {
  1054. $validator = new Validator();
  1055. $this->assertProxyMethod($validator, 'creditCard', 'all', ['all', true], 'cc');
  1056. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1057. }
  1058. /**
  1059. * Tests the greaterThan proxy method
  1060. *
  1061. * @return void
  1062. */
  1063. public function testGreaterThan()
  1064. {
  1065. $validator = new Validator();
  1066. $this->assertProxyMethod($validator, 'greaterThan', 5, ['>', 5], 'comparison');
  1067. $this->assertNotEmpty($validator->errors(['username' => 2]));
  1068. }
  1069. /**
  1070. * Tests the greaterThanOrEqual proxy method
  1071. *
  1072. * @return void
  1073. */
  1074. public function testGreaterThanOrEqual()
  1075. {
  1076. $validator = new Validator();
  1077. $this->assertProxyMethod($validator, 'greaterThanOrEqual', 5, ['>=', 5], 'comparison');
  1078. $this->assertNotEmpty($validator->errors(['username' => 2]));
  1079. }
  1080. /**
  1081. * Tests the lessThan proxy method
  1082. *
  1083. * @return void
  1084. */
  1085. public function testLessThan()
  1086. {
  1087. $validator = new Validator();
  1088. $this->assertProxyMethod($validator, 'lessThan', 5, ['<', 5], 'comparison');
  1089. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1090. }
  1091. /**
  1092. * Tests the lessThanOrEqual proxy method
  1093. *
  1094. * @return void
  1095. */
  1096. public function testLessThanOrEqual()
  1097. {
  1098. $validator = new Validator();
  1099. $this->assertProxyMethod($validator, 'lessThanOrEqual', 5, ['<=', 5], 'comparison');
  1100. $this->assertNotEmpty($validator->errors(['username' => 6]));
  1101. }
  1102. /**
  1103. * Tests the equals proxy method
  1104. *
  1105. * @return void
  1106. */
  1107. public function testEquals()
  1108. {
  1109. $validator = new Validator();
  1110. $this->assertProxyMethod($validator, 'equals', 5, ['=', 5], 'comparison');
  1111. $this->assertNotEmpty($validator->errors(['username' => 6]));
  1112. }
  1113. /**
  1114. * Tests the notEquals proxy method
  1115. *
  1116. * @return void
  1117. */
  1118. public function testNotEquals()
  1119. {
  1120. $validator = new Validator();
  1121. $this->assertProxyMethod($validator, 'notEquals', 5, ['!=', 5], 'comparison');
  1122. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1123. }
  1124. /**
  1125. * Tests the sameAs proxy method
  1126. *
  1127. * @return void
  1128. */
  1129. public function testSameAs()
  1130. {
  1131. $validator = new Validator();
  1132. $this->assertProxyMethod($validator, 'sameAs', 'other', ['other'], 'compareWith');
  1133. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1134. }
  1135. /**
  1136. * Tests the containsNonAlphaNumeric proxy method
  1137. *
  1138. * @return void
  1139. */
  1140. public function testContainsNonAlphaNumeric()
  1141. {
  1142. $validator = new Validator();
  1143. $this->assertProxyMethod($validator, 'containsNonAlphaNumeric', 2, [2]);
  1144. $this->assertNotEmpty($validator->errors(['username' => '$']));
  1145. }
  1146. /**
  1147. * Tests the date proxy method
  1148. *
  1149. * @return void
  1150. */
  1151. public function testDate()
  1152. {
  1153. $validator = new Validator();
  1154. $this->assertProxyMethod($validator, 'date', ['ymd'], [['ymd']]);
  1155. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1156. }
  1157. /**
  1158. * Tests the dateTime proxy method
  1159. *
  1160. * @return void
  1161. */
  1162. public function testDateTime()
  1163. {
  1164. $validator = new Validator();
  1165. $this->assertProxyMethod($validator, 'dateTime', ['ymd'], [['ymd']], 'datetime');
  1166. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1167. }
  1168. /**
  1169. * Tests the time proxy method
  1170. *
  1171. * @return void
  1172. */
  1173. public function testTime()
  1174. {
  1175. $validator = new Validator();
  1176. $this->assertProxyMethod($validator, 'time');
  1177. $this->assertNotEmpty($validator->errors(['username' => 'not a time']));
  1178. }
  1179. /**
  1180. * Tests the localizedTime proxy method
  1181. *
  1182. * @return void
  1183. */
  1184. public function testLocalizedTime()
  1185. {
  1186. $validator = new Validator();
  1187. $this->assertProxyMethod($validator, 'localizedTime', 'date', ['date']);
  1188. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1189. }
  1190. /**
  1191. * Tests the boolean proxy method
  1192. *
  1193. * @return void
  1194. */
  1195. public function testBoolean()
  1196. {
  1197. $validator = new Validator();
  1198. $this->assertProxyMethod($validator, 'boolean');
  1199. $this->assertNotEmpty($validator->errors(['username' => 'not a boolean']));
  1200. }
  1201. /**
  1202. * Tests the decimal proxy method
  1203. *
  1204. * @return void
  1205. */
  1206. public function testDecimal()
  1207. {
  1208. $validator = new Validator();
  1209. $this->assertProxyMethod($validator, 'decimal', 2, [2]);
  1210. $this->assertNotEmpty($validator->errors(['username' => 10.1]));
  1211. }
  1212. /**
  1213. * Tests the ip proxy methods
  1214. *
  1215. * @return void
  1216. */
  1217. public function testIps()
  1218. {
  1219. $validator = new Validator();
  1220. $this->assertProxyMethod($validator, 'ip');
  1221. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1222. $this->assertProxyMethod($validator, 'ipv4', null, ['ipv4'], 'ip');
  1223. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1224. $this->assertProxyMethod($validator, 'ipv6', null, ['ipv6'], 'ip');
  1225. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1226. }
  1227. /**
  1228. * Tests the minLength proxy method
  1229. *
  1230. * @return void
  1231. */
  1232. public function testMinLength()
  1233. {
  1234. $validator = new Validator();
  1235. $this->assertProxyMethod($validator, 'minLength', 2, [2]);
  1236. $this->assertNotEmpty($validator->errors(['username' => 'a']));
  1237. }
  1238. /**
  1239. * Tests the maxLength proxy method
  1240. *
  1241. * @return void
  1242. */
  1243. public function testMaxLength()
  1244. {
  1245. $validator = new Validator();
  1246. $this->assertProxyMethod($validator, 'maxLength', 2, [2]);
  1247. $this->assertNotEmpty($validator->errors(['username' => 'aaa']));
  1248. }
  1249. /**
  1250. * Tests the numeric proxy method
  1251. *
  1252. * @return void
  1253. */
  1254. public function testNumeric()
  1255. {
  1256. $validator = new Validator();
  1257. $this->assertProxyMethod($validator, 'numeric');
  1258. $this->assertEmpty($validator->errors(['username' => '22']));
  1259. $this->assertNotEmpty($validator->errors(['username' => 'a']));
  1260. }
  1261. /**
  1262. * Tests the naturalNumber proxy method
  1263. *
  1264. * @return void
  1265. */
  1266. public function testNaturalNumber()
  1267. {
  1268. $validator = new Validator();
  1269. $this->assertProxyMethod($validator, 'naturalNumber', null, [false]);
  1270. $this->assertNotEmpty($validator->errors(['username' => 0]));
  1271. }
  1272. /**
  1273. * Tests the nonNegativeInteger proxy method
  1274. *
  1275. * @return void
  1276. */
  1277. public function testNonNegativeInteger()
  1278. {
  1279. $validator = new Validator();
  1280. $this->assertProxyMethod($validator, 'nonNegativeInteger', null, [true], 'naturalNumber');
  1281. $this->assertNotEmpty($validator->errors(['username' => -1]));
  1282. }
  1283. /**
  1284. * Tests the range proxy method
  1285. *
  1286. * @return void
  1287. */
  1288. public function testRange()
  1289. {
  1290. $validator = new Validator();
  1291. $this->assertProxyMethod($validator, 'range', [1, 4], [1, 4]);
  1292. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1293. }
  1294. /**
  1295. * Tests the range failure case
  1296. *
  1297. * @expectedException InvalidArgumentException
  1298. * @return void
  1299. */
  1300. public function testRangeFailure()
  1301. {
  1302. $validator = new Validator();
  1303. $validator->range('username', [1]);
  1304. }
  1305. /**
  1306. * Tests the url proxy method
  1307. *
  1308. * @return void
  1309. */
  1310. public function testUrl()
  1311. {
  1312. $validator = new Validator();
  1313. $this->assertProxyMethod($validator, 'url', null, [false]);
  1314. $this->assertNotEmpty($validator->errors(['username' => 'not url']));
  1315. }
  1316. /**
  1317. * Tests the urlWithProtocol proxy method
  1318. *
  1319. * @return void
  1320. */
  1321. public function testUrlWithProtocol()
  1322. {
  1323. $validator = new Validator();
  1324. $this->assertProxyMethod($validator, 'urlWithProtocol', null, [true], 'url');
  1325. $this->assertNotEmpty($validator->errors(['username' => 'google.com']));
  1326. }
  1327. /**
  1328. * Tests the inList proxy method
  1329. *
  1330. * @return void
  1331. */
  1332. public function testInList()
  1333. {
  1334. $validator = new Validator();
  1335. $this->assertProxyMethod($validator, 'inList', ['a', 'b'], [['a', 'b']]);
  1336. $this->assertNotEmpty($validator->errors(['username' => 'c']));
  1337. }
  1338. /**
  1339. * Tests the uuid proxy method
  1340. *
  1341. * @return void
  1342. */
  1343. public function testUuid()
  1344. {
  1345. $validator = new Validator();
  1346. $this->assertProxyMethod($validator, 'uuid');
  1347. $this->assertNotEmpty($validator->errors(['username' => 'not uuid']));
  1348. }
  1349. /**
  1350. * Tests the uploadedFile proxy method
  1351. *
  1352. * @return void
  1353. */
  1354. public function testUploadedFile()
  1355. {
  1356. $validator = new Validator();
  1357. $this->assertProxyMethod($validator, 'uploadedFile', ['foo' => 'bar'], [['foo' => 'bar']]);
  1358. $this->assertNotEmpty($validator->errors(['username' => []]));
  1359. }
  1360. /**
  1361. * Tests the latlog proxy methods
  1362. *
  1363. * @return void
  1364. */
  1365. public function testLatLong()
  1366. {
  1367. $validator = new Validator();
  1368. $this->assertProxyMethod($validator, 'latLong', null, [], 'geoCoordinate');
  1369. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1370. $this->assertProxyMethod($validator, 'latitude');
  1371. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1372. $this->assertProxyMethod($validator, 'longitude');
  1373. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1374. }
  1375. /**
  1376. * Tests the ascii proxy method
  1377. *
  1378. * @return void
  1379. */
  1380. public function testAscii()
  1381. {
  1382. $validator = new Validator();
  1383. $this->assertProxyMethod($validator, 'ascii');
  1384. $this->assertNotEmpty($validator->errors(['username' => 'ü']));
  1385. }
  1386. /**
  1387. * Tests the utf8 proxy methods
  1388. *
  1389. * @return void
  1390. */
  1391. public function testUtf8()
  1392. {
  1393. // Grinning face
  1394. $extended = 'some' . "\xf0\x9f\x98\x80" . 'value';
  1395. $validator = new Validator();
  1396. $this->assertProxyMethod($validator, 'utf8', null, [['extended' => false]]);
  1397. $this->assertEmpty($validator->errors(['username' => 'ü']));
  1398. $this->assertNotEmpty($validator->errors(['username' => $extended]));
  1399. }
  1400. /**
  1401. * Test utf8extended proxy method.
  1402. *
  1403. * @return void
  1404. */
  1405. public function testUtf8Extended()
  1406. {
  1407. // Grinning face
  1408. $extended = 'some' . "\xf0\x9f\x98\x80" . 'value';
  1409. $validator = new Validator();
  1410. $this->assertProxyMethod($validator, 'utf8Extended', null, [['extended' => true]], 'utf8');
  1411. $this->assertEmpty($validator->errors(['username' => 'ü']));
  1412. $this->assertEmpty($validator->errors(['username' => $extended]));
  1413. }
  1414. /**
  1415. * Tests the email proxy method
  1416. *
  1417. * @return void
  1418. */
  1419. public function testEmail()
  1420. {
  1421. $validator = new Validator();
  1422. $validator->email('username');
  1423. $this->assertEmpty($validator->errors(['username' => 'test@example.com']));
  1424. $this->assertNotEmpty($validator->errors(['username' => 'not an email']));
  1425. }
  1426. /**
  1427. * Tests the integer proxy method
  1428. *
  1429. * @return void
  1430. */
  1431. public function testInteger()
  1432. {
  1433. $validator = new Validator();
  1434. $this->assertProxyMethod($validator, 'integer', null, [], 'isInteger');
  1435. $this->assertNotEmpty($validator->errors(['username' => 'not integer']));
  1436. }
  1437. /**
  1438. * Tests the multiple proxy method
  1439. *
  1440. * @return void
  1441. */
  1442. public function testMultiple()
  1443. {
  1444. $validator = new Validator();
  1445. $this->assertProxyMethod(
  1446. $validator,
  1447. 'multipleOptions',
  1448. ['min' => 1, 'caseInsensitive' => true],
  1449. [['min' => 1], false],
  1450. 'multiple'
  1451. );
  1452. $this->assertNotEmpty($validator->errors(['username' => '']));
  1453. }
  1454. /**
  1455. * Tests the hasAtLeast method
  1456. *
  1457. * @return void
  1458. */
  1459. public function testHasAtLeast()
  1460. {
  1461. $validator = new Validator();
  1462. $validator->hasAtLeast('things', 3);
  1463. $this->assertEmpty($validator->errors(['things' => [1, 2, 3]]));
  1464. $this->assertEmpty($validator->errors(['things' => [1, 2, 3, 4]]));
  1465. $this->assertNotEmpty($validator->errors(['things' => [1, 2]]));
  1466. $this->assertNotEmpty($validator->errors(['things' => []]));
  1467. $this->assertNotEmpty($validator->errors(['things' => 'string']));
  1468. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3]]]));
  1469. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3, 4]]]));
  1470. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => [1, 2]]]));
  1471. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => []]]));
  1472. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => 'string']]));
  1473. }
  1474. /**
  1475. * Tests the hasAtMost method
  1476. *
  1477. * @return void
  1478. */
  1479. public function testHasAtMost()
  1480. {
  1481. $validator = new Validator();
  1482. $validator->hasAtMost('things', 3);
  1483. $this->assertEmpty($validator->errors(['things' => [1, 2, 3]]));
  1484. $this->assertEmpty($validator->errors(['things' => [1]]));
  1485. $this->assertNotEmpty($validator->errors(['things' => [1, 2, 3, 4]]));
  1486. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3]]]));
  1487. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2]]]));
  1488. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3, 4]]]));
  1489. }
  1490. protected function assertProxyMethod($validator, $method, $extra = null, $pass = [], $name = null)
  1491. {
  1492. $name = $name ?: $method;
  1493. if ($extra !== null) {
  1494. $this->assertSame($validator, $validator->{$method}('username', $extra));
  1495. } else {
  1496. $this->assertSame($validator, $validator->{$method}('username'));
  1497. }
  1498. $rule = $validator->field('username')->rule($method);
  1499. $this->assertNotEmpty($rule, "Rule was not found for $method");
  1500. $this->assertNull($rule->get('message'), 'Message is present when it should not be');
  1501. $this->assertNull($rule->get('on'), 'On clause is present when it should not be');
  1502. $this->assertEquals($name, $rule->get('rule'), 'Rule name does not match');
  1503. $this->assertEquals($pass, $rule->get('pass'), 'Passed options are different');
  1504. $this->assertEquals('default', $rule->get('provider'), 'Provider does not match');
  1505. if ($extra !== null) {
  1506. $validator->{$method}('username', $extra, 'the message', 'create');
  1507. } else {
  1508. $validator->{$method}('username', 'the message', 'create');
  1509. }
  1510. $rule = $validator->field('username')->rule($method);
  1511. $this->assertEquals('the message', $rule->get('message'), 'Error messages are not the same');
  1512. $this->assertEquals('create', $rule->get('on'), 'On clause is wrong');
  1513. }
  1514. }