ValidatorTest.php 52 KB

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