URLTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Tests URL
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.url
  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_URLTest extends Unittest_TestCase
  17. {
  18. /**
  19. * Default values for the environment, see setEnvironment
  20. * @var array
  21. */
  22. // @codingStandardsIgnoreStart
  23. protected $environmentDefault = array(
  24. 'Kohana::$base_url' => '/kohana/',
  25. 'Kohana::$index_file'=> 'index.php',
  26. 'HTTP_HOST' => 'example.com',
  27. '_GET' => array(),
  28. );
  29. // @codingStandardsIgnoreEnd
  30. /**
  31. * Provides test data for test_base()
  32. *
  33. * @return array
  34. */
  35. public function provider_base()
  36. {
  37. return array(
  38. // $protocol, $index, $expected, $enviroment
  39. // Test with different combinations of parameters for max code coverage
  40. array(NULL, FALSE, '/kohana/'),
  41. array('http', FALSE, 'http://example.com/kohana/'),
  42. array(NULL, TRUE, '/kohana/index.php/'),
  43. array(NULL, TRUE, '/kohana/index.php/'),
  44. array('http', TRUE, 'http://example.com/kohana/index.php/'),
  45. array('https', TRUE, 'https://example.com/kohana/index.php/'),
  46. array('ftp', TRUE, 'ftp://example.com/kohana/index.php/'),
  47. // Test for automatic protocol detection, protocol = TRUE
  48. array(TRUE, TRUE, 'cli://example.com/kohana/index.php/', array('HTTPS' => FALSE, 'Request::$initial' => Request::factory('/')->protocol('cli'))),
  49. // Change base url'
  50. array('https', FALSE, 'https://example.com/kohana/', array('Kohana::$base_url' => 'omglol://example.com/kohana/')),
  51. // Use port in base url, issue #3307
  52. array('http', FALSE, 'http://example.com:8080/', array('Kohana::$base_url' => 'example.com:8080/')),
  53. // Use protocol from base url if none specified
  54. array(NULL, FALSE, 'http://www.example.com/', array('Kohana::$base_url' => 'http://www.example.com/')),
  55. // Use HTTP_HOST before SERVER_NAME
  56. array('http', FALSE, 'http://example.com/kohana/', array('HTTP_HOST' => 'example.com', 'SERVER_NAME' => 'example.org')),
  57. // Use SERVER_NAME if HTTP_HOST DNX
  58. array('http', FALSE, 'http://example.org/kohana/', array('HTTP_HOST' => NULL, 'SERVER_NAME' => 'example.org')),
  59. );
  60. }
  61. /**
  62. * Tests URL::base()
  63. *
  64. * @test
  65. * @dataProvider provider_base
  66. * @param boolean $protocol Parameter for Url::base()
  67. * @param boolean $index Parameter for Url::base()
  68. * @param string $expected Expected url
  69. * @param array $enviroment Array of enviroment vars to change @see Kohana_URLTest::setEnvironment()
  70. */
  71. public function test_base($protocol, $index, $expected, array $enviroment = array())
  72. {
  73. $this->setEnvironment($enviroment);
  74. $this->assertSame(
  75. $expected,
  76. URL::base($protocol, $index)
  77. );
  78. }
  79. /**
  80. * Provides test data for test_site()
  81. *
  82. * @return array
  83. */
  84. public function provider_site()
  85. {
  86. return array(
  87. array('', NULL, '/kohana/index.php/'),
  88. array('', 'http', 'http://example.com/kohana/index.php/'),
  89. array('my/site', NULL, '/kohana/index.php/my/site'),
  90. array('my/site', 'http', 'http://example.com/kohana/index.php/my/site'),
  91. // @ticket #3110
  92. array('my/site/page:5', NULL, '/kohana/index.php/my/site/page:5'),
  93. array('my/site/page:5', 'http', 'http://example.com/kohana/index.php/my/site/page:5'),
  94. array('my/site?var=asd&kohana=awesome', NULL, '/kohana/index.php/my/site?var=asd&kohana=awesome'),
  95. array('my/site?var=asd&kohana=awesome', 'http', 'http://example.com/kohana/index.php/my/site?var=asd&kohana=awesome'),
  96. array('?kohana=awesome&life=good', NULL, '/kohana/index.php/?kohana=awesome&life=good'),
  97. array('?kohana=awesome&life=good', 'http', 'http://example.com/kohana/index.php/?kohana=awesome&life=good'),
  98. array('?kohana=awesome&life=good#fact', NULL, '/kohana/index.php/?kohana=awesome&life=good#fact'),
  99. array('?kohana=awesome&life=good#fact', 'http', 'http://example.com/kohana/index.php/?kohana=awesome&life=good#fact'),
  100. array('some/long/route/goes/here?kohana=awesome&life=good#fact', NULL, '/kohana/index.php/some/long/route/goes/here?kohana=awesome&life=good#fact'),
  101. array('some/long/route/goes/here?kohana=awesome&life=good#fact', 'http', 'http://example.com/kohana/index.php/some/long/route/goes/here?kohana=awesome&life=good#fact'),
  102. array('/route/goes/here?kohana=awesome&life=good#fact', 'https', 'https://example.com/kohana/index.php/route/goes/here?kohana=awesome&life=good#fact'),
  103. array('/route/goes/here?kohana=awesome&life=good#fact', 'ftp', 'ftp://example.com/kohana/index.php/route/goes/here?kohana=awesome&life=good#fact'),
  104. );
  105. }
  106. /**
  107. * Tests URL::site()
  108. *
  109. * @test
  110. * @dataProvider provider_site
  111. * @param string $uri URI to use
  112. * @param boolean|string $protocol Protocol to use
  113. * @param string $expected Expected result
  114. * @param array $enviroment Array of enviroment vars to set
  115. */
  116. public function test_site($uri, $protocol, $expected, array $enviroment = array())
  117. {
  118. $this->setEnvironment($enviroment);
  119. $this->assertSame(
  120. $expected,
  121. URL::site($uri, $protocol)
  122. );
  123. }
  124. /**
  125. * Provides test data for test_site_url_encode_uri()
  126. * See issue #2680
  127. *
  128. * @return array
  129. */
  130. public function provider_site_url_encode_uri()
  131. {
  132. $provider = array(
  133. array('test', 'encode'),
  134. array('test', 'éñçø∂ë∂'),
  135. array('†é߆', 'encode'),
  136. array('†é߆', 'éñçø∂ë∂', 'µåñ¥'),
  137. );
  138. foreach ($provider as $i => $params)
  139. {
  140. // Every non-ASCII character except for forward slash should be encoded...
  141. $expected = implode('/', array_map('rawurlencode', $params));
  142. // ... from a URI that is not encoded
  143. $uri = implode('/', $params);
  144. $provider[$i] = array("/kohana/index.php/{$expected}", $uri);
  145. }
  146. return $provider;
  147. }
  148. /**
  149. * Tests URL::site for proper URL encoding when working with non-ASCII characters.
  150. *
  151. * @test
  152. * @dataProvider provider_site_url_encode_uri
  153. */
  154. public function test_site_url_encode_uri($expected, $uri)
  155. {
  156. $this->assertSame($expected, URL::site($uri, FALSE));
  157. }
  158. /**
  159. * Provides test data for test_title()
  160. * @return array
  161. */
  162. public function provider_title()
  163. {
  164. return array(
  165. // Tests that..
  166. // Title is converted to lowercase
  167. array('we-shall-not-be-moved', 'WE SHALL NOT BE MOVED', '-'),
  168. // Excessive white space is removed and replaced with 1 char
  169. array('thissssss-is-it', 'THISSSSSS IS IT ', '-'),
  170. // separator is either - (dash) or _ (underscore) & others are converted to underscores
  171. array('some-title', 'some title', '-'),
  172. array('some_title', 'some title', '_'),
  173. array('some!title', 'some title', '!'),
  174. array('some:title', 'some title', ':'),
  175. // Numbers are preserved
  176. array('99-ways-to-beat-apple', '99 Ways to beat apple', '-'),
  177. // ... with lots of spaces & caps
  178. array('99_ways_to_beat_apple', '99 ways TO beat APPLE', '_'),
  179. array('99-ways-to-beat-apple', '99 ways TO beat APPLE', '-'),
  180. // Invalid characters are removed
  181. array('each-gbp-is-now-worth-32-usd', 'Each GBP(£) is now worth 32 USD($)', '-'),
  182. // ... inc. separator
  183. array('is-it-reusable-or-re-usable', 'Is it reusable or re-usable?', '-'),
  184. // Doing some crazy UTF8 tests
  185. array('espana-wins', 'España-wins', '-', TRUE),
  186. );
  187. }
  188. /**
  189. * Tests URL::title()
  190. *
  191. * @test
  192. * @dataProvider provider_title
  193. * @param string $title Input to convert
  194. * @param string $separator Seperate to replace invalid characters with
  195. * @param string $expected Expected result
  196. */
  197. public function test_title($expected, $title, $separator, $ascii_only = FALSE)
  198. {
  199. $this->assertSame(
  200. $expected,
  201. URL::title($title, $separator, $ascii_only)
  202. );
  203. }
  204. /**
  205. * Provides test data for URL::query()
  206. * @return array
  207. */
  208. public function provider_query()
  209. {
  210. return array(
  211. array(array(), '', NULL),
  212. array(array('_GET' => array('test' => 'data')), '?test=data', NULL),
  213. array(array(), '?test=data', array('test' => 'data')),
  214. array(array('_GET' => array('more' => 'data')), '?more=data&test=data', array('test' => 'data')),
  215. array(array('_GET' => array('sort' => 'down')), '?test=data', array('test' => 'data'), FALSE),
  216. // http://dev.kohanaframework.org/issues/3362
  217. array(array(), '', array('key' => NULL)),
  218. array(array(), '?key=0', array('key' => FALSE)),
  219. array(array(), '?key=1', array('key' => TRUE)),
  220. array(array('_GET' => array('sort' => 'down')), '?sort=down&key=1', array('key' => TRUE)),
  221. array(array('_GET' => array('sort' => 'down')), '?sort=down&key=0', array('key' => FALSE)),
  222. // @issue 4240
  223. array(array('_GET' => array('foo' => array('a' => 100))), '?foo%5Ba%5D=100&foo%5Bb%5D=bar', array('foo' => array('b' => 'bar'))),
  224. array(array('_GET' => array('a' => 'a')), '?a=b', array('a' => 'b')),
  225. );
  226. }
  227. /**
  228. * Tests URL::query()
  229. *
  230. * @test
  231. * @dataProvider provider_query
  232. * @param array $enviroment Set environment
  233. * @param string $expected Expected result
  234. * @param array $params Query string
  235. * @param boolean $use_get Combine with GET parameters
  236. */
  237. public function test_query($enviroment, $expected, $params, $use_get = TRUE)
  238. {
  239. $this->setEnvironment($enviroment);
  240. $this->assertSame(
  241. $expected,
  242. URL::query($params, $use_get)
  243. );
  244. }
  245. }