1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * A set of unit tests for functions in wp-includes/rewrite.php |
---|
5 | * |
---|
6 | * @group rewrite |
---|
7 | */ |
---|
8 | class Tests_Rewrite extends WP_UnitTestCase { |
---|
9 | |
---|
10 | function setUp() { |
---|
11 | parent::setUp(); |
---|
12 | |
---|
13 | // Need rewrite rules in place to use url_to_postid |
---|
14 | global $wp_rewrite; |
---|
15 | update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' ); |
---|
16 | create_initial_taxonomies(); |
---|
17 | $GLOBALS['wp_rewrite']->init(); |
---|
18 | flush_rewrite_rules(); |
---|
19 | } |
---|
20 | |
---|
21 | function tearDown() { |
---|
22 | parent::tearDown(); |
---|
23 | $GLOBALS['wp_rewrite']->init(); |
---|
24 | } |
---|
25 | |
---|
26 | function test_url_to_postid() { |
---|
27 | |
---|
28 | $id = $this->factory->post->create(); |
---|
29 | $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); |
---|
30 | |
---|
31 | $id = $this->factory->post->create( array( 'post_type' => 'page' ) ); |
---|
32 | $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); |
---|
33 | } |
---|
34 | |
---|
35 | function test_url_to_postid_hierarchical() { |
---|
36 | |
---|
37 | $parent_id = $this->factory->post->create( array( 'post_title' => 'Parent', 'post_type' => 'page' ) ); |
---|
38 | $child_id = $this->factory->post->create( array( 'post_title' => 'Child', 'post_type' => 'page', 'post_parent' => $parent_id ) ); |
---|
39 | |
---|
40 | $this->assertEquals( $parent_id, url_to_postid( get_permalink( $parent_id ) ) ); |
---|
41 | $this->assertEquals( $child_id, url_to_postid( get_permalink( $child_id ) ) ); |
---|
42 | |
---|
43 | } |
---|
44 | |
---|
45 | function test_url_to_postid_home_has_path() { |
---|
46 | |
---|
47 | update_option( 'home', home_url('/example/') ); |
---|
48 | |
---|
49 | $id = $this->factory->post->create( array( 'post_title' => 'Hi', 'post_type' => 'page', 'post_name' => 'examp' ) ); |
---|
50 | $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); |
---|
51 | $this->assertEquals( $id, url_to_postid( site_url('/example/examp' ) ) ); |
---|
52 | $this->assertEquals( $id, url_to_postid( '/example/examp/' ) ); |
---|
53 | $this->assertEquals( $id, url_to_postid( '/example/examp' ) ); |
---|
54 | |
---|
55 | $this->assertEquals( 0, url_to_postid( site_url( '/example/ex' ) ) ); |
---|
56 | $this->assertEquals( 0, url_to_postid( '/example/ex' ) ); |
---|
57 | $this->assertEquals( 0, url_to_postid( '/example/ex/' ) ); |
---|
58 | $this->assertEquals( 0, url_to_postid( '/example-page/example/' ) ); |
---|
59 | $this->assertEquals( 0, url_to_postid( '/example-page/ex/' ) ); |
---|
60 | |
---|
61 | } |
---|
62 | |
---|
63 | function test_url_to_postid_dupe_path() { |
---|
64 | |
---|
65 | $this->knownWPBug(20560); |
---|
66 | |
---|
67 | update_option( 'home', home_url('/example/') ); |
---|
68 | |
---|
69 | $id = $this->factory->post->create( array( 'post_title' => 'Hi', 'post_type' => 'page', 'post_name' => 'example' ) ); |
---|
70 | |
---|
71 | $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); |
---|
72 | $this->assertEquals( $id, url_to_postid( site_url('/example/example/' ) ) ); |
---|
73 | $this->assertEquals( $id, url_to_postid( '/example/example/' ) ); |
---|
74 | $this->assertEquals( $id, url_to_postid( '/example/example' ) ); |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Reveals bug introduced in WP 3.0 |
---|
79 | * |
---|
80 | * @ticket 20560 |
---|
81 | */ |
---|
82 | function test_url_to_postid_home_url_collision() { |
---|
83 | |
---|
84 | $this->knownWPBug(20560); |
---|
85 | |
---|
86 | update_option( 'home', home_url('/example') ); |
---|
87 | |
---|
88 | $this->factory->post->create( array( 'post_title' => 'Collision', 'post_type' => 'page', 'post_name' => 'collision' ) ); |
---|
89 | |
---|
90 | // This url should NOT return a post ID |
---|
91 | $badurl = site_url('/example-collision'); |
---|
92 | $this->assertEquals( 0, url_to_postid( $badurl ) ); |
---|
93 | |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Reveals bug introduced in WP 3.0 |
---|
98 | * |
---|
99 | * Run tests using multisite `phpunit -c multisite` |
---|
100 | * |
---|
101 | * @ticket 20560 |
---|
102 | */ |
---|
103 | function test_url_to_postid_ms_home_url_collision() { |
---|
104 | |
---|
105 | if( ! is_multisite() ) { |
---|
106 | $this->markTestSkipped('test_url_to_postid_ms_home_url_collision requires multisite'); |
---|
107 | return false; |
---|
108 | } |
---|
109 | |
---|
110 | $this->knownWPBug(20560); |
---|
111 | |
---|
112 | $blog_id = $this->factory->blog->create( array( 'path' => '/example' ) ); |
---|
113 | switch_to_blog( $blog_id ); |
---|
114 | |
---|
115 | $this->factory->post->create( array( 'post_title' => 'Collision ', 'post_type' => 'page' ) ); |
---|
116 | |
---|
117 | // This url should NOT return a post ID |
---|
118 | $badurl = network_home_url('/example-collision'); |
---|
119 | $this->assertEquals( 0, url_to_postid( $badurl ) ); |
---|
120 | |
---|
121 | restore_current_blog(); |
---|
122 | |
---|
123 | } |
---|
124 | |
---|
125 | } |
---|
126 | |
---|
127 | ?> |
---|