1 | <?php |
---|
2 | |
---|
3 | class WP_Canonical_UnitTestCase extends WP_UnitTestCase { |
---|
4 | static $old_current_user; |
---|
5 | static $author_id; |
---|
6 | static $post_ids = array(); |
---|
7 | static $comment_ids = array(); |
---|
8 | static $term_ids = array(); |
---|
9 | static $terms = array(); |
---|
10 | static $old_options = array(); |
---|
11 | |
---|
12 | /** |
---|
13 | * This can be defined in a subclass of this class which contains its own data() method. |
---|
14 | * Those tests will be run against the specified permastruct. |
---|
15 | */ |
---|
16 | public $structure = '/%year%/%monthnum%/%day%/%postname%/'; |
---|
17 | |
---|
18 | public function setUp() { |
---|
19 | parent::setUp(); |
---|
20 | |
---|
21 | update_option( 'page_comments', true ); |
---|
22 | update_option( 'comments_per_page', 5 ); |
---|
23 | update_option( 'posts_per_page', 5 ); |
---|
24 | |
---|
25 | global $wp_rewrite; |
---|
26 | $wp_rewrite->init(); |
---|
27 | $wp_rewrite->set_permalink_structure( $this->structure ); |
---|
28 | |
---|
29 | create_initial_taxonomies(); |
---|
30 | |
---|
31 | $wp_rewrite->flush_rules(); |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * Generate fixtures to be shared between canonical tests. |
---|
36 | * |
---|
37 | * Abstracted here because it's invoked by setUpBeforeClass() in more than one class. |
---|
38 | * |
---|
39 | * @since 4.1.0 |
---|
40 | */ |
---|
41 | public static function generate_shared_fixtures( $factory ) { |
---|
42 | self::$old_current_user = get_current_user_id(); |
---|
43 | self::$author_id = $factory->user->create( array( 'user_login' => 'canonical-author' ) ); |
---|
44 | |
---|
45 | /* |
---|
46 | * Also set in self::setUp(), but we must configure here to make sure that |
---|
47 | * post authorship is properly attributed for fixtures. |
---|
48 | */ |
---|
49 | wp_set_current_user( self::$author_id ); |
---|
50 | |
---|
51 | // Already created by install defaults: |
---|
52 | // self::factory()->term->create( array( 'taxonomy' => 'category', 'name' => 'uncategorized' ) ); |
---|
53 | |
---|
54 | self::$post_ids[] = $factory->post->create( array( 'import_id' => 587, 'post_title' => 'post-format-test-audio', 'post_date' => '2008-06-02 00:00:00' ) ); |
---|
55 | self::$post_ids[] = $post_id = $factory->post->create( array( 'post_title' => 'post-format-test-gallery', 'post_date' => '2008-06-10 00:00:00' ) ); |
---|
56 | self::$post_ids[] = $factory->post->create( array( 'import_id' => 611, 'post_type' => 'attachment', 'post_title' => 'canola2', 'post_parent' => $post_id ) ); |
---|
57 | |
---|
58 | self::$post_ids[] = $factory->post->create( array( |
---|
59 | 'post_title' => 'images-test', |
---|
60 | 'post_date' => '2008-09-03 00:00:00', |
---|
61 | 'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3' |
---|
62 | ) ); |
---|
63 | |
---|
64 | self::$post_ids[] = $post_id = $factory->post->create( array( 'import_id' => 149, 'post_title' => 'comment-test', 'post_date' => '2008-03-03 00:00:00' ) ); |
---|
65 | self::$comment_ids = $factory->comment->create_post_comments( $post_id, 15 ); |
---|
66 | |
---|
67 | self::$post_ids[] = $factory->post->create( array( 'post_date' => '2008-09-05 00:00:00' ) ); |
---|
68 | |
---|
69 | self::$post_ids[] = $factory->post->create( array( 'import_id' => 123 ) ); |
---|
70 | self::$post_ids[] = $factory->post->create( array( 'import_id' => 1 ) ); |
---|
71 | self::$post_ids[] = $factory->post->create( array( 'import_id' => 358 ) ); |
---|
72 | |
---|
73 | self::$post_ids[] = $factory->post->create( array( 'post_type' => 'page', 'post_title' => 'sample-page' ) ); |
---|
74 | self::$post_ids[] = $factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about' ) ); |
---|
75 | self::$post_ids[] = $post_id = $factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) ); |
---|
76 | self::$post_ids[] = $factory->post->create( |
---|
77 | array( 'import_id' => 144, 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $post_id, |
---|
78 | ) ); |
---|
79 | |
---|
80 | self::$post_ids[] = $parent_id = $factory->post->create( array( |
---|
81 | 'post_name' => 'parent', |
---|
82 | 'post_type' => 'page', |
---|
83 | ) ); |
---|
84 | self::$post_ids[] = $child_id_1 = $factory->post->create( array( |
---|
85 | 'post_name' => 'child1', |
---|
86 | 'post_type' => 'page', |
---|
87 | 'post_parent' => $parent_id, |
---|
88 | ) ); |
---|
89 | self::$post_ids[] = $child_id_2 = $factory->post->create( array( |
---|
90 | 'post_name' => 'child2', |
---|
91 | 'post_type' => 'page', |
---|
92 | 'post_parent' => $parent_id, |
---|
93 | ) ); |
---|
94 | self::$post_ids[] = $grandchild_id_1 = $factory->post->create( array( |
---|
95 | 'post_name' => 'grandchild', |
---|
96 | 'post_type' => 'page', |
---|
97 | 'post_parent' => $child_id_1, |
---|
98 | ) ); |
---|
99 | self::$post_ids[] = $grandchild_id_2 = $factory->post->create( array( |
---|
100 | 'post_name' => 'grandchild', |
---|
101 | 'post_type' => 'page', |
---|
102 | 'post_parent' => $child_id_2, |
---|
103 | ) ); |
---|
104 | |
---|
105 | $cat1 = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'parent' ) ); |
---|
106 | self::$terms['/category/parent/'] = $cat1; |
---|
107 | self::$term_ids[ $cat1 ] = 'category'; |
---|
108 | |
---|
109 | $cat2 = $factory->term->create( array( |
---|
110 | 'taxonomy' => 'category', 'name' => 'child-1', 'parent' => self::$terms['/category/parent/'], |
---|
111 | ) ); |
---|
112 | self::$terms['/category/parent/child-1/'] = $cat2; |
---|
113 | self::$term_ids[ $cat2 ] = 'category'; |
---|
114 | |
---|
115 | $cat3 = $factory->term->create( array( |
---|
116 | 'taxonomy' => 'category', 'name' => 'child-2', 'parent' => self::$terms['/category/parent/child-1/'], |
---|
117 | ) ); |
---|
118 | self::$terms['/category/parent/child-1/child-2/'] = $cat3; |
---|
119 | self::$term_ids[ $cat3 ] = 'category'; |
---|
120 | |
---|
121 | $cat4 = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) ); |
---|
122 | self::$term_ids[ $cat4 ] = 'category'; |
---|
123 | |
---|
124 | $cat5 = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) ); |
---|
125 | self::$term_ids[ $cat5 ] = 'category'; |
---|
126 | |
---|
127 | $tag1 = $factory->term->create( array( 'name' => 'post-formats' ) ); |
---|
128 | self::$term_ids[ $tag1 ] = 'post_tag'; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Clean up shared fixtures. |
---|
133 | * |
---|
134 | * @since 4.1.0 |
---|
135 | */ |
---|
136 | public static function delete_shared_fixtures() { |
---|
137 | self::delete_user( self::$author_id ); |
---|
138 | |
---|
139 | foreach ( self::$post_ids as $pid ) { |
---|
140 | wp_delete_post( $pid, true ); |
---|
141 | } |
---|
142 | |
---|
143 | foreach ( self::$comment_ids as $cid ) { |
---|
144 | wp_delete_comment( $cid, true ); |
---|
145 | } |
---|
146 | |
---|
147 | foreach ( self::$term_ids as $tid => $tax ) { |
---|
148 | wp_delete_term( $tid, $tax ); |
---|
149 | } |
---|
150 | |
---|
151 | self::$author_id = null; |
---|
152 | self::$post_ids = array(); |
---|
153 | self::$comment_ids = array(); |
---|
154 | self::$term_ids = array(); |
---|
155 | self::$terms = array(); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Assert that a given URL is the same a the canonical URL generated by WP. |
---|
160 | * |
---|
161 | * @since 4.1.0 |
---|
162 | * |
---|
163 | * @param string $test_url Raw URL that will be run through redirect_canonical(). |
---|
164 | * @param string $expected Expected string. |
---|
165 | * @param int $ticket Optional. Trac ticket number. |
---|
166 | * @param array $expected_doing_it_wrong Array of class/function names expected to throw _doing_it_wrong() notices. |
---|
167 | */ |
---|
168 | public function assertCanonical( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) { |
---|
169 | $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, (array) $expected_doing_it_wrong ); |
---|
170 | |
---|
171 | if ( $ticket ) |
---|
172 | $this->knownWPBug( $ticket ); |
---|
173 | |
---|
174 | $ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null; |
---|
175 | global $wpdb; |
---|
176 | //print_r( $wpdb->get_results( "SELECT * FROM $wpdb->terms" ) ); |
---|
177 | if ( is_string($expected) ) |
---|
178 | $expected = array('url' => $expected); |
---|
179 | elseif ( is_array($expected) && !isset($expected['url']) && !isset($expected['qv']) ) |
---|
180 | $expected = array( 'qv' => $expected ); |
---|
181 | |
---|
182 | if ( !isset($expected['url']) && !isset($expected['qv']) ) |
---|
183 | $this->markTestSkipped('No valid expected output was provided'); |
---|
184 | |
---|
185 | $this->go_to( home_url( $test_url ) ); |
---|
186 | |
---|
187 | // Does the redirect match what's expected? |
---|
188 | $can_url = $this->get_canonical( $test_url ); |
---|
189 | $parsed_can_url = parse_url($can_url); |
---|
190 | |
---|
191 | // Just test the Path and Query if present |
---|
192 | if ( isset($expected['url']) ) { |
---|
193 | $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref ); |
---|
194 | } |
---|
195 | |
---|
196 | if ( ! isset($expected['qv']) ) |
---|
197 | return; |
---|
198 | |
---|
199 | // "make" that the request and check the query is correct |
---|
200 | $this->go_to( $can_url ); |
---|
201 | |
---|
202 | // Are all query vars accounted for, And correct? |
---|
203 | global $wp; |
---|
204 | |
---|
205 | $query_vars = array_diff($wp->query_vars, $wp->extra_query_vars); |
---|
206 | if ( !empty($parsed_can_url['query']) ) { |
---|
207 | parse_str($parsed_can_url['query'], $_qv); |
---|
208 | |
---|
209 | // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite) |
---|
210 | $this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref ); |
---|
211 | |
---|
212 | $query_vars = array_merge($query_vars, $_qv); |
---|
213 | } |
---|
214 | |
---|
215 | $this->assertEquals( $expected['qv'], $query_vars ); |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * Get the canonical URL given a raw URL. |
---|
220 | * |
---|
221 | * @param string $test_url Should be relative to the site "front", ie /category/uncategorized/ |
---|
222 | * as opposed to http://example.com/category/uncategorized/ |
---|
223 | * @return $can_url Returns the original $test_url if no canonical can be generated, otherwise returns |
---|
224 | * the fully-qualified URL as generated by redirect_canonical(). |
---|
225 | */ |
---|
226 | public function get_canonical( $test_url ) { |
---|
227 | $test_url = home_url( $test_url ); |
---|
228 | |
---|
229 | $can_url = redirect_canonical( $test_url, false ); |
---|
230 | if ( ! $can_url ) |
---|
231 | return $test_url; // No redirect will take place for this request |
---|
232 | |
---|
233 | return $can_url; |
---|
234 | } |
---|
235 | } |
---|