ValidatorTest.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  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 mesages generated when a field is not allowed to be empty
  630. *
  631. * @return void
  632. */
  633. public function testCustomErrorsWithEmptyNotAllowed()
  634. {
  635. $validator = new Validator;
  636. $validator->notEmpty('title', 'Custom message');
  637. $errors = $validator->errors(['title' => '']);
  638. $expected = ['title' => ['_empty' => 'Custom message']];
  639. $this->assertEquals($expected, $errors);
  640. }
  641. /**
  642. * Tests errors generated when a field is allowed to be empty
  643. *
  644. * @return void
  645. */
  646. public function testErrorsWithEmptyAllowed()
  647. {
  648. $validator = new Validator;
  649. $validator->allowEmpty('title');
  650. $errors = $validator->errors(['title' => '']);
  651. $this->assertEmpty($errors);
  652. $errors = $validator->errors(['title' => []]);
  653. $this->assertEmpty($errors);
  654. $errors = $validator->errors(['title' => null]);
  655. $this->assertEmpty($errors);
  656. $errors = $validator->errors(['title' => 0]);
  657. $this->assertEmpty($errors);
  658. $errors = $validator->errors(['title' => 0.0]);
  659. $this->assertEmpty($errors);
  660. $errors = $validator->errors(['title' => '0']);
  661. $this->assertEmpty($errors);
  662. $errors = $validator->errors(['title' => false]);
  663. $this->assertEmpty($errors);
  664. }
  665. /**
  666. * Test the provider() method
  667. *
  668. * @return void
  669. */
  670. public function testProvider()
  671. {
  672. $validator = new Validator;
  673. $object = new \stdClass;
  674. $this->assertSame($validator, $validator->provider('foo', $object));
  675. $this->assertSame($object, $validator->provider('foo'));
  676. $this->assertNull($validator->provider('bar'));
  677. $another = new \stdClass;
  678. $this->assertSame($validator, $validator->provider('bar', $another));
  679. $this->assertSame($another, $validator->provider('bar'));
  680. $this->assertEquals(new \Cake\Validation\RulesProvider, $validator->provider('default'));
  681. }
  682. /**
  683. * Tests errors() method when using validators from the default provider, this proves
  684. * that it returns a default validation message and the custom one set in the rule
  685. *
  686. * @return void
  687. */
  688. public function testErrorsFromDefaultProvider()
  689. {
  690. $validator = new Validator;
  691. $validator
  692. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  693. ->add('email', 'notBlank', ['rule' => 'notBlank'])
  694. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  695. $errors = $validator->errors(['email' => 'not an email!']);
  696. $expected = [
  697. 'email' => [
  698. 'alpha' => 'The provided value is invalid',
  699. 'email' => 'Y u no write email?'
  700. ]
  701. ];
  702. $this->assertEquals($expected, $errors);
  703. }
  704. /**
  705. * Tests using validation methods from different providers and returning the error
  706. * as a string
  707. *
  708. * @return void
  709. */
  710. public function testErrorsFromCustomProvider()
  711. {
  712. $validator = new Validator;
  713. $validator
  714. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  715. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  716. $thing = $this->getMock('\stdClass', ['isCool']);
  717. $thing->expects($this->once())->method('isCool')
  718. ->will($this->returnCallback(function ($data, $context) use ($thing) {
  719. $this->assertEquals('bar', $data);
  720. $expected = [
  721. 'default' => new \Cake\Validation\RulesProvider,
  722. 'thing' => $thing
  723. ];
  724. $expected = [
  725. 'newRecord' => true,
  726. 'providers' => $expected,
  727. 'data' => [
  728. 'email' => '!',
  729. 'title' => 'bar'
  730. ],
  731. 'field' => 'title'
  732. ];
  733. $this->assertEquals($expected, $context);
  734. return "That ain't cool, yo";
  735. }));
  736. $validator->provider('thing', $thing);
  737. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  738. $expected = [
  739. 'email' => ['alpha' => 'The provided value is invalid'],
  740. 'title' => ['cool' => "That ain't cool, yo"]
  741. ];
  742. $this->assertEquals($expected, $errors);
  743. }
  744. /**
  745. * Tests that it is possible to pass extra arguments to the validation function
  746. * and it still gets the providers as last argument
  747. *
  748. * @return void
  749. */
  750. public function testMethodsWithExtraArguments()
  751. {
  752. $validator = new Validator;
  753. $validator->add('title', 'cool', [
  754. 'rule' => ['isCool', 'and', 'awesome'],
  755. 'provider' => 'thing'
  756. ]);
  757. $thing = $this->getMock('\stdClass', ['isCool']);
  758. $thing->expects($this->once())->method('isCool')
  759. ->will($this->returnCallback(function ($data, $a, $b, $context) use ($thing) {
  760. $this->assertEquals('bar', $data);
  761. $this->assertEquals('and', $a);
  762. $this->assertEquals('awesome', $b);
  763. $expected = [
  764. 'default' => new \Cake\Validation\RulesProvider,
  765. 'thing' => $thing
  766. ];
  767. $expected = [
  768. 'newRecord' => true,
  769. 'providers' => $expected,
  770. 'data' => [
  771. 'email' => '!',
  772. 'title' => 'bar'
  773. ],
  774. 'field' => 'title'
  775. ];
  776. $this->assertEquals($expected, $context);
  777. return "That ain't cool, yo";
  778. }));
  779. $validator->provider('thing', $thing);
  780. $errors = $validator->errors(['email' => '!', 'title' => 'bar']);
  781. $expected = [
  782. 'title' => ['cool' => "That ain't cool, yo"]
  783. ];
  784. $this->assertEquals($expected, $errors);
  785. }
  786. /**
  787. * Tests that it is possible to use a closure as a rule
  788. *
  789. * @return void
  790. */
  791. public function testUsingClosureAsRule()
  792. {
  793. $validator = new Validator;
  794. $validator->add('name', 'myRule', [
  795. 'rule' => function ($data, $provider) {
  796. $this->assertEquals('foo', $data);
  797. return 'You fail';
  798. }
  799. ]);
  800. $expected = ['name' => ['myRule' => 'You fail']];
  801. $this->assertEquals($expected, $validator->errors(['name' => 'foo']));
  802. }
  803. /**
  804. * Tests that setting last to a rule will stop validating the rest of the rules
  805. *
  806. * @return void
  807. */
  808. public function testErrorsWithLastRule()
  809. {
  810. $validator = new Validator;
  811. $validator
  812. ->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
  813. ->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
  814. $errors = $validator->errors(['email' => 'not an email!']);
  815. $expected = [
  816. 'email' => [
  817. 'alpha' => 'The provided value is invalid'
  818. ]
  819. ];
  820. $this->assertEquals($expected, $errors);
  821. }
  822. /**
  823. * Tests it is possible to get validation sets for a field using an array interface
  824. *
  825. * @return void
  826. */
  827. public function testArrayAccessGet()
  828. {
  829. $validator = new Validator;
  830. $validator
  831. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  832. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  833. $this->assertSame($validator['email'], $validator->field('email'));
  834. $this->assertSame($validator['title'], $validator->field('title'));
  835. }
  836. /**
  837. * Tests it is possible to check for validation sets for a field using an array inteface
  838. *
  839. * @return void
  840. */
  841. public function testArrayAccessExists()
  842. {
  843. $validator = new Validator;
  844. $validator
  845. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  846. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  847. $this->assertTrue(isset($validator['email']));
  848. $this->assertTrue(isset($validator['title']));
  849. $this->assertFalse(isset($validator['foo']));
  850. }
  851. /**
  852. * Tests it is possible to set validation rules for a field using an array inteface
  853. *
  854. * @return void
  855. */
  856. public function testArrayAccessSet()
  857. {
  858. $validator = new Validator;
  859. $validator
  860. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  861. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  862. $validator['name'] = $validator->field('title');
  863. $this->assertSame($validator->field('title'), $validator->field('name'));
  864. $validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
  865. $this->assertEquals($validator->field('email'), $validator->field('email'));
  866. }
  867. /**
  868. * Tests it is possible to unset validation rules
  869. *
  870. * @return void
  871. */
  872. public function testArrayAccessUset()
  873. {
  874. $validator = new Validator;
  875. $validator
  876. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  877. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  878. $this->assertTrue(isset($validator['title']));
  879. unset($validator['title']);
  880. $this->assertFalse(isset($validator['title']));
  881. }
  882. /**
  883. * Tests the countable interface
  884. *
  885. * @return void
  886. */
  887. public function testCount()
  888. {
  889. $validator = new Validator;
  890. $validator
  891. ->add('email', 'alpha', ['rule' => 'alphanumeric'])
  892. ->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
  893. $this->assertCount(2, $validator);
  894. }
  895. /**
  896. * Tests adding rules via alternative syntax
  897. *
  898. * @return void
  899. */
  900. public function testAddMulitple()
  901. {
  902. $validator = new Validator;
  903. $validator->add('title', [
  904. 'notBlank' => [
  905. 'rule' => 'notBlank'
  906. ],
  907. 'length' => [
  908. 'rule' => ['minLength', 10],
  909. 'message' => 'Titles need to be at least 10 characters long'
  910. ]
  911. ]);
  912. $set = $validator->field('title');
  913. $this->assertInstanceOf('Cake\Validation\ValidationSet', $set);
  914. $this->assertCount(2, $set);
  915. }
  916. /**
  917. * Integration test for compareWith validator.
  918. *
  919. * @return void
  920. */
  921. public function testCompareWithIntegration()
  922. {
  923. $validator = new Validator;
  924. $validator->add('password', [
  925. 'compare' => [
  926. 'rule' => ['compareWith', 'password_compare']
  927. ],
  928. ]);
  929. $data = [
  930. 'password' => 'test',
  931. 'password_compare' => 'not the same'
  932. ];
  933. $this->assertNotEmpty($validator->errors($data), 'Validation should fail.');
  934. }
  935. /**
  936. * Test debugInfo helper method.
  937. *
  938. * @return void
  939. */
  940. public function testDebugInfo()
  941. {
  942. $validator = new Validator();
  943. $validator->provider('test', $this);
  944. $validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
  945. $validator->requirePresence('body');
  946. $validator->allowEmpty('published');
  947. $result = $validator->__debugInfo();
  948. $expected = [
  949. '_providers' => ['test'],
  950. '_fields' => [
  951. 'title' => [
  952. 'isPresenceRequired' => false,
  953. 'isEmptyAllowed' => false,
  954. 'rules' => ['not-empty'],
  955. ],
  956. 'body' => [
  957. 'isPresenceRequired' => true,
  958. 'isEmptyAllowed' => false,
  959. 'rules' => [],
  960. ],
  961. 'published' => [
  962. 'isPresenceRequired' => false,
  963. 'isEmptyAllowed' => true,
  964. 'rules' => [],
  965. ],
  966. ],
  967. '_presenceMessages' => [],
  968. '_allowEmptyMessages' => [],
  969. '_useI18n' => true,
  970. ];
  971. $this->assertEquals($expected, $result);
  972. }
  973. /**
  974. * Tests that the 'create' and 'update' modes are preserved when using
  975. * nested validators
  976. *
  977. * @return void
  978. */
  979. public function testNestedValidatorCreate()
  980. {
  981. $validator = new Validator();
  982. $inner = new Validator();
  983. $inner->add('username', 'email', ['rule' => 'email', 'on' => 'create']);
  984. $validator->addNested('user', $inner);
  985. $this->assertNotEmpty($validator->errors(['user' => ['username' => 'example']], true));
  986. $this->assertEmpty($validator->errors(['user' => ['username' => 'a']], false));
  987. }
  988. /**
  989. * Tests that the 'create' and 'update' modes are preserved when using
  990. * nested validators
  991. *
  992. * @return void
  993. */
  994. public function testNestedManyValidatorCreate()
  995. {
  996. $validator = new Validator();
  997. $inner = new Validator();
  998. $inner->add('username', 'email', ['rule' => 'email', 'on' => 'create']);
  999. $validator->addNestedMany('user', $inner);
  1000. $this->assertNotEmpty($validator->errors(['user' => [['username' => 'example']]], true));
  1001. $this->assertEmpty($validator->errors(['user' => [['username' => 'a']]], false));
  1002. }
  1003. /**
  1004. * Tests the notBlank proxy method
  1005. *
  1006. * @return void
  1007. */
  1008. public function testNotBlank()
  1009. {
  1010. $validator = new Validator();
  1011. $this->assertProxyMethod($validator, 'notBlank');
  1012. $this->assertNotEmpty($validator->errors(['username' => ' ']));
  1013. }
  1014. /**
  1015. * Tests the alphanumeric proxy method
  1016. *
  1017. * @return void
  1018. */
  1019. public function testAlphanumeric()
  1020. {
  1021. $validator = new Validator();
  1022. $this->assertProxyMethod($validator, 'alphaNumeric');
  1023. $this->assertNotEmpty($validator->errors(['username' => '$']));
  1024. }
  1025. /**
  1026. * Tests the lengthBetween proxy method
  1027. *
  1028. * @return void
  1029. */
  1030. public function testLengthBetween()
  1031. {
  1032. $validator = new Validator();
  1033. $this->assertProxyMethod($validator, 'lengthBetween', [5, 7], [5, 7]);
  1034. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1035. }
  1036. /**
  1037. * Tests the lengthBetween proxy method
  1038. *
  1039. * @expectedException InvalidArgumentException
  1040. * @return void
  1041. */
  1042. public function testLengthBetweenFailure()
  1043. {
  1044. $validator = new Validator();
  1045. $validator->lengthBetween('username', [7]);
  1046. }
  1047. /**
  1048. * Tests the creditCard proxy method
  1049. *
  1050. * @return void
  1051. */
  1052. public function testCreditCard()
  1053. {
  1054. $validator = new Validator();
  1055. $this->assertProxyMethod($validator, 'creditCard', 'all', ['all', true], 'cc');
  1056. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1057. }
  1058. /**
  1059. * Tests the greaterThan proxy method
  1060. *
  1061. * @return void
  1062. */
  1063. public function testGreaterThan()
  1064. {
  1065. $validator = new Validator();
  1066. $this->assertProxyMethod($validator, 'greaterThan', 5, ['>', 5], 'comparison');
  1067. $this->assertNotEmpty($validator->errors(['username' => 2]));
  1068. }
  1069. /**
  1070. * Tests the greaterThanOrEqual proxy method
  1071. *
  1072. * @return void
  1073. */
  1074. public function testGreaterThanOrEqual()
  1075. {
  1076. $validator = new Validator();
  1077. $this->assertProxyMethod($validator, 'greaterThanOrEqual', 5, ['>=', 5], 'comparison');
  1078. $this->assertNotEmpty($validator->errors(['username' => 2]));
  1079. }
  1080. /**
  1081. * Tests the lessThan proxy method
  1082. *
  1083. * @return void
  1084. */
  1085. public function testLessThan()
  1086. {
  1087. $validator = new Validator();
  1088. $this->assertProxyMethod($validator, 'lessThan', 5, ['<', 5], 'comparison');
  1089. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1090. }
  1091. /**
  1092. * Tests the lessThanOrEqual proxy method
  1093. *
  1094. * @return void
  1095. */
  1096. public function testLessThanOrEqual()
  1097. {
  1098. $validator = new Validator();
  1099. $this->assertProxyMethod($validator, 'lessThanOrEqual', 5, ['<=', 5], 'comparison');
  1100. $this->assertNotEmpty($validator->errors(['username' => 6]));
  1101. }
  1102. /**
  1103. * Tests the equals proxy method
  1104. *
  1105. * @return void
  1106. */
  1107. public function testEquals()
  1108. {
  1109. $validator = new Validator();
  1110. $this->assertProxyMethod($validator, 'equals', 5, ['=', 5], 'comparison');
  1111. $this->assertNotEmpty($validator->errors(['username' => 6]));
  1112. }
  1113. /**
  1114. * Tests the notEquals proxy method
  1115. *
  1116. * @return void
  1117. */
  1118. public function testNotEquals()
  1119. {
  1120. $validator = new Validator();
  1121. $this->assertProxyMethod($validator, 'notEquals', 5, ['!=', 5], 'comparison');
  1122. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1123. }
  1124. /**
  1125. * Tests the sameAs proxy method
  1126. *
  1127. * @return void
  1128. */
  1129. public function testSameAs()
  1130. {
  1131. $validator = new Validator();
  1132. $this->assertProxyMethod($validator, 'sameAs', 'other', ['other'], 'compareWith');
  1133. $this->assertNotEmpty($validator->errors(['username' => 'foo']));
  1134. }
  1135. /**
  1136. * Tests the containsNonAlphaNumeric proxy method
  1137. *
  1138. * @return void
  1139. */
  1140. public function testContainsNonAlphaNumeric()
  1141. {
  1142. $validator = new Validator();
  1143. $this->assertProxyMethod($validator, 'containsNonAlphaNumeric', 2, [2]);
  1144. $this->assertNotEmpty($validator->errors(['username' => '$']));
  1145. }
  1146. /**
  1147. * Tests the date proxy method
  1148. *
  1149. * @return void
  1150. */
  1151. public function testDate()
  1152. {
  1153. $validator = new Validator();
  1154. $this->assertProxyMethod($validator, 'date', ['ymd'], [['ymd']]);
  1155. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1156. }
  1157. /**
  1158. * Tests the dateTime proxy method
  1159. *
  1160. * @return void
  1161. */
  1162. public function testDateTime()
  1163. {
  1164. $validator = new Validator();
  1165. $this->assertProxyMethod($validator, 'dateTime', ['ymd'], [['ymd']], 'datetime');
  1166. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1167. }
  1168. /**
  1169. * Tests the time proxy method
  1170. *
  1171. * @return void
  1172. */
  1173. public function testTime()
  1174. {
  1175. $validator = new Validator();
  1176. $this->assertProxyMethod($validator, 'time');
  1177. $this->assertNotEmpty($validator->errors(['username' => 'not a time']));
  1178. }
  1179. /**
  1180. * Tests the localizedTime proxy method
  1181. *
  1182. * @return void
  1183. */
  1184. public function testLocalizedTime()
  1185. {
  1186. $validator = new Validator();
  1187. $this->assertProxyMethod($validator, 'localizedTime', 'date', ['date']);
  1188. $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
  1189. }
  1190. /**
  1191. * Tests the boolean proxy method
  1192. *
  1193. * @return void
  1194. */
  1195. public function testBoolean()
  1196. {
  1197. $validator = new Validator();
  1198. $this->assertProxyMethod($validator, 'boolean');
  1199. $this->assertNotEmpty($validator->errors(['username' => 'not a boolean']));
  1200. }
  1201. /**
  1202. * Tests the decimal proxy method
  1203. *
  1204. * @return void
  1205. */
  1206. public function testDecimal()
  1207. {
  1208. $validator = new Validator();
  1209. $this->assertProxyMethod($validator, 'decimal', 2, [2]);
  1210. $this->assertNotEmpty($validator->errors(['username' => 10.1]));
  1211. }
  1212. /**
  1213. * Tests the ip proxy methods
  1214. *
  1215. * @return void
  1216. */
  1217. public function testIps()
  1218. {
  1219. $validator = new Validator();
  1220. $this->assertProxyMethod($validator, 'ip');
  1221. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1222. $this->assertProxyMethod($validator, 'ipv4', null, ['ipv4'], 'ip');
  1223. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1224. $this->assertProxyMethod($validator, 'ipv6', null, ['ipv6'], 'ip');
  1225. $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
  1226. }
  1227. /**
  1228. * Tests the minLength proxy method
  1229. *
  1230. * @return void
  1231. */
  1232. public function testMinLength()
  1233. {
  1234. $validator = new Validator();
  1235. $this->assertProxyMethod($validator, 'minLength', 2, [2]);
  1236. $this->assertNotEmpty($validator->errors(['username' => 'a']));
  1237. }
  1238. /**
  1239. * Tests the maxLength proxy method
  1240. *
  1241. * @return void
  1242. */
  1243. public function testMaxLength()
  1244. {
  1245. $validator = new Validator();
  1246. $this->assertProxyMethod($validator, 'maxLength', 2, [2]);
  1247. $this->assertNotEmpty($validator->errors(['username' => 'aaa']));
  1248. }
  1249. /**
  1250. * Tests the numeric proxy method
  1251. *
  1252. * @return void
  1253. */
  1254. public function testNumeric()
  1255. {
  1256. $validator = new Validator();
  1257. $this->assertProxyMethod($validator, 'numeric');
  1258. $this->assertEmpty($validator->errors(['username' => '22']));
  1259. $this->assertNotEmpty($validator->errors(['username' => 'a']));
  1260. }
  1261. /**
  1262. * Tests the naturalNumber proxy method
  1263. *
  1264. * @return void
  1265. */
  1266. public function testNaturalNumber()
  1267. {
  1268. $validator = new Validator();
  1269. $this->assertProxyMethod($validator, 'naturalNumber', null, [false]);
  1270. $this->assertNotEmpty($validator->errors(['username' => 0]));
  1271. }
  1272. /**
  1273. * Tests the nonNegativeInteger proxy method
  1274. *
  1275. * @return void
  1276. */
  1277. public function testNonNegativeInteger()
  1278. {
  1279. $validator = new Validator();
  1280. $this->assertProxyMethod($validator, 'nonNegativeInteger', null, [true], 'naturalNumber');
  1281. $this->assertNotEmpty($validator->errors(['username' => -1]));
  1282. }
  1283. /**
  1284. * Tests the range proxy method
  1285. *
  1286. * @return void
  1287. */
  1288. public function testRange()
  1289. {
  1290. $validator = new Validator();
  1291. $this->assertProxyMethod($validator, 'range', [1, 4], [1, 4]);
  1292. $this->assertNotEmpty($validator->errors(['username' => 5]));
  1293. }
  1294. /**
  1295. * Tests the range failure case
  1296. *
  1297. * @expectedException InvalidArgumentException
  1298. * @return void
  1299. */
  1300. public function testRangeFailure()
  1301. {
  1302. $validator = new Validator();
  1303. $validator->range('username', [1]);
  1304. }
  1305. /**
  1306. * Tests the url proxy method
  1307. *
  1308. * @return void
  1309. */
  1310. public function testUrl()
  1311. {
  1312. $validator = new Validator();
  1313. $this->assertProxyMethod($validator, 'url', null, [false]);
  1314. $this->assertNotEmpty($validator->errors(['username' => 'not url']));
  1315. }
  1316. /**
  1317. * Tests the urlWithProtocol proxy method
  1318. *
  1319. * @return void
  1320. */
  1321. public function testUrlWithProtocol()
  1322. {
  1323. $validator = new Validator();
  1324. $this->assertProxyMethod($validator, 'urlWithProtocol', null, [true], 'url');
  1325. $this->assertNotEmpty($validator->errors(['username' => 'google.com']));
  1326. }
  1327. /**
  1328. * Tests the inList proxy method
  1329. *
  1330. * @return void
  1331. */
  1332. public function testInList()
  1333. {
  1334. $validator = new Validator();
  1335. $this->assertProxyMethod($validator, 'inList', ['a', 'b'], [['a', 'b']]);
  1336. $this->assertNotEmpty($validator->errors(['username' => 'c']));
  1337. }
  1338. /**
  1339. * Tests the uuid proxy method
  1340. *
  1341. * @return void
  1342. */
  1343. public function testUuid()
  1344. {
  1345. $validator = new Validator();
  1346. $this->assertProxyMethod($validator, 'uuid');
  1347. $this->assertNotEmpty($validator->errors(['username' => 'not uuid']));
  1348. }
  1349. /**
  1350. * Tests the uploadedFile proxy method
  1351. *
  1352. * @return void
  1353. */
  1354. public function testUploadedFile()
  1355. {
  1356. $validator = new Validator();
  1357. $this->assertProxyMethod($validator, 'uploadedFile', ['foo' => 'bar'], [['foo' => 'bar']]);
  1358. $this->assertNotEmpty($validator->errors(['username' => []]));
  1359. }
  1360. /**
  1361. * Tests the latlog proxy methods
  1362. *
  1363. * @return void
  1364. */
  1365. public function testLatLong()
  1366. {
  1367. $validator = new Validator();
  1368. $this->assertProxyMethod($validator, 'latLong', null, [], 'geoCoordinate');
  1369. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1370. $this->assertProxyMethod($validator, 'latitude');
  1371. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1372. $this->assertProxyMethod($validator, 'longitude');
  1373. $this->assertNotEmpty($validator->errors(['username' => 2000]));
  1374. }
  1375. /**
  1376. * Tests the ascii proxy method
  1377. *
  1378. * @return void
  1379. */
  1380. public function testAscii()
  1381. {
  1382. $validator = new Validator();
  1383. $this->assertProxyMethod($validator, 'ascii');
  1384. $this->assertNotEmpty($validator->errors(['username' => 'ü']));
  1385. }
  1386. /**
  1387. * Tests the utf8 proxy methods
  1388. *
  1389. * @return void
  1390. */
  1391. public function testUtf8()
  1392. {
  1393. // Grinning face
  1394. $extended = 'some' . "\xf0\x9f\x98\x80" . 'value';
  1395. $validator = new Validator();
  1396. $this->assertProxyMethod($validator, 'utf8', null, [['extended' => false]]);
  1397. $this->assertEmpty($validator->errors(['username' => 'ü']));
  1398. $this->assertNotEmpty($validator->errors(['username' => $extended]));
  1399. }
  1400. /**
  1401. * Test utf8extended proxy method.
  1402. *
  1403. * @return void
  1404. */
  1405. public function testUtf8Extended()
  1406. {
  1407. // Grinning face
  1408. $extended = 'some' . "\xf0\x9f\x98\x80" . 'value';
  1409. $validator = new Validator();
  1410. $this->assertProxyMethod($validator, 'utf8Extended', null, [['extended' => true]], 'utf8');
  1411. $this->assertEmpty($validator->errors(['username' => 'ü']));
  1412. $this->assertEmpty($validator->errors(['username' => $extended]));
  1413. }
  1414. /**
  1415. * Tests the email proxy method
  1416. *
  1417. * @return void
  1418. */
  1419. public function testEmail()
  1420. {
  1421. $validator = new Validator();
  1422. $validator->email('username');
  1423. $this->assertEmpty($validator->errors(['username' => 'test@example.com']));
  1424. $this->assertNotEmpty($validator->errors(['username' => 'not an email']));
  1425. }
  1426. /**
  1427. * Tests the integer proxy method
  1428. *
  1429. * @return void
  1430. */
  1431. public function testInteger()
  1432. {
  1433. $validator = new Validator();
  1434. $this->assertProxyMethod($validator, 'integer', null, [], 'isInteger');
  1435. $this->assertNotEmpty($validator->errors(['username' => 'not integer']));
  1436. }
  1437. /**
  1438. * Tests the multiple proxy method
  1439. *
  1440. * @return void
  1441. */
  1442. public function testMultiple()
  1443. {
  1444. $validator = new Validator();
  1445. $this->assertProxyMethod(
  1446. $validator,
  1447. 'multipleOptions',
  1448. ['min' => 1, 'caseInsensitive' => true],
  1449. [['min' => 1], false],
  1450. 'multiple'
  1451. );
  1452. $this->assertNotEmpty($validator->errors(['username' => '']));
  1453. }
  1454. protected function assertProxyMethod($validator, $method, $extra = null, $pass = [], $name = null)
  1455. {
  1456. $name = $name ?: $method;
  1457. if ($extra !== null) {
  1458. $this->assertSame($validator, $validator->{$method}('username', $extra));
  1459. } else {
  1460. $this->assertSame($validator, $validator->{$method}('username'));
  1461. }
  1462. $rule = $validator->field('username')->rule($method);
  1463. $this->assertNotEmpty($rule, "Rule was not found for $method");
  1464. $this->assertNull($rule->get('message'), 'Message is present when it should not be');
  1465. $this->assertNull($rule->get('on'), 'On clause is present when it should not be');
  1466. $this->assertEquals($name, $rule->get('rule'), 'Rule name does not match');
  1467. $this->assertEquals($pass, $rule->get('pass'), 'Passed options are different');
  1468. $this->assertEquals('default', $rule->get('provider'), 'Provider does not match');
  1469. if ($extra !== null) {
  1470. $validator->{$method}('username', $extra, 'the message', 'create');
  1471. } else {
  1472. $validator->{$method}('username', 'the message', 'create');
  1473. }
  1474. $rule = $validator->field('username')->rule($method);
  1475. $this->assertEquals('the message', $rule->get('message'), 'Error messages are not the same');
  1476. $this->assertEquals('create', $rule->get('on'), 'On clause is wrong');
  1477. }
  1478. }