ValidatorTest.php 65 KB

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