UtilUrlTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. use PHPUnit\Framework\Attributes\DataProvider;
  3. /**
  4. * URL helpers, link detection, markers, and method-name conversion.
  5. */
  6. class UtilUrlTest extends \PHPUnit\Framework\TestCase
  7. {
  8. #[DataProvider('providerForEncodeUrlParams')]
  9. public function testEncodeUrlParams($a, $aExclude, $aOnly, $sOut)
  10. {
  11. $this->assertSame($sOut, bx_encode_url_params($a, $aExclude, $aOnly));
  12. }
  13. static public function providerForEncodeUrlParams()
  14. {
  15. return [
  16. [['a' => '1', 'b' => '2'], [], false, 'a=1&b=2&'],
  17. [['a' => '1', 'b' => '2'], ['b'], false, 'a=1&'],
  18. [['a' => '1', 'b' => '2'], [], ['b'], 'b=2&'],
  19. [['q' => 'a b'], [], false, 'q=a%20b&'],
  20. [['ids' => ['1', '2']], [], false, 'ids[]=1&ids[]=2&'],
  21. ];
  22. }
  23. #[DataProvider('providerForParseStr')]
  24. public function testParseStr($sIn, $aOut)
  25. {
  26. $this->assertSame($aOut, bx_parse_str($sIn));
  27. }
  28. static public function providerForParseStr()
  29. {
  30. return [
  31. ['a=1&b=2', ['a' => '1', 'b' => '2']],
  32. ['q=%20', ['q' => '%20']],
  33. ['ids[]=1&ids[]=2', ['ids' => ['1', '2']]],
  34. ['a=1&a=2', ['a' => ['1', '2']]],
  35. ];
  36. }
  37. #[DataProvider('providerForAppendUrlParams')]
  38. public function testAppendUrlParams($sUrl, $mixedParams, $bEncode, $sOut)
  39. {
  40. $this->assertSame($sOut, bx_append_url_params($sUrl, $mixedParams, $bEncode));
  41. }
  42. static public function providerForAppendUrlParams()
  43. {
  44. return [
  45. ['http://example.com', ['a' => '1'], true, 'http://example.com?a=1'],
  46. ['http://example.com?x=1', ['a' => '1'], true, 'http://example.com?x=1&a=1'],
  47. ['http://example.com', ['q' => 'a b'], true, 'http://example.com?q=a%20b'],
  48. ['http://example.com', ['q' => 'a b'], false, 'http://example.com?q=a b'],
  49. ['http://example.com', [], true, 'http://example.com'],
  50. ['http://example.com', ['ids' => ['1', '2']], true, 'http://example.com?ids[]=1&ids[]=2'],
  51. ];
  52. }
  53. public function testLinkifyExternalUrl()
  54. {
  55. $s = bx_linkify('see www.example.com now');
  56. $this->assertStringContainsString('<a ', $s);
  57. $this->assertStringContainsString('href="http://www.example.com"', $s);
  58. $this->assertStringContainsString('target="_blank"', $s);
  59. if (getParam('sys_add_nofollow') == 'on')
  60. $this->assertStringContainsString('rel="nofollow"', $s);
  61. else
  62. $this->assertStringNotContainsString('rel="nofollow"', $s);
  63. }
  64. #[DataProvider('providerForIsUrlInContent')]
  65. public function testIsUrlInContent($sContent, $bSkipLocal, $bOut)
  66. {
  67. $this->assertSame($bOut, bx_is_url_in_content($sContent, $bSkipLocal));
  68. }
  69. static public function providerForIsUrlInContent()
  70. {
  71. return [
  72. ['hello http://example.com', false, true],
  73. ['hello https://example.com', false, true],
  74. ['visit www.example.com', false, true],
  75. ['file.com archive', false, true],
  76. ['no links here', false, false],
  77. ];
  78. }
  79. public function testIsUrlInContentSkipsLocal()
  80. {
  81. if (!defined('BX_DOL_URL_ROOT') || !BX_DOL_URL_ROOT)
  82. $this->markTestSkipped('BX_DOL_URL_ROOT is not defined');
  83. $this->assertFalse(bx_is_url_in_content(BX_DOL_URL_ROOT . 'only', true));
  84. $this->assertTrue(bx_is_url_in_content(BX_DOL_URL_ROOT . ' and http://example.com', true));
  85. }
  86. #[DataProvider('providerForGenMethodName')]
  87. public function testGenMethodName($sIn, $sOut)
  88. {
  89. $this->assertSame($sOut, bx_gen_method_name($sIn));
  90. }
  91. static public function providerForGenMethodName()
  92. {
  93. return [
  94. ['some_method', 'SomeMethod'],
  95. ['is_spam', 'IsSpam'],
  96. ['alreadyCamel', 'AlreadyCamel'],
  97. ['one', 'One'],
  98. ];
  99. }
  100. #[DataProvider('providerForLtrimStr')]
  101. public function testLtrimStr($sString, $sPrefix, $sReplace, $sOut)
  102. {
  103. $this->assertSame($sOut, bx_ltrim_str($sString, $sPrefix, $sReplace));
  104. }
  105. static public function providerForLtrimStr()
  106. {
  107. return [
  108. ['/var/www/file', '/var/www/', '', 'file'],
  109. ['file', '/var/www/', '', 'file'],
  110. ['/var/www/file', '/var/www/', 'ROOT/', 'ROOT/file'],
  111. ['ROOT/file', '/var/www/', 'ROOT/', 'ROOT/file'],
  112. ];
  113. }
  114. #[DataProvider('providerForReplaceMarkers')]
  115. public function testReplaceMarkers($mixed, $aMarkers, $mixedOut)
  116. {
  117. $this->assertSame($mixedOut, bx_replace_markers($mixed, $aMarkers));
  118. }
  119. static public function providerForReplaceMarkers()
  120. {
  121. return [
  122. ['Hello {name}', ['name' => 'Ada'], 'Hello Ada'],
  123. ['{a} and {b}', ['a' => '1', 'b' => '2'], '1 and 2'],
  124. ['unchanged', [], 'unchanged'],
  125. ['{missing}', ['name' => 'Ada'], '{missing}'],
  126. [['x' => 'hi {name}'], ['name' => 'Ada'], ['x' => 'hi Ada']],
  127. ['null {x}', ['x' => null], 'null '],
  128. ];
  129. }
  130. }