ValidatorTest.php 55 KB

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