Make WordPress Core

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

Last change on this file since 35242 was 35242, checked in by wonderboymusic, 10 years ago

Unit Tests: after [35225], make factory a method/getter on WP_UnitTestCase and add magic methods for BC for every plugin that is extending WP_UnitTestCase and accessing the $factory instance prop.

Props nerrad, wonderboymusic.
See #30017, #33968.

File size: 8.6 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( $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                if ( is_multisite() ) {
138                        wpmu_delete_user( self::$author_id );
139                } else {
140                        wp_delete_user( self::$author_id );
141                }
142
143                foreach ( self::$post_ids as $pid ) {
144                        wp_delete_post( $pid, true );
145                }
146
147                foreach ( self::$comment_ids as $cid ) {
148                        wp_delete_comment( $cid, true );
149                }
150
151                foreach ( self::$term_ids as $tid => $tax ) {
152                        wp_delete_term( $tid, $tax );
153                }
154
155                self::$author_id = null;
156                self::$post_ids = array();
157                self::$comment_ids = array();
158                self::$term_ids = array();
159                self::$terms = array();
160        }
161
162        /**
163         * Assert that a given URL is the same a the canonical URL generated by WP.
164         *
165         * @since 4.1.0
166         *
167         * @param string $test_url                Raw URL that will be run through redirect_canonical().
168         * @param string $expected                Expected string.
169         * @param int    $ticket                  Optional. Trac ticket number.
170         * @param array  $expected_doing_it_wrong Array of class/function names expected to throw _doing_it_wrong() notices.
171         */
172        public function assertCanonical( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
173                $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, (array) $expected_doing_it_wrong );
174
175                if ( $ticket )
176                        $this->knownWPBug( $ticket );
177
178                $ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null;
179global $wpdb;
180//print_r( $wpdb->get_results( "SELECT * FROM $wpdb->terms" ) );
181                if ( is_string($expected) )
182                        $expected = array('url' => $expected);
183                elseif ( is_array($expected) && !isset($expected['url']) && !isset($expected['qv']) )
184                        $expected = array( 'qv' => $expected );
185
186                if ( !isset($expected['url']) && !isset($expected['qv']) )
187                        $this->markTestSkipped('No valid expected output was provided');
188
189                $this->go_to( home_url( $test_url ) );
190
191                // Does the redirect match what's expected?
192                $can_url = $this->get_canonical( $test_url );
193                $parsed_can_url = parse_url($can_url);
194
195                // Just test the Path and Query if present
196                if ( isset($expected['url']) ) {
197                        $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref );
198                }
199
200                if ( ! isset($expected['qv']) )
201                        return;
202
203                // "make" that the request and check the query is correct
204                $this->go_to( $can_url );
205
206                // Are all query vars accounted for, And correct?
207                global $wp;
208
209                $query_vars = array_diff($wp->query_vars, $wp->extra_query_vars);
210                if ( !empty($parsed_can_url['query']) ) {
211                        parse_str($parsed_can_url['query'], $_qv);
212
213                        // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite)
214                        $this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref );
215
216                        $query_vars = array_merge($query_vars, $_qv);
217                }
218
219                $this->assertEquals( $expected['qv'], $query_vars );
220        }
221
222        /**
223         * Get the canonical URL given a raw URL.
224         *
225         * @param string $test_url Should be relative to the site "front", ie /category/uncategorized/
226         *                         as opposed to http://example.com/category/uncategorized/
227         * @return $can_url Returns the original $test_url if no canonical can be generated, otherwise returns
228         *                  the fully-qualified URL as generated by redirect_canonical().
229         */
230        public function get_canonical( $test_url ) {
231                $test_url = home_url( $test_url );
232
233                $can_url = redirect_canonical( $test_url, false );
234                if ( ! $can_url )
235                        return $test_url; // No redirect will take place for this request
236
237                return $can_url;
238        }
239}
Note: See TracBrowser for help on using the repository browser.