Drupal investigation

UrlTest.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /*
  3. * This file is part of the webmozart/path-util package.
  4. *
  5. * (c) Bernhard Schussek <bschussek@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Webmozart\PathUtil\Tests;
  11. use Webmozart\PathUtil\Url;
  12. /**
  13. * @since 2.3
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. * @author Claudio Zizza <claudio@budgegeria.de>
  17. */
  18. class UrlTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @dataProvider provideMakeRelativeTests
  22. * @covers Webmozart\PathUtil\Url
  23. */
  24. public function testMakeRelative($absolutePath, $basePath, $relativePath)
  25. {
  26. $host = 'http://example.com';
  27. $relative = Url::makeRelative($host.$absolutePath, $host.$basePath);
  28. $this->assertSame($relativePath, $relative);
  29. $relative = Url::makeRelative($absolutePath, $host.$basePath);
  30. $this->assertSame($relativePath, $relative);
  31. }
  32. /**
  33. * @dataProvider provideMakeRelativeIsAlreadyRelativeTests
  34. * @covers Webmozart\PathUtil\Url
  35. */
  36. public function testMakeRelativeIsAlreadyRelative($absolutePath, $basePath, $relativePath)
  37. {
  38. $host = 'http://example.com';
  39. $relative = Url::makeRelative($absolutePath, $host.$basePath);
  40. $this->assertSame($relativePath, $relative);
  41. }
  42. /**
  43. * @dataProvider provideMakeRelativeTests
  44. * @covers Webmozart\PathUtil\Url
  45. */
  46. public function testMakeRelativeWithFullUrl($absolutePath, $basePath, $relativePath)
  47. {
  48. $host = 'ftp://user:password@example.com:8080';
  49. $relative = Url::makeRelative($host.$absolutePath, $host.$basePath);
  50. $this->assertSame($relativePath, $relative);
  51. }
  52. /**
  53. * @expectedException \InvalidArgumentException
  54. * @expectedExceptionMessage The URL must be a string. Got: array
  55. * @covers Webmozart\PathUtil\Url
  56. */
  57. public function testMakeRelativeFailsIfInvalidUrl()
  58. {
  59. Url::makeRelative(array(), 'http://example.com/webmozart/puli');
  60. }
  61. /**
  62. * @expectedException \InvalidArgumentException
  63. * @expectedExceptionMessage The base URL must be a string. Got: array
  64. * @covers Webmozart\PathUtil\Url
  65. */
  66. public function testMakeRelativeFailsIfInvalidBaseUrl()
  67. {
  68. Url::makeRelative('http://example.com/webmozart/puli/css/style.css', array());
  69. }
  70. /**
  71. * @expectedException \InvalidArgumentException
  72. * @expectedExceptionMessage "webmozart/puli" is not an absolute Url.
  73. * @covers Webmozart\PathUtil\Url
  74. */
  75. public function testMakeRelativeFailsIfBaseUrlNoUrl()
  76. {
  77. Url::makeRelative('http://example.com/webmozart/puli/css/style.css', 'webmozart/puli');
  78. }
  79. /**
  80. * @expectedException \InvalidArgumentException
  81. * @expectedExceptionMessage "" is not an absolute Url.
  82. * @covers Webmozart\PathUtil\Url
  83. */
  84. public function testMakeRelativeFailsIfBaseUrlEmpty()
  85. {
  86. Url::makeRelative('http://example.com/webmozart/puli/css/style.css', '');
  87. }
  88. /**
  89. * @expectedException \InvalidArgumentException
  90. * @expectedExceptionMessage The base URL must be a string. Got: NULL
  91. * @covers Webmozart\PathUtil\Url
  92. */
  93. public function testMakeRelativeFailsIfBaseUrlNull()
  94. {
  95. Url::makeRelative('http://example.com/webmozart/puli/css/style.css', null);
  96. }
  97. /**
  98. * @expectedException \InvalidArgumentException
  99. * @expectedExceptionMessage The URL "http://example.com" cannot be made relative to "http://example2.com" since
  100. * their host names are different.
  101. * @covers Webmozart\PathUtil\Url
  102. */
  103. public function testMakeRelativeFailsIfDifferentDomains()
  104. {
  105. Url::makeRelative('http://example.com/webmozart/puli/css/style.css', 'http://example2.com/webmozart/puli');
  106. }
  107. public function provideMakeRelativeTests()
  108. {
  109. return array(
  110. array('/webmozart/puli/css/style.css', '/webmozart/puli', 'css/style.css'),
  111. array('/webmozart/puli/css/style.css?key=value&key2=value', '/webmozart/puli', 'css/style.css?key=value&key2=value'),
  112. array('/webmozart/puli/css/style.css?key[]=value&key[]=value', '/webmozart/puli', 'css/style.css?key[]=value&key[]=value'),
  113. array('/webmozart/css/style.css', '/webmozart/puli', '../css/style.css'),
  114. array('/css/style.css', '/webmozart/puli', '../../css/style.css'),
  115. array('/', '/', ''),
  116. // relative to root
  117. array('/css/style.css', '/', 'css/style.css'),
  118. // same sub directories in different base directories
  119. array('/puli/css/style.css', '/webmozart/css', '../../puli/css/style.css'),
  120. array('/webmozart/puli/./css/style.css', '/webmozart/puli', 'css/style.css'),
  121. array('/webmozart/puli/../css/style.css', '/webmozart/puli', '../css/style.css'),
  122. array('/webmozart/puli/.././css/style.css', '/webmozart/puli', '../css/style.css'),
  123. array('/webmozart/puli/./../css/style.css', '/webmozart/puli', '../css/style.css'),
  124. array('/webmozart/puli/../../css/style.css', '/webmozart/puli', '../../css/style.css'),
  125. array('/webmozart/puli/css/style.css', '/webmozart/./puli', 'css/style.css'),
  126. array('/webmozart/puli/css/style.css', '/webmozart/../puli', '../webmozart/puli/css/style.css'),
  127. array('/webmozart/puli/css/style.css', '/webmozart/./../puli', '../webmozart/puli/css/style.css'),
  128. array('/webmozart/puli/css/style.css', '/webmozart/.././puli', '../webmozart/puli/css/style.css'),
  129. array('/webmozart/puli/css/style.css', '/webmozart/../../puli', '../webmozart/puli/css/style.css'),
  130. // first argument shorter than second
  131. array('/css', '/webmozart/puli', '../../css'),
  132. // second argument shorter than first
  133. array('/webmozart/puli', '/css', '../webmozart/puli'),
  134. array('', '', ''),
  135. );
  136. }
  137. public function provideMakeRelativeIsAlreadyRelativeTests()
  138. {
  139. return array(
  140. array('css/style.css', '/webmozart/puli', 'css/style.css'),
  141. array('css/style.css', '', 'css/style.css'),
  142. array('css/../style.css', '', 'style.css'),
  143. array('css/./style.css', '', 'css/style.css'),
  144. array('../style.css', '/', 'style.css'),
  145. array('./style.css', '/', 'style.css'),
  146. array('../../style.css', '/', 'style.css'),
  147. array('../../style.css', '', 'style.css'),
  148. array('./style.css', '', 'style.css'),
  149. array('../style.css', '', 'style.css'),
  150. array('./../style.css', '', 'style.css'),
  151. array('css/./../style.css', '', 'style.css'),
  152. array('css//style.css', '', 'css/style.css'),
  153. );
  154. }
  155. }