/tests/url.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests · PHP · 342 lines · 257 code · 64 blank · 21 comment · 9 complexity · c11413318edadb50d718e825239570b1 MD5 · raw file

  1. <?php
  2. // tests for link-template.php and related URL functions
  3. class Tests_URL extends WP_UnitTestCase {
  4. var $_old_server;
  5. function setUp() {
  6. parent::setUp();
  7. $this->_old_server = $_SERVER;
  8. }
  9. function tearDown() {
  10. $_SERVER = $this->_old_server;
  11. parent::tearDown();
  12. }
  13. function test_is_ssl_positive() {
  14. $_SERVER['HTTPS'] = 'on';
  15. $this->assertTrue( is_ssl() );
  16. $_SERVER['HTTPS'] = 'ON';
  17. $this->assertTrue( is_ssl() );
  18. $_SERVER['HTTPS'] = '1';
  19. $this->assertTrue( is_ssl() );
  20. unset( $_SERVER['HTTPS'] );
  21. $_SERVER['SERVER_PORT'] = '443';
  22. $this->assertTrue( is_ssl() );
  23. }
  24. function test_is_ssl_negative() {
  25. $_SERVER['HTTPS'] = 'off';
  26. $this->assertFalse( is_ssl() );
  27. $_SERVER['HTTPS'] = 'OFF';
  28. $this->assertFalse( is_ssl() );
  29. unset($_SERVER['HTTPS']);
  30. $this->assertFalse( is_ssl() );
  31. }
  32. function test_admin_url_valid() {
  33. $paths = array(
  34. '' => "/wp-admin/",
  35. 'foo' => "/wp-admin/foo",
  36. '/foo' => "/wp-admin/foo",
  37. '/foo/' => "/wp-admin/foo/",
  38. 'foo.php' => "/wp-admin/foo.php",
  39. '/foo.php' => "/wp-admin/foo.php",
  40. '/foo.php?bar=1' => "/wp-admin/foo.php?bar=1",
  41. );
  42. $https = array('on', 'off');
  43. foreach ($https as $val) {
  44. $_SERVER['HTTPS'] = $val;
  45. $siteurl = get_option('siteurl');
  46. if ( $val == 'on' )
  47. $siteurl = str_replace('http://', 'https://', $siteurl);
  48. foreach ($paths as $in => $out) {
  49. $this->assertEquals( $siteurl.$out, admin_url($in), "admin_url('{$in}') should equal '{$siteurl}{$out}'");
  50. }
  51. }
  52. }
  53. function test_admin_url_invalid() {
  54. $paths = array(
  55. null => "/wp-admin/",
  56. 0 => "/wp-admin/",
  57. -1 => "/wp-admin/",
  58. '///' => "/wp-admin/",
  59. );
  60. $https = array('on', 'off');
  61. foreach ($https as $val) {
  62. $_SERVER['HTTPS'] = $val;
  63. $siteurl = get_option('siteurl');
  64. if ( $val == 'on' )
  65. $siteurl = str_replace('http://', 'https://', $siteurl);
  66. foreach ($paths as $in => $out) {
  67. $this->assertEquals( $siteurl.$out, admin_url($in), "admin_url('{$in}') should equal '{$siteurl}{$out}'");
  68. }
  69. }
  70. }
  71. function test_home_url_valid() {
  72. $paths = array(
  73. '' => "",
  74. 'foo' => "/foo",
  75. '/foo' => "/foo",
  76. '/foo/' => "/foo/",
  77. 'foo.php' => "/foo.php",
  78. '/foo.php' => "/foo.php",
  79. '/foo.php?bar=1' => "/foo.php?bar=1",
  80. );
  81. $https = array('on', 'off');
  82. foreach ($https as $val) {
  83. $_SERVER['HTTPS'] = $val;
  84. $home = get_option('home');
  85. if ( $val == 'on' )
  86. $home = str_replace('http://', 'https://', $home);
  87. foreach ($paths as $in => $out) {
  88. $this->assertEquals( $home.$out, home_url($in), "home_url('{$in}') should equal '{$home}{$out}'");
  89. }
  90. }
  91. }
  92. function test_home_url_invalid() {
  93. $paths = array(
  94. null => "",
  95. 0 => "",
  96. -1 => "",
  97. '///' => "/",
  98. );
  99. $https = array('on', 'off');
  100. foreach ($https as $val) {
  101. $_SERVER['HTTPS'] = $val;
  102. $home = get_option('home');
  103. if ( $val == 'on' )
  104. $home = str_replace('http://', 'https://', $home);
  105. foreach ($paths as $in => $out) {
  106. $this->assertEquals( $home.$out, home_url($in), "home_url('{$in}') should equal '{$home}{$out}'");
  107. }
  108. }
  109. }
  110. function test_home_url_from_admin() {
  111. $screen = get_current_screen();
  112. // Pretend to be in the site admin
  113. set_current_screen( 'dashboard' );
  114. $home = get_option('home');
  115. // home_url() should return http when in the admin
  116. $_SERVER['HTTPS'] = 'on';
  117. $this->assertEquals( $home, home_url() );
  118. $_SERVER['HTTPS'] = 'off';
  119. $this->assertEquals( $home, home_url() );
  120. // If not in the admin, is_ssl() should determine the scheme
  121. set_current_screen( 'front' );
  122. $this->assertEquals( $home, home_url() );
  123. $_SERVER['HTTPS'] = 'on';
  124. $home = str_replace('http://', 'https://', $home);
  125. $this->assertEquals( $home, home_url() );
  126. // Test with https in home
  127. update_option( 'home', set_url_scheme( $home, 'https' ) );
  128. // Pretend to be in the site admin
  129. set_current_screen( 'dashboard' );
  130. $home = get_option('home');
  131. // home_url() should return whatever scheme is set in the home option when in the admin
  132. $_SERVER['HTTPS'] = 'on';
  133. $this->assertEquals( $home, home_url() );
  134. $_SERVER['HTTPS'] = 'off';
  135. $this->assertEquals( $home, home_url() );
  136. // If not in the admin, is_ssl() should determine the scheme unless https hard-coded in home
  137. set_current_screen( 'front' );
  138. $this->assertEquals( $home, home_url() );
  139. $_SERVER['HTTPS'] = 'on';
  140. $this->assertEquals( $home, home_url() );
  141. $_SERVER['HTTPS'] = 'off';
  142. $this->assertEquals( $home, home_url() );
  143. update_option( 'home', set_url_scheme( $home, 'http' ) );
  144. $GLOBALS['current_screen'] = $screen;
  145. }
  146. function test_network_home_url_from_admin() {
  147. $screen = get_current_screen();
  148. // Pretend to be in the site admin
  149. set_current_screen( 'dashboard' );
  150. $home = network_home_url();
  151. // home_url() should return http when in the admin
  152. $this->assertEquals( 0, strpos( $home, 'http://') );
  153. $_SERVER['HTTPS'] = 'on';
  154. $this->assertEquals( $home, network_home_url() );
  155. $_SERVER['HTTPS'] = 'off';
  156. $this->assertEquals( $home, network_home_url() );
  157. // If not in the admin, is_ssl() should determine the scheme
  158. set_current_screen( 'front' );
  159. $this->assertEquals( $home, network_home_url() );
  160. $_SERVER['HTTPS'] = 'on';
  161. $home = str_replace('http://', 'https://', $home);
  162. $this->assertEquals( $home, network_home_url() );
  163. $GLOBALS['current_screen'] = $screen;
  164. }
  165. function test_set_url_scheme() {
  166. if ( ! function_exists( 'set_url_scheme' ) )
  167. return;
  168. $links = array(
  169. 'http://wordpress.org/',
  170. 'https://wordpress.org/',
  171. 'http://wordpress.org/news/',
  172. 'http://wordpress.org',
  173. );
  174. $https_links = array(
  175. 'https://wordpress.org/',
  176. 'https://wordpress.org/',
  177. 'https://wordpress.org/news/',
  178. 'https://wordpress.org',
  179. );
  180. $http_links = array(
  181. 'http://wordpress.org/',
  182. 'http://wordpress.org/',
  183. 'http://wordpress.org/news/',
  184. 'http://wordpress.org',
  185. );
  186. $relative_links = array(
  187. '/',
  188. '/',
  189. '/news/',
  190. ''
  191. );
  192. $forced_admin = force_ssl_admin();
  193. $forced_login = force_ssl_login();
  194. $i = 0;
  195. foreach ( $links as $link ) {
  196. $this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'https' ) );
  197. $this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'http' ) );
  198. $this->assertEquals( $relative_links[ $i ], set_url_scheme( $link, 'relative' ) );
  199. $_SERVER['HTTPS'] = 'on';
  200. $this->assertEquals( $https_links[ $i ], set_url_scheme( $link ) );
  201. $_SERVER['HTTPS'] = 'off';
  202. $this->assertEquals( $http_links[ $i ], set_url_scheme( $link ) );
  203. force_ssl_login( false );
  204. force_ssl_admin( true );
  205. $this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'admin' ) );
  206. $this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'login_post' ) );
  207. $this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'login' ) );
  208. $this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'rpc' ) );
  209. force_ssl_admin( false );
  210. $this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'admin' ) );
  211. $this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'login_post' ) );
  212. $this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'login' ) );
  213. $this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'rpc' ) );
  214. force_ssl_login( true );
  215. $this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'admin' ) );
  216. $this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'login_post' ) );
  217. $this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'login' ) );
  218. $this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'rpc' ) );
  219. $i++;
  220. }
  221. force_ssl_admin( $forced_admin );
  222. force_ssl_login( $forced_login );
  223. }
  224. function test_get_adjacent_post() {
  225. $post_id = $this->factory->post->create();
  226. sleep( 1 ); // get_adjacent_post() doesn't handle posts created in the same second.
  227. $post_id2 = $this->factory->post->create();
  228. $orig_post = $GLOBALS['post'];
  229. $GLOBALS['post'] = get_post( $post_id2 );
  230. $p = get_adjacent_post();
  231. $this->assertInstanceOf( 'WP_Post', $p );
  232. $this->assertEquals( $post_id, $p->ID );
  233. // The same again to make sure a cached query returns the same result
  234. $p = get_adjacent_post();
  235. $this->assertInstanceOf( 'WP_Post', $p );
  236. $this->assertEquals( $post_id, $p->ID );
  237. // Test next
  238. $p = get_adjacent_post( false, '', false );
  239. $this->assertEquals( '', $p );
  240. unset( $GLOBALS['post'] );
  241. $this->assertNull( get_adjacent_post() );
  242. $GLOBALS['post'] = $orig_post;
  243. // Tests requiring creating more posts can't be run since the query
  244. // cache in get_adjacent_post() requires a fresh page load to invalidate.
  245. }
  246. /**
  247. * Test that *_url functions handle paths with ".."
  248. *
  249. * @ticket 19032
  250. */
  251. public function test_url_functions_for_dots_in_paths() {
  252. $functions = array(
  253. 'site_url',
  254. 'home_url',
  255. 'admin_url',
  256. 'network_admin_url',
  257. 'user_admin_url',
  258. 'includes_url',
  259. 'network_site_url',
  260. 'network_home_url',
  261. 'content_url',
  262. 'plugins_url',
  263. );
  264. foreach ( $functions as $function ) {
  265. $this->assertEquals( call_user_func( $function, '/' ) . '../',
  266. call_user_func( $function, '../' ) );
  267. $this->assertEquals( call_user_func( $function, '/' ) . 'something...here',
  268. call_user_func( $function, 'something...here' ) );
  269. }
  270. // These functions accept a blog ID argument.
  271. foreach ( array( 'get_site_url', 'get_home_url', 'get_admin_url' ) as $function ) {
  272. $this->assertEquals( call_user_func( $function, null, '/' ) . '../',
  273. call_user_func( $function, null, '../' ) );
  274. $this->assertEquals( call_user_func( $function, null, '/' ) . 'something...here',
  275. call_user_func( $function, null, 'something...here' ) );
  276. }
  277. }
  278. }