ValidatorTest.php 45 KB

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