ValidatorTest.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  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. * Tests the allowEmpty as array method
  491. *
  492. * @return void
  493. */
  494. public function testAllowEmptyAsArray()
  495. {
  496. $validator = new Validator;
  497. $validator->allowEmpty([
  498. 'title',
  499. 'subject',
  500. 'posted_at' => [
  501. 'when' => false
  502. ],
  503. 'updated_at' => [
  504. 'when' => true
  505. ],
  506. 'show_at' => [
  507. 'when' => 'update'
  508. ]
  509. ], 'create');
  510. $this->assertEquals('create', $validator->field('title')->isEmptyAllowed());
  511. $this->assertEquals('create', $validator->field('subject')->isEmptyAllowed());
  512. $this->assertFalse($validator->field('posted_at')->isEmptyAllowed());
  513. $this->assertTrue($validator->field('updated_at')->isEmptyAllowed());
  514. $this->assertEquals('update', $validator->field('show_at')->isEmptyAllowed());
  515. }
  516. /**
  517. * Tests the allowEmpty failure case
  518. *
  519. * @expectedException InvalidArgumentException
  520. * @return void
  521. */
  522. public function testAllowEmptyAsArrayFailure()
  523. {
  524. $validator = new Validator();
  525. $validator->allowEmpty(['title' => 'derp', 'created' => false]);
  526. }
  527. /**
  528. * Test the notEmpty() method.
  529. *
  530. * @return void
  531. */
  532. public function testNotEmpty()
  533. {
  534. $validator = new Validator;
  535. $validator->notEmpty('title');
  536. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  537. $validator->allowEmpty('title');
  538. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  539. }
  540. /**
  541. * Tests the notEmpty as array method
  542. *
  543. * @return void
  544. */
  545. public function testNotEmptyAsArray()
  546. {
  547. $validator = new Validator;
  548. $validator->notEmpty(['title', 'created']);
  549. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  550. $this->assertFalse($validator->field('created')->isEmptyAllowed());
  551. $validator->notEmpty([
  552. 'title' => [
  553. 'when' => false
  554. ],
  555. 'content' => [
  556. 'when' => 'update'
  557. ],
  558. 'posted_at' => [
  559. 'when' => 'create'
  560. ],
  561. 'show_at' => [
  562. 'message' => 'Show date cannot be empty',
  563. 'when' => false
  564. ],
  565. 'subject'
  566. ], 'Not empty', true);
  567. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  568. $this->assertTrue($validator->isEmptyAllowed('content', true));
  569. $this->assertFalse($validator->isEmptyAllowed('content', false));
  570. $this->assertFalse($validator->isEmptyAllowed('posted_at', true));
  571. $this->assertTrue($validator->isEmptyAllowed('posted_at', false));
  572. $this->assertTrue($validator->field('subject')->isEmptyAllowed());
  573. $errors = $validator->errors([
  574. 'title' => '',
  575. 'content' => '',
  576. 'posted_at' => null,
  577. 'show_at' => null,
  578. 'subject' => ''
  579. ], false);
  580. $expected = [
  581. 'title' => ['_empty' => 'Not empty'],
  582. 'content' => ['_empty' => 'Not empty'],
  583. 'show_at' => ['_empty' => 'Show date cannot be empty']
  584. ];
  585. $this->assertEquals($expected, $errors);
  586. }
  587. /**
  588. * Tests the notEmpty failure case
  589. *
  590. * @expectedException InvalidArgumentException
  591. * @return void
  592. */
  593. public function testNotEmptyAsArrayFailure()
  594. {
  595. $validator = new Validator();
  596. $validator->notEmpty(['title' => 'derp', 'created' => false]);
  597. }
  598. /**
  599. * Test the notEmpty() method.
  600. *
  601. * @return void
  602. */
  603. public function testNotEmptyModes()
  604. {
  605. $validator = new Validator;
  606. $validator->notEmpty('title', 'Need a title', 'create');
  607. $this->assertFalse($validator->isEmptyAllowed('title', true));
  608. $this->assertTrue($validator->isEmptyAllowed('title', false));
  609. $validator->notEmpty('title', 'Need a title', 'update');
  610. $this->assertTrue($validator->isEmptyAllowed('title', true));
  611. $this->assertFalse($validator->isEmptyAllowed('title', false));
  612. $validator->notEmpty('title', 'Need a title');
  613. $this->assertFalse($validator->isEmptyAllowed('title', true));
  614. $this->assertFalse($validator->isEmptyAllowed('title', false));
  615. $validator->notEmpty('title');
  616. $this->assertFalse($validator->isEmptyAllowed('title', true));
  617. $this->assertFalse($validator->isEmptyAllowed('title', false));
  618. }
  619. /**
  620. * Test interactions between notEmpty() and isAllowed().
  621. *
  622. * @return void
  623. */
  624. public function testNotEmptyAndIsAllowed()
  625. {
  626. $validator = new Validator;
  627. $validator->allowEmpty('title')
  628. ->notEmpty('title', 'Need it', 'update');
  629. $this->assertTrue($validator->isEmptyAllowed('title', true));
  630. $this->assertFalse($validator->isEmptyAllowed('title', false));
  631. $validator->allowEmpty('title')
  632. ->notEmpty('title');
  633. $this->assertFalse($validator->isEmptyAllowed('title', true));
  634. $this->assertFalse($validator->isEmptyAllowed('title', false));
  635. $validator->notEmpty('title')
  636. ->allowEmpty('title', 'create');
  637. $this->assertTrue($validator->isEmptyAllowed('title', true));
  638. $this->assertFalse($validator->isEmptyAllowed('title', false));
  639. }
  640. /**
  641. * Tests the allowEmpty method when passing a callback
  642. *
  643. * @return void
  644. */
  645. public function testAllowEmptyCallback()
  646. {
  647. $validator = new Validator;
  648. $allow = true;
  649. $validator->allowEmpty('title', function ($context) use (&$allow) {
  650. $this->assertEquals([], $context['data']);
  651. $this->assertEquals([], $context['providers']);
  652. $this->assertTrue($context['newRecord']);
  653. return $allow;
  654. });
  655. $this->assertTrue($validator->isEmptyAllowed('title', true));
  656. $allow = false;
  657. $this->assertFalse($validator->isEmptyAllowed('title', true));
  658. }
  659. /**
  660. * Tests the notEmpty method when passing a callback
  661. *
  662. * @return void
  663. */
  664. public function testNotEmptyCallback()
  665. {
  666. $validator = new Validator;
  667. $prevent = true;
  668. $validator->notEmpty('title', 'error message', function ($context) use (&$prevent) {
  669. $this->assertEquals([], $context['data']);
  670. $this->assertEquals([], $context['providers']);
  671. $this->assertFalse($context['newRecord']);
  672. return $prevent;
  673. });
  674. $this->assertFalse($validator->isEmptyAllowed('title', false));
  675. $prevent = false;
  676. $this->assertTrue($validator->isEmptyAllowed('title', false));
  677. }
  678. /**
  679. * Tests the isEmptyAllowed method
  680. *
  681. * @return void
  682. */
  683. public function testIsEmptyAllowed()
  684. {
  685. $validator = new Validator;
  686. $this->assertSame($validator, $validator->allowEmpty('title'));
  687. $this->assertTrue($validator->isEmptyAllowed('title', true));
  688. $this->assertTrue($validator->isEmptyAllowed('title', false));
  689. $validator->notEmpty('title');
  690. $this->assertFalse($validator->isEmptyAllowed('title', true));
  691. $this->assertFalse($validator->isEmptyAllowed('title', false));
  692. $validator->allowEmpty('title', 'create');
  693. $this->assertTrue($validator->isEmptyAllowed('title', true));
  694. $this->assertFalse($validator->isEmptyAllowed('title', false));
  695. $validator->allowEmpty('title', 'update');
  696. $this->assertTrue($validator->isEmptyAllowed('title', false));
  697. $this->assertFalse($validator->isEmptyAllowed('title', true));
  698. }
  699. /**
  700. * Tests errors generated when a field is not allowed to be empty
  701. *
  702. * @return void
  703. */
  704. public function testErrorsWithEmptyNotAllowed()
  705. {
  706. $validator = new Validator;
  707. $validator->notEmpty('title');
  708. $errors = $validator->errors(['title' => '']);
  709. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  710. $this->assertEquals($expected, $errors);
  711. $errors = $validator->errors(['title' => []]);
  712. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  713. $this->assertEquals($expected, $errors);
  714. $errors = $validator->errors(['title' => null]);
  715. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  716. $this->assertEquals($expected, $errors);
  717. $errors = $validator->errors(['title' => 0]);
  718. $this->assertEmpty($errors);
  719. $errors = $validator->errors(['title' => '0']);
  720. $this->assertEmpty($errors);
  721. $errors = $validator->errors(['title' => false]);
  722. $this->assertEmpty($errors);
  723. }
  724. /**
  725. * Tests custom error mesages generated when a field is not allowed to be empty
  726. *
  727. * @return void
  728. */
  729. public function testCustomErrorsWithEmptyNotAllowed()
  730. {
  731. $validator = new Validator;
  732. $validator->notEmpty('title', 'Custom message');
  733. $errors = $validator->errors(['title' => '']);
  734. $expected = ['title' => ['_empty' => 'Custom message']];
  735. $this->assertEquals($expected, $errors);
  736. }
  737. /**
  738. * Tests errors generated when a field is allowed to be empty
  739. *
  740. * @return void
  741. */
  742. public function testErrorsWithEmptyAllowed()
  743. {
  744. $validator = new Validator;
  745. $validator->allowEmpty('title');
  746. $errors = $validator->errors(['title' => '']);
  747. $this->assertEmpty($errors);
  748. $errors = $validator->errors(['title' => []]);
  749. $this->assertEmpty($errors);
  750. $errors = $validator->errors(['title' => null]);
  751. $this->assertEmpty($errors);
  752. $errors = $validator->errors(['title' => 0]);
  753. $this->assertEmpty($errors);
  754. $errors = $validator->errors(['title' => 0.0]);
  755. $this->assertEmpty($errors);
  756. $errors = $validator->errors(['title' => '0']);
  757. $this->assertEmpty($errors);
  758. $errors = $validator->errors(['title' => false]);
  759. $this->assertEmpty($errors);
  760. }
  761. /**
  762. * Test the provider() method
  763. *
  764. * @return void
  765. */
  766. public function testProvider()
  767. {
  768. $validator = new Validator;
  769. $object = new \stdClass;
  770. $this->assertSame($validator, $validator->provider('foo', $object));
  771. $this->assertSame($object, $validator->provider('foo'));
  772. $this->assertNull($validator->provider('bar'));
  773. $another = new \stdClass;
  774. $this->assertSame($validator, $validator->provider('bar', $another));
  775. $this->assertSame($another, $validator->provider('bar'));
  776. $this->assertEquals(new \Cake\Validation\RulesProvider, $validator->provider('default'));
  777. }
  778. /**
  779. * Tests errors() method when using validators from the default provider, this proves
  780. * that it returns a default validation message and the custom one set in the rule
  781. *
  782. * @return void
  783. */
  784. public function testErrorsFromDefaultProvider()
  785. {
  786. $validator = new Validator;
  787. $validator
  788. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  789. ->add('email', 'notBlank', ['rule' => 'notBlank'])
  790. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  791. $errors = $validator->errors(['email' => 'not an email!']);
  792. $expected = [
  793. 'email' => [
  794. 'alpha' => 'The provided value is invalid',
  795. 'email' => 'Y u no write email?'
  796. ]
  797. ];
  798. $this->assertEquals($expected, $errors);
  799. }
  800. /**
  801. * Tests using validation methods from different providers and returning the error
  802. * as a string
  803. *
  804. * @return void
  805. */
  806. public function testErrorsFromCustomProvider()
  807. {
  808. $validator = new Validator;
  809. $validator
  810. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  811. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  812. $thing = $this->getMock('\stdClass', ['isCool']);
  813. $thing->expects($this->once())->method('isCool')
  814. ->will($this->returnCallback(function ($data, $context) use ($thing) {
  815. $this->assertEquals('bar', $data);
  816. $expected = [
  817. 'default' => new \Cake\Validation\RulesProvider,
  818. 'thing' => $thing
  819. ];
  820. $expected = [
  821. 'newRecord' => true,
  822. 'providers' => $expected,
  823. 'data' => [
  824. 'email' => '!',
  825. 'title' => 'bar'
  826. ],
  827. 'field' => 'title'
  828. ];
  829. $this->assertEquals($expected, $context);
  830. return "That ain't cool, yo";
  831. }));
  832. $validator->provider('thing', $thing);
  833. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  834. $expected = [
  835. 'email' => ['alpha' => 'The provided value is invalid'],
  836. 'title' => ['cool' => "That ain't cool, yo"]
  837. ];
  838. $this->assertEquals($expected, $errors);
  839. }
  840. /**
  841. * Tests that it is possible to pass extra arguments to the validation function
  842. * and it still gets the providers as last argument
  843. *
  844. * @return void
  845. */
  846. public function testMethodsWithExtraArguments()
  847. {
  848. $validator = new Validator;
  849. $validator->add('title', 'cool', [
  850. 'rule' => ['isCool', 'and', 'awesome'],
  851. 'provider' => 'thing'
  852. ]);
  853. $thing = $this->getMock('\stdClass', ['isCool']);
  854. $thing->expects($this->once())->method('isCool')
  855. ->will($this->returnCallback(function ($data, $a, $b, $context) use ($thing) {
  856. $this->assertEquals('bar', $data);
  857. $this->assertEquals('and', $a);
  858. $this->assertEquals('awesome', $b);
  859. $expected = [
  860. 'default' => new \Cake\Validation\RulesProvider,
  861. 'thing' => $thing
  862. ];
  863. $expected = [
  864. 'newRecord' => true,
  865. 'providers' => $expected,
  866. 'data' => [
  867. 'email' => '!',
  868. 'title' => 'bar'
  869. ],
  870. 'field' => 'title'
  871. ];
  872. $this->assertEquals($expected, $context);
  873. return "That ain't cool, yo";
  874. }));
  875. $validator->provider('thing', $thing);
  876. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  877. $expected = [
  878. 'title' => ['cool' => "That ain't cool, yo"]
  879. ];
  880. $this->assertEquals($expected, $errors);
  881. }
  882. /**
  883. * Tests that it is possible to use a closure as a rule
  884. *
  885. * @return void
  886. */
  887. public function testUsingClosureAsRule()
  888. {
  889. $validator = new Validator;
  890. $validator->add('name', 'myRule', [
  891. 'rule' => function ($data, $provider) {
  892. $this->assertEquals('foo', $data);
  893. return 'You fail';
  894. }
  895. ]);
  896. $expected = ['name' => ['myRule' => 'You fail']];
  897. $this->assertEquals($expected, $validator->errors(['name' => 'foo']));
  898. }
  899. /**
  900. * Tests that setting last to a rule will stop validating the rest of the rules
  901. *
  902. * @return void
  903. */
  904. public function testErrorsWithLastRule()
  905. {
  906. $validator = new Validator;
  907. $validator
  908. ->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
  909. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  910. $errors = $validator->errors(['email' => 'not an email!']);
  911. $expected = [
  912. 'email' => [
  913. 'alpha' => 'The provided value is invalid'
  914. ]
  915. ];
  916. $this->assertEquals($expected, $errors);
  917. }
  918. /**
  919. * Tests it is possible to get validation sets for a field using an array interface
  920. *
  921. * @return void
  922. */
  923. public function testArrayAccessGet()
  924. {
  925. $validator = new Validator;
  926. $validator
  927. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  928. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  929. $this->assertSame($validator['email'], $validator->field('email'));
  930. $this->assertSame($validator['title'], $validator->field('title'));
  931. }
  932. /**
  933. * Tests it is possible to check for validation sets for a field using an array inteface
  934. *
  935. * @return void
  936. */
  937. public function testArrayAccessExists()
  938. {
  939. $validator = new Validator;
  940. $validator
  941. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  942. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  943. $this->assertTrue(isset($validator['email']));
  944. $this->assertTrue(isset($validator['title']));
  945. $this->assertFalse(isset($validator['foo']));
  946. }
  947. /**
  948. * Tests it is possible to set validation rules for a field using an array inteface
  949. *
  950. * @return void
  951. */
  952. public function testArrayAccessSet()
  953. {
  954. $validator = new Validator;
  955. $validator
  956. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  957. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  958. $validator['name'] = $validator->field('title');
  959. $this->assertSame($validator->field('title'), $validator->field('name'));
  960. $validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
  961. $this->assertEquals($validator->field('email'), $validator->field('email'));
  962. }
  963. /**
  964. * Tests it is possible to unset validation rules
  965. *
  966. * @return void
  967. */
  968. public function testArrayAccessUset()
  969. {
  970. $validator = new Validator;
  971. $validator
  972. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  973. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  974. $this->assertTrue(isset($validator['title']));
  975. unset($validator['title']);
  976. $this->assertFalse(isset($validator['title']));
  977. }
  978. /**
  979. * Tests the countable interface
  980. *
  981. * @return void
  982. */
  983. public function testCount()
  984. {
  985. $validator = new Validator;
  986. $validator
  987. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  988. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  989. $this->assertCount(2, $validator);
  990. }
  991. /**
  992. * Tests adding rules via alternative syntax
  993. *
  994. * @return void
  995. */
  996. public function testAddMulitple()
  997. {
  998. $validator = new Validator;
  999. $validator->add('title', [
  1000. 'notBlank' => [
  1001. 'rule' => 'notBlank'
  1002. ],
  1003. 'length' => [
  1004. 'rule' => ['minLength', 10],
  1005. 'message' => 'Titles need to be at least 10 characters long'
  1006. ]
  1007. ]);
  1008. $set = $validator->field('title');
  1009. $this->assertInstanceOf('Cake\Validation\ValidationSet', $set);
  1010. $this->assertCount(2, $set);
  1011. }
  1012. /**
  1013. * Integration test for compareWith validator.
  1014. *
  1015. * @return void
  1016. */
  1017. public function testCompareWithIntegration()
  1018. {
  1019. $validator = new Validator;
  1020. $validator->add('password', [
  1021. 'compare' => [
  1022. 'rule' => ['compareWith', 'password_compare']
  1023. ],
  1024. ]);
  1025. $data = [
  1026. 'password' => 'test',
  1027. 'password_compare' => 'not the same'
  1028. ];
  1029. $this->assertNotEmpty($validator->errors($data), 'Validation should fail.');
  1030. }
  1031. /**
  1032. * Test debugInfo helper method.
  1033. *
  1034. * @return void
  1035. */
  1036. public function testDebugInfo()
  1037. {
  1038. $validator = new Validator();
  1039. $validator->provider('test', $this);
  1040. $validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
  1041. $validator->requirePresence('body');
  1042. $validator->allowEmpty('published');
  1043. $result = $validator->__debugInfo();
  1044. $expected = [
  1045. '_providers' => ['test'],
  1046. '_fields' => [
  1047. 'title' => [
  1048. 'isPresenceRequired' => false,
  1049. 'isEmptyAllowed' => false,
  1050. 'rules' => ['not-empty'],
  1051. ],
  1052. 'body' => [
  1053. 'isPresenceRequired' => true,
  1054. 'isEmptyAllowed' => false,
  1055. 'rules' => [],
  1056. ],
  1057. 'published' => [
  1058. 'isPresenceRequired' => false,
  1059. 'isEmptyAllowed' => true,
  1060. 'rules' => [],
  1061. ],
  1062. ],
  1063. '_presenceMessages' => [],
  1064. '_allowEmptyMessages' => [],
  1065. '_useI18n' => true,
  1066. ];
  1067. $this->assertEquals($expected, $result);
  1068. }
  1069. /**
  1070. * Tests that the 'create' and 'update' modes are preserved when using
  1071. * nested validators
  1072. *
  1073. * @return void
  1074. */
  1075. public function testNestedValidatorCreate()
  1076. {
  1077. $validator = new Validator();
  1078. $inner = new Validator();
  1079. $inner->add('username', 'email', ['rule' => 'email', 'on' => 'create']);
  1080. $validator->addNested('user', $inner);
  1081. $this->assertNotEmpty($validator->errors(['user' => ['username' => 'example']], true));
  1082. $this->assertEmpty($validator->errors(['user' => ['username' => 'a']], false));
  1083. }
  1084. /**
  1085. * Tests that the 'create' and 'update' modes are preserved when using
  1086. * nested validators
  1087. *
  1088. * @return void
  1089. */
  1090. public function testNestedManyValidatorCreate()
  1091. {
  1092. $validator = new Validator();
  1093. $inner = new Validator();
  1094. $inner->add('username', 'email', ['rule' => 'email', 'on' => 'create']);
  1095. $validator->addNestedMany('user', $inner);
  1096. $this->assertNotEmpty($validator->errors(['user' => [['username' => 'example']]], true));
  1097. $this->assertEmpty($validator->errors(['user' => [['username' => 'a']]], false));
  1098. }
  1099. /**
  1100. * Tests the notBlank proxy method
  1101. *
  1102. * @return void
  1103. */
  1104. public function testNotBlank()
  1105. {
  1106. $validator = new Validator();
  1107. $this->assertProxyMethod($validator, 'notBlank');
  1108. $this->assertNotEmpty($validator->errors(['username' => ' ']));
  1109. }
  1110. /**
  1111. * Tests the alphanumeric proxy method
  1112. *
  1113. * @return void
  1114. */
  1115. public function testAlphanumeric()
  1116. {
  1117. $validator = new Validator();
  1118. $this->assertProxyMethod($validator, 'alphaNumeric');
  1119. $this->assertNotEmpty($validator->errors(['username' => '$']));
  1120. }
  1121. /**
  1122. * Tests the lengthBetween proxy method
  1123. *
  1124. * @return void
  1125. */
  1126. public function testLengthBetween()
  1127. {
  1128. $validator = new Validator();
  1129. $this->assertProxyMethod($validator, 'lengthBetween', [5, 7], [5, 7]);
  1130. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1131. }
  1132. /**
  1133. * Tests the lengthBetween proxy method
  1134. *
  1135. * @expectedException InvalidArgumentException
  1136. * @return void
  1137. */
  1138. public function testLengthBetweenFailure()
  1139. {
  1140. $validator = new Validator();
  1141. $validator->lengthBetween('username', [7]);
  1142. }
  1143. /**
  1144. * Tests the creditCard proxy method
  1145. *
  1146. * @return void
  1147. */
  1148. public function testCreditCard()
  1149. {
  1150. $validator = new Validator();
  1151. $this->assertProxyMethod($validator, 'creditCard', 'all', ['all', true], 'cc');
  1152. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1153. }
  1154. /**
  1155. * Tests the greaterThan proxy method
  1156. *
  1157. * @return void
  1158. */
  1159. public function testGreaterThan()
  1160. {
  1161. $validator = new Validator();
  1162. $this->assertProxyMethod($validator, 'greaterThan', 5, ['>', 5], 'comparison');
  1163. $this->assertNotEmpty($validator->errors(['username' => 2]));
  1164. }
  1165. /**
  1166. * Tests the greaterThanOrEqual proxy method
  1167. *
  1168. * @return void
  1169. */
  1170. public function testGreaterThanOrEqual()
  1171. {
  1172. $validator = new Validator();
  1173. $this->assertProxyMethod($validator, 'greaterThanOrEqual', 5, ['>=', 5], 'comparison');
  1174. $this->assertNotEmpty($validator->errors(['username' => 2]));
  1175. }
  1176. /**
  1177. * Tests the lessThan proxy method
  1178. *
  1179. * @return void
  1180. */
  1181. public function testLessThan()
  1182. {
  1183. $validator = new Validator();
  1184. $this->assertProxyMethod($validator, 'lessThan', 5, ['<', 5], 'comparison');
  1185. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1186. }
  1187. /**
  1188. * Tests the lessThanOrEqual proxy method
  1189. *
  1190. * @return void
  1191. */
  1192. public function testLessThanOrEqual()
  1193. {
  1194. $validator = new Validator();
  1195. $this->assertProxyMethod($validator, 'lessThanOrEqual', 5, ['<=', 5], 'comparison');
  1196. $this->assertNotEmpty($validator->errors(['username' => 6]));
  1197. }
  1198. /**
  1199. * Tests the equals proxy method
  1200. *
  1201. * @return void
  1202. */
  1203. public function testEquals()
  1204. {
  1205. $validator = new Validator();
  1206. $this->assertProxyMethod($validator, 'equals', 5, ['=', 5], 'comparison');
  1207. $this->assertNotEmpty($validator->errors(['username' => 6]));
  1208. }
  1209. /**
  1210. * Tests the notEquals proxy method
  1211. *
  1212. * @return void
  1213. */
  1214. public function testNotEquals()
  1215. {
  1216. $validator = new Validator();
  1217. $this->assertProxyMethod($validator, 'notEquals', 5, ['!=', 5], 'comparison');
  1218. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1219. }
  1220. /**
  1221. * Tests the sameAs proxy method
  1222. *
  1223. * @return void
  1224. */
  1225. public function testSameAs()
  1226. {
  1227. $validator = new Validator();
  1228. $this->assertProxyMethod($validator, 'sameAs', 'other', ['other'], 'compareWith');
  1229. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1230. }
  1231. /**
  1232. * Tests the containsNonAlphaNumeric proxy method
  1233. *
  1234. * @return void
  1235. */
  1236. public function testContainsNonAlphaNumeric()
  1237. {
  1238. $validator = new Validator();
  1239. $this->assertProxyMethod($validator, 'containsNonAlphaNumeric', 2, [2]);
  1240. $this->assertNotEmpty($validator->errors(['username' => '$']));
  1241. }
  1242. /**
  1243. * Tests the date proxy method
  1244. *
  1245. * @return void
  1246. */
  1247. public function testDate()
  1248. {
  1249. $validator = new Validator();
  1250. $this->assertProxyMethod($validator, 'date', ['ymd'], [['ymd']]);
  1251. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1252. }
  1253. /**
  1254. * Tests the dateTime proxy method
  1255. *
  1256. * @return void
  1257. */
  1258. public function testDateTime()
  1259. {
  1260. $validator = new Validator();
  1261. $this->assertProxyMethod($validator, 'dateTime', ['ymd'], [['ymd']], 'datetime');
  1262. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1263. }
  1264. /**
  1265. * Tests the time proxy method
  1266. *
  1267. * @return void
  1268. */
  1269. public function testTime()
  1270. {
  1271. $validator = new Validator();
  1272. $this->assertProxyMethod($validator, 'time');
  1273. $this->assertNotEmpty($validator->errors(['username' => 'not a time']));
  1274. }
  1275. /**
  1276. * Tests the localizedTime proxy method
  1277. *
  1278. * @return void
  1279. */
  1280. public function testLocalizedTime()
  1281. {
  1282. $validator = new Validator();
  1283. $this->assertProxyMethod($validator, 'localizedTime', 'date', ['date']);
  1284. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1285. }
  1286. /**
  1287. * Tests the boolean proxy method
  1288. *
  1289. * @return void
  1290. */
  1291. public function testBoolean()
  1292. {
  1293. $validator = new Validator();
  1294. $this->assertProxyMethod($validator, 'boolean');
  1295. $this->assertNotEmpty($validator->errors(['username' => 'not a boolean']));
  1296. }
  1297. /**
  1298. * Tests the decimal proxy method
  1299. *
  1300. * @return void
  1301. */
  1302. public function testDecimal()
  1303. {
  1304. $validator = new Validator();
  1305. $this->assertProxyMethod($validator, 'decimal', 2, [2]);
  1306. $this->assertNotEmpty($validator->errors(['username' => 10.1]));
  1307. }
  1308. /**
  1309. * Tests the ip proxy methods
  1310. *
  1311. * @return void
  1312. */
  1313. public function testIps()
  1314. {
  1315. $validator = new Validator();
  1316. $this->assertProxyMethod($validator, 'ip');
  1317. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1318. $this->assertProxyMethod($validator, 'ipv4', null, ['ipv4'], 'ip');
  1319. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1320. $this->assertProxyMethod($validator, 'ipv6', null, ['ipv6'], 'ip');
  1321. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1322. }
  1323. /**
  1324. * Tests the minLength proxy method
  1325. *
  1326. * @return void
  1327. */
  1328. public function testMinLength()
  1329. {
  1330. $validator = new Validator();
  1331. $this->assertProxyMethod($validator, 'minLength', 2, [2]);
  1332. $this->assertNotEmpty($validator->errors(['username' => 'a']));
  1333. }
  1334. /**
  1335. * Tests the maxLength proxy method
  1336. *
  1337. * @return void
  1338. */
  1339. public function testMaxLength()
  1340. {
  1341. $validator = new Validator();
  1342. $this->assertProxyMethod($validator, 'maxLength', 2, [2]);
  1343. $this->assertNotEmpty($validator->errors(['username' => 'aaa']));
  1344. }
  1345. /**
  1346. * Tests the numeric proxy method
  1347. *
  1348. * @return void
  1349. */
  1350. public function testNumeric()
  1351. {
  1352. $validator = new Validator();
  1353. $this->assertProxyMethod($validator, 'numeric');
  1354. $this->assertEmpty($validator->errors(['username' => '22']));
  1355. $this->assertNotEmpty($validator->errors(['username' => 'a']));
  1356. }
  1357. /**
  1358. * Tests the naturalNumber proxy method
  1359. *
  1360. * @return void
  1361. */
  1362. public function testNaturalNumber()
  1363. {
  1364. $validator = new Validator();
  1365. $this->assertProxyMethod($validator, 'naturalNumber', null, [false]);
  1366. $this->assertNotEmpty($validator->errors(['username' => 0]));
  1367. }
  1368. /**
  1369. * Tests the nonNegativeInteger proxy method
  1370. *
  1371. * @return void
  1372. */
  1373. public function testNonNegativeInteger()
  1374. {
  1375. $validator = new Validator();
  1376. $this->assertProxyMethod($validator, 'nonNegativeInteger', null, [true], 'naturalNumber');
  1377. $this->assertNotEmpty($validator->errors(['username' => -1]));
  1378. }
  1379. /**
  1380. * Tests the range proxy method
  1381. *
  1382. * @return void
  1383. */
  1384. public function testRange()
  1385. {
  1386. $validator = new Validator();
  1387. $this->assertProxyMethod($validator, 'range', [1, 4], [1, 4]);
  1388. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1389. }
  1390. /**
  1391. * Tests the range failure case
  1392. *
  1393. * @expectedException InvalidArgumentException
  1394. * @return void
  1395. */
  1396. public function testRangeFailure()
  1397. {
  1398. $validator = new Validator();
  1399. $validator->range('username', [1]);
  1400. }
  1401. /**
  1402. * Tests the url proxy method
  1403. *
  1404. * @return void
  1405. */
  1406. public function testUrl()
  1407. {
  1408. $validator = new Validator();
  1409. $this->assertProxyMethod($validator, 'url', null, [false]);
  1410. $this->assertNotEmpty($validator->errors(['username' => 'not url']));
  1411. }
  1412. /**
  1413. * Tests the urlWithProtocol proxy method
  1414. *
  1415. * @return void
  1416. */
  1417. public function testUrlWithProtocol()
  1418. {
  1419. $validator = new Validator();
  1420. $this->assertProxyMethod($validator, 'urlWithProtocol', null, [true], 'url');
  1421. $this->assertNotEmpty($validator->errors(['username' => 'google.com']));
  1422. }
  1423. /**
  1424. * Tests the inList proxy method
  1425. *
  1426. * @return void
  1427. */
  1428. public function testInList()
  1429. {
  1430. $validator = new Validator();
  1431. $this->assertProxyMethod($validator, 'inList', ['a', 'b'], [['a', 'b']]);
  1432. $this->assertNotEmpty($validator->errors(['username' => 'c']));
  1433. }
  1434. /**
  1435. * Tests the uuid proxy method
  1436. *
  1437. * @return void
  1438. */
  1439. public function testUuid()
  1440. {
  1441. $validator = new Validator();
  1442. $this->assertProxyMethod($validator, 'uuid');
  1443. $this->assertNotEmpty($validator->errors(['username' => 'not uuid']));
  1444. }
  1445. /**
  1446. * Tests the uploadedFile proxy method
  1447. *
  1448. * @return void
  1449. */
  1450. public function testUploadedFile()
  1451. {
  1452. $validator = new Validator();
  1453. $this->assertProxyMethod($validator, 'uploadedFile', ['foo' => 'bar'], [['foo' => 'bar']]);
  1454. $this->assertNotEmpty($validator->errors(['username' => []]));
  1455. }
  1456. /**
  1457. * Tests the latlog proxy methods
  1458. *
  1459. * @return void
  1460. */
  1461. public function testLatLong()
  1462. {
  1463. $validator = new Validator();
  1464. $this->assertProxyMethod($validator, 'latLong', null, [], 'geoCoordinate');
  1465. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1466. $this->assertProxyMethod($validator, 'latitude');
  1467. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1468. $this->assertProxyMethod($validator, 'longitude');
  1469. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1470. }
  1471. /**
  1472. * Tests the ascii proxy method
  1473. *
  1474. * @return void
  1475. */
  1476. public function testAscii()
  1477. {
  1478. $validator = new Validator();
  1479. $this->assertProxyMethod($validator, 'ascii');
  1480. $this->assertNotEmpty($validator->errors(['username' => 'ü']));
  1481. }
  1482. /**
  1483. * Tests the utf8 proxy methods
  1484. *
  1485. * @return void
  1486. */
  1487. public function testUtf8()
  1488. {
  1489. // Grinning face
  1490. $extended = 'some' . "\xf0\x9f\x98\x80" . 'value';
  1491. $validator = new Validator();
  1492. $this->assertProxyMethod($validator, 'utf8', null, [['extended' => false]]);
  1493. $this->assertEmpty($validator->errors(['username' => 'ü']));
  1494. $this->assertNotEmpty($validator->errors(['username' => $extended]));
  1495. }
  1496. /**
  1497. * Test utf8extended proxy method.
  1498. *
  1499. * @return void
  1500. */
  1501. public function testUtf8Extended()
  1502. {
  1503. // Grinning face
  1504. $extended = 'some' . "\xf0\x9f\x98\x80" . 'value';
  1505. $validator = new Validator();
  1506. $this->assertProxyMethod($validator, 'utf8Extended', null, [['extended' => true]], 'utf8');
  1507. $this->assertEmpty($validator->errors(['username' => 'ü']));
  1508. $this->assertEmpty($validator->errors(['username' => $extended]));
  1509. }
  1510. /**
  1511. * Tests the email proxy method
  1512. *
  1513. * @return void
  1514. */
  1515. public function testEmail()
  1516. {
  1517. $validator = new Validator();
  1518. $validator->email('username');
  1519. $this->assertEmpty($validator->errors(['username' => 'test@example.com']));
  1520. $this->assertNotEmpty($validator->errors(['username' => 'not an email']));
  1521. }
  1522. /**
  1523. * Tests the integer proxy method
  1524. *
  1525. * @return void
  1526. */
  1527. public function testInteger()
  1528. {
  1529. $validator = new Validator();
  1530. $this->assertProxyMethod($validator, 'integer', null, [], 'isInteger');
  1531. $this->assertNotEmpty($validator->errors(['username' => 'not integer']));
  1532. }
  1533. /**
  1534. * Tests the multiple proxy method
  1535. *
  1536. * @return void
  1537. */
  1538. public function testMultiple()
  1539. {
  1540. $validator = new Validator();
  1541. $this->assertProxyMethod(
  1542. $validator,
  1543. 'multipleOptions',
  1544. ['min' => 1, 'caseInsensitive' => true],
  1545. [['min' => 1], false],
  1546. 'multiple'
  1547. );
  1548. $this->assertNotEmpty($validator->errors(['username' => '']));
  1549. }
  1550. /**
  1551. * Tests the hasAtLeast method
  1552. *
  1553. * @return void
  1554. */
  1555. public function testHasAtLeast()
  1556. {
  1557. $validator = new Validator();
  1558. $validator->hasAtLeast('things', 3);
  1559. $this->assertEmpty($validator->errors(['things' => [1, 2, 3]]));
  1560. $this->assertEmpty($validator->errors(['things' => [1, 2, 3, 4]]));
  1561. $this->assertNotEmpty($validator->errors(['things' => [1, 2]]));
  1562. $this->assertNotEmpty($validator->errors(['things' => []]));
  1563. $this->assertNotEmpty($validator->errors(['things' => 'string']));
  1564. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3]]]));
  1565. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3, 4]]]));
  1566. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => [1, 2]]]));
  1567. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => []]]));
  1568. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => 'string']]));
  1569. }
  1570. /**
  1571. * Tests the hasAtMost method
  1572. *
  1573. * @return void
  1574. */
  1575. public function testHasAtMost()
  1576. {
  1577. $validator = new Validator();
  1578. $validator->hasAtMost('things', 3);
  1579. $this->assertEmpty($validator->errors(['things' => [1, 2, 3]]));
  1580. $this->assertEmpty($validator->errors(['things' => [1]]));
  1581. $this->assertNotEmpty($validator->errors(['things' => [1, 2, 3, 4]]));
  1582. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3]]]));
  1583. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2]]]));
  1584. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3, 4]]]));
  1585. }
  1586. protected function assertProxyMethod($validator, $method, $extra = null, $pass = [], $name = null)
  1587. {
  1588. $name = $name ?: $method;
  1589. if ($extra !== null) {
  1590. $this->assertSame($validator, $validator->{$method}('username', $extra));
  1591. } else {
  1592. $this->assertSame($validator, $validator->{$method}('username'));
  1593. }
  1594. $rule = $validator->field('username')->rule($method);
  1595. $this->assertNotEmpty($rule, "Rule was not found for $method");
  1596. $this->assertNull($rule->get('message'), 'Message is present when it should not be');
  1597. $this->assertNull($rule->get('on'), 'On clause is present when it should not be');
  1598. $this->assertEquals($name, $rule->get('rule'), 'Rule name does not match');
  1599. $this->assertEquals($pass, $rule->get('pass'), 'Passed options are different');
  1600. $this->assertEquals('default', $rule->get('provider'), 'Provider does not match');
  1601. if ($extra !== null) {
  1602. $validator->{$method}('username', $extra, 'the message', 'create');
  1603. } else {
  1604. $validator->{$method}('username', 'the message', 'create');
  1605. }
  1606. $rule = $validator->field('username')->rule($method);
  1607. $this->assertEquals('the message', $rule->get('message'), 'Error messages are not the same');
  1608. $this->assertEquals('create', $rule->get('on'), 'On clause is wrong');
  1609. }
  1610. }