ValidTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. <?php
  2. /**
  3. * Tests the Valid class
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.valid
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. * @author Kohana Team
  12. * @author BRMatt <matthew@sigswitch.com>
  13. * @copyright (c) Kohana Team
  14. * @license https://koseven.ga/LICENSE.md
  15. */
  16. class Kohana_ValidTest extends Unittest_TestCase
  17. {
  18. /**
  19. * Provides test data for test_alpha()
  20. * @return array
  21. */
  22. public function provider_alpha()
  23. {
  24. return [
  25. ['asdavafaiwnoabwiubafpowf', TRUE],
  26. ['!aidhfawiodb', FALSE],
  27. ['51535oniubawdawd78', FALSE],
  28. ['!"£$(G$W£(HFW£F(HQ)"n', FALSE],
  29. // UTF-8 tests
  30. ['あいうえお', TRUE, TRUE],
  31. ['¥', FALSE, TRUE],
  32. // Empty test
  33. ['', FALSE, FALSE],
  34. [NULL, FALSE, FALSE],
  35. [FALSE, FALSE, FALSE],
  36. ];
  37. }
  38. /**
  39. * Tests Valid::alpha()
  40. *
  41. * Checks whether a string consists of alphabetical characters only.
  42. *
  43. * @test
  44. * @dataProvider provider_alpha
  45. * @param string $string
  46. * @param boolean $expected
  47. */
  48. public function test_alpha($string, $expected, $utf8 = FALSE)
  49. {
  50. $this->assertSame(
  51. $expected,
  52. Valid::alpha($string, $utf8)
  53. );
  54. }
  55. /*
  56. * Provides test data for test_alpha_numeric
  57. */
  58. public function provide_alpha_numeric()
  59. {
  60. return [
  61. ['abcd1234', TRUE],
  62. ['abcd', TRUE],
  63. ['1234', TRUE],
  64. ['abc123&^/-', FALSE],
  65. // UTF-8 tests
  66. ['あいうえお', TRUE, TRUE],
  67. ['零一二三四五', TRUE, TRUE],
  68. ['あい四五£^£^', FALSE, TRUE],
  69. // Empty test
  70. ['', FALSE, FALSE],
  71. [NULL, FALSE, FALSE],
  72. [FALSE, FALSE, FALSE],
  73. ];
  74. }
  75. /**
  76. * Tests Valid::alpha_numeric()
  77. *
  78. * Checks whether a string consists of alphabetical characters and numbers only.
  79. *
  80. * @test
  81. * @dataProvider provide_alpha_numeric
  82. * @param string $input The string to test
  83. * @param boolean $expected Is $input valid
  84. */
  85. public function test_alpha_numeric($input, $expected, $utf8 = FALSE)
  86. {
  87. $this->assertSame(
  88. $expected,
  89. Valid::alpha_numeric($input, $utf8)
  90. );
  91. }
  92. /**
  93. * Provides test data for test_alpha_dash
  94. */
  95. public function provider_alpha_dash()
  96. {
  97. return [
  98. ['abcdef', TRUE],
  99. ['12345', TRUE],
  100. ['abcd1234', TRUE],
  101. ['abcd1234-', TRUE],
  102. ['abc123&^/-', FALSE],
  103. // Empty test
  104. ['', FALSE],
  105. [NULL, FALSE],
  106. [FALSE, FALSE],
  107. ];
  108. }
  109. /**
  110. * Tests Valid::alpha_dash()
  111. *
  112. * Checks whether a string consists of alphabetical characters, numbers, underscores and dashes only.
  113. *
  114. * @test
  115. * @dataProvider provider_alpha_dash
  116. * @param string $input The string to test
  117. * @param boolean $contains_utf8 Does the string contain utf8 specific characters
  118. * @param boolean $expected Is $input valid?
  119. */
  120. public function test_alpha_dash($input, $expected, $contains_utf8 = FALSE)
  121. {
  122. if ( ! $contains_utf8)
  123. {
  124. $this->assertSame(
  125. $expected,
  126. Valid::alpha_dash($input)
  127. );
  128. }
  129. $this->assertSame(
  130. $expected,
  131. Valid::alpha_dash($input, TRUE)
  132. );
  133. }
  134. /**
  135. * DataProvider for the valid::date() test
  136. */
  137. public function provider_date()
  138. {
  139. return [
  140. ['now',TRUE],
  141. ['10 September 2010',TRUE],
  142. ['+1 day',TRUE],
  143. ['+1 week',TRUE],
  144. ['+1 week 2 days 4 hours 2 seconds',TRUE],
  145. ['next Thursday',TRUE],
  146. ['last Monday',TRUE],
  147. ['blarg',FALSE],
  148. ['in the year 2000',FALSE],
  149. ['324824',FALSE],
  150. // Empty test
  151. ['', FALSE],
  152. [NULL, FALSE],
  153. [FALSE, FALSE],
  154. ];
  155. }
  156. /**
  157. * Tests Valid::date()
  158. *
  159. * @test
  160. * @dataProvider provider_date
  161. * @param string $date The date to validate
  162. * @param integer $expected
  163. */
  164. public function test_date($date, $expected)
  165. {
  166. $this->assertSame(
  167. $expected,
  168. Valid::date($date, $expected)
  169. );
  170. }
  171. /**
  172. * DataProvider for the valid::decimal() test
  173. */
  174. public function provider_decimal()
  175. {
  176. return [
  177. // Empty test
  178. ['', 2, NULL, FALSE],
  179. [NULL, 2, NULL, FALSE],
  180. [FALSE, 2, NULL, FALSE],
  181. ['45.1664', 3, NULL, FALSE],
  182. ['45.1664', 4, NULL, TRUE],
  183. ['45.1664', 4, 2, TRUE],
  184. ['-45.1664', 4, NULL, TRUE],
  185. ['+45.1664', 4, NULL, TRUE],
  186. ['-45.1664', 3, NULL, FALSE],
  187. ];
  188. }
  189. /**
  190. * Tests Valid::decimal()
  191. *
  192. * @test
  193. * @dataProvider provider_decimal
  194. * @param string $decimal The decimal to validate
  195. * @param integer $places The number of places to check to
  196. * @param integer $digits The number of digits preceding the point to check
  197. * @param boolean $expected Whether $decimal conforms to $places AND $digits
  198. */
  199. public function test_decimal($decimal, $places, $digits, $expected)
  200. {
  201. $this->assertSame(
  202. $expected,
  203. Valid::decimal($decimal, $places, $digits),
  204. 'Decimal: "'.$decimal.'" to '.$places.' places and '.$digits.' digits (preceeding period)'
  205. );
  206. }
  207. /**
  208. * Provides test data for test_digit
  209. * @return array
  210. */
  211. public function provider_digit()
  212. {
  213. return [
  214. ['12345', TRUE],
  215. ['10.5', FALSE],
  216. ['abcde', FALSE],
  217. ['abcd1234', FALSE],
  218. ['-5', FALSE],
  219. [-5, FALSE],
  220. // Empty test
  221. ['', FALSE],
  222. [NULL, FALSE],
  223. [FALSE, FALSE],
  224. ];
  225. }
  226. /**
  227. * Tests Valid::digit()
  228. *
  229. * @test
  230. * @dataProvider provider_digit
  231. * @param mixed $input Input to validate
  232. * @param boolean $expected Is $input valid
  233. */
  234. public function test_digit($input, $expected, $contains_utf8 = FALSE)
  235. {
  236. if ( ! $contains_utf8)
  237. {
  238. $this->assertSame(
  239. $expected,
  240. Valid::digit($input)
  241. );
  242. }
  243. $this->assertSame(
  244. $expected,
  245. Valid::digit($input, TRUE)
  246. );
  247. }
  248. /**
  249. * DataProvider for the valid::color() test
  250. */
  251. public function provider_color()
  252. {
  253. return [
  254. ['#000000', TRUE],
  255. ['#GGGGGG', FALSE],
  256. ['#AbCdEf', TRUE],
  257. ['#000', TRUE],
  258. ['#abc', TRUE],
  259. ['#DEF', TRUE],
  260. ['000000', TRUE],
  261. ['GGGGGG', FALSE],
  262. ['AbCdEf', TRUE],
  263. ['000', TRUE],
  264. ['DEF', TRUE],
  265. // Empty test
  266. ['', FALSE],
  267. [NULL, FALSE],
  268. [FALSE, FALSE],
  269. ];
  270. }
  271. /**
  272. * Tests Valid::color()
  273. *
  274. * @test
  275. * @dataProvider provider_color
  276. * @param string $color The color to test
  277. * @param boolean $expected Is $color valid
  278. */
  279. public function test_color($color, $expected)
  280. {
  281. $this->assertSame(
  282. $expected,
  283. Valid::color($color)
  284. );
  285. }
  286. /**
  287. * Provides test data for test_credit_card()
  288. */
  289. public function provider_credit_card()
  290. {
  291. return [
  292. ['4222222222222', 'visa', TRUE],
  293. ['4012888888881881', 'visa', TRUE],
  294. ['4012888888881881', NULL, TRUE],
  295. ['4012888888881881', ['mastercard', 'visa'], TRUE],
  296. ['4012888888881881', ['discover', 'mastercard'], FALSE],
  297. ['4012888888881881', 'mastercard', FALSE],
  298. ['5105105105105100', 'mastercard', TRUE],
  299. ['6011111111111117', 'discover', TRUE],
  300. ['6011111111111117', 'visa', FALSE],
  301. // Empty test
  302. ['', NULL, FALSE],
  303. [NULL, NULL, FALSE],
  304. [FALSE, NULL, FALSE],
  305. ];
  306. }
  307. /**
  308. * Tests Valid::credit_card()
  309. *
  310. * @test
  311. * @covers Valid::credit_card
  312. * @dataProvider provider_credit_card()
  313. * @param string $number Credit card number
  314. * @param string $type Credit card type
  315. * @param boolean $expected
  316. */
  317. public function test_credit_card($number, $type, $expected)
  318. {
  319. $this->assertSame(
  320. $expected,
  321. Valid::credit_card($number, $type)
  322. );
  323. }
  324. /**
  325. * Provides test data for test_credit_card()
  326. */
  327. public function provider_luhn()
  328. {
  329. return [
  330. ['4222222222222', TRUE],
  331. ['4012888888881881', TRUE],
  332. ['5105105105105100', TRUE],
  333. ['6011111111111117', TRUE],
  334. ['60111111111111.7', FALSE],
  335. ['6011111111111117X', FALSE],
  336. ['6011111111111117 ', FALSE],
  337. ['WORD ', FALSE],
  338. // Empty test
  339. ['', FALSE],
  340. [NULL, FALSE],
  341. [FALSE, FALSE],
  342. ];
  343. }
  344. /**
  345. * Tests Valid::luhn()
  346. *
  347. * @test
  348. * @covers Valid::luhn
  349. * @dataProvider provider_luhn()
  350. * @param string $number Credit card number
  351. * @param boolean $expected
  352. */
  353. public function test_luhn($number, $expected)
  354. {
  355. $this->assertSame(
  356. $expected,
  357. Valid::luhn($number)
  358. );
  359. }
  360. /**
  361. * Provides test data for test_email()
  362. *
  363. * @return array
  364. */
  365. public function provider_email()
  366. {
  367. return [
  368. ['foo', TRUE, FALSE],
  369. ['foo', FALSE, FALSE],
  370. ['foo@bar', TRUE, FALSE],
  371. // RFC is less strict than the normal regex, presumably to allow
  372. // admin@localhost, therefore we IGNORE IT!!!
  373. ['foo@bar', FALSE, FALSE],
  374. ['foo@bar.com', FALSE, TRUE],
  375. ['foo@barcom:80', FALSE, FALSE],
  376. ['foo@bar.sub.com', FALSE, TRUE],
  377. ['foo+asd@bar.sub.com', FALSE, TRUE],
  378. ['foo.asd@bar.sub.com', FALSE, TRUE],
  379. // RFC says 254 length max #4011
  380. [Text::random(NULL, 200).'@'.Text::random(NULL, 50).'.com', FALSE, FALSE]
  381. ];
  382. }
  383. /**
  384. * Tests Valid::email()
  385. *
  386. * Check an email address for correct format.
  387. *
  388. * @test
  389. * @dataProvider provider_email
  390. * @param string $email Address to check
  391. * @param boolean $strict Use strict settings
  392. * @param boolean $correct Is $email address valid?
  393. */
  394. public function test_email($email, $strict, $correct)
  395. {
  396. $this->assertSame(
  397. $correct,
  398. Valid::email($email, $strict)
  399. );
  400. }
  401. /**
  402. * Returns test data for test_email_domain()
  403. *
  404. * @return array
  405. */
  406. public function provider_email_domain()
  407. {
  408. return [
  409. ['google.com', TRUE],
  410. // Don't anybody dare register this...
  411. ['DAWOMAWIDAIWNDAIWNHDAWIHDAIWHDAIWOHDAIOHDAIWHD.com', FALSE],
  412. // Empty test
  413. ['', FALSE],
  414. [NULL, FALSE],
  415. [FALSE, FALSE],
  416. ];
  417. }
  418. /**
  419. * Tests Valid::email_domain()
  420. *
  421. * Validate the domain of an email address by checking if the domain has a
  422. * valid MX record.
  423. *
  424. * Test skips on windows
  425. *
  426. * @test
  427. * @dataProvider provider_email_domain
  428. * @param string $email Email domain to check
  429. * @param boolean $correct Is it correct?
  430. */
  431. public function test_email_domain($email, $correct)
  432. {
  433. if ( ! $this->hasInternet())
  434. {
  435. $this->markTestSkipped('An internet connection is required for this test');
  436. }
  437. $this->assertSame(
  438. $correct,
  439. Valid::email_domain($email)
  440. );
  441. }
  442. /**
  443. * Provides data for test_exact_length()
  444. *
  445. * @return array
  446. */
  447. public function provider_exact_length()
  448. {
  449. return [
  450. ['somestring', 10, TRUE],
  451. ['somestring', 11, FALSE],
  452. ['anotherstring', 13, TRUE],
  453. // Empty test
  454. ['', 10, FALSE],
  455. [NULL, 10, FALSE],
  456. [FALSE, 10, FALSE],
  457. // Test array of allowed lengths
  458. ['somestring', [1, 3, 5, 7, 9, 10], TRUE],
  459. ['somestring', [1, 3, 5, 7, 9], FALSE],
  460. ];
  461. }
  462. /**
  463. *
  464. * Tests Valid::exact_length()
  465. *
  466. * Checks that a field is exactly the right length.
  467. *
  468. * @test
  469. * @dataProvider provider_exact_length
  470. * @param string $string The string to length check
  471. * @param integer $length The length of the string
  472. * @param boolean $correct Is $length the actual length of the string?
  473. * @return bool
  474. */
  475. public function test_exact_length($string, $length, $correct)
  476. {
  477. return $this->assertSame(
  478. $correct,
  479. Valid::exact_length($string, $length),
  480. 'Reported string length is not correct'
  481. );
  482. }
  483. /**
  484. * Provides data for test_equals()
  485. *
  486. * @return array
  487. */
  488. public function provider_equals()
  489. {
  490. return [
  491. ['foo', 'foo', TRUE],
  492. ['1', '1', TRUE],
  493. [1, '1', FALSE],
  494. ['011', 011, FALSE],
  495. // Empty test
  496. ['', 123, FALSE],
  497. [NULL, 123, FALSE],
  498. [FALSE, 123, FALSE],
  499. ];
  500. }
  501. /**
  502. * Tests Valid::equals()
  503. *
  504. * @test
  505. * @dataProvider provider_equals
  506. * @param string $string value to check
  507. * @param integer $required required value
  508. * @param boolean $correct is $string the same as $required?
  509. * @return boolean
  510. */
  511. public function test_equals($string, $required, $correct)
  512. {
  513. return $this->assertSame(
  514. $correct,
  515. Valid::equals($string, $required),
  516. 'Values are not equal'
  517. );
  518. }
  519. /**
  520. * DataProvider for the valid::ip() test
  521. * @return array
  522. */
  523. public function provider_ip()
  524. {
  525. return [
  526. ['75.125.175.50', FALSE, TRUE],
  527. ['127.0.0.1', FALSE, FALSE],
  528. ['256.257.258.259', FALSE, FALSE],
  529. ['255.255.255.255', FALSE, FALSE],
  530. ['192.168.0.1', FALSE, FALSE],
  531. ['192.168.0.1', TRUE, TRUE],
  532. // Empty test
  533. ['', TRUE, FALSE],
  534. [NULL, TRUE, FALSE],
  535. [FALSE, TRUE, FALSE],
  536. ];
  537. }
  538. /**
  539. * Tests Valid::ip()
  540. *
  541. * @test
  542. * @dataProvider provider_ip
  543. * @param string $input_ip
  544. * @param boolean $allow_private
  545. * @param boolean $expected_result
  546. */
  547. public function test_ip($input_ip, $allow_private, $expected_result)
  548. {
  549. $this->assertEquals(
  550. $expected_result,
  551. Valid::ip($input_ip, $allow_private)
  552. );
  553. }
  554. /**
  555. * Returns test data for test_max_length()
  556. *
  557. * @return array
  558. */
  559. public function provider_max_length()
  560. {
  561. return [
  562. // Border line
  563. ['some', 4, TRUE],
  564. // Exceeds
  565. ['KOHANARULLLES', 2, FALSE],
  566. // Under
  567. ['CakeSucks', 10, TRUE],
  568. // Empty test
  569. ['', -10, FALSE],
  570. [NULL, -10, FALSE],
  571. [FALSE, -10, FALSE],
  572. ];
  573. }
  574. /**
  575. * Tests Valid::max_length()
  576. *
  577. * Checks that a field is short enough.
  578. *
  579. * @test
  580. * @dataProvider provider_max_length
  581. * @param string $string String to test
  582. * @param integer $maxlength Max length for this string
  583. * @param boolean $correct Is $string <= $maxlength
  584. */
  585. public function test_max_length($string, $maxlength, $correct)
  586. {
  587. $this->assertSame(
  588. $correct,
  589. Valid::max_length($string, $maxlength)
  590. );
  591. }
  592. /**
  593. * Returns test data for test_min_length()
  594. *
  595. * @return array
  596. */
  597. public function provider_min_length()
  598. {
  599. return [
  600. ['This is obviously long enough', 10, TRUE],
  601. ['This is not', 101, FALSE],
  602. ['This is on the borderline', 25, TRUE],
  603. // Empty test
  604. ['', 10, FALSE],
  605. [NULL, 10, FALSE],
  606. [FALSE, 10, FALSE],
  607. ];
  608. }
  609. /**
  610. * Tests Valid::min_length()
  611. *
  612. * Checks that a field is long enough.
  613. *
  614. * @test
  615. * @dataProvider provider_min_length
  616. * @param string $string String to compare
  617. * @param integer $minlength The minimum allowed length
  618. * @param boolean $correct Is $string 's length >= $minlength
  619. */
  620. public function test_min_length($string, $minlength, $correct)
  621. {
  622. $this->assertSame(
  623. $correct,
  624. Valid::min_length($string, $minlength)
  625. );
  626. }
  627. /**
  628. * Returns test data for test_not_empty()
  629. *
  630. * @return array
  631. */
  632. public function provider_not_empty()
  633. {
  634. // Create a blank arrayObject
  635. $ao = new ArrayObject;
  636. // arrayObject with value
  637. $ao1 = new ArrayObject;
  638. $ao1['test'] = 'value';
  639. return [
  640. [[], FALSE],
  641. [NULL, FALSE],
  642. ['', FALSE],
  643. [$ao, FALSE],
  644. [$ao1, TRUE],
  645. [[NULL], TRUE],
  646. [0, TRUE],
  647. ['0', TRUE],
  648. ['Something', TRUE],
  649. ];
  650. }
  651. /**
  652. * Tests Valid::not_empty()
  653. *
  654. * Checks if a field is not empty.
  655. *
  656. * @test
  657. * @dataProvider provider_not_empty
  658. * @param mixed $value Value to check
  659. * @param boolean $empty Is the value really empty?
  660. */
  661. public function test_not_empty($value, $empty)
  662. {
  663. return $this->assertSame(
  664. $empty,
  665. Valid::not_empty($value)
  666. );
  667. }
  668. /**
  669. * DataProvider for the Valid::numeric() test
  670. */
  671. public function provider_numeric()
  672. {
  673. return [
  674. [12345, TRUE],
  675. [123.45, TRUE],
  676. ['12345', TRUE],
  677. ['10.5', TRUE],
  678. ['-10.5', TRUE],
  679. ['10.5a', FALSE],
  680. // @issue 3240
  681. [.4, TRUE],
  682. [-.4, TRUE],
  683. [4., TRUE],
  684. [-4., TRUE],
  685. ['.5', TRUE],
  686. ['-.5', TRUE],
  687. ['5.', TRUE],
  688. ['-5.', TRUE],
  689. ['.', FALSE],
  690. ['1.2.3', FALSE],
  691. // Empty test
  692. ['', FALSE],
  693. [NULL, FALSE],
  694. [FALSE, FALSE],
  695. ];
  696. }
  697. /**
  698. * Tests Valid::numeric()
  699. *
  700. * @test
  701. * @dataProvider provider_numeric
  702. * @param string $input Input to test
  703. * @param boolean $expected Whether or not $input is numeric
  704. */
  705. public function test_numeric($input, $expected)
  706. {
  707. $this->assertSame(
  708. $expected,
  709. Valid::numeric($input)
  710. );
  711. }
  712. /**
  713. * Provides test data for test_phone()
  714. * @return array
  715. */
  716. public function provider_phone()
  717. {
  718. return [
  719. ['0163634840', NULL, TRUE],
  720. ['+27173634840', NULL, TRUE],
  721. ['123578', NULL, FALSE],
  722. // Some uk numbers
  723. ['01234456778', NULL, TRUE],
  724. ['+0441234456778', NULL, FALSE],
  725. // Google UK case you're interested
  726. ['+44 20-7031-2017', [12], TRUE],
  727. // BT Corporate
  728. ['020 7356 5000', NULL, TRUE],
  729. // Empty test
  730. ['', NULL, FALSE],
  731. [NULL, NULL, FALSE],
  732. [FALSE, NULL, FALSE],
  733. ];
  734. }
  735. /**
  736. * Tests Valid::phone()
  737. *
  738. * @test
  739. * @dataProvider provider_phone
  740. * @param string $phone Phone number to test
  741. * @param boolean $expected Is $phone valid
  742. */
  743. public function test_phone($phone, $lengths, $expected)
  744. {
  745. $this->assertSame(
  746. $expected,
  747. Valid::phone($phone, $lengths)
  748. );
  749. }
  750. /**
  751. * DataProvider for the valid::regex() test
  752. */
  753. public function provider_regex()
  754. {
  755. return [
  756. ['hello world', '/[a-zA-Z\s]++/', TRUE],
  757. ['123456789', '/[0-9]++/', TRUE],
  758. ['£$%£%', '/[abc]/', FALSE],
  759. ['Good evening', '/hello/', FALSE],
  760. // Empty test
  761. ['', '/hello/', FALSE],
  762. [NULL, '/hello/', FALSE],
  763. [FALSE, '/hello/', FALSE],
  764. ];
  765. }
  766. /**
  767. * Tests Valid::range()
  768. *
  769. * Tests if a number is within a range.
  770. *
  771. * @test
  772. * @dataProvider provider_regex
  773. * @param string $value Value to test against
  774. * @param string $regex Valid pcre regular expression
  775. * @param bool $expected Does the value match the expression?
  776. */
  777. public function test_regex($value, $regex, $expected)
  778. {
  779. $this->AssertSame(
  780. $expected,
  781. Valid::regex($value, $regex)
  782. );
  783. }
  784. /**
  785. * DataProvider for the valid::range() test
  786. */
  787. public function provider_range()
  788. {
  789. return [
  790. [1, 0, 2, NULL, TRUE],
  791. [-1, -5, 0, NULL, TRUE],
  792. [-1, 0, 1, NULL, FALSE],
  793. [1, 0, 0, NULL, FALSE],
  794. [2147483647, 0, 200000000000000, NULL, TRUE],
  795. [-2147483647, -2147483655, 2147483645, NULL, TRUE],
  796. // #4043
  797. [2, 0, 10, 2, TRUE],
  798. [3, 0, 10, 2, FALSE],
  799. // #4672
  800. [0, 0, 10, NULL, TRUE],
  801. [10, 0, 10, NULL, TRUE],
  802. [-10, -10, 10, NULL, TRUE],
  803. [-10, -1, 1, NULL, FALSE],
  804. [0, 0, 10, 2, TRUE], // with $step
  805. [10, 0, 10, 2, TRUE],
  806. [10, 0, 10, 3, FALSE], // max outside $step
  807. [12, 0, 12, 3, TRUE],
  808. // Empty test
  809. ['', 5, 10, NULL, FALSE],
  810. [NULL, 5, 10, NULL, FALSE],
  811. [FALSE, 5, 10, NULL, FALSE],
  812. ];
  813. }
  814. /**
  815. * Tests Valid::range()
  816. *
  817. * Tests if a number is within a range.
  818. *
  819. * @test
  820. * @dataProvider provider_range
  821. * @param integer $number Number to test
  822. * @param integer $min Lower bound
  823. * @param integer $max Upper bound
  824. * @param boolean $expected Is Number within the bounds of $min && $max
  825. */
  826. public function test_range($number, $min, $max, $step, $expected)
  827. {
  828. $this->AssertSame(
  829. $expected,
  830. Valid::range($number, $min, $max, $step)
  831. );
  832. }
  833. /**
  834. * Provides test data for test_url()
  835. *
  836. * @return array
  837. */
  838. public function provider_url()
  839. {
  840. $data = [
  841. ['http://google.com', TRUE],
  842. ['http://google.com/', TRUE],
  843. ['http://google.com/?q=abc', TRUE],
  844. ['http://google.com/#hash', TRUE],
  845. ['http://localhost', TRUE],
  846. ['http://hello-world.pl', TRUE],
  847. ['http://hello--world.pl', TRUE],
  848. ['http://h.e.l.l.0.pl', TRUE],
  849. ['http://server.tld/get/info', TRUE],
  850. ['http://127.0.0.1', TRUE],
  851. ['http://127.0.0.1:80', TRUE],
  852. ['http://user@127.0.0.1', TRUE],
  853. ['http://user:pass@127.0.0.1', TRUE],
  854. ['ftp://my.server.com', TRUE],
  855. ['rss+xml://rss.example.com', TRUE],
  856. ['http://google.2com', FALSE],
  857. ['http://google.com?q=abc', FALSE],
  858. ['http://google.com#hash', FALSE],
  859. ['http://hello-.pl', FALSE],
  860. ['http://hel.-lo.world.pl', FALSE],
  861. ['http://ww£.google.com', FALSE],
  862. ['http://127.0.0.1234', FALSE],
  863. ['http://127.0.0.1.1', FALSE],
  864. ['http://user:@127.0.0.1', FALSE],
  865. ["http://finalnewline.com\n", FALSE],
  866. // Empty test
  867. ['', FALSE],
  868. [NULL, FALSE],
  869. [FALSE, FALSE],
  870. ];
  871. $data[] = ['http://'.str_repeat('123456789.', 25).'com/', TRUE]; // 253 chars
  872. $data[] = ['http://'.str_repeat('123456789.', 25).'info/', FALSE]; // 254 chars
  873. return $data;
  874. }
  875. /**
  876. * Tests Valid::url()
  877. *
  878. * @test
  879. * @dataProvider provider_url
  880. * @param string $url The url to test
  881. * @param boolean $expected Is it valid?
  882. */
  883. public function test_url($url, $expected)
  884. {
  885. $this->assertSame(
  886. $expected,
  887. Valid::url($url)
  888. );
  889. }
  890. /**
  891. * DataProvider for the valid::matches() test
  892. */
  893. public function provider_matches()
  894. {
  895. return [
  896. [['a' => 'hello', 'b' => 'hello'], 'a', 'b', TRUE],
  897. [['a' => 'hello', 'b' => 'hello '], 'a', 'b', FALSE],
  898. [['a' => '1', 'b' => 1], 'a', 'b', FALSE],
  899. // Empty test
  900. [['a' => '', 'b' => 'hello'], 'a', 'b', FALSE],
  901. [['a' => NULL, 'b' => 'hello'], 'a', 'b', FALSE],
  902. [['a' => FALSE, 'b' => 'hello'], 'a', 'b', FALSE],
  903. ];
  904. }
  905. /**
  906. * Tests Valid::matches()
  907. *
  908. * Tests if a field matches another from an array of data
  909. *
  910. * @test
  911. * @dataProvider provider_matches
  912. * @param array $data Array of fields
  913. * @param integer $field First field name
  914. * @param integer $match Field name that must match $field in $data
  915. * @param boolean $expected Do the two fields match?
  916. */
  917. public function test_matches($data, $field, $match, $expected)
  918. {
  919. $this->AssertSame(
  920. $expected,
  921. Valid::matches($data, $field, $match)
  922. );
  923. }
  924. }