/tests/unit/suites/libraries/joomla/string/JStringTest.php

https://github.com/dextercowley/joomla-cms · PHP · 911 lines · 312 code · 70 blank · 529 comment · 21 complexity · fbd7a74603ab704590ed6538711c7d31 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. * @subpackage String
  5. *
  6. * @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. require_once 'TestHelpers/JString-helper-dataset.php';
  10. /**
  11. * Test class for JString.
  12. * Generated by PHPUnit on 2009-10-26 at 22:29:34.
  13. *
  14. * @package Joomla.UnitTest
  15. * @subpackage String
  16. * @since 11.3
  17. */
  18. class JStringTest extends PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var JString
  22. * @since 11.2
  23. */
  24. protected $object;
  25. /**
  26. * Test...
  27. *
  28. * @return array
  29. *
  30. * @since 11.2
  31. */
  32. public function getIncrementData()
  33. {
  34. return JStringTest_DataSet::$increment;
  35. }
  36. /**
  37. * Test...
  38. *
  39. * @return array
  40. *
  41. * @since 11.2
  42. */
  43. public function getStrposData()
  44. {
  45. return JStringTest_DataSet::$strposTests;
  46. }
  47. /**
  48. * Test...
  49. *
  50. * @return array
  51. *
  52. * @since 11.2
  53. */
  54. public function getStrrposData()
  55. {
  56. return JStringTest_DataSet::$strrposTests;
  57. }
  58. /**
  59. * Test...
  60. *
  61. * @return array
  62. *
  63. * @since 11.2
  64. */
  65. public function getSubstrData()
  66. {
  67. return JStringTest_DataSet::$substrTests;
  68. }
  69. /**
  70. * Test...
  71. *
  72. * @return array
  73. *
  74. * @since 11.2
  75. */
  76. public function getStrtolowerData()
  77. {
  78. return JStringTest_DataSet::$strtolowerTests;
  79. }
  80. /**
  81. * Test...
  82. *
  83. * @return array
  84. *
  85. * @since 11.2
  86. */
  87. public function getStrtoupperData()
  88. {
  89. return JStringTest_DataSet::$strtoupperTests;
  90. }
  91. /**
  92. * Test...
  93. *
  94. * @return array
  95. *
  96. * @since 11.2
  97. */
  98. public function getStrlenData()
  99. {
  100. return JStringTest_DataSet::$strlenTests;
  101. }
  102. /**
  103. * Test...
  104. *
  105. * @return array
  106. *
  107. * @since 11.2
  108. */
  109. public function getStr_ireplaceData()
  110. {
  111. return JStringTest_DataSet::$str_ireplaceTests;
  112. }
  113. /**
  114. * Test...
  115. *
  116. * @return array
  117. *
  118. * @since 11.2
  119. */
  120. public function getStr_splitData()
  121. {
  122. return JStringTest_DataSet::$str_splitTests;
  123. }
  124. /**
  125. * Test...
  126. *
  127. * @return array
  128. *
  129. * @since 11.2
  130. */
  131. public function getStrcasecmpData()
  132. {
  133. return JStringTest_DataSet::$strcasecmpTests;
  134. }
  135. /**
  136. * Test...
  137. *
  138. * @return array
  139. *
  140. * @since 11.2
  141. */
  142. public function getStrcmpData()
  143. {
  144. return JStringTest_DataSet::$strcmpTests;
  145. }
  146. /**
  147. * Test...
  148. *
  149. * @return array
  150. *
  151. * @since 11.2
  152. */
  153. public function getStrcspnData()
  154. {
  155. return JStringTest_DataSet::$strcspnTests;
  156. }
  157. /**
  158. * Test...
  159. *
  160. * @return array
  161. *
  162. * @since 11.2
  163. */
  164. public function getStristrData()
  165. {
  166. return JStringTest_DataSet::$stristrTests;
  167. }
  168. /**
  169. * Test...
  170. *
  171. * @return array
  172. *
  173. * @since 11.2
  174. */
  175. public function getStrrevData()
  176. {
  177. return JStringTest_DataSet::$strrevTests;
  178. }
  179. /**
  180. * Test...
  181. *
  182. * @return array
  183. *
  184. * @since 11.2
  185. */
  186. public function getStrspnData()
  187. {
  188. return JStringTest_DataSet::$strspnTests;
  189. }
  190. /**
  191. * Test...
  192. *
  193. * @return array
  194. *
  195. * @since 11.2
  196. */
  197. public function getSubstr_replaceData()
  198. {
  199. return JStringTest_DataSet::$substr_replaceTests;
  200. }
  201. /**
  202. * Test...
  203. *
  204. * @return array
  205. *
  206. * @since 11.2
  207. */
  208. public function getLtrimData()
  209. {
  210. return JStringTest_DataSet::$ltrimTests;
  211. }
  212. /**
  213. * Test...
  214. *
  215. * @return array
  216. *
  217. * @since 11.2
  218. */
  219. public function getRtrimData()
  220. {
  221. return JStringTest_DataSet::$rtrimTests;
  222. }
  223. /**
  224. * Test...
  225. *
  226. * @return array
  227. *
  228. * @since 11.2
  229. */
  230. public function getTrimData()
  231. {
  232. return JStringTest_DataSet::$trimTests;
  233. }
  234. /**
  235. * Test...
  236. *
  237. * @return array
  238. *
  239. * @since 11.2
  240. */
  241. public function getUcfirstData()
  242. {
  243. return JStringTest_DataSet::$ucfirstTests;
  244. }
  245. /**
  246. * Test...
  247. *
  248. * @return array
  249. *
  250. * @since 11.2
  251. */
  252. public function getUcwordsData()
  253. {
  254. return JStringTest_DataSet::$ucwordsTests;
  255. }
  256. /**
  257. * Test...
  258. *
  259. * @return array
  260. *
  261. * @since 11.2
  262. */
  263. public function getTranscodeData()
  264. {
  265. return JStringTest_DataSet::$transcodeTests;
  266. }
  267. /**
  268. * Test...
  269. *
  270. * @return array
  271. *
  272. * @since 11.2
  273. */
  274. public function getValidData()
  275. {
  276. return JStringTest_DataSet::$validTests;
  277. }
  278. /**
  279. * Test...
  280. *
  281. * @param string $string @todo
  282. * @param string $style @todo
  283. * @param string $number @todo
  284. * @param string $expected @todo
  285. *
  286. * @return void
  287. *
  288. * @dataProvider getIncrementData
  289. * @since 11.2
  290. * @covers JString::increment
  291. */
  292. public function testIncrement($string, $style, $number, $expected)
  293. {
  294. $this->assertThat(
  295. JString::increment($string, $style, $number),
  296. $this->equalTo($expected)
  297. );
  298. }
  299. // @codingStandardsIgnoreStart
  300. // @todo Arguments with default values must be at the end of the argument list
  301. /**
  302. * Test...
  303. *
  304. * @param string $haystack @todo
  305. * @param string $needle @todo
  306. * @param integer $offset @todo
  307. * @param string $expect @todo
  308. *
  309. * @return void
  310. *
  311. * @dataProvider getStrposData
  312. * @since 11.2
  313. * @covers JString::strpos
  314. */
  315. public function testStrpos($haystack, $needle, $offset = 0, $expect)
  316. {
  317. $actual = JString::strpos($haystack, $needle, $offset);
  318. $this->assertEquals($expect, $actual);
  319. }
  320. // @codingStandardsIgnoreEnd
  321. // @codingStandardsIgnoreStart
  322. // @todo Arguments with default values must be at the end of the argument list
  323. /**
  324. * Test...
  325. *
  326. * @param string $haystack @todo
  327. * @param string $needle @todo
  328. * @param integer $offset @todo
  329. * @param string $expect @todo
  330. *
  331. * @return array
  332. *
  333. * @dataProvider getStrrposData
  334. * @since 11.2
  335. * @covers JString::strrpos
  336. */
  337. public function testStrrpos($haystack, $needle, $offset = 0, $expect)
  338. {
  339. $actual = JString::strrpos($haystack, $needle, $offset);
  340. $this->assertEquals($expect, $actual);
  341. }
  342. // @codingStandardsIgnoreEnd
  343. // @codingStandardsIgnoreStart
  344. // @todo Arguments with default values must be at the end of the argument list
  345. /**
  346. * Test...
  347. *
  348. * @param string $string @todo
  349. * @param string $start @todo
  350. * @param bool|int $length @todo
  351. * @param string $expect @todo
  352. *
  353. * @return array
  354. *
  355. * @dataProvider getSubstrData
  356. * @since 11.2
  357. * @covers JString::substr
  358. */
  359. public function testSubstr($string, $start, $length = false, $expect)
  360. {
  361. $actual = JString::substr($string, $start, $length);
  362. $this->assertEquals($expect, $actual);
  363. }
  364. // @codingStandardsIgnoreEnd
  365. /**
  366. * Test...
  367. *
  368. * @param string $string @todo
  369. * @param string $expect @todo
  370. *
  371. * @return array
  372. *
  373. * @dataProvider getStrtolowerData
  374. * @since 11.2
  375. * @covers JString::strtolower
  376. */
  377. public function testStrtolower($string, $expect)
  378. {
  379. $actual = JString::strtolower($string);
  380. $this->assertEquals($expect, $actual);
  381. }
  382. /**
  383. * Test...
  384. *
  385. * @param string $string @todo
  386. * @param string $expect @todo
  387. *
  388. * @return array
  389. *
  390. * @dataProvider getStrtoupperData
  391. * @since 11.2
  392. * @covers JString::strtoupper
  393. */
  394. public function testStrtoupper($string, $expect)
  395. {
  396. $actual = JString::strtoupper($string);
  397. $this->assertEquals($expect, $actual);
  398. }
  399. /**
  400. * Test...
  401. *
  402. * @param string $string @todo
  403. * @param string $expect @todo
  404. *
  405. * @return array
  406. *
  407. * @dataProvider getStrlenData
  408. * @since 11.2
  409. * @covers JString::strlen
  410. */
  411. public function testStrlen($string, $expect)
  412. {
  413. $actual = JString::strlen($string);
  414. $this->assertEquals($expect, $actual);
  415. }
  416. /**
  417. * Test...
  418. *
  419. * @param string $search @todo
  420. * @param string $replace @todo
  421. * @param string $subject @todo
  422. * @param integer $count @todo
  423. * @param string $expect @todo
  424. *
  425. * @return array
  426. *
  427. * @dataProvider getStr_ireplaceData
  428. * @since 11.2
  429. * @covers JString::str_ireplace
  430. */
  431. public function testStr_ireplace($search, $replace, $subject, $count, $expect)
  432. {
  433. $actual = JString::str_ireplace($search, $replace, $subject, $count);
  434. $this->assertEquals($expect, $actual);
  435. }
  436. /**
  437. * Test...
  438. *
  439. * @param string $string @todo
  440. * @param string $split_length @todo
  441. * @param string $expect @todo
  442. *
  443. * @return array
  444. *
  445. * @dataProvider getStr_splitData
  446. * @since 11.2
  447. * @covers JString::str_split
  448. */
  449. public function testStr_split($string, $split_length, $expect)
  450. {
  451. $actual = JString::str_split($string, $split_length);
  452. $this->assertEquals($expect, $actual);
  453. }
  454. /**
  455. * Test...
  456. *
  457. * @param string $string1 @todo
  458. * @param string $string2 @todo
  459. * @param string $locale @todo
  460. * @param string $expect @todo
  461. *
  462. * @return array
  463. *
  464. * @dataProvider getStrcasecmpData
  465. * @since 11.2
  466. * @covers JString::strcasecmp
  467. */
  468. public function testStrcasecmp($string1, $string2, $locale, $expect)
  469. {
  470. // Convert the $locale param to a string if it is an array
  471. if (is_array($locale))
  472. {
  473. $locale = "'" . implode("', '", $locale) . "'";
  474. }
  475. if (substr(php_uname(), 0, 6) == 'Darwin' && $locale != false)
  476. {
  477. $this->markTestSkipped('Darwin bug prevents foreign conversion from working properly');
  478. }
  479. elseif ($locale != false && !setlocale(LC_COLLATE, $locale))
  480. {
  481. $this->markTestSkipped("Locale {$locale} is not available.");
  482. }
  483. else
  484. {
  485. $actual = JString::strcasecmp($string1, $string2, $locale);
  486. if ($actual != 0)
  487. {
  488. $actual = $actual / abs($actual);
  489. }
  490. $this->assertEquals($expect, $actual);
  491. }
  492. }
  493. /**
  494. * Test...
  495. *
  496. * @param string $string1 @todo
  497. * @param string $string2 @todo
  498. * @param string $locale @todo
  499. * @param string $expect @todo
  500. *
  501. * @return array
  502. *
  503. * @dataProvider getStrcmpData
  504. * @since 11.2
  505. * @covers JString::strcmp
  506. */
  507. public function testStrcmp($string1, $string2, $locale, $expect)
  508. {
  509. // Convert the $locale param to a string if it is an array
  510. if (is_array($locale))
  511. {
  512. $locale = "'" . implode("', '", $locale) . "'";
  513. }
  514. if (substr(php_uname(), 0, 6) == 'Darwin' && $locale != false)
  515. {
  516. $this->markTestSkipped('Darwin bug prevents foreign conversion from working properly');
  517. }
  518. elseif ($locale != false && !setlocale(LC_COLLATE, $locale))
  519. {
  520. // If the locale is not available, we can't have to transcode the string and can't reliably compare it.
  521. $this->markTestSkipped("Locale {$locale} is not available.");
  522. }
  523. else
  524. {
  525. $actual = JString::strcmp($string1, $string2, $locale);
  526. if ($actual != 0)
  527. {
  528. $actual = $actual / abs($actual);
  529. }
  530. $this->assertEquals($expect, $actual);
  531. }
  532. }
  533. /**
  534. * Test...
  535. *
  536. * @param string $haystack @todo
  537. * @param string $needles @todo
  538. * @param integer $start @todo
  539. * @param integer $len @todo
  540. * @param string $expect @todo
  541. *
  542. * @return array
  543. *
  544. * @dataProvider getStrcspnData
  545. * @since 11.2
  546. * @covers JString::strcspn
  547. */
  548. public function testStrcspn($haystack, $needles, $start, $len, $expect)
  549. {
  550. $actual = JString::strcspn($haystack, $needles, $start, $len);
  551. $this->assertEquals($expect, $actual);
  552. }
  553. /**
  554. * Test...
  555. *
  556. * @param string $haystack @todo
  557. * @param string $needle @todo
  558. * @param string $expect @todo
  559. *
  560. * @return array
  561. *
  562. * @dataProvider getStristrData
  563. * @since 11.2
  564. * @covers JString::stristr
  565. */
  566. public function testStristr($haystack, $needle, $expect)
  567. {
  568. $actual = JString::stristr($haystack, $needle);
  569. $this->assertEquals($expect, $actual);
  570. }
  571. /**
  572. * Test...
  573. *
  574. * @param string $string @todo
  575. * @param string $expect @todo
  576. *
  577. * @return array
  578. *
  579. * @dataProvider getStrrevData
  580. * @since 11.2
  581. * @covers JString::strrev
  582. */
  583. public function testStrrev($string, $expect)
  584. {
  585. $actual = JString::strrev($string);
  586. $this->assertEquals($expect, $actual);
  587. }
  588. /**
  589. * Test...
  590. *
  591. * @param string $subject @todo
  592. * @param string $mask @todo
  593. * @param integer $start @todo
  594. * @param integer $length @todo
  595. * @param string $expect @todo
  596. *
  597. * @return array
  598. *
  599. * @dataProvider getStrspnData
  600. * @since 11.2
  601. * @covers JString::strspn
  602. */
  603. public function testStrspn($subject, $mask, $start, $length, $expect)
  604. {
  605. $actual = JString::strspn($subject, $mask, $start, $length);
  606. $this->assertEquals($expect, $actual);
  607. }
  608. /**
  609. * Test...
  610. *
  611. * @param string $string @todo
  612. * @param string $replacement @todo
  613. * @param integer $start @todo
  614. * @param integer $length @todo
  615. * @param string $expect @todo
  616. *
  617. * @return array
  618. *
  619. * @dataProvider getSubstr_replaceData
  620. * @since 11.2
  621. * @covers JString::substr_replace
  622. */
  623. public function testSubstr_replace($string, $replacement, $start, $length, $expect)
  624. {
  625. $actual = JString::substr_replace($string, $replacement, $start, $length);
  626. $this->assertEquals($expect, $actual);
  627. }
  628. /**
  629. * Test...
  630. *
  631. * @param string $string @todo
  632. * @param string $charlist @todo
  633. * @param string $expect @todo
  634. *
  635. * @return array
  636. *
  637. * @dataProvider getLtrimData
  638. * @since 11.2
  639. * @covers JString::ltrim
  640. */
  641. public function testLtrim($string, $charlist, $expect)
  642. {
  643. if ($charlist === null)
  644. {
  645. $actual = JString::ltrim($string);
  646. }
  647. else
  648. {
  649. $actual = JString::ltrim($string, $charlist);
  650. }
  651. $this->assertEquals($expect, $actual);
  652. }
  653. /**
  654. * Test...
  655. *
  656. * @param string $string @todo
  657. * @param string $charlist @todo
  658. * @param string $expect @todo
  659. *
  660. * @return array
  661. *
  662. * @dataProvider getRtrimData
  663. * @since 11.2
  664. * @covers JString::rtrim
  665. */
  666. public function testRtrim($string, $charlist, $expect)
  667. {
  668. if ($charlist === null)
  669. {
  670. $actual = JString::rtrim($string);
  671. }
  672. else
  673. {
  674. $actual = JString::rtrim($string, $charlist);
  675. }
  676. $this->assertEquals($expect, $actual);
  677. }
  678. /**
  679. * Test...
  680. *
  681. * @param string $string @todo
  682. * @param string $charlist @todo
  683. * @param string $expect @todo
  684. *
  685. * @return array
  686. *
  687. * @dataProvider getTrimData
  688. * @since 11.2
  689. * @covers JString::trim
  690. */
  691. public function testTrim($string, $charlist, $expect)
  692. {
  693. if ($charlist === null)
  694. {
  695. $actual = JString::trim($string);
  696. }
  697. else
  698. {
  699. $actual = JString::trim($string, $charlist);
  700. }
  701. $this->assertEquals($expect, $actual);
  702. }
  703. /**
  704. * Test...
  705. *
  706. * @param string $string @todo
  707. * @param string $delimiter @todo
  708. * @param string $newDelimiter @todo
  709. * @param string $expect @todo
  710. *
  711. * @return array
  712. *
  713. * @dataProvider getUcfirstData
  714. * @since 11.2
  715. * @covers JString::ucfirst
  716. */
  717. public function testUcfirst($string, $delimiter, $newDelimiter, $expect)
  718. {
  719. $actual = JString::ucfirst($string, $delimiter, $newDelimiter);
  720. $this->assertEquals($expect, $actual);
  721. }
  722. /**
  723. * Test...
  724. *
  725. * @param string $string @todo
  726. * @param string $expect @todo
  727. *
  728. * @return array
  729. *
  730. * @dataProvider getUcwordsData
  731. * @since 11.2
  732. * @covers JString::ucwords
  733. */
  734. public function testUcwords($string, $expect)
  735. {
  736. $actual = JString::ucwords($string);
  737. $this->assertEquals($expect, $actual);
  738. }
  739. /**
  740. * Test...
  741. *
  742. * @param string $source @todo
  743. * @param string $from_encoding @todo
  744. * @param string $to_encoding @todo
  745. * @param string $expect @todo
  746. *
  747. * @return array
  748. *
  749. * @dataProvider getTranscodeData
  750. * @since 11.2
  751. * @covers JString::transcode
  752. */
  753. public function testTranscode($source, $from_encoding, $to_encoding, $expect)
  754. {
  755. $actual = JString::transcode($source, $from_encoding, $to_encoding);
  756. $this->assertEquals($expect, $actual);
  757. }
  758. /**
  759. * Test...
  760. *
  761. * @param string $string @todo
  762. * @param string $expect @todo
  763. *
  764. * @return array
  765. *
  766. * @dataProvider getValidData
  767. * @since 11.2
  768. * @covers JString::valid
  769. */
  770. public function testValid($string, $expect)
  771. {
  772. $actual = JString::valid($string);
  773. $this->assertEquals($expect, $actual);
  774. }
  775. /**
  776. * Test...
  777. *
  778. * @param string $string @todo
  779. * @param string $expect @todo
  780. *
  781. * @return array
  782. *
  783. * @dataProvider getValidData
  784. * @since 11.2
  785. * @covers JString::compliant
  786. */
  787. public function testCompliant($string, $expect)
  788. {
  789. $actual = JString::compliant($string);
  790. $this->assertEquals($expect, $actual);
  791. }
  792. /**
  793. * Test...
  794. *
  795. * @return array
  796. *
  797. * @since 11.2
  798. * @covers JString::parse_url
  799. */
  800. public function testParse_Url()
  801. {
  802. $url = 'http://localhost/joomla_development/j16_trunk/administrator/index.php?option=com_contact&view=contact&layout=edit&id=5';
  803. $expected = parse_url($url);
  804. $actual = JString::parse_url($url);
  805. $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal');
  806. // Test all parts of query
  807. $url = 'https://john:doe@www.google.com:80/folder/page.html#id?var=kay&var2=key&true';
  808. $expected = parse_url($url);
  809. $actual = JString::parse_url($url);
  810. $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal');
  811. // Test special characters in URL
  812. $url = 'http://joomla.org/mytestpath/È';
  813. $expected = parse_url($url);
  814. // Fix up path for UTF-8 characters
  815. $expected['path'] = '/mytestpath/È';
  816. $actual = JString::parse_url($url);
  817. $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal');
  818. // Test special characters in URL
  819. $url = 'http://mydomain.com/!*\'();:@&=+$,/?%#[]" \\';
  820. $expected = parse_url($url);
  821. $actual = JString::parse_url($url);
  822. $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal');
  823. // Test url encoding in URL
  824. $url = 'http://mydomain.com/%21%2A%27%28%29%3B%3A%40%26%3D%24%2C%2F%3F%25%23%5B%22%20%5C';
  825. $expected = parse_url($url);
  826. $actual = JString::parse_url($url);
  827. $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal');
  828. // Test a mix of the above
  829. $url = 'http://john:doe@mydomain.com:80/%È21%25È3*%(';
  830. $expected = parse_url($url);
  831. // Fix up path for UTF-8 characters
  832. $expected['path'] = '/%È21%25È3*%(';
  833. $actual = JString::parse_url($url);
  834. $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal');
  835. // Test invalild URL
  836. $url = 'http:///mydomain.com';
  837. $expected = parse_url($url);
  838. $actual = JString::parse_url($url);
  839. $this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal');
  840. }
  841. }