validation.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Validation class tests
  15. *
  16. * @group Core
  17. * @group Validation
  18. */
  19. class Test_Validation extends TestCase
  20. {
  21. public static function form_provider()
  22. {
  23. return array(
  24. array(
  25. array(
  26. 'foo' => 'bar',
  27. 'bar' => 'foo',
  28. 'boo' => 'bar',
  29. 'cat' => '',
  30. 'dog' => '245abc',
  31. 'php' => 1,
  32. 'sky' => 'blue',
  33. 'one' => '[email protected]',
  34. 'two' => '[email protected], [email protected]',
  35. 'owt' => '[email protected]; [email protected]',
  36. 'six' => 6,
  37. 'ten' => 10,
  38. 'dns' => '127.0.0.1',
  39. 'snd' => '127.0.0',
  40. 'url' => 'http://www.google.com',
  41. )
  42. )
  43. );
  44. }
  45. /**
  46. * Validation: required
  47. * Expecting: success
  48. *
  49. * @dataProvider form_provider
  50. */
  51. public function test_validation_required_success($input)
  52. {
  53. $val = Validation::forge(__FUNCTION__);
  54. $val->add_field('foo', 'Foo', 'required');
  55. $output = $val->run($input);
  56. $expected = true;
  57. $this->assertEquals($expected, $output);
  58. }
  59. /**
  60. * Validation: required
  61. * Expecting: failure
  62. *
  63. * @dataProvider form_provider
  64. */
  65. public function test_validation_required_failure($input)
  66. {
  67. $val = Validation::forge(__FUNCTION__);
  68. $val->add_field('cat', 'Cat', 'required');
  69. $val->run($input);
  70. $output = $val->error('cat', false) ? true : false;
  71. $expected = true;
  72. $this->assertEquals($expected, $output);
  73. }
  74. /**
  75. * Validation: match_value
  76. * Expecting: success
  77. *
  78. * @dataProvider form_provider
  79. */
  80. public function test_validation_match_value_success($input)
  81. {
  82. $val = Validation::forge(__FUNCTION__);
  83. $val->add_field('foo', 'Foo', 'match_value[bar]');
  84. $output = $val->run($input);
  85. $expected = true;
  86. $this->assertEquals($expected, $output);
  87. }
  88. /**
  89. * Validation: match_value
  90. * Expecting: failure
  91. *
  92. * @dataProvider form_provider
  93. */
  94. public function test_validation_match_value_failure($input)
  95. {
  96. $val = Validation::forge(__FUNCTION__);
  97. $val->add_field('foo', 'Foo', "match_value['foo']");
  98. $val->run($input);
  99. $output = $val->error('foo', false) ? true : false;
  100. $expected = true;
  101. $this->assertEquals($expected, $output);
  102. }
  103. /**
  104. * Validation: match_value (strict)
  105. * Expecting: success
  106. *
  107. * @dataProvider form_provider
  108. */
  109. public function test_validation_match_value_strict_success($input)
  110. {
  111. $val = Validation::forge(__FUNCTION__);
  112. $val->add_field('php', 'PHP', '')->add_rule('match_value', 1, 1);
  113. $output = $val->run($input);
  114. $expected = true;
  115. $this->assertEquals($expected, $output);
  116. }
  117. /**
  118. * Validation: match_value (strict)
  119. * Expecting: failure
  120. *
  121. * @dataProvider form_provider
  122. */
  123. public function test_validation_match_value_strict_failure($input)
  124. {
  125. $val = Validation::forge(__FUNCTION__);
  126. $val->add_field('php', 'PHP', '')->add_rule('match_value', '1', 1);
  127. $val->run($input);
  128. $output = $val->error('php', false) ? true : false;
  129. $expected = true;
  130. $this->assertEquals($expected, $output);
  131. }
  132. /**
  133. * Validation: match_pattern
  134. * Expecting: success
  135. *
  136. * @dataProvider form_provider
  137. */
  138. public function test_validation_match_pattern_success($input)
  139. {
  140. $val = Validation::forge(__FUNCTION__);
  141. $val->add_field('dog', 'Dog', '')->add_rule('match_pattern', '/\d{3}\w{3}/');
  142. $output = $val->run($input);
  143. $expected = true;
  144. $this->assertEquals($expected, $output);
  145. }
  146. /**
  147. * Validation: match_pattern
  148. * Expecting: failure
  149. *
  150. * @dataProvider form_provider
  151. */
  152. public function test_validation_match_pattern_failure($input)
  153. {
  154. $val = Validation::forge(__FUNCTION__);
  155. $val->add_field('dog', 'Dog', '')->add_rule('match_pattern', '/^\d{2}[abc]{3}/');
  156. $val->run($input);
  157. $output = $val->error('dog', false) ? true : false;
  158. $expected = true;
  159. $this->assertEquals($expected, $output);
  160. }
  161. /**
  162. * Validation: match_field
  163. * Expecting: success
  164. *
  165. * @dataProvider form_provider
  166. */
  167. public function test_validation_match_field_success($input)
  168. {
  169. $val = Validation::forge(__FUNCTION__);
  170. $val->add_field('foo', 'Foo', 'valid_string');
  171. $val->add_field('boo', 'Boo', 'match_field[foo]');
  172. $output = $val->run($input);
  173. $expected = true;
  174. $this->assertEquals($expected, $output);
  175. }
  176. /**
  177. * Validation: match_field
  178. * Expecting: failure
  179. *
  180. * @dataProvider form_provider
  181. */
  182. public function test_validation_match_field_failure($input)
  183. {
  184. $val = Validation::forge(__FUNCTION__);
  185. $val->add_field('bar', 'Bar', 'valid_string');
  186. $val->add_field('boo', 'Boo', 'match_field[bar]');
  187. $val->run($input);
  188. $output = $val->error('boo', false) ? true : false;
  189. $expected = true;
  190. $this->assertEquals($expected, $output);
  191. }
  192. /**
  193. * Validation: min_length
  194. * Expecting: success
  195. *
  196. * @dataProvider form_provider
  197. */
  198. public function test_validation_min_length_success($input)
  199. {
  200. $val = Validation::forge(__FUNCTION__);
  201. $val->add_field('boo', 'Boo', 'min_length[2]');
  202. $output = $val->run($input);
  203. $expected = true;
  204. $this->assertEquals($expected, $output);
  205. }
  206. /**
  207. * Validation: min_length
  208. * Expecting: failure
  209. *
  210. * @dataProvider form_provider
  211. */
  212. public function test_validation_min_length_failure($input)
  213. {
  214. $val = Validation::forge(__FUNCTION__);
  215. $val->add_field('boo', 'Boo', 'min_length[4]');
  216. $val->run($input);
  217. $output = $val->error('boo', false) ? true : false;
  218. $expected = true;
  219. $this->assertEquals($expected, $output);
  220. }
  221. /**
  222. * Validation: max_length
  223. * Expecting: success
  224. *
  225. * @dataProvider form_provider
  226. */
  227. public function test_validation_max_length_success($input)
  228. {
  229. $val = Validation::forge(__FUNCTION__);
  230. $val->add_field('boo', 'Boo', 'max_length[4]');
  231. $output = $val->run($input);
  232. $expected = true;
  233. $this->assertEquals($expected, $output);
  234. }
  235. /**
  236. * Validation: max_length
  237. * Expecting: failure
  238. *
  239. * @dataProvider form_provider
  240. */
  241. public function test_validation_max_length_failure($input)
  242. {
  243. $val = Validation::forge(__FUNCTION__);
  244. $val->add_field('boo', 'Boo', 'max_length[2]');
  245. $val->run($input);
  246. $output = $val->error('boo', false) ? true : false;
  247. $expected = true;
  248. $this->assertEquals($expected, $output);
  249. }
  250. /**
  251. * Validation: exact_length
  252. * Expecting: success
  253. *
  254. * @dataProvider form_provider
  255. */
  256. public function test_validation_exact_length_success($input)
  257. {
  258. $val = Validation::forge(__FUNCTION__);
  259. $val->add_field('boo', 'Boo', 'exact_length[3]');
  260. $output = $val->run($input);
  261. $expected = true;
  262. $this->assertEquals($expected, $output);
  263. }
  264. /**
  265. * Validation: exact_length
  266. * Expecting: failure
  267. *
  268. * @dataProvider form_provider
  269. */
  270. public function test_validation_exact_length_failure($input)
  271. {
  272. $val = Validation::forge(__FUNCTION__);
  273. $val->add_field('boo', 'Boo', 'max_length[2]');
  274. $val->run($input);
  275. $output = $val->error('boo', false) ? true : false;
  276. $expected = true;
  277. $this->assertEquals($expected, $output);
  278. }
  279. /**
  280. * Validation: valid_email
  281. * Expecting: success
  282. *
  283. * @dataProvider form_provider
  284. */
  285. public function test_validation_valid_email_success($input)
  286. {
  287. $val = Validation::forge(__FUNCTION__);
  288. $val->add_field('one', 'Email', 'valid_email');
  289. $output = $val->run($input);
  290. $expected = true;
  291. $this->assertEquals($expected, $output);
  292. }
  293. /**
  294. * Validation: valid_email
  295. * Expecting: failure
  296. *
  297. * @dataProvider form_provider
  298. */
  299. public function test_validation_valid_email_failure($input)
  300. {
  301. $val = Validation::forge(__FUNCTION__);
  302. $val->add_field('boo', 'Email', 'valid_email');
  303. $val->run($input);
  304. $output = $val->error('boo', false) ? true : false;
  305. $expected = true;
  306. $this->assertEquals($expected, $output);
  307. }
  308. /**
  309. * Validation: valid_emails
  310. * Expecting: success
  311. *
  312. * @dataProvider form_provider
  313. */
  314. public function test_validation_valid_emails_success($input)
  315. {
  316. $val = Validation::forge(__FUNCTION__);
  317. $val->add_field('two', 'Emails', 'valid_emails');
  318. $output = $val->run($input);
  319. $expected = true;
  320. $this->assertEquals($expected, $output);
  321. }
  322. /**
  323. * Validation: valid_emails (different separator)
  324. * Expecting: success
  325. *
  326. * @dataProvider form_provider
  327. */
  328. public function test_validation_valid_emails_separator_success($input)
  329. {
  330. $val = Validation::forge(__FUNCTION__);
  331. $val->add('owt', 'Emails')->add_rule('valid_emails', ';');
  332. $output = $val->run($input);
  333. $expected = true;
  334. $this->assertEquals($expected, $output);
  335. }
  336. /**
  337. * Validation: valid_emails
  338. * Expecting: failure
  339. *
  340. * @dataProvider form_provider
  341. */
  342. public function test_validation_valid_emails_failure($input)
  343. {
  344. $val = Validation::forge(__FUNCTION__);
  345. $val->add_field('boo', 'Email', 'valid_emails');
  346. $val->run($input);
  347. $output = $val->error('boo', false) ? true : false;
  348. $expected = true;
  349. $this->assertEquals($expected, $output);
  350. }
  351. /**
  352. * Validation: valid_url
  353. * Expecting: success
  354. *
  355. * @dataProvider form_provider
  356. */
  357. public function test_validation_valid_url_success($input)
  358. {
  359. $val = Validation::forge(__FUNCTION__);
  360. $val->add_field('url', 'Url', 'valid_url');
  361. $output = $val->run($input);
  362. $expected = true;
  363. $this->assertEquals($expected, $output);
  364. }
  365. /**
  366. * Validation: valid_url
  367. * Expecting: failure
  368. *
  369. * @dataProvider form_provider
  370. */
  371. public function test_validation_valid_url_failure($input)
  372. {
  373. $val = Validation::forge(__FUNCTION__);
  374. $val->add_field('boo', 'Url', 'valid_url');
  375. $val->run($input);
  376. $output = $val->error('boo', false) ? true : false;
  377. $expected = true;
  378. $this->assertEquals($expected, $output);
  379. }
  380. /**
  381. * Validation: valid_ip
  382. * Expecting: success
  383. *
  384. * @dataProvider form_provider
  385. */
  386. public function test_validation_valid_ip_success($input)
  387. {
  388. $val = Validation::forge(__FUNCTION__);
  389. $val->add_field('dns', 'IP', 'valid_ip');
  390. $output = $val->run($input);
  391. $expected = true;
  392. $this->assertEquals($expected, $output);
  393. }
  394. /**
  395. * Validation: valid_ip
  396. * Expecting: failure
  397. *
  398. * @dataProvider form_provider
  399. */
  400. public function test_validation_valid_ip_failure($input)
  401. {
  402. $val = Validation::forge(__FUNCTION__);
  403. $val->add_field('snd', 'IP', 'valid_ip');
  404. $val->run($input);
  405. $output = $val->error('snd', false) ? true : false;
  406. $expected = true;
  407. $this->assertEquals($expected, $output);
  408. }
  409. /**
  410. * Validation: numeric_min
  411. * Expecting: success
  412. *
  413. * @dataProvider form_provider
  414. */
  415. public function test_validation_numeric_min_success($input)
  416. {
  417. $val = Validation::forge(__FUNCTION__);
  418. $val->add_field('six', 'Number', 'numeric_min[4]');
  419. $output = $val->run($input);
  420. $expected = true;
  421. $this->assertEquals($expected, $output);
  422. }
  423. /**
  424. * Validation: numeric_min
  425. * Expecting: failure
  426. *
  427. * @dataProvider form_provider
  428. */
  429. public function test_validation_numeric_min_failure($input)
  430. {
  431. $val = Validation::forge(__FUNCTION__);
  432. $val->add_field('six', 'Number', 'numeric_min[8]');
  433. $val->run($input);
  434. $output = $val->error('six', false) ? true : false;
  435. $expected = true;
  436. $this->assertEquals($expected, $output);
  437. }
  438. /**
  439. * Validation: numeric_max
  440. * Expecting: success
  441. *
  442. * @dataProvider form_provider
  443. */
  444. public function test_validation_numeric_max_success($input)
  445. {
  446. $val = Validation::forge(__FUNCTION__);
  447. $val->add_field('ten', 'Number', 'numeric_max[11]');
  448. $output = $val->run($input);
  449. $expected = true;
  450. $this->assertEquals($expected, $output);
  451. }
  452. /**
  453. * Validation: numeric_max
  454. * Expecting: failure
  455. *
  456. * @dataProvider form_provider
  457. */
  458. public function test_validation_numeric_max_failure($input)
  459. {
  460. $val = Validation::forge(__FUNCTION__);
  461. $val->add_field('ten', 'Number', 'numeric_max[8]');
  462. $val->run($input);
  463. $output = $val->error('ten', false) ? true : false;
  464. $expected = true;
  465. $this->assertEquals($expected, $output);
  466. }
  467. /**
  468. * Validation: numeric_between
  469. * Expecting: success
  470. *
  471. * @dataProvider form_provider
  472. */
  473. public function test_validation_numeric_between_success($input)
  474. {
  475. $val = Validation::forge(__FUNCTION__);
  476. $val->add_field('ten', 'Number', 'numeric_between[9,11]');
  477. $output = $val->run($input);
  478. $expected = true;
  479. $this->assertEquals($expected, $output);
  480. }
  481. /**
  482. * Validation: numeric_between
  483. * Expecting: failure
  484. *
  485. * @dataProvider form_provider
  486. */
  487. public function test_validation_numeric_between_failure($input)
  488. {
  489. $val = Validation::forge(__FUNCTION__);
  490. $val->add_field('ten', 'Number', 'numeric_between[7,8]');
  491. $val->run($input);
  492. $output = $val->error('ten', false) ? true : false;
  493. $expected = true;
  494. $this->assertEquals($expected, $output);
  495. }
  496. /**
  497. * Validation: required_with
  498. * Expecting: success
  499. *
  500. * @dataProvider form_provider
  501. */
  502. public function test_validation_required_with_success($input)
  503. {
  504. $val = Validation::forge(__FUNCTION__);
  505. $val->add_field('foo', 'Foo', 'valid_string');
  506. $val->add_field('bar', 'Bar', 'required_with[foo]');
  507. $output = $val->run($input);
  508. $expected = true;
  509. $this->assertEquals($expected, $output);
  510. }
  511. /**
  512. * Validation: valid_string numeric
  513. * Expecting: success
  514. */
  515. public function test_validation_valid_string_numeric_success()
  516. {
  517. $post = array(
  518. 'f1' => '123456',
  519. );
  520. $val = Validation::forge(__FUNCTION__);
  521. $val->add('f1', 'F1')->add_rule('valid_string', 'numeric');
  522. $test = $val->run($post);
  523. $expected = true;
  524. $this->assertEquals($expected, $test);
  525. }
  526. /**
  527. * Validation: valid_string numeric
  528. * Expecting: failure
  529. */
  530. public function test_validation_valid_string_numeric_failure()
  531. {
  532. $post = array(
  533. 'f1' => 'a123456',
  534. );
  535. $val = Validation::forge(__FUNCTION__);
  536. $val->add('f1', 'F1')->add_rule('valid_string', 'numeric');
  537. $test = $val->run($post);
  538. $expected = false;
  539. $this->assertEquals($expected, $test);
  540. }
  541. /**
  542. * Validation: valid_string alpha,numeric
  543. * Expecting: failure
  544. */
  545. public function test_validation_valid_string_multiple_flags_error_message_add_rule() {
  546. $post = array(
  547. 'f1' => '123 abc',
  548. );
  549. $val = Validation::forge(__FUNCTION__);
  550. $val->add('f1', 'F1')->add_rule('valid_string', array('alpha', 'numeric'));
  551. $val->set_message('valid_string', 'The valid string rule :rule(:param:1) failed for field :label');
  552. $val->run($post);
  553. $test = $val->error('f1')->get_message();
  554. $expected = 'The valid string rule valid_string(alpha, numeric) failed for field F1';
  555. $this->assertEquals($expected, $test);
  556. }
  557. /**
  558. * Validation: valid_string alpha,numeric
  559. * Expecting: failure
  560. */
  561. public function test_validation_valid_string_multiple_flags_error_message_add_field() {
  562. $post = array(
  563. 'f1' => '123 abc',
  564. );
  565. $val = Validation::forge(__FUNCTION__);
  566. $val->add_field('f1', 'F1', 'valid_string[alpha,numeric]');
  567. $val->set_message('valid_string', 'The valid string rule :rule(:param:1) failed for field :label');
  568. $val->run($post);
  569. $test = $val->error('f1')->get_message();
  570. $expected = 'The valid string rule valid_string(alpha, numeric) failed for field F1';
  571. $this->assertEquals($expected, $test);
  572. }
  573. }