ValidatorTest.php 47 KB

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