Make WordPress Core

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

Last change on this file since 34928 was 34802, checked in by DrewAPicture, 10 years ago

Tests: Introduce WP_UnitTestCase::reset_permalinks(), an attempt to DRY up logic for resetting and restoring default permalinks on setUp() and tearDown().

See #33968.

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