ElggCoreUrlHelpersTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @see \ElggCoreHelpersTest
  4. * @todo migrate similar simpletest tests to this class
  5. */
  6. class ElggCoreUrlHelpersTest extends \PHPUnit_Framework_TestCase {
  7. /**
  8. * Test if elgg_http_add_url_query_elements() preserves original url when no params are passed
  9. */
  10. public function testElggHttpAddURLQueryElementsPreserveURL() {
  11. $tests = array(
  12. array('', array(), '?'),
  13. array('/', array(), '/'),
  14. array('/path', array(), '/path'),
  15. array('example.com', array(), 'example.com'),
  16. array('example.com/path', array(), 'example.com/path'),
  17. array('http://example.com', array(), 'http://example.com?'),
  18. array('http://example.com/path', array(), 'http://example.com/path'),
  19. array('http://example.com/path#anchor', array(), 'http://example.com/path#anchor'),
  20. array('https://example.com', array(), 'https://example.com?'),
  21. array('https://example.com#anchor', array(), 'https://example.com?#anchor'),
  22. array('https://example.com/path', array(), 'https://example.com/path'),
  23. array('http://example-time.com', array(), 'http://example-time.com?'),
  24. array('http://example-time.com/path', array(), 'http://example-time.com/path'),
  25. array('ftp://example.com/', array(), 'ftp://example.com/'),
  26. array('ftp://example.com/file', array(), 'ftp://example.com/file'),
  27. array('app://endpoint', array(), 'app://endpoint?'),
  28. array('app://endpoint/path', array(), 'app://endpoint/path'),
  29. array('https://example.com?foo=123&bar=abc', array(), 'https://example.com?foo=123&bar=abc'),
  30. array('https://example.com/path?foo=123&bar=abc', array(), 'https://example.com/path?foo=123&bar=abc'),
  31. );
  32. foreach ($tests as $test) {
  33. list($input, $params, $output) = $test;
  34. $this->assertEquals($output, elgg_http_add_url_query_elements($input, $params));
  35. }
  36. }
  37. /**
  38. * Test elgg_http_add_url_query_elements() addition of parameters
  39. */
  40. public function testElggHttpAddURLQueryElementsAddElements() {
  41. $tests = array(
  42. array('', array('foo' => 'bar'), '?foo=bar'),
  43. array('/', array('foo' => 'bar'), '/?foo=bar'),
  44. array('/path', array('foo' => 'bar'), '/path?foo=bar'),
  45. array('example.com', array('foo' => 'bar'), 'example.com?foo=bar'),
  46. array('example.com/path', array('foo' => 'bar'), 'example.com/path?foo=bar'),
  47. array('http://example.com', array('foo' => 'bar'), 'http://example.com?foo=bar'),
  48. array('http://example.com/#anchor', array('foo' => 'bar'), 'http://example.com/?foo=bar#anchor'),
  49. array('http://example.com/path', array('foo' => 'bar'), 'http://example.com/path?foo=bar'),
  50. array('https://example.com', array('foo' => 'bar'), 'https://example.com?foo=bar'),
  51. array('https://example.com/path', array('foo' => 'bar'), 'https://example.com/path?foo=bar'),
  52. array('http://example-time.com', array('foo' => 'bar'), 'http://example-time.com?foo=bar'),
  53. array('http://example-time.com/path', array('foo' => 'bar'), 'http://example-time.com/path?foo=bar'),
  54. array('ftp://example.com/', array('foo' => 'bar'), 'ftp://example.com/?foo=bar'),
  55. array('ftp://example.com/file', array('foo' => 'bar'), 'ftp://example.com/file?foo=bar'),
  56. array('app://endpoint', array('foo' => 'bar'), 'app://endpoint?foo=bar'),
  57. array('app://endpoint/path', array('foo' => 'bar'), 'app://endpoint/path?foo=bar'),
  58. array('https://example.com?foo=123&bar=abc', array('foo2' => 'bar2'), 'https://example.com?foo=123&bar=abc&foo2=bar2'),
  59. array('https://example.com/path?foo=123&bar=abc', array('foo' => 'bar'), 'https://example.com/path?foo=bar&bar=abc'),
  60. array('https://example.com?foo=123&bar=abc', array('foo2' => 'bar2', '123' => 456), 'https://example.com?foo=123&bar=abc&foo2=bar2&123=456'),
  61. array('https://example.com/path?foo=123&bar=abc', array('foo' => 'bar'), 'https://example.com/path?foo=bar&bar=abc'),
  62. );
  63. foreach ($tests as $test) {
  64. list($input, $params, $output) = $test;
  65. $this->assertEquals($output, elgg_http_add_url_query_elements($input, $params));
  66. }
  67. }
  68. /**
  69. * Test elgg_http_add_url_query_elements() removal of parameters
  70. */
  71. public function testElggHttpAddURLQueryElementsRemoveElements() {
  72. $tests = array(
  73. array('?foo=bar', array('foo' => ''), '?foo='),
  74. array('?foo=bar', array('foo' => 0), '?foo=0'),
  75. array('?foo=bar', array('foo' => false), '?foo=0'),
  76. array('?foo=bar', array('foo' => null), '?'),
  77. array('/?foo=bar', array('foo' => null), '/'),
  78. array('/path?foo=bar', array('foo' => null), '/path'),
  79. array('example.com', array('foo' => null), 'example.com'),
  80. array('example.com?foo=bar', array('foo' => null), 'example.com'),
  81. array('example.com/path?foo=bar', array('foo' => null), 'example.com/path'),
  82. array('http://example.com', array('foo' => null), 'http://example.com?'),
  83. array('http://example.com?foo=bar', array('foo' => null), 'http://example.com?'),
  84. array('http://example.com/?foo=bar#anchor', array('foo' => null), 'http://example.com/#anchor'),
  85. array('http://example.com/path?foo=bar', array('foo' => null), 'http://example.com/path'),
  86. array('https://example.com?foo=bar', array('foo' => null), 'https://example.com?'),
  87. array('https://example.com/path?foo=bar', array('foo' => null), 'https://example.com/path'),
  88. array('http://example-time.com?foo=bar', array('foo' => null), 'http://example-time.com?'),
  89. array('http://example-time.com/path?foo=bar', array('foo' => null), 'http://example-time.com/path'),
  90. array('ftp://example.com/?foo=bar', array('foo' => null), 'ftp://example.com/'),
  91. array('ftp://example.com/file?foo=bar', array('foo' => null), 'ftp://example.com/file'),
  92. array('app://endpoint?foo=bar', array('foo' => null), 'app://endpoint?'),
  93. array('app://endpoint/path?foo=bar', array('foo' => null), 'app://endpoint/path'),
  94. //add and delete at the same time
  95. array('https://example.com?foo=123&bar=abc', array('foo' => null, 'foo2' => 'bar2'), 'https://example.com?bar=abc&foo2=bar2'),
  96. array('https://example.com/path?bar=abc&foo=123', array('foo' => null, 'foo2' => 'bar'), 'https://example.com/path?bar=abc&foo2=bar'),
  97. array('https://example.com?foo=123&bar=abc', array('foo' => null, 'foo2' => 'bar2', '123' => 456), 'https://example.com?bar=abc&foo2=bar2&123=456'),
  98. array('https://example.com/path?foo=123&bar=abc', array('foo2' => 'bar', 'foo' => null), 'https://example.com/path?bar=abc&foo2=bar'),
  99. );
  100. foreach ($tests as $test) {
  101. list($input, $params, $output) = $test;
  102. $this->assertEquals($output, elgg_http_add_url_query_elements($input, $params));
  103. if ($params === array('foo' => null)) {
  104. $this->assertEquals($output, elgg_http_remove_url_query_element($input, 'foo'));
  105. }
  106. }
  107. }
  108. }