ValidatorTest.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Validation;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\Validation\Validation;
  18. use Cake\Validation\ValidationRule;
  19. use Cake\Validation\ValidationSet;
  20. use Cake\Validation\Validator;
  21. /**
  22. * Tests Validator class
  23. */
  24. class ValidatorTest extends TestCase
  25. {
  26. /**
  27. * Testing you can dynamically add rules to a field
  28. *
  29. * @return void
  30. */
  31. public function testAddingRulesToField()
  32. {
  33. $validator = new Validator();
  34. $validator->add('title', 'not-blank', ['rule' => 'notBlank']);
  35. $set = $validator->field('title');
  36. $this->assertInstanceOf('Cake\Validation\ValidationSet', $set);
  37. $this->assertCount(1, $set);
  38. $validator->add('title', 'another', ['rule' => 'alphanumeric']);
  39. $this->assertCount(2, $set);
  40. $validator->add('body', 'another', ['rule' => 'crazy']);
  41. $this->assertCount(1, $validator->field('body'));
  42. $this->assertCount(2, $validator);
  43. $validator->add('email', 'notBlank');
  44. $result = $validator->field('email')->rule('notBlank')->get('rule');
  45. $this->assertEquals('notBlank', $result);
  46. $rule = new ValidationRule();
  47. $validator->add('field', 'myrule', $rule);
  48. $result = $validator->field('field')->rule('myrule');
  49. $this->assertSame($rule, $result);
  50. }
  51. /**
  52. * Testing addNested field rules
  53. *
  54. * @return void
  55. */
  56. public function testAddNestedSingle()
  57. {
  58. $validator = new Validator();
  59. $inner = new Validator();
  60. $inner->add('username', 'not-blank', ['rule' => 'notBlank']);
  61. $this->assertSame($validator, $validator->addNested('user', $inner));
  62. $this->assertCount(1, $validator->field('user'));
  63. }
  64. /**
  65. * Testing addNested connects providers
  66. *
  67. * @return void
  68. */
  69. public function testAddNestedSingleProviders()
  70. {
  71. $validator = new Validator();
  72. $validator->setProvider('test', $this);
  73. $inner = new Validator();
  74. $inner->add('username', 'not-blank', ['rule' => function () use ($inner, $validator) {
  75. $this->assertSame($validator->providers(), $inner->providers(), 'Providers should match');
  76. return false;
  77. }]);
  78. $validator->addNested('user', $inner);
  79. $result = $validator->errors(['user' => ['username' => 'example']]);
  80. $this->assertNotEmpty($result, 'Validation should fail');
  81. }
  82. /**
  83. * Testing addNested with extra `$message` and `$when` params
  84. *
  85. * @return void
  86. */
  87. public function testAddNestedWithExtra()
  88. {
  89. $inner = new Validator();
  90. $inner->requirePresence('username');
  91. $validator = new Validator();
  92. $validator->addNested('user', $inner, 'errors found', 'create');
  93. $this->assertCount(1, $validator->field('user'));
  94. $rule = $validator->field('user')->rule(Validator::NESTED);
  95. $this->assertSame('create', $rule->get('on'));
  96. $errors = $validator->errors(['user' => 'string']);
  97. $this->assertArrayHasKey('user', $errors);
  98. $this->assertArrayHasKey(Validator::NESTED, $errors['user']);
  99. $this->assertSame('errors found', $errors['user'][Validator::NESTED]);
  100. $errors = $validator->errors(['user' => ['key' => 'value']]);
  101. $this->assertArrayHasKey('user', $errors);
  102. $this->assertArrayHasKey(Validator::NESTED, $errors['user']);
  103. $this->assertEmpty($validator->errors(['user' => ['key' => 'value']], false));
  104. }
  105. /**
  106. * Testing addNestedMany field rules
  107. *
  108. * @return void
  109. */
  110. public function testAddNestedMany()
  111. {
  112. $validator = new Validator();
  113. $inner = new Validator();
  114. $inner->add('comment', 'not-blank', ['rule' => 'notBlank']);
  115. $this->assertSame($validator, $validator->addNestedMany('comments', $inner));
  116. $this->assertCount(1, $validator->field('comments'));
  117. }
  118. /**
  119. * Testing addNestedMany connects providers
  120. *
  121. * @return void
  122. */
  123. public function testAddNestedManyProviders()
  124. {
  125. $validator = new Validator();
  126. $validator->setProvider('test', $this);
  127. $inner = new Validator();
  128. $inner->add('comment', 'not-blank', ['rule' => function () use ($inner, $validator) {
  129. $this->assertSame($validator->providers(), $inner->providers(), 'Providers should match');
  130. return false;
  131. }]);
  132. $validator->addNestedMany('comments', $inner);
  133. $result = $validator->errors(['comments' => [['comment' => 'example']]]);
  134. $this->assertNotEmpty($result, 'Validation should fail');
  135. }
  136. /**
  137. * Testing addNestedMany with extra `$message` and `$when` params
  138. *
  139. * @return void
  140. */
  141. public function testAddNestedManyWithExtra()
  142. {
  143. $inner = new Validator();
  144. $inner->requirePresence('body');
  145. $validator = new Validator();
  146. $validator->addNestedMany('comments', $inner, 'errors found', 'create');
  147. $this->assertCount(1, $validator->field('comments'));
  148. $rule = $validator->field('comments')->rule(Validator::NESTED);
  149. $this->assertSame('create', $rule->get('on'));
  150. $errors = $validator->errors(['comments' => 'string']);
  151. $this->assertArrayHasKey('comments', $errors);
  152. $this->assertArrayHasKey(Validator::NESTED, $errors['comments']);
  153. $this->assertSame('errors found', $errors['comments'][Validator::NESTED]);
  154. $errors = $validator->errors(['comments' => ['string']]);
  155. $this->assertArrayHasKey('comments', $errors);
  156. $this->assertArrayHasKey(Validator::NESTED, $errors['comments']);
  157. $this->assertSame('errors found', $errors['comments'][Validator::NESTED]);
  158. $errors = $validator->errors(['comments' => [['body' => null]]]);
  159. $this->assertArrayHasKey('comments', $errors);
  160. $this->assertArrayHasKey(Validator::NESTED, $errors['comments']);
  161. $this->assertEmpty($validator->errors(['comments' => [['body' => null]]], false));
  162. }
  163. /**
  164. * Tests that calling field will create a default validation set for it
  165. *
  166. * @return void
  167. */
  168. public function testFieldDefault()
  169. {
  170. $validator = new Validator();
  171. $this->assertFalse($validator->hasField('foo'));
  172. $field = $validator->field('foo');
  173. $this->assertInstanceOf('Cake\Validation\ValidationSet', $field);
  174. $this->assertCount(0, $field);
  175. $this->assertTrue($validator->hasField('foo'));
  176. }
  177. /**
  178. * Tests that field method can be used as a setter
  179. *
  180. * @return void
  181. */
  182. public function testFieldSetter()
  183. {
  184. $validator = new Validator();
  185. $validationSet = new ValidationSet;
  186. $validator->field('thing', $validationSet);
  187. $this->assertSame($validationSet, $validator->field('thing'));
  188. }
  189. /**
  190. * Tests the remove method
  191. *
  192. * @return void
  193. */
  194. public function testRemove()
  195. {
  196. $validator = new Validator();
  197. $validator->add('title', 'not-blank', ['rule' => 'notBlank']);
  198. $validator->add('title', 'foo', ['rule' => 'bar']);
  199. $this->assertCount(2, $validator->field('title'));
  200. $validator->remove('title');
  201. $this->assertCount(0, $validator->field('title'));
  202. $validator->remove('title');
  203. $validator->add('title', 'not-blank', ['rule' => 'notBlank']);
  204. $validator->add('title', 'foo', ['rule' => 'bar']);
  205. $this->assertCount(2, $validator->field('title'));
  206. $validator->remove('title', 'foo');
  207. $this->assertCount(1, $validator->field('title'));
  208. $this->assertNull($validator->field('title')->rule('foo'));
  209. }
  210. /**
  211. * Tests the requirePresence method
  212. *
  213. * @return void
  214. */
  215. public function testRequirePresence()
  216. {
  217. $validator = new Validator();
  218. $this->assertSame($validator, $validator->requirePresence('title'));
  219. $this->assertTrue($validator->field('title')->isPresenceRequired());
  220. $validator->requirePresence('title', false);
  221. $this->assertFalse($validator->field('title')->isPresenceRequired());
  222. $validator->requirePresence('title', 'create');
  223. $this->assertEquals('create', $validator->field('title')->isPresenceRequired());
  224. $validator->requirePresence('title', 'update');
  225. $this->assertEquals('update', $validator->field('title')->isPresenceRequired());
  226. }
  227. /**
  228. * Tests the requirePresence method
  229. *
  230. * @return void
  231. */
  232. public function testRequirePresenceAsArray()
  233. {
  234. $validator = new Validator();
  235. $validator->requirePresence(['title', 'created']);
  236. $this->assertTrue($validator->field('title')->isPresenceRequired());
  237. $this->assertTrue($validator->field('created')->isPresenceRequired());
  238. $validator->requirePresence([
  239. 'title' => [
  240. 'mode' => false
  241. ],
  242. 'content' => [
  243. 'mode' => 'update'
  244. ],
  245. 'subject'
  246. ], true);
  247. $this->assertFalse($validator->field('title')->isPresenceRequired());
  248. $this->assertEquals('update', $validator->field('content')->isPresenceRequired());
  249. $this->assertTrue($validator->field('subject')->isPresenceRequired());
  250. }
  251. /**
  252. * Tests the requirePresence failure case
  253. *
  254. * @return void
  255. */
  256. public function testRequirePresenceAsArrayFailure()
  257. {
  258. $this->expectException(\InvalidArgumentException::class);
  259. $validator = new Validator();
  260. $validator->requirePresence(['title' => 'derp', 'created' => false]);
  261. }
  262. /**
  263. * Tests the requirePresence method when passing a callback
  264. *
  265. * @return void
  266. */
  267. public function testRequirePresenceCallback()
  268. {
  269. $validator = new Validator();
  270. $require = true;
  271. $validator->requirePresence('title', function ($context) use (&$require) {
  272. $this->assertEquals([], $context['data']);
  273. $this->assertEquals([], $context['providers']);
  274. $this->assertEquals('title', $context['field']);
  275. $this->assertTrue($context['newRecord']);
  276. return $require;
  277. });
  278. $this->assertTrue($validator->isPresenceRequired('title', true));
  279. $require = false;
  280. $this->assertFalse($validator->isPresenceRequired('title', true));
  281. }
  282. /**
  283. * Tests the isPresenceRequired method
  284. *
  285. * @return void
  286. */
  287. public function testIsPresenceRequired()
  288. {
  289. $validator = new Validator();
  290. $this->assertSame($validator, $validator->requirePresence('title'));
  291. $this->assertTrue($validator->isPresenceRequired('title', true));
  292. $this->assertTrue($validator->isPresenceRequired('title', false));
  293. $validator->requirePresence('title', false);
  294. $this->assertFalse($validator->isPresenceRequired('title', true));
  295. $this->assertFalse($validator->isPresenceRequired('title', false));
  296. $validator->requirePresence('title', 'create');
  297. $this->assertTrue($validator->isPresenceRequired('title', true));
  298. $this->assertFalse($validator->isPresenceRequired('title', false));
  299. $validator->requirePresence('title', 'update');
  300. $this->assertTrue($validator->isPresenceRequired('title', false));
  301. $this->assertFalse($validator->isPresenceRequired('title', true));
  302. }
  303. /**
  304. * Tests errors generated when a field presence is required
  305. *
  306. * @return void
  307. */
  308. public function testErrorsWithPresenceRequired()
  309. {
  310. $validator = new Validator();
  311. $validator->requirePresence('title');
  312. $errors = $validator->errors(['foo' => 'something']);
  313. $expected = ['title' => ['_required' => 'This field is required']];
  314. $this->assertEquals($expected, $errors);
  315. $this->assertEmpty($validator->errors(['title' => 'bar']));
  316. $validator->requirePresence('title', false);
  317. $this->assertEmpty($validator->errors(['foo' => 'bar']));
  318. }
  319. /**
  320. * Test that validation on a certain condition generate errors
  321. *
  322. * @return void
  323. */
  324. public function testErrorsWithPresenceRequiredOnCreate()
  325. {
  326. $validator = new Validator();
  327. $validator->requirePresence('id', 'update');
  328. $validator->allowEmpty('id', 'create');
  329. $validator->requirePresence('title');
  330. $data = [
  331. 'title' => 'Example title'
  332. ];
  333. $expected = [];
  334. $result = $validator->errors($data);
  335. $this->assertEquals($expected, $result);
  336. }
  337. /**
  338. * Test that errors() can work with nested data.
  339. *
  340. * @return void
  341. */
  342. public function testErrorsWithNestedFields()
  343. {
  344. $validator = new Validator();
  345. $user = new Validator();
  346. $user->add('username', 'letter', ['rule' => 'alphanumeric']);
  347. $comments = new Validator();
  348. $comments->add('comment', 'letter', ['rule' => 'alphanumeric']);
  349. $validator->addNested('user', $user);
  350. $validator->addNestedMany('comments', $comments);
  351. $data = [
  352. 'user' => [
  353. 'username' => 'is wrong'
  354. ],
  355. 'comments' => [
  356. ['comment' => 'is wrong']
  357. ]
  358. ];
  359. $errors = $validator->errors($data);
  360. $expected = [
  361. 'user' => [
  362. 'username' => ['letter' => 'The provided value is invalid']
  363. ],
  364. 'comments' => [
  365. 0 => ['comment' => ['letter' => 'The provided value is invalid']]
  366. ]
  367. ];
  368. $this->assertEquals($expected, $errors);
  369. }
  370. /**
  371. * Test nested fields with many, but invalid data.
  372. *
  373. * @return void
  374. */
  375. public function testErrorsWithNestedSingleInvalidType()
  376. {
  377. $validator = new Validator();
  378. $user = new Validator();
  379. $user->add('user', 'letter', ['rule' => 'alphanumeric']);
  380. $validator->addNested('user', $user);
  381. $data = [
  382. 'user' => 'a string',
  383. ];
  384. $errors = $validator->errors($data);
  385. $expected = [
  386. 'user' => ['_nested' => 'The provided value is invalid'],
  387. ];
  388. $this->assertEquals($expected, $errors);
  389. }
  390. /**
  391. * Test nested fields with many, but invalid data.
  392. *
  393. * @return void
  394. */
  395. public function testErrorsWithNestedManyInvalidType()
  396. {
  397. $validator = new Validator();
  398. $comments = new Validator();
  399. $comments->add('comment', 'letter', ['rule' => 'alphanumeric']);
  400. $validator->addNestedMany('comments', $comments);
  401. $data = [
  402. 'comments' => 'a string',
  403. ];
  404. $errors = $validator->errors($data);
  405. $expected = [
  406. 'comments' => ['_nested' => 'The provided value is invalid'],
  407. ];
  408. $this->assertEquals($expected, $errors);
  409. }
  410. /**
  411. * Test nested fields with many, but invalid data.
  412. *
  413. * @return void
  414. */
  415. public function testErrorsWithNestedManySomeInvalid()
  416. {
  417. $validator = new Validator();
  418. $comments = new Validator();
  419. $comments->add('comment', 'letter', ['rule' => 'alphanumeric']);
  420. $validator->addNestedMany('comments', $comments);
  421. $data = [
  422. 'comments' => [
  423. 'a string',
  424. ['comment' => 'letters'],
  425. ['comment' => 'more invalid']
  426. ]
  427. ];
  428. $errors = $validator->errors($data);
  429. $expected = [
  430. 'comments' => [
  431. '_nested' => 'The provided value is invalid',
  432. ],
  433. ];
  434. $this->assertEquals($expected, $errors);
  435. }
  436. /**
  437. * Tests custom error messages generated when a field presence is required
  438. *
  439. * @return void
  440. */
  441. public function testCustomErrorsWithPresenceRequired()
  442. {
  443. $validator = new Validator();
  444. $validator->requirePresence('title', true, 'Custom message');
  445. $errors = $validator->errors(['foo' => 'something']);
  446. $expected = ['title' => ['_required' => 'Custom message']];
  447. $this->assertEquals($expected, $errors);
  448. }
  449. /**
  450. * Tests custom error messages generated when a field presence is required
  451. *
  452. * @return void
  453. */
  454. public function testCustomErrorsWithPresenceRequiredAsArray()
  455. {
  456. $validator = new Validator();
  457. $validator->requirePresence(['title', 'content'], true, 'Custom message');
  458. $errors = $validator->errors(['foo' => 'something']);
  459. $expected = [
  460. 'title' => ['_required' => 'Custom message'],
  461. 'content' => ['_required' => 'Custom message']
  462. ];
  463. $this->assertEquals($expected, $errors);
  464. $validator->requirePresence([
  465. 'title' => [
  466. 'message' => 'Test message'
  467. ],
  468. 'content'
  469. ], true, 'Custom message');
  470. $errors = $validator->errors(['foo' => 'something']);
  471. $expected = [
  472. 'title' => ['_required' => 'Test message'],
  473. 'content' => ['_required' => 'Custom message']
  474. ];
  475. $this->assertEquals($expected, $errors);
  476. }
  477. /**
  478. * Tests the allowEmpty method
  479. *
  480. * @return void
  481. */
  482. public function testAllowEmpty()
  483. {
  484. $validator = new Validator();
  485. $this->assertSame($validator, $validator->allowEmpty('title'));
  486. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  487. $validator->allowEmpty('title', 'create');
  488. $this->assertEquals('create', $validator->field('title')->isEmptyAllowed());
  489. $validator->allowEmpty('title', 'update');
  490. $this->assertEquals('update', $validator->field('title')->isEmptyAllowed());
  491. }
  492. /**
  493. * Tests the allowEmpty method with date/time fields.
  494. *
  495. * @return void
  496. */
  497. public function testAllowEmptyDateTime()
  498. {
  499. $validator = new Validator();
  500. $validator->allowEmpty('created')
  501. ->add('created', 'date', ['rule' => 'date']);
  502. $data = [
  503. 'created' => [
  504. 'year' => '',
  505. 'month' => '',
  506. 'day' => ''
  507. ]
  508. ];
  509. $result = $validator->errors($data);
  510. $this->assertEmpty($result, 'No errors on empty date');
  511. $data = [
  512. 'created' => [
  513. 'year' => '',
  514. 'month' => '',
  515. 'day' => '',
  516. 'hour' => '',
  517. 'minute' => '',
  518. 'second' => '',
  519. 'meridian' => '',
  520. ]
  521. ];
  522. $result = $validator->errors($data);
  523. $this->assertEmpty($result, 'No errors on empty datetime');
  524. $data = [
  525. 'created' => [
  526. 'hour' => '',
  527. 'minute' => '',
  528. 'meridian' => '',
  529. ]
  530. ];
  531. $result = $validator->errors($data);
  532. $this->assertEmpty($result, 'No errors on empty time');
  533. }
  534. /**
  535. * Tests the allowEmpty method with file fields.
  536. *
  537. * @return void
  538. */
  539. public function testAllowEmptyFileFields()
  540. {
  541. $validator = new Validator();
  542. $validator->allowEmpty('picture')
  543. ->add('picture', 'file', ['rule' => 'uploadedFile']);
  544. $data = [
  545. 'picture' => [
  546. 'name' => '',
  547. 'type' => '',
  548. 'tmp_name' => '',
  549. 'error' => UPLOAD_ERR_NO_FILE,
  550. ]
  551. ];
  552. $result = $validator->errors($data);
  553. $this->assertEmpty($result, 'No errors on empty date');
  554. $data = [
  555. 'picture' => [
  556. 'name' => 'fake.png',
  557. 'type' => '',
  558. 'tmp_name' => '',
  559. 'error' => UPLOAD_ERR_OK,
  560. ]
  561. ];
  562. $result = $validator->errors($data);
  563. $this->assertNotEmpty($result, 'Invalid file should be caught still.');
  564. }
  565. /**
  566. * Tests the allowEmpty as array method
  567. *
  568. * @return void
  569. */
  570. public function testAllowEmptyAsArray()
  571. {
  572. $validator = new Validator();
  573. $validator->allowEmpty([
  574. 'title',
  575. 'subject',
  576. 'posted_at' => [
  577. 'when' => false,
  578. 'message' => 'Post time cannot be empty'
  579. ],
  580. 'updated_at' => [
  581. 'when' => true
  582. ],
  583. 'show_at' => [
  584. 'when' => 'update'
  585. ]
  586. ], 'create', 'Cannot be empty');
  587. $this->assertEquals('create', $validator->field('title')->isEmptyAllowed());
  588. $this->assertEquals('create', $validator->field('subject')->isEmptyAllowed());
  589. $this->assertFalse($validator->field('posted_at')->isEmptyAllowed());
  590. $this->assertTrue($validator->field('updated_at')->isEmptyAllowed());
  591. $this->assertEquals('update', $validator->field('show_at')->isEmptyAllowed());
  592. $errors = $validator->errors([
  593. 'title' => '',
  594. 'subject' => null,
  595. 'posted_at' => null,
  596. 'updated_at' => null,
  597. 'show_at' => ''
  598. ], false);
  599. $expected = [
  600. 'title' => ['_empty' => 'Cannot be empty'],
  601. 'subject' => ['_empty' => 'Cannot be empty'],
  602. 'posted_at' => ['_empty' => 'Post time cannot be empty']
  603. ];
  604. $this->assertEquals($expected, $errors);
  605. }
  606. /**
  607. * Tests the allowEmpty failure case
  608. *
  609. * @return void
  610. */
  611. public function testAllowEmptyAsArrayFailure()
  612. {
  613. $this->expectException(\InvalidArgumentException::class);
  614. $validator = new Validator();
  615. $validator->allowEmpty(['title' => 'derp', 'created' => false]);
  616. }
  617. /**
  618. * Test the notEmpty() method.
  619. *
  620. * @return void
  621. */
  622. public function testNotEmpty()
  623. {
  624. $validator = new Validator();
  625. $validator->notEmpty('title');
  626. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  627. $validator->allowEmpty('title');
  628. $this->assertTrue($validator->field('title')->isEmptyAllowed());
  629. }
  630. /**
  631. * Tests the notEmpty as array method
  632. *
  633. * @return void
  634. */
  635. public function testNotEmptyAsArray()
  636. {
  637. $validator = new Validator();
  638. $validator->notEmpty(['title', 'created']);
  639. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  640. $this->assertFalse($validator->field('created')->isEmptyAllowed());
  641. $validator->notEmpty([
  642. 'title' => [
  643. 'when' => false
  644. ],
  645. 'content' => [
  646. 'when' => 'update'
  647. ],
  648. 'posted_at' => [
  649. 'when' => 'create'
  650. ],
  651. 'show_at' => [
  652. 'message' => 'Show date cannot be empty',
  653. 'when' => false
  654. ],
  655. 'subject'
  656. ], 'Not empty', true);
  657. $this->assertFalse($validator->field('title')->isEmptyAllowed());
  658. $this->assertTrue($validator->isEmptyAllowed('content', true));
  659. $this->assertFalse($validator->isEmptyAllowed('content', false));
  660. $this->assertFalse($validator->isEmptyAllowed('posted_at', true));
  661. $this->assertTrue($validator->isEmptyAllowed('posted_at', false));
  662. $this->assertTrue($validator->field('subject')->isEmptyAllowed());
  663. $errors = $validator->errors([
  664. 'title' => '',
  665. 'content' => '',
  666. 'posted_at' => null,
  667. 'show_at' => null,
  668. 'subject' => ''
  669. ], false);
  670. $expected = [
  671. 'title' => ['_empty' => 'Not empty'],
  672. 'content' => ['_empty' => 'Not empty'],
  673. 'show_at' => ['_empty' => 'Show date cannot be empty']
  674. ];
  675. $this->assertEquals($expected, $errors);
  676. }
  677. /**
  678. * Tests the notEmpty failure case
  679. *
  680. * @return void
  681. */
  682. public function testNotEmptyAsArrayFailure()
  683. {
  684. $this->expectException(\InvalidArgumentException::class);
  685. $validator = new Validator();
  686. $validator->notEmpty(['title' => 'derp', 'created' => false]);
  687. }
  688. /**
  689. * Test the notEmpty() method.
  690. *
  691. * @return void
  692. */
  693. public function testNotEmptyModes()
  694. {
  695. $validator = new Validator();
  696. $validator->notEmpty('title', 'Need a title', 'create');
  697. $this->assertFalse($validator->isEmptyAllowed('title', true));
  698. $this->assertTrue($validator->isEmptyAllowed('title', false));
  699. $validator->notEmpty('title', 'Need a title', 'update');
  700. $this->assertTrue($validator->isEmptyAllowed('title', true));
  701. $this->assertFalse($validator->isEmptyAllowed('title', false));
  702. $validator->notEmpty('title', 'Need a title');
  703. $this->assertFalse($validator->isEmptyAllowed('title', true));
  704. $this->assertFalse($validator->isEmptyAllowed('title', false));
  705. $validator->notEmpty('title');
  706. $this->assertFalse($validator->isEmptyAllowed('title', true));
  707. $this->assertFalse($validator->isEmptyAllowed('title', false));
  708. }
  709. /**
  710. * Test interactions between notEmpty() and isAllowed().
  711. *
  712. * @return void
  713. */
  714. public function testNotEmptyAndIsAllowed()
  715. {
  716. $validator = new Validator();
  717. $validator->allowEmpty('title')
  718. ->notEmpty('title', 'Need it', 'update');
  719. $this->assertTrue($validator->isEmptyAllowed('title', true));
  720. $this->assertFalse($validator->isEmptyAllowed('title', false));
  721. $validator->allowEmpty('title')
  722. ->notEmpty('title');
  723. $this->assertFalse($validator->isEmptyAllowed('title', true));
  724. $this->assertFalse($validator->isEmptyAllowed('title', false));
  725. $validator->notEmpty('title')
  726. ->allowEmpty('title', 'create');
  727. $this->assertTrue($validator->isEmptyAllowed('title', true));
  728. $this->assertFalse($validator->isEmptyAllowed('title', false));
  729. }
  730. /**
  731. * Tests the allowEmpty method when passing a callback
  732. *
  733. * @return void
  734. */
  735. public function testAllowEmptyCallback()
  736. {
  737. $validator = new Validator();
  738. $allow = true;
  739. $validator->allowEmpty('title', function ($context) use (&$allow) {
  740. $this->assertEquals([], $context['data']);
  741. $this->assertEquals([], $context['providers']);
  742. $this->assertTrue($context['newRecord']);
  743. return $allow;
  744. });
  745. $this->assertTrue($validator->isEmptyAllowed('title', true));
  746. $allow = false;
  747. $this->assertFalse($validator->isEmptyAllowed('title', true));
  748. }
  749. /**
  750. * Tests the notEmpty method when passing a callback
  751. *
  752. * @return void
  753. */
  754. public function testNotEmptyCallback()
  755. {
  756. $validator = new Validator();
  757. $prevent = true;
  758. $validator->notEmpty('title', 'error message', function ($context) use (&$prevent) {
  759. $this->assertEquals([], $context['data']);
  760. $this->assertEquals([], $context['providers']);
  761. $this->assertFalse($context['newRecord']);
  762. return $prevent;
  763. });
  764. $this->assertFalse($validator->isEmptyAllowed('title', false));
  765. $prevent = false;
  766. $this->assertTrue($validator->isEmptyAllowed('title', false));
  767. }
  768. /**
  769. * Tests the isEmptyAllowed method
  770. *
  771. * @return void
  772. */
  773. public function testIsEmptyAllowed()
  774. {
  775. $validator = new Validator();
  776. $this->assertSame($validator, $validator->allowEmpty('title'));
  777. $this->assertTrue($validator->isEmptyAllowed('title', true));
  778. $this->assertTrue($validator->isEmptyAllowed('title', false));
  779. $validator->notEmpty('title');
  780. $this->assertFalse($validator->isEmptyAllowed('title', true));
  781. $this->assertFalse($validator->isEmptyAllowed('title', false));
  782. $validator->allowEmpty('title', 'create');
  783. $this->assertTrue($validator->isEmptyAllowed('title', true));
  784. $this->assertFalse($validator->isEmptyAllowed('title', false));
  785. $validator->allowEmpty('title', 'update');
  786. $this->assertTrue($validator->isEmptyAllowed('title', false));
  787. $this->assertFalse($validator->isEmptyAllowed('title', true));
  788. }
  789. /**
  790. * Tests errors generated when a field is not allowed to be empty
  791. *
  792. * @return void
  793. */
  794. public function testErrorsWithEmptyNotAllowed()
  795. {
  796. $validator = new Validator();
  797. $validator->notEmpty('title');
  798. $errors = $validator->errors(['title' => '']);
  799. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  800. $this->assertEquals($expected, $errors);
  801. $errors = $validator->errors(['title' => []]);
  802. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  803. $this->assertEquals($expected, $errors);
  804. $errors = $validator->errors(['title' => null]);
  805. $expected = ['title' => ['_empty' => 'This field cannot be left empty']];
  806. $this->assertEquals($expected, $errors);
  807. $errors = $validator->errors(['title' => 0]);
  808. $this->assertEmpty($errors);
  809. $errors = $validator->errors(['title' => '0']);
  810. $this->assertEmpty($errors);
  811. $errors = $validator->errors(['title' => false]);
  812. $this->assertEmpty($errors);
  813. }
  814. /**
  815. * Tests custom error messages generated when a field is allowed to be empty
  816. *
  817. * @return void
  818. */
  819. public function testCustomErrorsWithAllowedEmpty()
  820. {
  821. $validator = new Validator();
  822. $validator->allowEmpty('title', false, 'Custom message');
  823. $errors = $validator->errors(['title' => null]);
  824. $expected = ['title' => ['_empty' => 'Custom message']];
  825. $this->assertEquals($expected, $errors);
  826. }
  827. /**
  828. * Tests custom error messages generated when a field is not allowed to be empty
  829. *
  830. * @return void
  831. */
  832. public function testCustomErrorsWithEmptyNotAllowed()
  833. {
  834. $validator = new Validator();
  835. $validator->notEmpty('title', 'Custom message');
  836. $errors = $validator->errors(['title' => '']);
  837. $expected = ['title' => ['_empty' => 'Custom message']];
  838. $this->assertEquals($expected, $errors);
  839. }
  840. /**
  841. * Tests errors generated when a field is allowed to be empty
  842. *
  843. * @return void
  844. */
  845. public function testErrorsWithEmptyAllowed()
  846. {
  847. $validator = new Validator();
  848. $validator->allowEmpty('title');
  849. $errors = $validator->errors(['title' => '']);
  850. $this->assertEmpty($errors);
  851. $errors = $validator->errors(['title' => []]);
  852. $this->assertEmpty($errors);
  853. $errors = $validator->errors(['title' => null]);
  854. $this->assertEmpty($errors);
  855. $errors = $validator->errors(['title' => 0]);
  856. $this->assertEmpty($errors);
  857. $errors = $validator->errors(['title' => 0.0]);
  858. $this->assertEmpty($errors);
  859. $errors = $validator->errors(['title' => '0']);
  860. $this->assertEmpty($errors);
  861. $errors = $validator->errors(['title' => false]);
  862. $this->assertEmpty($errors);
  863. }
  864. /**
  865. * Test the provider() method
  866. *
  867. * @return void
  868. */
  869. public function testProvider()
  870. {
  871. $validator = new Validator();
  872. $object = new \stdClass;
  873. $this->assertSame($validator, $validator->setProvider('foo', $object));
  874. $this->assertSame($object, $validator->getProvider('foo'));
  875. $this->assertNull($validator->getProvider('bar'));
  876. $another = new \stdClass;
  877. $this->assertSame($validator, $validator->setProvider('bar', $another));
  878. $this->assertSame($another, $validator->getProvider('bar'));
  879. $this->assertEquals(new \Cake\Validation\RulesProvider, $validator->getProvider('default'));
  880. }
  881. /**
  882. * Tests errors() method when using validators from the default provider, this proves
  883. * that it returns a default validation message and the custom one set in the rule
  884. *
  885. * @return void
  886. */
  887. public function testErrorsFromDefaultProvider()
  888. {
  889. $validator = new Validator();
  890. $validator
  891. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  892. ->add('email', 'notBlank', ['rule' => 'notBlank'])
  893. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  894. $errors = $validator->errors(['email' => 'not an email!']);
  895. $expected = [
  896. 'email' => [
  897. 'alpha' => 'The provided value is invalid',
  898. 'email' => 'Y u no write email?'
  899. ]
  900. ];
  901. $this->assertEquals($expected, $errors);
  902. }
  903. /**
  904. * Tests using validation methods from different providers and returning the error
  905. * as a string
  906. *
  907. * @return void
  908. */
  909. public function testErrorsFromCustomProvider()
  910. {
  911. $validator = new Validator();
  912. $validator
  913. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  914. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  915. $thing = $this->getMockBuilder('\stdClass')
  916. ->setMethods(['isCool'])
  917. ->getMock();
  918. $thing->expects($this->once())->method('isCool')
  919. ->will($this->returnCallback(function ($data, $context) use ($thing) {
  920. $this->assertEquals('bar', $data);
  921. $expected = [
  922. 'default' => new \Cake\Validation\RulesProvider,
  923. 'thing' => $thing
  924. ];
  925. $expected = [
  926. 'newRecord' => true,
  927. 'providers' => $expected,
  928. 'data' => [
  929. 'email' => '!',
  930. 'title' => 'bar'
  931. ],
  932. 'field' => 'title'
  933. ];
  934. $this->assertEquals($expected, $context);
  935. return "That ain't cool, yo";
  936. }));
  937. $validator->setProvider('thing', $thing);
  938. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  939. $expected = [
  940. 'email' => ['alpha' => 'The provided value is invalid'],
  941. 'title' => ['cool' => "That ain't cool, yo"]
  942. ];
  943. $this->assertEquals($expected, $errors);
  944. }
  945. /**
  946. * Tests that it is possible to pass extra arguments to the validation function
  947. * and it still gets the providers as last argument
  948. *
  949. * @return void
  950. */
  951. public function testMethodsWithExtraArguments()
  952. {
  953. $validator = new Validator();
  954. $validator->add('title', 'cool', [
  955. 'rule' => ['isCool', 'and', 'awesome'],
  956. 'provider' => 'thing'
  957. ]);
  958. $thing = $this->getMockBuilder('\stdClass')
  959. ->setMethods(['isCool'])
  960. ->getMock();
  961. $thing->expects($this->once())->method('isCool')
  962. ->will($this->returnCallback(function ($data, $a, $b, $context) use ($thing) {
  963. $this->assertEquals('bar', $data);
  964. $this->assertEquals('and', $a);
  965. $this->assertEquals('awesome', $b);
  966. $expected = [
  967. 'default' => new \Cake\Validation\RulesProvider,
  968. 'thing' => $thing
  969. ];
  970. $expected = [
  971. 'newRecord' => true,
  972. 'providers' => $expected,
  973. 'data' => [
  974. 'email' => '!',
  975. 'title' => 'bar'
  976. ],
  977. 'field' => 'title'
  978. ];
  979. $this->assertEquals($expected, $context);
  980. return "That ain't cool, yo";
  981. }));
  982. $validator->setProvider('thing', $thing);
  983. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  984. $expected = [
  985. 'title' => ['cool' => "That ain't cool, yo"]
  986. ];
  987. $this->assertEquals($expected, $errors);
  988. }
  989. /**
  990. * Tests that it is possible to use a closure as a rule
  991. *
  992. * @return void
  993. */
  994. public function testUsingClosureAsRule()
  995. {
  996. $validator = new Validator();
  997. $validator->add('name', 'myRule', [
  998. 'rule' => function ($data, $provider) {
  999. $this->assertEquals('foo', $data);
  1000. return 'You fail';
  1001. }
  1002. ]);
  1003. $expected = ['name' => ['myRule' => 'You fail']];
  1004. $this->assertEquals($expected, $validator->errors(['name' => 'foo']));
  1005. }
  1006. /**
  1007. * Tests that setting last to a rule will stop validating the rest of the rules
  1008. *
  1009. * @return void
  1010. */
  1011. public function testErrorsWithLastRule()
  1012. {
  1013. $validator = new Validator();
  1014. $validator
  1015. ->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
  1016. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  1017. $errors = $validator->errors(['email' => 'not an email!']);
  1018. $expected = [
  1019. 'email' => [
  1020. 'alpha' => 'The provided value is invalid'
  1021. ]
  1022. ];
  1023. $this->assertEquals($expected, $errors);
  1024. }
  1025. /**
  1026. * Tests it is possible to get validation sets for a field using an array interface
  1027. *
  1028. * @return void
  1029. */
  1030. public function testArrayAccessGet()
  1031. {
  1032. $validator = new Validator();
  1033. $validator
  1034. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  1035. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  1036. $this->assertSame($validator['email'], $validator->field('email'));
  1037. $this->assertSame($validator['title'], $validator->field('title'));
  1038. }
  1039. /**
  1040. * Tests it is possible to check for validation sets for a field using an array interface
  1041. *
  1042. * @return void
  1043. */
  1044. public function testArrayAccessExists()
  1045. {
  1046. $validator = new Validator();
  1047. $validator
  1048. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  1049. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  1050. $this->assertArrayHasKey('email', $validator);
  1051. $this->assertArrayHasKey('title', $validator);
  1052. $this->assertArrayNotHasKey('foo', $validator);
  1053. }
  1054. /**
  1055. * Tests it is possible to set validation rules for a field using an array interface
  1056. *
  1057. * @return void
  1058. */
  1059. public function testArrayAccessSet()
  1060. {
  1061. $validator = new Validator();
  1062. $validator
  1063. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  1064. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  1065. $validator['name'] = $validator->field('title');
  1066. $this->assertSame($validator->field('title'), $validator->field('name'));
  1067. $validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
  1068. $this->assertEquals($validator->field('email'), $validator->field('email'));
  1069. }
  1070. /**
  1071. * Tests it is possible to unset validation rules
  1072. *
  1073. * @return void
  1074. */
  1075. public function testArrayAccessUnset()
  1076. {
  1077. $validator = new Validator();
  1078. $validator
  1079. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  1080. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  1081. $this->assertArrayHasKey('title', $validator);
  1082. unset($validator['title']);
  1083. $this->assertArrayNotHasKey('title', $validator);
  1084. }
  1085. /**
  1086. * Tests the countable interface
  1087. *
  1088. * @return void
  1089. */
  1090. public function testCount()
  1091. {
  1092. $validator = new Validator();
  1093. $validator
  1094. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  1095. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  1096. $this->assertCount(2, $validator);
  1097. }
  1098. /**
  1099. * Tests adding rules via alternative syntax
  1100. *
  1101. * @return void
  1102. */
  1103. public function testAddMultiple()
  1104. {
  1105. $validator = new Validator();
  1106. $validator->add('title', [
  1107. 'notBlank' => [
  1108. 'rule' => 'notBlank'
  1109. ],
  1110. 'length' => [
  1111. 'rule' => ['minLength', 10],
  1112. 'message' => 'Titles need to be at least 10 characters long'
  1113. ]
  1114. ]);
  1115. $set = $validator->field('title');
  1116. $this->assertInstanceOf('Cake\Validation\ValidationSet', $set);
  1117. $this->assertCount(2, $set);
  1118. }
  1119. /**
  1120. * Integration test for compareWith validator.
  1121. *
  1122. * @return void
  1123. */
  1124. public function testCompareWithIntegration()
  1125. {
  1126. $validator = new Validator();
  1127. $validator->add('password', [
  1128. 'compare' => [
  1129. 'rule' => ['compareWith', 'password_compare']
  1130. ],
  1131. ]);
  1132. $data = [
  1133. 'password' => 'test',
  1134. 'password_compare' => 'not the same'
  1135. ];
  1136. $this->assertNotEmpty($validator->errors($data), 'Validation should fail.');
  1137. }
  1138. /**
  1139. * Test debugInfo helper method.
  1140. *
  1141. * @return void
  1142. */
  1143. public function testDebugInfo()
  1144. {
  1145. $validator = new Validator();
  1146. $validator->setProvider('test', $this);
  1147. $validator->add('title', 'not-empty', ['rule' => 'notBlank']);
  1148. $validator->requirePresence('body');
  1149. $validator->allowEmpty('published');
  1150. $result = $validator->__debugInfo();
  1151. $expected = [
  1152. '_providers' => ['test'],
  1153. '_fields' => [
  1154. 'title' => [
  1155. 'isPresenceRequired' => false,
  1156. 'isEmptyAllowed' => false,
  1157. 'rules' => ['not-empty'],
  1158. ],
  1159. 'body' => [
  1160. 'isPresenceRequired' => true,
  1161. 'isEmptyAllowed' => false,
  1162. 'rules' => [],
  1163. ],
  1164. 'published' => [
  1165. 'isPresenceRequired' => false,
  1166. 'isEmptyAllowed' => true,
  1167. 'rules' => [],
  1168. ],
  1169. ],
  1170. '_presenceMessages' => [],
  1171. '_allowEmptyMessages' => [],
  1172. '_useI18n' => true,
  1173. ];
  1174. $this->assertEquals($expected, $result);
  1175. }
  1176. /**
  1177. * Tests that the 'create' and 'update' modes are preserved when using
  1178. * nested validators
  1179. *
  1180. * @return void
  1181. */
  1182. public function testNestedValidatorCreate()
  1183. {
  1184. $validator = new Validator();
  1185. $inner = new Validator();
  1186. $inner->add('username', 'email', ['rule' => 'email', 'on' => 'create']);
  1187. $validator->addNested('user', $inner);
  1188. $this->assertNotEmpty($validator->errors(['user' => ['username' => 'example']], true));
  1189. $this->assertEmpty($validator->errors(['user' => ['username' => 'a']], false));
  1190. }
  1191. /**
  1192. * Tests that the 'create' and 'update' modes are preserved when using
  1193. * nested validators
  1194. *
  1195. * @return void
  1196. */
  1197. public function testNestedManyValidatorCreate()
  1198. {
  1199. $validator = new Validator();
  1200. $inner = new Validator();
  1201. $inner->add('username', 'email', ['rule' => 'email', 'on' => 'create']);
  1202. $validator->addNestedMany('user', $inner);
  1203. $this->assertNotEmpty($validator->errors(['user' => [['username' => 'example']]], true));
  1204. $this->assertEmpty($validator->errors(['user' => [['username' => 'a']]], false));
  1205. }
  1206. /**
  1207. * Tests the notBlank proxy method
  1208. *
  1209. * @return void
  1210. */
  1211. public function testNotBlank()
  1212. {
  1213. $validator = new Validator();
  1214. $this->assertProxyMethod($validator, 'notBlank');
  1215. $this->assertNotEmpty($validator->errors(['username' => ' ']));
  1216. }
  1217. /**
  1218. * Tests the alphanumeric proxy method
  1219. *
  1220. * @return void
  1221. */
  1222. public function testAlphanumeric()
  1223. {
  1224. $validator = new Validator();
  1225. $this->assertProxyMethod($validator, 'alphaNumeric');
  1226. $this->assertNotEmpty($validator->errors(['username' => '$']));
  1227. }
  1228. /**
  1229. * Tests the lengthBetween proxy method
  1230. *
  1231. * @return void
  1232. */
  1233. public function testLengthBetween()
  1234. {
  1235. $validator = new Validator();
  1236. $this->assertProxyMethod($validator, 'lengthBetween', [5, 7], [5, 7]);
  1237. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1238. }
  1239. /**
  1240. * Tests the lengthBetween proxy method
  1241. *
  1242. * @return void
  1243. */
  1244. public function testLengthBetweenFailure()
  1245. {
  1246. $this->expectException(\InvalidArgumentException::class);
  1247. $validator = new Validator();
  1248. $validator->lengthBetween('username', [7]);
  1249. }
  1250. /**
  1251. * Tests the creditCard proxy method
  1252. *
  1253. * @return void
  1254. */
  1255. public function testCreditCard()
  1256. {
  1257. $validator = new Validator();
  1258. $this->assertProxyMethod($validator, 'creditCard', 'all', ['all', true], 'cc');
  1259. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1260. }
  1261. /**
  1262. * Tests the greaterThan proxy method
  1263. *
  1264. * @return void
  1265. */
  1266. public function testGreaterThan()
  1267. {
  1268. $validator = new Validator();
  1269. $this->assertProxyMethod($validator, 'greaterThan', 5, [Validation::COMPARE_GREATER, 5], 'comparison');
  1270. $this->assertNotEmpty($validator->errors(['username' => 2]));
  1271. }
  1272. /**
  1273. * Tests the greaterThanOrEqual proxy method
  1274. *
  1275. * @return void
  1276. */
  1277. public function testGreaterThanOrEqual()
  1278. {
  1279. $validator = new Validator();
  1280. $this->assertProxyMethod($validator, 'greaterThanOrEqual', 5, [Validation::COMPARE_GREATER_OR_EQUAL, 5], 'comparison');
  1281. $this->assertNotEmpty($validator->errors(['username' => 2]));
  1282. }
  1283. /**
  1284. * Tests the lessThan proxy method
  1285. *
  1286. * @return void
  1287. */
  1288. public function testLessThan()
  1289. {
  1290. $validator = new Validator();
  1291. $this->assertProxyMethod($validator, 'lessThan', 5, [Validation::COMPARE_LESS, 5], 'comparison');
  1292. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1293. }
  1294. /**
  1295. * Tests the lessThanOrEqual proxy method
  1296. *
  1297. * @return void
  1298. */
  1299. public function testLessThanOrEqual()
  1300. {
  1301. $validator = new Validator();
  1302. $this->assertProxyMethod($validator, 'lessThanOrEqual', 5, [Validation::COMPARE_LESS_OR_EQUAL, 5], 'comparison');
  1303. $this->assertNotEmpty($validator->errors(['username' => 6]));
  1304. }
  1305. /**
  1306. * Tests the equals proxy method
  1307. *
  1308. * @return void
  1309. */
  1310. public function testEquals()
  1311. {
  1312. $validator = new Validator();
  1313. $this->assertProxyMethod($validator, 'equals', 5, [Validation::COMPARE_EQUAL, 5], 'comparison');
  1314. $this->assertEmpty($validator->errors(['username' => 5]));
  1315. $this->assertNotEmpty($validator->errors(['username' => 6]));
  1316. }
  1317. /**
  1318. * Tests the notEquals proxy method
  1319. *
  1320. * @return void
  1321. */
  1322. public function testNotEquals()
  1323. {
  1324. $validator = new Validator();
  1325. $this->assertProxyMethod($validator, 'notEquals', 5, [Validation::COMPARE_NOT_EQUAL, 5], 'comparison');
  1326. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1327. }
  1328. /**
  1329. * Tests the sameAs proxy method
  1330. *
  1331. * @return void
  1332. */
  1333. public function testSameAs()
  1334. {
  1335. $validator = new Validator();
  1336. $this->assertProxyMethod($validator, 'sameAs', 'other', ['other', Validation::COMPARE_SAME], 'compareFields');
  1337. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1338. $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => '1']));
  1339. }
  1340. /**
  1341. * Tests the notSameAs proxy method
  1342. *
  1343. * @return void
  1344. */
  1345. public function testNotSameAs()
  1346. {
  1347. $validator = new Validator();
  1348. $this->assertProxyMethod($validator, 'notSameAs', 'other', ['other', Validation::COMPARE_NOT_SAME], 'compareFields');
  1349. $this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'foo']));
  1350. }
  1351. /**
  1352. * Tests the equalToField proxy method
  1353. *
  1354. * @return void
  1355. */
  1356. public function testEqualToField()
  1357. {
  1358. $validator = new Validator();
  1359. $this->assertProxyMethod($validator, 'equalToField', 'other', ['other', Validation::COMPARE_EQUAL], 'compareFields');
  1360. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1361. $this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'bar']));
  1362. }
  1363. /**
  1364. * Tests the notEqualToField proxy method
  1365. *
  1366. * @return void
  1367. */
  1368. public function testNotEqualToField()
  1369. {
  1370. $validator = new Validator();
  1371. $this->assertProxyMethod($validator, 'notEqualToField', 'other', ['other', Validation::COMPARE_NOT_EQUAL], 'compareFields');
  1372. $this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'foo']));
  1373. }
  1374. /**
  1375. * Tests the greaterThanField proxy method
  1376. *
  1377. * @return void
  1378. */
  1379. public function testGreaterThanField()
  1380. {
  1381. $validator = new Validator();
  1382. $this->assertProxyMethod($validator, 'greaterThanField', 'other', ['other', Validation::COMPARE_GREATER], 'compareFields');
  1383. $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 1]));
  1384. $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 2]));
  1385. }
  1386. /**
  1387. * Tests the greaterThanOrEqualToField proxy method
  1388. *
  1389. * @return void
  1390. */
  1391. public function testGreaterThanOrEqualToField()
  1392. {
  1393. $validator = new Validator();
  1394. $this->assertProxyMethod($validator, 'greaterThanOrEqualToField', 'other', ['other', Validation::COMPARE_GREATER_OR_EQUAL], 'compareFields');
  1395. $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 2]));
  1396. }
  1397. /**
  1398. * Tests the lessThanField proxy method
  1399. *
  1400. * @return void
  1401. */
  1402. public function testLessThanField()
  1403. {
  1404. $validator = new Validator();
  1405. $this->assertProxyMethod($validator, 'lessThanField', 'other', ['other', Validation::COMPARE_LESS], 'compareFields');
  1406. $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 1]));
  1407. $this->assertNotEmpty($validator->errors(['username' => 2, 'other' => 1]));
  1408. }
  1409. /**
  1410. * Tests the lessThanOrEqualToField proxy method
  1411. *
  1412. * @return void
  1413. */
  1414. public function testLessThanOrEqualToField()
  1415. {
  1416. $validator = new Validator();
  1417. $this->assertProxyMethod($validator, 'lessThanOrEqualToField', 'other', ['other', Validation::COMPARE_LESS_OR_EQUAL], 'compareFields');
  1418. $this->assertNotEmpty($validator->errors(['username' => 2, 'other' => 1]));
  1419. }
  1420. /**
  1421. * Tests the containsNonAlphaNumeric proxy method
  1422. *
  1423. * @return void
  1424. */
  1425. public function testContainsNonAlphaNumeric()
  1426. {
  1427. $validator = new Validator();
  1428. $this->assertProxyMethod($validator, 'containsNonAlphaNumeric', 2, [2]);
  1429. $this->assertNotEmpty($validator->errors(['username' => '$']));
  1430. }
  1431. /**
  1432. * Tests the date proxy method
  1433. *
  1434. * @return void
  1435. */
  1436. public function testDate()
  1437. {
  1438. $validator = new Validator();
  1439. $this->assertProxyMethod($validator, 'date', ['ymd'], [['ymd']]);
  1440. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1441. }
  1442. /**
  1443. * Tests the dateTime proxy method
  1444. *
  1445. * @return void
  1446. */
  1447. public function testDateTime()
  1448. {
  1449. $validator = new Validator();
  1450. $this->assertProxyMethod($validator, 'dateTime', ['ymd'], [['ymd']], 'datetime');
  1451. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1452. }
  1453. /**
  1454. * Tests the time proxy method
  1455. *
  1456. * @return void
  1457. */
  1458. public function testTime()
  1459. {
  1460. $validator = new Validator();
  1461. $this->assertProxyMethod($validator, 'time');
  1462. $this->assertNotEmpty($validator->errors(['username' => 'not a time']));
  1463. }
  1464. /**
  1465. * Tests the localizedTime proxy method
  1466. *
  1467. * @return void
  1468. */
  1469. public function testLocalizedTime()
  1470. {
  1471. $validator = new Validator();
  1472. $this->assertProxyMethod($validator, 'localizedTime', 'date', ['date']);
  1473. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1474. }
  1475. /**
  1476. * Tests the boolean proxy method
  1477. *
  1478. * @return void
  1479. */
  1480. public function testBoolean()
  1481. {
  1482. $validator = new Validator();
  1483. $this->assertProxyMethod($validator, 'boolean');
  1484. $this->assertNotEmpty($validator->errors(['username' => 'not a boolean']));
  1485. }
  1486. /**
  1487. * Tests the decimal proxy method
  1488. *
  1489. * @return void
  1490. */
  1491. public function testDecimal()
  1492. {
  1493. $validator = new Validator();
  1494. $this->assertProxyMethod($validator, 'decimal', 2, [2]);
  1495. $this->assertNotEmpty($validator->errors(['username' => 10.1]));
  1496. }
  1497. /**
  1498. * Tests the ip proxy methods
  1499. *
  1500. * @return void
  1501. */
  1502. public function testIps()
  1503. {
  1504. $validator = new Validator();
  1505. $this->assertProxyMethod($validator, 'ip');
  1506. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1507. $this->assertProxyMethod($validator, 'ipv4', null, ['ipv4'], 'ip');
  1508. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1509. $this->assertProxyMethod($validator, 'ipv6', null, ['ipv6'], 'ip');
  1510. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1511. }
  1512. /**
  1513. * Tests the minLength proxy method
  1514. *
  1515. * @return void
  1516. */
  1517. public function testMinLength()
  1518. {
  1519. $validator = new Validator();
  1520. $this->assertProxyMethod($validator, 'minLength', 2, [2]);
  1521. $this->assertNotEmpty($validator->errors(['username' => 'a']));
  1522. }
  1523. /**
  1524. * Tests the minLengthBytes proxy method
  1525. *
  1526. * @return void
  1527. */
  1528. public function testMinLengthBytes()
  1529. {
  1530. $validator = new Validator();
  1531. $this->assertProxyMethod($validator, 'minLengthBytes', 11, [11]);
  1532. $this->assertNotEmpty($validator->errors(['username' => 'ÆΔΩЖÇ']));
  1533. }
  1534. /**
  1535. * Tests the maxLength proxy method
  1536. *
  1537. * @return void
  1538. */
  1539. public function testMaxLength()
  1540. {
  1541. $validator = new Validator();
  1542. $this->assertProxyMethod($validator, 'maxLength', 2, [2]);
  1543. $this->assertNotEmpty($validator->errors(['username' => 'aaa']));
  1544. }
  1545. /**
  1546. * Tests the maxLengthBytes proxy method
  1547. *
  1548. * @return void
  1549. */
  1550. public function testMaxLengthBytes()
  1551. {
  1552. $validator = new Validator();
  1553. $this->assertProxyMethod($validator, 'maxLengthBytes', 9, [9]);
  1554. $this->assertNotEmpty($validator->errors(['username' => 'ÆΔΩЖÇ']));
  1555. }
  1556. /**
  1557. * Tests the numeric proxy method
  1558. *
  1559. * @return void
  1560. */
  1561. public function testNumeric()
  1562. {
  1563. $validator = new Validator();
  1564. $this->assertProxyMethod($validator, 'numeric');
  1565. $this->assertEmpty($validator->errors(['username' => '22']));
  1566. $this->assertNotEmpty($validator->errors(['username' => 'a']));
  1567. }
  1568. /**
  1569. * Tests the naturalNumber proxy method
  1570. *
  1571. * @return void
  1572. */
  1573. public function testNaturalNumber()
  1574. {
  1575. $validator = new Validator();
  1576. $this->assertProxyMethod($validator, 'naturalNumber', null, [false]);
  1577. $this->assertNotEmpty($validator->errors(['username' => 0]));
  1578. }
  1579. /**
  1580. * Tests the nonNegativeInteger proxy method
  1581. *
  1582. * @return void
  1583. */
  1584. public function testNonNegativeInteger()
  1585. {
  1586. $validator = new Validator();
  1587. $this->assertProxyMethod($validator, 'nonNegativeInteger', null, [true], 'naturalNumber');
  1588. $this->assertNotEmpty($validator->errors(['username' => -1]));
  1589. }
  1590. /**
  1591. * Tests the range proxy method
  1592. *
  1593. * @return void
  1594. */
  1595. public function testRange()
  1596. {
  1597. $validator = new Validator();
  1598. $this->assertProxyMethod($validator, 'range', [1, 4], [1, 4]);
  1599. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1600. }
  1601. /**
  1602. * Tests the range failure case
  1603. *
  1604. * @return void
  1605. */
  1606. public function testRangeFailure()
  1607. {
  1608. $this->expectException(\InvalidArgumentException::class);
  1609. $validator = new Validator();
  1610. $validator->range('username', [1]);
  1611. }
  1612. /**
  1613. * Tests the url proxy method
  1614. *
  1615. * @return void
  1616. */
  1617. public function testUrl()
  1618. {
  1619. $validator = new Validator();
  1620. $this->assertProxyMethod($validator, 'url', null, [false]);
  1621. $this->assertNotEmpty($validator->errors(['username' => 'not url']));
  1622. }
  1623. /**
  1624. * Tests the urlWithProtocol proxy method
  1625. *
  1626. * @return void
  1627. */
  1628. public function testUrlWithProtocol()
  1629. {
  1630. $validator = new Validator();
  1631. $this->assertProxyMethod($validator, 'urlWithProtocol', null, [true], 'url');
  1632. $this->assertNotEmpty($validator->errors(['username' => 'google.com']));
  1633. }
  1634. /**
  1635. * Tests the inList proxy method
  1636. *
  1637. * @return void
  1638. */
  1639. public function testInList()
  1640. {
  1641. $validator = new Validator();
  1642. $this->assertProxyMethod($validator, 'inList', ['a', 'b'], [['a', 'b']]);
  1643. $this->assertNotEmpty($validator->errors(['username' => 'c']));
  1644. }
  1645. /**
  1646. * Tests the uuid proxy method
  1647. *
  1648. * @return void
  1649. */
  1650. public function testUuid()
  1651. {
  1652. $validator = new Validator();
  1653. $this->assertProxyMethod($validator, 'uuid');
  1654. $this->assertNotEmpty($validator->errors(['username' => 'not uuid']));
  1655. }
  1656. /**
  1657. * Tests the uploadedFile proxy method
  1658. *
  1659. * @return void
  1660. */
  1661. public function testUploadedFile()
  1662. {
  1663. $validator = new Validator();
  1664. $this->assertProxyMethod($validator, 'uploadedFile', ['foo' => 'bar'], [['foo' => 'bar']]);
  1665. $this->assertNotEmpty($validator->errors(['username' => []]));
  1666. }
  1667. /**
  1668. * Tests the latlog proxy methods
  1669. *
  1670. * @return void
  1671. */
  1672. public function testLatLong()
  1673. {
  1674. $validator = new Validator();
  1675. $this->assertProxyMethod($validator, 'latLong', null, [], 'geoCoordinate');
  1676. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1677. $this->assertProxyMethod($validator, 'latitude');
  1678. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1679. $this->assertProxyMethod($validator, 'longitude');
  1680. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1681. }
  1682. /**
  1683. * Tests the ascii proxy method
  1684. *
  1685. * @return void
  1686. */
  1687. public function testAscii()
  1688. {
  1689. $validator = new Validator();
  1690. $this->assertProxyMethod($validator, 'ascii');
  1691. $this->assertNotEmpty($validator->errors(['username' => 'ü']));
  1692. }
  1693. /**
  1694. * Tests the utf8 proxy methods
  1695. *
  1696. * @return void
  1697. */
  1698. public function testUtf8()
  1699. {
  1700. // Grinning face
  1701. $extended = 'some' . "\xf0\x9f\x98\x80" . 'value';
  1702. $validator = new Validator();
  1703. $this->assertProxyMethod($validator, 'utf8', null, [['extended' => false]]);
  1704. $this->assertEmpty($validator->errors(['username' => 'ü']));
  1705. $this->assertNotEmpty($validator->errors(['username' => $extended]));
  1706. }
  1707. /**
  1708. * Test utf8extended proxy method.
  1709. *
  1710. * @return void
  1711. */
  1712. public function testUtf8Extended()
  1713. {
  1714. // Grinning face
  1715. $extended = 'some' . "\xf0\x9f\x98\x80" . 'value';
  1716. $validator = new Validator();
  1717. $this->assertProxyMethod($validator, 'utf8Extended', null, [['extended' => true]], 'utf8');
  1718. $this->assertEmpty($validator->errors(['username' => 'ü']));
  1719. $this->assertEmpty($validator->errors(['username' => $extended]));
  1720. }
  1721. /**
  1722. * Tests the email proxy method
  1723. *
  1724. * @return void
  1725. */
  1726. public function testEmail()
  1727. {
  1728. $validator = new Validator();
  1729. $validator->email('username');
  1730. $this->assertEmpty($validator->errors(['username' => 'test@example.com']));
  1731. $this->assertNotEmpty($validator->errors(['username' => 'not an email']));
  1732. }
  1733. /**
  1734. * Tests the integer proxy method
  1735. *
  1736. * @return void
  1737. */
  1738. public function testInteger()
  1739. {
  1740. $validator = new Validator();
  1741. $this->assertProxyMethod($validator, 'integer', null, [], 'isInteger');
  1742. $this->assertNotEmpty($validator->errors(['username' => 'not integer']));
  1743. }
  1744. /**
  1745. * Tests the isArray proxy method
  1746. *
  1747. * @return void
  1748. */
  1749. public function testIsArray()
  1750. {
  1751. $validator = new Validator();
  1752. $validator->isArray('username');
  1753. $this->assertEmpty($validator->errors(['username' => [1, 2, 3]]));
  1754. $this->assertNotEmpty($validator->errors(['username' => 'is not an array']));
  1755. }
  1756. /**
  1757. * Tests the scalar proxy method
  1758. *
  1759. * @return void
  1760. */
  1761. public function testScalar()
  1762. {
  1763. $validator = new Validator();
  1764. $validator->scalar('username');
  1765. $this->assertEmpty($validator->errors(['username' => 'scalar']));
  1766. $this->assertNotEmpty($validator->errors(['username' => ['array']]));
  1767. }
  1768. /**
  1769. * Tests the hexColor proxy method
  1770. *
  1771. * @return void
  1772. */
  1773. public function testHexColor()
  1774. {
  1775. $validator = new Validator();
  1776. $this->assertProxyMethod($validator, 'hexColor');
  1777. $this->assertEmpty($validator->errors(['username' => '#FFFFFF']));
  1778. $this->assertNotEmpty($validator->errors(['username' => 'FFFFFF']));
  1779. }
  1780. /**
  1781. * Tests the multiple proxy method
  1782. *
  1783. * @return void
  1784. */
  1785. public function testMultiple()
  1786. {
  1787. $validator = new Validator();
  1788. $this->assertProxyMethod(
  1789. $validator,
  1790. 'multipleOptions',
  1791. ['min' => 1, 'caseInsensitive' => true],
  1792. [['min' => 1], true],
  1793. 'multiple'
  1794. );
  1795. $this->assertProxyMethod(
  1796. $validator,
  1797. 'multipleOptions',
  1798. ['min' => 1, 'caseInsensitive' => false],
  1799. [['min' => 1], false],
  1800. 'multiple'
  1801. );
  1802. $this->assertNotEmpty($validator->errors(['username' => '']));
  1803. }
  1804. /**
  1805. * Tests the hasAtLeast method
  1806. *
  1807. * @return void
  1808. */
  1809. public function testHasAtLeast()
  1810. {
  1811. $validator = new Validator();
  1812. $validator->hasAtLeast('things', 3);
  1813. $this->assertEmpty($validator->errors(['things' => [1, 2, 3]]));
  1814. $this->assertEmpty($validator->errors(['things' => [1, 2, 3, 4]]));
  1815. $this->assertNotEmpty($validator->errors(['things' => [1, 2]]));
  1816. $this->assertNotEmpty($validator->errors(['things' => []]));
  1817. $this->assertNotEmpty($validator->errors(['things' => 'string']));
  1818. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3]]]));
  1819. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3, 4]]]));
  1820. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => [1, 2]]]));
  1821. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => []]]));
  1822. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => 'string']]));
  1823. }
  1824. /**
  1825. * Tests the hasAtMost method
  1826. *
  1827. * @return void
  1828. */
  1829. public function testHasAtMost()
  1830. {
  1831. $validator = new Validator();
  1832. $validator->hasAtMost('things', 3);
  1833. $this->assertEmpty($validator->errors(['things' => [1, 2, 3]]));
  1834. $this->assertEmpty($validator->errors(['things' => [1]]));
  1835. $this->assertNotEmpty($validator->errors(['things' => [1, 2, 3, 4]]));
  1836. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3]]]));
  1837. $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2]]]));
  1838. $this->assertNotEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3, 4]]]));
  1839. }
  1840. /**
  1841. * Tests the regex proxy method
  1842. *
  1843. * @return void
  1844. */
  1845. public function testRegex()
  1846. {
  1847. $validator = new Validator();
  1848. $this->assertProxyMethod($validator, 'regex', '/(?<!\\S)\\d++(?!\\S)/', ['/(?<!\\S)\\d++(?!\\S)/'], 'custom');
  1849. $this->assertEmpty($validator->errors(['username' => '123']));
  1850. $this->assertNotEmpty($validator->errors(['username' => 'Foo']));
  1851. }
  1852. /**
  1853. * Tests that a rule in the Validator class exists and was configured as expected.
  1854. *
  1855. * @param Validator $validator
  1856. * @param string $method
  1857. * @param mixed $extra
  1858. * @param array $pass
  1859. * @param string|null $name
  1860. */
  1861. protected function assertProxyMethod($validator, $method, $extra = null, $pass = [], $name = null)
  1862. {
  1863. $name = $name ?: $method;
  1864. if ($extra !== null) {
  1865. $this->assertSame($validator, $validator->{$method}('username', $extra));
  1866. } else {
  1867. $this->assertSame($validator, $validator->{$method}('username'));
  1868. }
  1869. $rule = $validator->field('username')->rule($method);
  1870. $this->assertNotEmpty($rule, "Rule was not found for $method");
  1871. $this->assertNull($rule->get('message'), 'Message is present when it should not be');
  1872. $this->assertNull($rule->get('on'), 'On clause is present when it should not be');
  1873. $this->assertEquals($name, $rule->get('rule'), 'Rule name does not match');
  1874. $this->assertEquals($pass, $rule->get('pass'), 'Passed options are different');
  1875. $this->assertEquals('default', $rule->get('provider'), 'Provider does not match');
  1876. if ($extra !== null) {
  1877. $validator->{$method}('username', $extra, 'the message', 'create');
  1878. } else {
  1879. $validator->{$method}('username', 'the message', 'create');
  1880. }
  1881. $rule = $validator->field('username')->rule($method);
  1882. $this->assertEquals('the message', $rule->get('message'), 'Error messages are not the same');
  1883. $this->assertEquals('create', $rule->get('on'), 'On clause is wrong');
  1884. }
  1885. /**
  1886. * Testing adding DefaultProvider
  1887. *
  1888. * @return void
  1889. */
  1890. public function testAddingDefaultProvider()
  1891. {
  1892. $validator = new Validator();
  1893. $this->assertEmpty($validator->providers(), 'Providers should be empty');
  1894. Validator::addDefaultProvider('test-provider', 'MyNameSpace\Validation\MyProvider');
  1895. $validator = new Validator();
  1896. $this->assertEquals($validator->providers(), ['test-provider'], 'Default provider `test-provider` is missing');
  1897. }
  1898. /**
  1899. * Testing getting DefaultProvider(s)
  1900. *
  1901. * @return void
  1902. */
  1903. public function testGetDefaultProvider()
  1904. {
  1905. Validator::addDefaultProvider('test-provider', 'MyNameSpace\Validation\MyProvider');
  1906. $this->assertEquals(Validator::getDefaultProvider('test-provider'), 'MyNameSpace\Validation\MyProvider', 'Default provider `test-provider` is missing');
  1907. $this->assertNull(Validator::getDefaultProvider('invalid-provider'), 'Default provider (`invalid-provider`) should be missing');
  1908. $this->assertNull(Validator::getDefaultProvider(null), 'Default provider (null) should be missing');
  1909. Validator::addDefaultProvider('test-provider2', 'MyNameSpace\Validation\MySecondProvider');
  1910. $this->assertEquals(Validator::getDefaultProviders(), ['test-provider', 'test-provider2'], 'Default providers incorrect');
  1911. }
  1912. }