forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrewrite.php
More file actions
124 lines (91 loc) · 3.99 KB
/
rewrite.php
File metadata and controls
124 lines (91 loc) · 3.99 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
<?php
/**
* A set of unit tests for functions in wp-includes/rewrite.php
*
* @group rewrite
*/
class Tests_Rewrite extends WP_UnitTestCase {
function setUp() {
global $wp_rewrite;
parent::setUp();
// Need rewrite rules in place to use url_to_postid
$wp_rewrite->init();
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
create_initial_taxonomies();
$wp_rewrite->flush_rules();
}
function tearDown() {
global $wp_rewrite;
$wp_rewrite->init();
parent::tearDown();
}
function test_url_to_postid() {
$id = $this->factory->post->create();
$this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
$id = $this->factory->post->create( array( 'post_type' => 'page' ) );
$this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
}
function test_url_to_postid_custom_post_type() {
delete_option( 'rewrite_rules' );
$post_type = rand_str( 12 );
register_post_type( $post_type, array( 'public' => true ) );
$id = $this->factory->post->create( array( 'post_type' => $post_type ) );
$this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
_unregister_post_type( $post_type );
}
function test_url_to_postid_hierarchical() {
$parent_id = $this->factory->post->create( array( 'post_title' => 'Parent', 'post_type' => 'page' ) );
$child_id = $this->factory->post->create( array( 'post_title' => 'Child', 'post_type' => 'page', 'post_parent' => $parent_id ) );
$this->assertEquals( $parent_id, url_to_postid( get_permalink( $parent_id ) ) );
$this->assertEquals( $child_id, url_to_postid( get_permalink( $child_id ) ) );
}
function test_url_to_postid_home_has_path() {
update_option( 'home', home_url( '/example/' ) );
$id = $this->factory->post->create( array( 'post_title' => 'Hi', 'post_type' => 'page', 'post_name' => 'examp' ) );
$this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
$this->assertEquals( $id, url_to_postid( site_url('/example/examp' ) ) );
$this->assertEquals( $id, url_to_postid( '/example/examp/' ) );
$this->assertEquals( $id, url_to_postid( '/example/examp' ) );
$this->assertEquals( 0, url_to_postid( site_url( '/example/ex' ) ) );
$this->assertEquals( 0, url_to_postid( '/example/ex' ) );
$this->assertEquals( 0, url_to_postid( '/example/ex/' ) );
$this->assertEquals( 0, url_to_postid( '/example-page/example/' ) );
$this->assertEquals( 0, url_to_postid( '/example-page/ex/' ) );
}
function test_url_to_postid_dupe_path() {
update_option( 'home', home_url('/example/') );
$id = $this->factory->post->create( array( 'post_title' => 'Hi', 'post_type' => 'page', 'post_name' => 'example' ) );
$this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
$this->assertEquals( $id, url_to_postid( site_url( '/example/example/' ) ) );
$this->assertEquals( $id, url_to_postid( '/example/example/' ) );
$this->assertEquals( $id, url_to_postid( '/example/example' ) );
}
/**
* Reveals bug introduced in WP 3.0
*/
function test_url_to_postid_home_url_collision() {
update_option( 'home', home_url( '/example' ) );
$this->factory->post->create( array( 'post_title' => 'Collision', 'post_type' => 'page', 'post_name' => 'collision' ) );
// This url should NOT return a post ID
$badurl = site_url( '/example-collision' );
$this->assertEquals( 0, url_to_postid( $badurl ) );
}
/**
* Reveals bug introduced in WP 3.0
*
* Run tests using multisite `phpunit -c multisite`
*/
function test_url_to_postid_ms_home_url_collision() {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'test_url_to_postid_ms_home_url_collision requires multisite' );
return false;
}
$blog_id = $this->factory->blog->create( array( 'path' => '/example' ) );
switch_to_blog( $blog_id );
$this->factory->post->create( array( 'post_title' => 'Collision ', 'post_type' => 'page' ) );
// This url should NOT return a post ID
$badurl = network_home_url( '/example-collision' );
$this->assertEquals( 0, url_to_postid( $badurl ) );
restore_current_blog();
}
}