forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.php
More file actions
428 lines (347 loc) · 12.4 KB
/
url.php
File metadata and controls
428 lines (347 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
// tests for link-template.php and related URL functions
/**
* @group url
*/
class Tests_URL extends WP_UnitTestCase {
var $_old_server;
function setUp() {
parent::setUp();
$this->_old_server = $_SERVER;
$GLOBALS['pagenow'] = '';
}
function tearDown() {
$_SERVER = $this->_old_server;
parent::tearDown();
}
function test_is_ssl_positive() {
$_SERVER['HTTPS'] = 'on';
$this->assertTrue( is_ssl() );
$_SERVER['HTTPS'] = 'ON';
$this->assertTrue( is_ssl() );
$_SERVER['HTTPS'] = '1';
$this->assertTrue( is_ssl() );
unset( $_SERVER['HTTPS'] );
$_SERVER['SERVER_PORT'] = '443';
$this->assertTrue( is_ssl() );
}
function test_is_ssl_negative() {
$_SERVER['HTTPS'] = 'off';
$this->assertFalse( is_ssl() );
$_SERVER['HTTPS'] = 'OFF';
$this->assertFalse( is_ssl() );
unset($_SERVER['HTTPS']);
$this->assertFalse( is_ssl() );
}
function test_admin_url_valid() {
$paths = array(
'' => "/wp-admin/",
'foo' => "/wp-admin/foo",
'/foo' => "/wp-admin/foo",
'/foo/' => "/wp-admin/foo/",
'foo.php' => "/wp-admin/foo.php",
'/foo.php' => "/wp-admin/foo.php",
'/foo.php?bar=1' => "/wp-admin/foo.php?bar=1",
);
$https = array('on', 'off');
foreach ($https as $val) {
$_SERVER['HTTPS'] = $val;
$siteurl = get_option('siteurl');
if ( $val == 'on' )
$siteurl = str_replace('http://', 'https://', $siteurl);
foreach ($paths as $in => $out) {
$this->assertEquals( $siteurl.$out, admin_url($in), "admin_url('{$in}') should equal '{$siteurl}{$out}'");
}
}
}
function test_admin_url_invalid() {
$paths = array(
null => "/wp-admin/",
0 => "/wp-admin/",
-1 => "/wp-admin/",
'///' => "/wp-admin/",
);
$https = array('on', 'off');
foreach ($https as $val) {
$_SERVER['HTTPS'] = $val;
$siteurl = get_option('siteurl');
if ( $val == 'on' )
$siteurl = str_replace('http://', 'https://', $siteurl);
foreach ($paths as $in => $out) {
$this->assertEquals( $siteurl.$out, admin_url($in), "admin_url('{$in}') should equal '{$siteurl}{$out}'");
}
}
}
function test_home_url_valid() {
$paths = array(
'' => "",
'foo' => "/foo",
'/foo' => "/foo",
'/foo/' => "/foo/",
'foo.php' => "/foo.php",
'/foo.php' => "/foo.php",
'/foo.php?bar=1' => "/foo.php?bar=1",
);
$https = array('on', 'off');
foreach ($https as $val) {
$_SERVER['HTTPS'] = $val;
$home = get_option('home');
if ( $val == 'on' )
$home = str_replace('http://', 'https://', $home);
foreach ($paths as $in => $out) {
$this->assertEquals( $home.$out, home_url($in), "home_url('{$in}') should equal '{$home}{$out}'");
}
}
}
function test_home_url_invalid() {
$paths = array(
null => "",
0 => "",
-1 => "",
'///' => "/",
);
$https = array('on', 'off');
foreach ($https as $val) {
$_SERVER['HTTPS'] = $val;
$home = get_option('home');
if ( $val == 'on' )
$home = str_replace('http://', 'https://', $home);
foreach ($paths as $in => $out) {
$this->assertEquals( $home.$out, home_url($in), "home_url('{$in}') should equal '{$home}{$out}'");
}
}
}
function test_home_url_from_admin() {
$screen = get_current_screen();
// Pretend to be in the site admin
set_current_screen( 'dashboard' );
$home = get_option('home');
// home_url() should return http when in the admin
$_SERVER['HTTPS'] = 'on';
$this->assertEquals( $home, home_url() );
$_SERVER['HTTPS'] = 'off';
$this->assertEquals( $home, home_url() );
// If not in the admin, is_ssl() should determine the scheme
set_current_screen( 'front' );
$this->assertEquals( $home, home_url() );
$_SERVER['HTTPS'] = 'on';
$home = str_replace('http://', 'https://', $home);
$this->assertEquals( $home, home_url() );
// Test with https in home
update_option( 'home', set_url_scheme( $home, 'https' ) );
// Pretend to be in the site admin
set_current_screen( 'dashboard' );
$home = get_option('home');
// home_url() should return whatever scheme is set in the home option when in the admin
$_SERVER['HTTPS'] = 'on';
$this->assertEquals( $home, home_url() );
$_SERVER['HTTPS'] = 'off';
$this->assertEquals( $home, home_url() );
// If not in the admin, is_ssl() should determine the scheme unless https hard-coded in home
set_current_screen( 'front' );
$this->assertEquals( $home, home_url() );
$_SERVER['HTTPS'] = 'on';
$this->assertEquals( $home, home_url() );
$_SERVER['HTTPS'] = 'off';
$this->assertEquals( $home, home_url() );
update_option( 'home', set_url_scheme( $home, 'http' ) );
$GLOBALS['current_screen'] = $screen;
}
function test_network_home_url_from_admin() {
$screen = get_current_screen();
// Pretend to be in the site admin
set_current_screen( 'dashboard' );
$home = network_home_url();
// home_url() should return http when in the admin
$this->assertEquals( 0, strpos( $home, 'http://') );
$_SERVER['HTTPS'] = 'on';
$this->assertEquals( $home, network_home_url() );
$_SERVER['HTTPS'] = 'off';
$this->assertEquals( $home, network_home_url() );
// If not in the admin, is_ssl() should determine the scheme
set_current_screen( 'front' );
$this->assertEquals( $home, network_home_url() );
$_SERVER['HTTPS'] = 'on';
$home = str_replace('http://', 'https://', $home);
$this->assertEquals( $home, network_home_url() );
$GLOBALS['current_screen'] = $screen;
}
function test_set_url_scheme() {
if ( ! function_exists( 'set_url_scheme' ) )
return;
$links = array(
'http://wordpress.org/',
'https://wordpress.org/',
'http://wordpress.org/news/',
'http://wordpress.org',
);
$https_links = array(
'https://wordpress.org/',
'https://wordpress.org/',
'https://wordpress.org/news/',
'https://wordpress.org',
);
$http_links = array(
'http://wordpress.org/',
'http://wordpress.org/',
'http://wordpress.org/news/',
'http://wordpress.org',
);
$relative_links = array(
'/',
'/',
'/news/',
''
);
$forced_admin = force_ssl_admin();
$forced_login = force_ssl_login();
$i = 0;
foreach ( $links as $link ) {
$this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'https' ) );
$this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'http' ) );
$this->assertEquals( $relative_links[ $i ], set_url_scheme( $link, 'relative' ) );
$_SERVER['HTTPS'] = 'on';
$this->assertEquals( $https_links[ $i ], set_url_scheme( $link ) );
$_SERVER['HTTPS'] = 'off';
$this->assertEquals( $http_links[ $i ], set_url_scheme( $link ) );
force_ssl_login( false );
force_ssl_admin( true );
$this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'admin' ) );
$this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'login_post' ) );
$this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'login' ) );
$this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'rpc' ) );
force_ssl_admin( false );
$this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'admin' ) );
$this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'login_post' ) );
$this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'login' ) );
$this->assertEquals( $http_links[ $i ], set_url_scheme( $link, 'rpc' ) );
force_ssl_login( true );
$this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'admin' ) );
$this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'login_post' ) );
$this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'login' ) );
$this->assertEquals( $https_links[ $i ], set_url_scheme( $link, 'rpc' ) );
$i++;
}
force_ssl_admin( $forced_admin );
force_ssl_login( $forced_login );
}
public function test_get_adjacent_post() {
$now = time();
$post_id = $this->factory->post->create( array( 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
$post_id2 = $this->factory->post->create( array( 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
if ( ! isset( $GLOBALS['post'] ) )
$GLOBALS['post'] = null;
$orig_post = $GLOBALS['post'];
$GLOBALS['post'] = get_post( $post_id2 );
$p = get_adjacent_post();
$this->assertInstanceOf( 'WP_Post', $p );
$this->assertEquals( $post_id, $p->ID );
// The same again to make sure a cached query returns the same result
$p = get_adjacent_post();
$this->assertInstanceOf( 'WP_Post', $p );
$this->assertEquals( $post_id, $p->ID );
// Test next
$p = get_adjacent_post( false, '', false );
$this->assertEquals( '', $p );
unset( $GLOBALS['post'] );
$this->assertNull( get_adjacent_post() );
$GLOBALS['post'] = $orig_post;
}
/**
* Test get_adjacent_post returns the next private post when the author is the currently logged in user.
*
* @ticket 30287
*/
public function test_get_adjacent_post_should_return_private_posts_belonging_to_the_current_user() {
$u = $this->factory->user->create( array( 'role' => 'author' ) );
$old_uid = get_current_user_id();
wp_set_current_user( $u );
$now = time();
$p1 = $this->factory->post->create( array( 'post_author' => $u, 'post_status' => 'private', 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
$p2 = $this->factory->post->create( array( 'post_author' => $u, 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
if ( ! isset( $GLOBALS['post'] ) ) {
$GLOBALS['post'] = null;
}
$orig_post = $GLOBALS['post'];
$GLOBALS['post'] = get_post( $p2 );
$p = get_adjacent_post();
$this->assertEquals( $p1, $p->ID );
$GLOBALS['post'] = $orig_post;
wp_set_current_user( $old_uid );
}
/**
* @ticket 30287
*/
public function test_get_adjacent_post_should_return_private_posts_belonging_to_other_users_if_the_current_user_can_read_private_posts() {
$u1 = $this->factory->user->create( array( 'role' => 'author' ) );
$u2 = $this->factory->user->create( array( 'role' => 'administrator' ) );
$old_uid = get_current_user_id();
wp_set_current_user( $u2 );
$now = time();
$p1 = $this->factory->post->create( array( 'post_author' => $u1, 'post_status' => 'private', 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
$p2 = $this->factory->post->create( array( 'post_author' => $u1, 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
if ( ! isset( $GLOBALS['post'] ) ) {
$GLOBALS['post'] = null;
}
$orig_post = $GLOBALS['post'];
$GLOBALS['post'] = get_post( $p2 );
$p = get_adjacent_post();
$this->assertEquals( $p1, $p->ID );
$GLOBALS['post'] = $orig_post;
wp_set_current_user( $old_uid );
}
/**
* @ticket 30287
*/
public function test_get_adjacent_post_should_not_return_private_posts_belonging_to_other_users_if_the_current_user_cannot_read_private_posts() {
$u1 = $this->factory->user->create( array( 'role' => 'author' ) );
$u2 = $this->factory->user->create( array( 'role' => 'author' ) );
$old_uid = get_current_user_id();
wp_set_current_user( $u2 );
$now = time();
$p1 = $this->factory->post->create( array( 'post_author' => $u1, 'post_date' => date( 'Y-m-d H:i:s', $now - 2 ) ) );
$p2 = $this->factory->post->create( array( 'post_author' => $u1, 'post_status' => 'private', 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
$p3 = $this->factory->post->create( array( 'post_author' => $u1, 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
if ( ! isset( $GLOBALS['post'] ) ) {
$GLOBALS['post'] = null;
}
$orig_post = $GLOBALS['post'];
$GLOBALS['post'] = get_post( $p3 );
$p = get_adjacent_post();
$this->assertEquals( $p1, $p->ID );
$GLOBALS['post'] = $orig_post;
wp_set_current_user( $old_uid );
}
/**
* Test that *_url functions handle paths with ".."
*
* @ticket 19032
*/
public function test_url_functions_for_dots_in_paths() {
$functions = array(
'site_url',
'home_url',
'admin_url',
'network_admin_url',
'user_admin_url',
'includes_url',
'network_site_url',
'network_home_url',
'content_url',
'plugins_url',
);
foreach ( $functions as $function ) {
$this->assertEquals( call_user_func( $function, '/' ) . '../',
call_user_func( $function, '../' ) );
$this->assertEquals( call_user_func( $function, '/' ) . 'something...here',
call_user_func( $function, 'something...here' ) );
}
// These functions accept a blog ID argument.
foreach ( array( 'get_site_url', 'get_home_url', 'get_admin_url' ) as $function ) {
$this->assertEquals( call_user_func( $function, null, '/' ) . '../',
call_user_func( $function, null, '../' ) );
$this->assertEquals( call_user_func( $function, null, '/' ) . 'something...here',
call_user_func( $function, null, 'something...here' ) );
}
}
}