Make WordPress Core

source: trunk/tests/phpunit/includes/testcase-canonical.php

Last change on this file was 54865, checked in by SergeyBiryukov, 3 years ago

Tests: Correct references to set_up() and tear_down() in various DocBlocks.

The setUp() and tearDown() methods were renamed to set_up() and tear_down(), respectively, as part of implementing the void return type solution for PHPUnit 8.0.

Follow-up to [29120], [29251], [30277], [32173], [32806], [38829], [42379], [50450], [51276], [51568].

See #56793.

  • Property svn:eol-style set to native
File size: 9.4 KB
Line 
1<?php
2
3abstract class WP_Canonical_UnitTestCase extends WP_UnitTestCase {
4        public static $old_current_user;
5        public static $author_id;
6        public static $post_ids    = array();
7        public static $comment_ids = array();
8        public static $term_ids    = array();
9        public static $terms       = array();
10        public 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 static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
19                self::generate_shared_fixtures( $factory );
20        }
21
22        public static function wpTearDownAfterClass() {
23                self::delete_shared_fixtures();
24        }
25
26        public function set_up() {
27                parent::set_up();
28
29                update_option( 'page_comments', true );
30                update_option( 'comments_per_page', 5 );
31                update_option( 'posts_per_page', 5 );
32
33                $this->set_permalink_structure( $this->structure );
34                create_initial_taxonomies();
35        }
36
37        /**
38         * Generate fixtures to be shared between canonical tests.
39         *
40         * Abstracted here because it's invoked by wpSetUpBeforeClass() in more than one class.
41         *
42         * @since 4.1.0
43         */
44        public static function generate_shared_fixtures( WP_UnitTest_Factory $factory ) {
45                self::$old_current_user = get_current_user_id();
46                self::$author_id        = $factory->user->create( array( 'user_login' => 'canonical-author' ) );
47
48                /*
49                 * Also set in self::set_up(), but we must configure here to make sure that
50                 * post authorship is properly attributed for fixtures.
51                 */
52                wp_set_current_user( self::$author_id );
53
54                // Already created by install defaults:
55                // $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'uncategorized' ) );
56
57                self::$post_ids[] = $factory->post->create(
58                        array(
59                                'import_id'  => 587,
60                                'post_title' => 'post-format-test-audio',
61                                'post_date'  => '2008-06-02 00:00:00',
62                        )
63                );
64
65                $gallery_post_id = $factory->post->create(
66                        array(
67                                'post_title' => 'post-format-test-gallery',
68                                'post_date'  => '2008-06-10 00:00:00',
69                        )
70                );
71
72                self::$post_ids[] = $gallery_post_id;
73
74                self::$post_ids[] = $factory->post->create(
75                        array(
76                                'import_id'   => 611,
77                                'post_type'   => 'attachment',
78                                'post_title'  => 'canola2',
79                                'post_parent' => $gallery_post_id,
80                        )
81                );
82
83                self::$post_ids[] = $factory->post->create(
84                        array(
85                                'post_title' => 'images-test',
86                                'post_date'  => '2008-09-03 00:00:00',
87                        )
88                );
89
90                self::$post_ids[] = $factory->post->create(
91                        array(
92                                'post_title'   => 'multipage-post-test',
93                                'post_date'    => '2008-09-03 00:00:00',
94                                'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3',
95                        )
96                );
97
98                self::$post_ids[] = $factory->post->create(
99                        array(
100                                'post_title' => 'non-paged-post-test',
101                                'post_date'  => '2008-09-03 00:00:00',
102                        )
103                );
104
105                $comment_post_id = $factory->post->create(
106                        array(
107                                'import_id'  => 149,
108                                'post_title' => 'comment-test',
109                                'post_date'  => '2008-03-03 00:00:00',
110                        )
111                );
112
113                self::$post_ids[]  = $comment_post_id;
114                self::$comment_ids = $factory->comment->create_post_comments( $comment_post_id, 15 );
115
116                self::$post_ids[] = $factory->post->create( array( 'post_date' => '2008-09-05 00:00:00' ) );
117
118                self::$post_ids[] = $factory->post->create( array( 'import_id' => 123 ) );
119                self::$post_ids[] = $factory->post->create( array( 'import_id' => 1 ) );
120                self::$post_ids[] = $factory->post->create( array( 'import_id' => 358 ) );
121
122                self::$post_ids[] = $factory->post->create(
123                        array(
124                                'post_type'  => 'page',
125                                'post_title' => 'sample-page',
126                        )
127                );
128
129                self::$post_ids[] = $factory->post->create(
130                        array(
131                                'post_type'  => 'page',
132                                'post_title' => 'about',
133                        )
134                );
135
136                $parent_page_id = $factory->post->create(
137                        array(
138                                'post_type'  => 'page',
139                                'post_title' => 'parent-page',
140                        )
141                );
142
143                self::$post_ids[] = $parent_page_id;
144
145                self::$post_ids[] = $factory->post->create(
146                        array(
147                                'import_id'   => 144,
148                                'post_type'   => 'page',
149                                'post_title'  => 'child-page-1',
150                                'post_parent' => $parent_page_id,
151                        )
152                );
153
154                $parent_page_id = $factory->post->create(
155                        array(
156                                'post_name' => 'parent',
157                                'post_type' => 'page',
158                        )
159                );
160
161                self::$post_ids[] = $parent_page_id;
162
163                $child_id_1 = $factory->post->create(
164                        array(
165                                'post_name'   => 'child1',
166                                'post_type'   => 'page',
167                                'post_parent' => $parent_page_id,
168                        )
169                );
170
171                self::$post_ids[] = $child_id_1;
172
173                $child_id_2 = $factory->post->create(
174                        array(
175                                'post_name'   => 'child2',
176                                'post_type'   => 'page',
177                                'post_parent' => $parent_page_id,
178                        )
179                );
180
181                self::$post_ids[] = $child_id_2;
182
183                $grandchild_id_1 = $factory->post->create(
184                        array(
185                                'post_name'   => 'grandchild',
186                                'post_type'   => 'page',
187                                'post_parent' => $child_id_1,
188                        )
189                );
190
191                self::$post_ids[] = $grandchild_id_1;
192
193                $grandchild_id_2 = $factory->post->create(
194                        array(
195                                'post_name'   => 'grandchild',
196                                'post_type'   => 'page',
197                                'post_parent' => $child_id_2,
198                        )
199                );
200
201                self::$post_ids[] = $grandchild_id_2;
202
203                $cat1 = $factory->term->create(
204                        array(
205                                'taxonomy' => 'category',
206                                'name'     => 'parent',
207                        )
208                );
209
210                self::$terms['/category/parent/'] = $cat1;
211
212                self::$term_ids[ $cat1 ] = 'category';
213
214                $cat2 = $factory->term->create(
215                        array(
216                                'taxonomy' => 'category',
217                                'name'     => 'child-1',
218                                'parent'   => self::$terms['/category/parent/'],
219                        )
220                );
221
222                self::$terms['/category/parent/child-1/'] = $cat2;
223
224                self::$term_ids[ $cat2 ] = 'category';
225
226                $cat3 = $factory->term->create(
227                        array(
228                                'taxonomy' => 'category',
229                                'name'     => 'child-2',
230                                'parent'   => self::$terms['/category/parent/child-1/'],
231                        )
232                );
233
234                self::$terms['/category/parent/child-1/child-2/'] = $cat3;
235
236                self::$term_ids[ $cat3 ] = 'category';
237
238                $cat4 = $factory->term->create(
239                        array(
240                                'taxonomy' => 'category',
241                                'name'     => 'cat-a',
242                        )
243                );
244
245                self::$term_ids[ $cat4 ] = 'category';
246
247                $cat5 = $factory->term->create(
248                        array(
249                                'taxonomy' => 'category',
250                                'name'     => 'cat-b',
251                        )
252                );
253
254                self::$term_ids[ $cat5 ] = 'category';
255
256                $tag1 = $factory->term->create(
257                        array(
258                                'name' => 'post-formats',
259                        )
260                );
261
262                self::$term_ids[ $tag1 ] = 'post_tag';
263        }
264
265        /**
266         * Clean up shared fixtures.
267         *
268         * @since 4.1.0
269         */
270        public static function delete_shared_fixtures() {
271                self::$author_id   = null;
272                self::$post_ids    = array();
273                self::$comment_ids = array();
274                self::$term_ids    = array();
275                self::$terms       = array();
276        }
277
278        /**
279         * Assert that a given URL is the same a the canonical URL generated by WP.
280         *
281         * @since 4.1.0
282         *
283         * @param string $test_url                Raw URL that will be run through redirect_canonical().
284         * @param string $expected                Expected string.
285         * @param int    $ticket                  Optional. Trac ticket number.
286         * @param array  $expected_doing_it_wrong Array of class/function names expected to throw _doing_it_wrong() notices.
287         */
288        public function assertCanonical( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
289                $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, (array) $expected_doing_it_wrong );
290
291                $ticket_ref = ( $ticket > 0 ) ? 'Ticket #' . $ticket : '';
292
293                if ( is_string( $expected ) ) {
294                        $expected = array( 'url' => $expected );
295                } elseif ( is_array( $expected ) && ! isset( $expected['url'] ) && ! isset( $expected['qv'] ) ) {
296                        $expected = array( 'qv' => $expected );
297                }
298
299                if ( ! isset( $expected['url'] ) && ! isset( $expected['qv'] ) ) {
300                        $this->fail( 'No valid expected output was provided' );
301                }
302
303                $this->go_to( home_url( $test_url ) );
304
305                // Does the redirect match what's expected?
306                $can_url        = $this->get_canonical( $test_url );
307                $parsed_can_url = parse_url( $can_url );
308
309                // Just test the path and query if present.
310                if ( isset( $expected['url'] ) ) {
311                        $this->assertSame( $expected['url'], $parsed_can_url['path'] . ( ! empty( $parsed_can_url['query'] ) ? '?' . $parsed_can_url['query'] : '' ), $ticket_ref );
312                }
313
314                // If the test data doesn't include expected query vars, then we're done here.
315                if ( ! isset( $expected['qv'] ) ) {
316                        return;
317                }
318
319                // "make" that the request and check the query is correct.
320                $this->go_to( $can_url );
321
322                // Are all query vars accounted for, and correct?
323                global $wp;
324
325                $query_vars = array_diff( $wp->query_vars, $wp->extra_query_vars );
326                if ( ! empty( $parsed_can_url['query'] ) ) {
327                        parse_str( $parsed_can_url['query'], $_qv );
328
329                        // $_qv should not contain any elements which are set in $query_vars already
330                        // (i.e. $_GET vars should not be present in the Rewrite).
331                        $this->assertSame( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref );
332
333                        $query_vars = array_merge( $query_vars, $_qv );
334                }
335
336                $this->assertEquals( $expected['qv'], $query_vars );
337        }
338
339        /**
340         * Get the canonical URL given a raw URL.
341         *
342         * @param string $test_url Should be relative to the site "front", ie /category/uncategorized/
343         *                         as opposed to http://example.com/category/uncategorized/
344         * @return $can_url Returns the original $test_url if no canonical can be generated, otherwise returns
345         *                  the fully-qualified URL as generated by redirect_canonical().
346         */
347        public function get_canonical( $test_url ) {
348                $test_url = home_url( $test_url );
349
350                $can_url = redirect_canonical( $test_url, false );
351                if ( ! $can_url ) {
352                        return $test_url; // No redirect will take place for this request.
353                }
354
355                return $can_url;
356        }
357}
Note: See TracBrowser for help on using the repository browser.