ValidTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  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 <[email protected]>
  13. * @copyright (c) 2008-2012 Kohana Team
  14. * @license http://kohanaframework.org/license
  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 array(
  25. array('asdavafaiwnoabwiubafpowf', TRUE),
  26. array('!aidhfawiodb', FALSE),
  27. array('51535oniubawdawd78', FALSE),
  28. array('!"£$(G$W£(HFW£F(HQ)"n', FALSE),
  29. // UTF-8 tests
  30. array('あいうえお', TRUE, TRUE),
  31. array('¥', FALSE, TRUE),
  32. // Empty test
  33. array('', FALSE, FALSE),
  34. array(NULL, FALSE, FALSE),
  35. array(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 array(
  61. array('abcd1234', TRUE),
  62. array('abcd', TRUE),
  63. array('1234', TRUE),
  64. array('abc123&^/-', FALSE),
  65. // UTF-8 tests
  66. array('あいうえお', TRUE, TRUE),
  67. array('零一二三四五', TRUE, TRUE),
  68. array('あい四五£^£^', FALSE, TRUE),
  69. // Empty test
  70. array('', FALSE, FALSE),
  71. array(NULL, FALSE, FALSE),
  72. array(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 array(
  98. array('abcdef', TRUE),
  99. array('12345', TRUE),
  100. array('abcd1234', TRUE),
  101. array('abcd1234-', TRUE),
  102. array('abc123&^/-', FALSE),
  103. // Empty test
  104. array('', FALSE),
  105. array(NULL, FALSE),
  106. array(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 array(
  140. array('now',TRUE),
  141. array('10 September 2010',TRUE),
  142. array('+1 day',TRUE),
  143. array('+1 week',TRUE),
  144. array('+1 week 2 days 4 hours 2 seconds',TRUE),
  145. array('next Thursday',TRUE),
  146. array('last Monday',TRUE),
  147. array('blarg',FALSE),
  148. array('in the year 2000',FALSE),
  149. array('324824',FALSE),
  150. // Empty test
  151. array('', FALSE),
  152. array(NULL, FALSE),
  153. array(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 array(
  177. // Empty test
  178. array('', 2, NULL, FALSE),
  179. array(NULL, 2, NULL, FALSE),
  180. array(FALSE, 2, NULL, FALSE),
  181. array('45.1664', 3, NULL, FALSE),
  182. array('45.1664', 4, NULL, TRUE),
  183. array('45.1664', 4, 2, TRUE),
  184. array('-45.1664', 4, NULL, TRUE),
  185. array('+45.1664', 4, NULL, TRUE),
  186. array('-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 array(
  214. array('12345', TRUE),
  215. array('10.5', FALSE),
  216. array('abcde', FALSE),
  217. array('abcd1234', FALSE),
  218. array('-5', FALSE),
  219. array(-5, FALSE),
  220. // Empty test
  221. array('', FALSE),
  222. array(NULL, FALSE),
  223. array(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 array(
  254. array('#000000', TRUE),
  255. array('#GGGGGG', FALSE),
  256. array('#AbCdEf', TRUE),
  257. array('#000', TRUE),
  258. array('#abc', TRUE),
  259. array('#DEF', TRUE),
  260. array('000000', TRUE),
  261. array('GGGGGG', FALSE),
  262. array('AbCdEf', TRUE),
  263. array('000', TRUE),
  264. array('DEF', TRUE),
  265. // Empty test
  266. array('', FALSE),
  267. array(NULL, FALSE),
  268. array(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 array(
  292. array('4222222222222', 'visa', TRUE),
  293. array('4012888888881881', 'visa', TRUE),
  294. array('4012888888881881', NULL, TRUE),
  295. array('4012888888881881', array('mastercard', 'visa'), TRUE),
  296. array('4012888888881881', array('discover', 'mastercard'), FALSE),
  297. array('4012888888881881', 'mastercard', FALSE),
  298. array('5105105105105100', 'mastercard', TRUE),
  299. array('6011111111111117', 'discover', TRUE),
  300. array('6011111111111117', 'visa', FALSE),
  301. // Empty test
  302. array('', NULL, FALSE),
  303. array(NULL, NULL, FALSE),
  304. array(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 array(
  330. array('4222222222222', TRUE),
  331. array('4012888888881881', TRUE),
  332. array('5105105105105100', TRUE),
  333. array('6011111111111117', TRUE),
  334. array('60111111111111.7', FALSE),
  335. array('6011111111111117X', FALSE),
  336. array('6011111111111117 ', FALSE),
  337. array('WORD ', FALSE),
  338. // Empty test
  339. array('', FALSE),
  340. array(NULL, FALSE),
  341. array(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 array(
  368. array('foo', TRUE, FALSE),
  369. array('foo', FALSE, FALSE),
  370. array('foo@bar', TRUE, TRUE),
  371. // RFC is less strict than the normal regex, presumably to allow
  372. // admin@localhost, therefore we IGNORE IT!!!
  373. array('foo@bar', FALSE, FALSE),
  374. array('[email protected]', FALSE, TRUE),
  375. array('foo@barcom:80', FALSE, FALSE),
  376. array('[email protected]', FALSE, TRUE),
  377. array('[email protected]', FALSE, TRUE),
  378. array('[email protected]', FALSE, TRUE),
  379. // RFC says 254 length max #4011
  380. array(Text::random(NULL, 200).'@'.Text::random(NULL, 50).'.com', FALSE, FALSE),
  381. // Empty test
  382. array('', TRUE, FALSE),
  383. array(NULL, TRUE, FALSE),
  384. array(FALSE, TRUE, FALSE),
  385. );
  386. }
  387. /**
  388. * Tests Valid::email()
  389. *
  390. * Check an email address for correct format.
  391. *
  392. * @test
  393. * @dataProvider provider_email
  394. * @param string $email Address to check
  395. * @param boolean $strict Use strict settings
  396. * @param boolean $correct Is $email address valid?
  397. */
  398. public function test_email($email, $strict, $correct)
  399. {
  400. $this->assertSame(
  401. $correct,
  402. Valid::email($email, $strict)
  403. );
  404. }
  405. /**
  406. * Returns test data for test_email_domain()
  407. *
  408. * @return array
  409. */
  410. public function provider_email_domain()
  411. {
  412. return array(
  413. array('google.com', TRUE),
  414. // Don't anybody dare register this...
  415. array('DAWOMAWIDAIWNDAIWNHDAWIHDAIWHDAIWOHDAIOHDAIWHD.com', FALSE),
  416. // Empty test
  417. array('', FALSE),
  418. array(NULL, FALSE),
  419. array(FALSE, FALSE),
  420. );
  421. }
  422. /**
  423. * Tests Valid::email_domain()
  424. *
  425. * Validate the domain of an email address by checking if the domain has a
  426. * valid MX record.
  427. *
  428. * Test skips on windows
  429. *
  430. * @test
  431. * @dataProvider provider_email_domain
  432. * @param string $email Email domain to check
  433. * @param boolean $correct Is it correct?
  434. */
  435. public function test_email_domain($email, $correct)
  436. {
  437. if ( ! $this->hasInternet())
  438. {
  439. $this->markTestSkipped('An internet connection is required for this test');
  440. }
  441. if ( ! Kohana::$is_windows OR version_compare(PHP_VERSION, '5.3.0', '>='))
  442. {
  443. $this->assertSame(
  444. $correct,
  445. Valid::email_domain($email)
  446. );
  447. }
  448. else
  449. {
  450. $this->markTestSkipped('checkdnsrr() was not added on windows until PHP 5.3');
  451. }
  452. }
  453. /**
  454. * Provides data for test_exact_length()
  455. *
  456. * @return array
  457. */
  458. public function provider_exact_length()
  459. {
  460. return array(
  461. array('somestring', 10, TRUE),
  462. array('somestring', 11, FALSE),
  463. array('anotherstring', 13, TRUE),
  464. // Empty test
  465. array('', 10, FALSE),
  466. array(NULL, 10, FALSE),
  467. array(FALSE, 10, FALSE),
  468. // Test array of allowed lengths
  469. array('somestring', array(1, 3, 5, 7, 9, 10), TRUE),
  470. array('somestring', array(1, 3, 5, 7, 9), FALSE),
  471. );
  472. }
  473. /**
  474. *
  475. * Tests Valid::exact_length()
  476. *
  477. * Checks that a field is exactly the right length.
  478. *
  479. * @test
  480. * @dataProvider provider_exact_length
  481. * @param string $string The string to length check
  482. * @param integer $length The length of the string
  483. * @param boolean $correct Is $length the actual length of the string?
  484. * @return bool
  485. */
  486. public function test_exact_length($string, $length, $correct)
  487. {
  488. return $this->assertSame(
  489. $correct,
  490. Valid::exact_length($string, $length),
  491. 'Reported string length is not correct'
  492. );
  493. }
  494. /**
  495. * Provides data for test_equals()
  496. *
  497. * @return array
  498. */
  499. public function provider_equals()
  500. {
  501. return array(
  502. array('foo', 'foo', TRUE),
  503. array('1', '1', TRUE),
  504. array(1, '1', FALSE),
  505. array('011', 011, FALSE),
  506. // Empty test
  507. array('', 123, FALSE),
  508. array(NULL, 123, FALSE),
  509. array(FALSE, 123, FALSE),
  510. );
  511. }
  512. /**
  513. * Tests Valid::equals()
  514. *
  515. * @test
  516. * @dataProvider provider_equals
  517. * @param string $string value to check
  518. * @param integer $required required value
  519. * @param boolean $correct is $string the same as $required?
  520. * @return boolean
  521. */
  522. public function test_equals($string, $required, $correct)
  523. {
  524. return $this->assertSame(
  525. $correct,
  526. Valid::equals($string, $required),
  527. 'Values are not equal'
  528. );
  529. }
  530. /**
  531. * DataProvider for the valid::ip() test
  532. * @return array
  533. */
  534. public function provider_ip()
  535. {
  536. return array(
  537. array('75.125.175.50', FALSE, TRUE),
  538. // PHP 5.3.6 fixed a bug that allowed 127.0.0.1 as a public ip: http://bugs.php.net/53150
  539. array('127.0.0.1', FALSE, version_compare(PHP_VERSION, '5.3.6', '<')),
  540. array('256.257.258.259', FALSE, FALSE),
  541. array('255.255.255.255', FALSE, FALSE),
  542. array('192.168.0.1', FALSE, FALSE),
  543. array('192.168.0.1', TRUE, TRUE),
  544. // Empty test
  545. array('', TRUE, FALSE),
  546. array(NULL, TRUE, FALSE),
  547. array(FALSE, TRUE, FALSE),
  548. );
  549. }
  550. /**
  551. * Tests Valid::ip()
  552. *
  553. * @test
  554. * @dataProvider provider_ip
  555. * @param string $input_ip
  556. * @param boolean $allow_private
  557. * @param boolean $expected_result
  558. */
  559. public function test_ip($input_ip, $allow_private, $expected_result)
  560. {
  561. $this->assertEquals(
  562. $expected_result,
  563. Valid::ip($input_ip, $allow_private)
  564. );
  565. }
  566. /**
  567. * Returns test data for test_max_length()
  568. *
  569. * @return array
  570. */
  571. public function provider_max_length()
  572. {
  573. return array(
  574. // Border line
  575. array('some', 4, TRUE),
  576. // Exceeds
  577. array('KOHANARULLLES', 2, FALSE),
  578. // Under
  579. array('CakeSucks', 10, TRUE),
  580. // Empty test
  581. array('', -10, FALSE),
  582. array(NULL, -10, FALSE),
  583. array(FALSE, -10, FALSE),
  584. );
  585. }
  586. /**
  587. * Tests Valid::max_length()
  588. *
  589. * Checks that a field is short enough.
  590. *
  591. * @test
  592. * @dataProvider provider_max_length
  593. * @param string $string String to test
  594. * @param integer $maxlength Max length for this string
  595. * @param boolean $correct Is $string <= $maxlength
  596. */
  597. public function test_max_length($string, $maxlength, $correct)
  598. {
  599. $this->assertSame(
  600. $correct,
  601. Valid::max_length($string, $maxlength)
  602. );
  603. }
  604. /**
  605. * Returns test data for test_min_length()
  606. *
  607. * @return array
  608. */
  609. public function provider_min_length()
  610. {
  611. return array(
  612. array('This is obviously long enough', 10, TRUE),
  613. array('This is not', 101, FALSE),
  614. array('This is on the borderline', 25, TRUE),
  615. // Empty test
  616. array('', 10, FALSE),
  617. array(NULL, 10, FALSE),
  618. array(FALSE, 10, FALSE),
  619. );
  620. }
  621. /**
  622. * Tests Valid::min_length()
  623. *
  624. * Checks that a field is long enough.
  625. *
  626. * @test
  627. * @dataProvider provider_min_length
  628. * @param string $string String to compare
  629. * @param integer $minlength The minimum allowed length
  630. * @param boolean $correct Is $string 's length >= $minlength
  631. */
  632. public function test_min_length($string, $minlength, $correct)
  633. {
  634. $this->assertSame(
  635. $correct,
  636. Valid::min_length($string, $minlength)
  637. );
  638. }
  639. /**
  640. * Returns test data for test_not_empty()
  641. *
  642. * @return array
  643. */
  644. public function provider_not_empty()
  645. {
  646. // Create a blank arrayObject
  647. $ao = new ArrayObject;
  648. // arrayObject with value
  649. $ao1 = new ArrayObject;
  650. $ao1['test'] = 'value';
  651. return array(
  652. array(array(), FALSE),
  653. array(NULL, FALSE),
  654. array('', FALSE),
  655. array($ao, FALSE),
  656. array($ao1, TRUE),
  657. array(array(NULL), TRUE),
  658. array(0, TRUE),
  659. array('0', TRUE),
  660. array('Something', TRUE),
  661. );
  662. }
  663. /**
  664. * Tests Valid::not_empty()
  665. *
  666. * Checks if a field is not empty.
  667. *
  668. * @test
  669. * @dataProvider provider_not_empty
  670. * @param mixed $value Value to check
  671. * @param boolean $empty Is the value really empty?
  672. */
  673. public function test_not_empty($value, $empty)
  674. {
  675. return $this->assertSame(
  676. $empty,
  677. Valid::not_empty($value)
  678. );
  679. }
  680. /**
  681. * DataProvider for the Valid::numeric() test
  682. */
  683. public function provider_numeric()
  684. {
  685. return array(
  686. array(12345, TRUE),
  687. array(123.45, TRUE),
  688. array('12345', TRUE),
  689. array('10.5', TRUE),
  690. array('-10.5', TRUE),
  691. array('10.5a', FALSE),
  692. // @issue 3240
  693. array(.4, TRUE),
  694. array(-.4, TRUE),
  695. array(4., TRUE),
  696. array(-4., TRUE),
  697. array('.5', TRUE),
  698. array('-.5', TRUE),
  699. array('5.', TRUE),
  700. array('-5.', TRUE),
  701. array('.', FALSE),
  702. array('1.2.3', FALSE),
  703. // Empty test
  704. array('', FALSE),
  705. array(NULL, FALSE),
  706. array(FALSE, FALSE),
  707. );
  708. }
  709. /**
  710. * Tests Valid::numeric()
  711. *
  712. * @test
  713. * @dataProvider provider_numeric
  714. * @param string $input Input to test
  715. * @param boolean $expected Whether or not $input is numeric
  716. */
  717. public function test_numeric($input, $expected)
  718. {
  719. $this->assertSame(
  720. $expected,
  721. Valid::numeric($input)
  722. );
  723. }
  724. /**
  725. * Provides test data for test_phone()
  726. * @return array
  727. */
  728. public function provider_phone()
  729. {
  730. return array(
  731. array('0163634840', NULL, TRUE),
  732. array('+27173634840', NULL, TRUE),
  733. array('123578', NULL, FALSE),
  734. // Some uk numbers
  735. array('01234456778', NULL, TRUE),
  736. array('+0441234456778', NULL, FALSE),
  737. // Google UK case you're interested
  738. array('+44 20-7031-3000', array(12), TRUE),
  739. // BT Corporate
  740. array('020 7356 5000', NULL, TRUE),
  741. // Empty test
  742. array('', NULL, FALSE),
  743. array(NULL, NULL, FALSE),
  744. array(FALSE, NULL, FALSE),
  745. );
  746. }
  747. /**
  748. * Tests Valid::phone()
  749. *
  750. * @test
  751. * @dataProvider provider_phone
  752. * @param string $phone Phone number to test
  753. * @param boolean $expected Is $phone valid
  754. */
  755. public function test_phone($phone, $lengths, $expected)
  756. {
  757. $this->assertSame(
  758. $expected,
  759. Valid::phone($phone, $lengths)
  760. );
  761. }
  762. /**
  763. * DataProvider for the valid::regex() test
  764. */
  765. public function provider_regex()
  766. {
  767. return array(
  768. array('hello world', '/[a-zA-Z\s]++/', TRUE),
  769. array('123456789', '/[0-9]++/', TRUE),
  770. array('£$%£%', '/[abc]/', FALSE),
  771. array('Good evening', '/hello/', FALSE),
  772. // Empty test
  773. array('', '/hello/', FALSE),
  774. array(NULL, '/hello/', FALSE),
  775. array(FALSE, '/hello/', FALSE),
  776. );
  777. }
  778. /**
  779. * Tests Valid::range()
  780. *
  781. * Tests if a number is within a range.
  782. *
  783. * @test
  784. * @dataProvider provider_regex
  785. * @param string $value Value to test against
  786. * @param string $regex Valid pcre regular expression
  787. * @param bool $expected Does the value match the expression?
  788. */
  789. public function test_regex($value, $regex, $expected)
  790. {
  791. $this->AssertSame(
  792. $expected,
  793. Valid::regex($value, $regex)
  794. );
  795. }
  796. /**
  797. * DataProvider for the valid::range() test
  798. */
  799. public function provider_range()
  800. {
  801. return array(
  802. array(1, 0, 2, NULL, TRUE),
  803. array(-1, -5, 0, NULL, TRUE),
  804. array(-1, 0, 1, NULL, FALSE),
  805. array(1, 0, 0, NULL, FALSE),
  806. array(2147483647, 0, 200000000000000, NULL, TRUE),
  807. array(-2147483647, -2147483655, 2147483645, NULL, TRUE),
  808. // #4043
  809. array(2, 0, 10, 2, TRUE),
  810. array(3, 0, 10, 2, FALSE),
  811. // Empty test
  812. array('', 5, 10, NULL, FALSE),
  813. array(NULL, 5, 10, NULL, FALSE),
  814. array(FALSE, 5, 10, NULL, FALSE),
  815. );
  816. }
  817. /**
  818. * Tests Valid::range()
  819. *
  820. * Tests if a number is within a range.
  821. *
  822. * @test
  823. * @dataProvider provider_range
  824. * @param integer $number Number to test
  825. * @param integer $min Lower bound
  826. * @param integer $max Upper bound
  827. * @param boolean $expected Is Number within the bounds of $min && $max
  828. */
  829. public function test_range($number, $min, $max, $step, $expected)
  830. {
  831. $this->AssertSame(
  832. $expected,
  833. Valid::range($number, $min, $max, $step)
  834. );
  835. }
  836. /**
  837. * Provides test data for test_url()
  838. *
  839. * @return array
  840. */
  841. public function provider_url()
  842. {
  843. $data = array(
  844. array('http://google.com', TRUE),
  845. array('http://google.com/', TRUE),
  846. array('http://google.com/?q=abc', TRUE),
  847. array('http://google.com/#hash', TRUE),
  848. array('http://localhost', TRUE),
  849. array('http://hello-world.pl', TRUE),
  850. array('http://hello--world.pl', TRUE),
  851. array('http://h.e.l.l.0.pl', TRUE),
  852. array('http://server.tld/get/info', TRUE),
  853. array('http://127.0.0.1', TRUE),
  854. array('http://127.0.0.1:80', TRUE),
  855. array('http://[email protected]', TRUE),
  856. array('http://user:[email protected]', TRUE),
  857. array('ftp://my.server.com', TRUE),
  858. array('rss+xml://rss.example.com', TRUE),
  859. array('http://google.2com', FALSE),
  860. array('http://google.com?q=abc', FALSE),
  861. array('http://google.com#hash', FALSE),
  862. array('http://hello-.pl', FALSE),
  863. array('http://hel.-lo.world.pl', FALSE),
  864. array('http://ww£.google.com', FALSE),
  865. array('http://127.0.0.1234', FALSE),
  866. array('http://127.0.0.1.1', FALSE),
  867. array('http://user:@127.0.0.1', FALSE),
  868. array("http://finalnewline.com\n", FALSE),
  869. // Empty test
  870. array('', FALSE),
  871. array(NULL, FALSE),
  872. array(FALSE, FALSE),
  873. );
  874. $data[] = array('http://'.str_repeat('123456789.', 25).'com/', TRUE); // 253 chars
  875. $data[] = array('http://'.str_repeat('123456789.', 25).'info/', FALSE); // 254 chars
  876. return $data;
  877. }
  878. /**
  879. * Tests Valid::url()
  880. *
  881. * @test
  882. * @dataProvider provider_url
  883. * @param string $url The url to test
  884. * @param boolean $expected Is it valid?
  885. */
  886. public function test_url($url, $expected)
  887. {
  888. $this->assertSame(
  889. $expected,
  890. Valid::url($url)
  891. );
  892. }
  893. /**
  894. * DataProvider for the valid::matches() test
  895. */
  896. public function provider_matches()
  897. {
  898. return array(
  899. array(array('a' => 'hello', 'b' => 'hello'), 'a', 'b', TRUE),
  900. array(array('a' => 'hello', 'b' => 'hello '), 'a', 'b', FALSE),
  901. array(array('a' => '1', 'b' => 1), 'a', 'b', FALSE),
  902. // Empty test
  903. array(array('a' => '', 'b' => 'hello'), 'a', 'b', FALSE),
  904. array(array('a' => NULL, 'b' => 'hello'), 'a', 'b', FALSE),
  905. array(array('a' => FALSE, 'b' => 'hello'), 'a', 'b', FALSE),
  906. );
  907. }
  908. /**
  909. * Tests Valid::matches()
  910. *
  911. * Tests if a field matches another from an array of data
  912. *
  913. * @test
  914. * @dataProvider provider_matches
  915. * @param array $data Array of fields
  916. * @param integer $field First field name
  917. * @param integer $match Field name that must match $field in $data
  918. * @param boolean $expected Do the two fields match?
  919. */
  920. public function test_matches($data, $field, $match, $expected)
  921. {
  922. $this->AssertSame(
  923. $expected,
  924. Valid::matches($data, $field, $match)
  925. );
  926. }
  927. }