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