WP_Canonical_UnitTestCase::assertCanonical()   C
last analyzed

Complexity

Conditions 13
Paths 144

Size

Total Lines 53
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 26
nc 144
nop 4
dl 0
loc 53
rs 5.7588
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
240
     *                  the fully-qualified URL as generated by redirect_canonical().
241
     */
242
    public function get_canonical( $test_url ) 
243
    {
244
        $test_url = home_url($test_url);
245
246
        $can_url = redirect_canonical($test_url, false);
247
        if (! $can_url ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
248
              return $test_url;
249
        }
250
        // No redirect will take place for this request
251
252
        return $can_url;
253
    }
254
}
255