Tests_Post_getPages::test_get_pages_cache()   D
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 90
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 58
nc 128
nop 0
dl 0
loc 90
rs 4.921
c 0
b 0
f 0

How to fix   Long Method   

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
/**
4
 * @group post
5
 */
6
7
class Tests_Post_getPages extends WP_UnitTestCase
8
{
9
    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...
10
    {
11
        parent::setUp();
12
    }
13
14
    /**
15
     * @ticket 23167
16
     */
17
    function test_get_pages_cache() 
18
    {
19
        global $wpdb;
20
21
        self::factory()->post->create_many(3, array( 'post_type' => 'page' ));
22
        wp_cache_delete('last_changed', 'posts');
23
        $this->assertFalse(wp_cache_get('last_changed', 'posts'));
24
25
        $pages = get_pages();
26
        $this->assertEquals(3, count($pages));
27
        $this->assertNotEmpty($time1 = wp_cache_get('last_changed', 'posts'));
28
        $num_queries = $wpdb->num_queries;
29
        foreach ( $pages as $page ) {
30
              $this->assertInstanceOf('WP_Post', $page);
31
        }
32
33
        // Again. num_queries and last_changed should remain the same.
34
        $pages = get_pages();
35
        $this->assertEquals(3, count($pages));
36
        $this->assertEquals($time1, wp_cache_get('last_changed', 'posts'));
37
        $this->assertEquals($num_queries, $wpdb->num_queries);
38
        foreach ( $pages as $page ) {
39
              $this->assertInstanceOf('WP_Post', $page);
40
        }
41
42
        // Again with different args. last_changed should not increment because of
43
        // different args to get_pages(). num_queries should bump by 1.
44
        $pages = get_pages(array( 'number' => 2 ));
45
        $this->assertEquals(2, count($pages));
46
        $this->assertEquals($time1, wp_cache_get('last_changed', 'posts'));
47
        $this->assertEquals($num_queries + 1, $wpdb->num_queries);
48
        foreach ( $pages as $page ) {
49
              $this->assertInstanceOf('WP_Post', $page);
50
        }
51
52
        $num_queries = $wpdb->num_queries;
53
54
        // Again. num_queries and last_changed should remain the same.
55
        $pages = get_pages(array( 'number' => 2 ));
56
        $this->assertEquals(2, count($pages));
57
        $this->assertEquals($time1, wp_cache_get('last_changed', 'posts'));
58
        $this->assertEquals($num_queries, $wpdb->num_queries);
59
        foreach ( $pages as $page ) {
60
              $this->assertInstanceOf('WP_Post', $page);
61
        }
62
63
        // Do the first query again. The interim queries should not affect it.
64
        $pages = get_pages();
65
        $this->assertEquals(3, count($pages));
66
        $this->assertEquals($time1, wp_cache_get('last_changed', 'posts'));
67
        $this->assertEquals($num_queries, $wpdb->num_queries);
68
        foreach ( $pages as $page ) {
69
              $this->assertInstanceOf('WP_Post', $page);
70
        }
71
72
        // Force last_changed to increment.
73
        clean_post_cache($pages[0]->ID);
74
        $this->assertNotEquals($time1, $time2 = wp_cache_get('last_changed', 'posts'));
75
76
        $num_queries = $wpdb->num_queries;
77
78
        // last_changed bumped so num_queries should increment.
79
        $pages = get_pages(array( 'number' => 2 ));
80
        $this->assertEquals(2, count($pages));
81
        $this->assertEquals($time2, wp_cache_get('last_changed', 'posts'));
82
        $this->assertEquals($num_queries + 1, $wpdb->num_queries);
83
        foreach ( $pages as $page ) {
84
              $this->assertInstanceOf('WP_Post', $page);
85
        }
86
87
        $last_changed = wp_cache_get('last_changed', 'posts');
88
89
        // This should bump last_changed.
90
        wp_delete_post($pages[0]->ID);
91
        $old_changed_float = $this->_microtime_to_float($last_changed);
92
        $new_changed_float = $this->_microtime_to_float(wp_cache_get('last_changed', 'posts'));
93
        $this->assertGreaterThan($old_changed_float, $new_changed_float);
94
95
        $num_queries = $wpdb->num_queries;
96
        $last_changed = wp_cache_get('last_changed', 'posts');
97
98
        // num_queries should bump after wp_delete_post() bumps last_changed.
99
        $pages = get_pages();
100
        $this->assertEquals(2, count($pages));
101
        $this->assertEquals($last_changed, wp_cache_get('last_changed', 'posts'));
102
        $this->assertEquals($num_queries + 1, $wpdb->num_queries);
103
        foreach ( $pages as $page ) {
104
              $this->assertInstanceOf('WP_Post', $page);
105
        }
106
    }
107
108
    /**
109
     * @ticket 20376
110
     */
111
    function test_get_pages_meta() 
112
    {
113
        $posts = self::factory()->post->create_many(3, array( 'post_type' => 'page' ));
114
        add_post_meta($posts[0], 'some-meta-key', '0');
115
        add_post_meta($posts[1], 'some-meta-key', '');
116
        add_post_meta($posts[2], 'some-meta-key', '1');
117
118
        $this->assertEquals(1, count(get_pages(array( 'meta_key' => 'some-meta-key', 'meta_value' => '0' ))));
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
119
        $this->assertEquals(1, count(get_pages(array( 'meta_key' => 'some-meta-key', 'meta_value' => '1' ))));
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
120
        $this->assertEquals(3, count(get_pages(array( 'meta_key' => 'some-meta-key' ))));
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
121
    }
122
123
    /**
124
     * @ticket 22074
125
     */
126
    function test_get_pages_include_exclude() 
127
    {
128
        $page_ids = array();
129
130
        foreach ( range(1, 20) as $i ) {
131
              $page_ids[] = self::factory()->post->create(array( 'post_type' => 'page' ));
132
        }
133
134
        $inc = array_slice($page_ids, 0, 10);
135
        sort($inc);
136
        $exc = array_slice($page_ids, 10);
137
        sort($exc);
138
139
        $include = get_pages(array( 'include' => $inc ));
140
        $inc_result = wp_list_pluck($include, 'ID');
141
        sort($inc_result);
142
        $this->assertEquals($inc, $inc_result);
143
144
        $exclude = get_pages(array( 'exclude' => $exc ));
145
        $exc_result = wp_list_pluck($exclude, 'ID');
146
        sort($exc_result);
147
        $this->assertEquals($inc, $exc_result);
148
    }
149
150
    /**
151
     * @ticket 9470
152
     */
153
    function test_get_pages_parent() 
154
    {
155
        $page_id1 = self::factory()->post->create(array( 'post_type' => 'page' ));
156
        $page_id2 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_id1 ));
157
        $page_id3 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_id2 ));
158
        $page_id4 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_id1 ));
159
160
        $pages = get_pages(array( 'parent' => 0, 'hierarchical' => false ));
161
        $this->assertEqualSets(array( $page_id1 ), wp_list_pluck($pages, 'ID'));
162
163
        $pages = get_pages(array( 'parent' => $page_id1, 'hierarchical' => false ));
164
        $this->assertEqualSets(array( $page_id2, $page_id4 ), wp_list_pluck($pages, 'ID'));
165
166
        $pages = get_pages(array( 'parent' => array( $page_id1, $page_id2 ), 'hierarchical' => false ));
167
        $this->assertEqualSets(array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck($pages, 'ID'));
168
169
        $pages = get_pages(array( 'parent' => 0 ));
170
        $this->assertEqualSets(array( $page_id1 ), wp_list_pluck($pages, 'ID'));
171
172
        $pages = get_pages(array( 'parent' => $page_id1 ));
173
        $this->assertEqualSets(array( $page_id2, $page_id4 ), wp_list_pluck($pages, 'ID'));
174
175
        $pages = get_pages(array( 'parent' => array( $page_id1, $page_id2 ) ));
176
        $this->assertEqualSets(array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck($pages, 'ID'));
177
    }
178
179
    /**
180
     * @ticket 22389
181
     */
182
    function test_wp_dropdown_pages() 
183
    {
184
        self::factory()->post->create_many(5, array( 'post_type' => 'page' ));
185
186
        preg_match_all('#<option#', wp_dropdown_pages('echo=0'), $matches);
187
188
        $this->assertEquals(5, count($matches[0]));
189
    }
190
191
    /**
192
     * @ticket 22208
193
     */
194
    function test_get_chidren_fields_ids() 
195
    {
196
        $post_id = self::factory()->post->create();
197
        $child_ids = self::factory()->post->create_many(5, array( 'post_parent' => $post_id ));
198
199
        $post_ids = get_children(array( 'fields' => 'ids', 'post_parent' => $post_id ));
200
        $this->assertEqualSets($child_ids, $post_ids);
201
    }
202
203
    /**
204
     * @ticket 25750
205
     */
206
    function test_get_pages_hierarchical_and_no_parent() 
207
    {
208
        global $wpdb;
209
        $page_1 = self::factory()->post->create(array( 'post_type' => 'page' ));
210
        $page_2 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_1 ));
211
        $page_3 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_1 ));
212
        $page_4 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_2 ));
213
214
        $pages = get_pages(); // Defaults: hierarchical = true, parent = -1
215
        $pages_default_args = get_pages(array( 'hierarchical' => true, 'parent' => -1 ));
216
        // Confirm the defaults.
217
        $this->assertEquals($pages, $pages_default_args);
218
219
        /*
220
         * Here's the tree we are testing:
221
         *
222
         * page 1
223
         * - page 2
224
         * -- page 4
225
         * - page 3
226
         *
227
         * If hierarchical => true works, the order will be 1,2,4,3.
228
         * If it doesn't, they will be in the creation order, 1,2,3,4.
229
         */
230
231
        $this->assertEqualSets(array( $page_1, $page_2, $page_4, $page_3 ), wp_list_pluck($pages, 'ID'));
232
    }
233
234
    /**
235
     * @ticket 18701
236
     */
237
    public function test_get_pages_hierarchical_empty_child_of() 
238
    {
239
        $page_1 = self::factory()->post->create(array( 'post_type' => 'page' ));
240
        $page_2 = self::factory()->post->create(array( 'post_type' => 'page' ));
241
        $page_3 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_1 ));
242
        $page_4 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_1 ));
243
244
        $pages = get_pages(); // Defaults: hierarchical = true, child_of = '', parent = -1
245
        $default_args = get_pages(
246
            array(
247
            'hierarchical' => true,
248
            'child_of'     => ''
249
            ) 
250
        );
251
252
        $this->assertEquals($pages, $default_args);
253
254
        /*
255
         * Page tree:
256
         *
257
         * page 1 (parent 0)
258
         * – page 3 (parent 1)
259
         * – page 4 (parent 1)
260
         * page 2 (parent 0)
261
         *
262
         * With default arguments, if child_of is empty (normalized to 0), only pages with a matching
263
         * post_parent will be returned, in the order they were created: 1, 2.
264
         */
265
266
        $found_pages = wp_list_filter($pages, array( 'post_parent' => 0 ));
267
268
        $this->assertEqualSets(array( $page_1, $page_2 ), wp_list_pluck($found_pages, 'ID'));
269
    }
270
271
    /**
272
     * @ticket 18701
273
     */
274
    public function test_get_pages_non_hierarchical_empty_child_of() 
275
    {
276
        $page_1 = self::factory()->post->create(array( 'post_type' => 'page' ));
277
        $page_2 = self::factory()->post->create(array( 'post_type' => 'page' ));
278
        $page_3 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_1 ));
279
        $page_4 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_1 ));
280
281
        $pages = get_pages(array( 'hierarchical' => false )); // child_of = '', parent = -1
282
283
        /*
284
         * Page tree:
285
         *
286
         * page 1 (parent 0)
287
         * – page 3 (parent 1)
288
         * – page 4 (parent 1)
289
         * page 2 (parent 0)
290
         *
291
         * If hierarchical is false and child_of is empty (normalized to 0), pages will be returned
292
         * in order of creation: 1, 2, 3, 4, regardless of parent.
293
         */
294
295
        $this->assertEqualSets(array( $page_1, $page_2, $page_3, $page_4 ), wp_list_pluck($pages, 'ID'));
296
    }
297
298
    /**
299
     * @ticket 18701
300
     */
301
    public function test_get_pages_hierarchical_non_empty_child_of() 
302
    {
303
        $page_1 = self::factory()->post->create(array( 'post_type' => 'page' ));
304
        $page_2 = self::factory()->post->create(array( 'post_type' => 'page' ));
305
        $page_3 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_1 ));
306
        $page_4 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_3 ));
307
        $page_5 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_1 ));
308
309
        $pages = get_pages(array( 'child_of' => $page_1 )); // Defaults: hierarchical = true, parent = -1.
310
311
        /*
312
         * Page tree:
313
         *
314
         * page 1 (parent 0)
315
         * – page 3 (parent 1)
316
         * –– page 4 (parent 3)
317
         * – page 5 (parent 1)
318
         * page 2 (parent 0)
319
         *
320
         * If hierarchical is true (default), and child_of is not empty, pages will be returned
321
         * hierarchically in order of creation: 3, 4, 5.
322
         */
323
324
        $this->assertEqualSets(array( $page_3, $page_4, $page_5 ), wp_list_pluck($pages, 'ID'));
325
    }
326
327
    /**
328
     * @ticket 18701
329
     */
330
    public function test_get_pages_non_hierarchical_non_empty_child_of() 
331
    {
332
        $page_1 = self::factory()->post->create(array( 'post_type' => 'page' ));
333
        $page_2 = self::factory()->post->create(array( 'post_type' => 'page' ));
334
        $page_3 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_1 ));
335
        $page_4 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_3 ));
336
        $page_5 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $page_1 ));
337
338
        $pages = get_pages(array( 'hierarchical' => false, 'child_of' => $page_1 ));
339
340
        /*
341
         * Page tree:
342
         *
343
         * page 1 (parent 0)
344
         * – page 3 (parent 1)
345
         * –– page 4 (parent 3)
346
         * – page 5 (parent 1)
347
         * page 2 (parent 0)
348
         *
349
         * If hierarchical is false, and child_of is not empty, pages will (apparently) be returned
350
         * hierarchically anyway in order of creation: 3, 4, 5.
351
         */
352
        $this->assertEqualSets(array( $page_3, $page_4, $page_5 ), wp_list_pluck($pages, 'ID'));
353
354
        // How it should work.
355
        $found_pages = wp_list_filter($pages, array( 'post_parent' => $page_1 ));
356
        $this->assertEqualSets(array( $page_3, $page_5 ), wp_list_pluck($found_pages, 'ID'));
357
358
    }
359
360
    function test_wp_list_pages_classes() 
361
    {
362
        $type = 'taco';
363
        register_post_type($type, array( 'hierarchical' => true, 'public' => true ));
364
365
        $posts = self::factory()->post->create_many(2, array( 'post_type' => $type ));
366
        $post_id = reset($posts);
367
368
        $this->go_to("/?p=$post_id&post_type=$type");
369
370
        $this->assertEquals($post_id, get_queried_object_id());
371
372
        $output = wp_list_pages(
373
            array(
374
            'echo' => false,
375
            'title_li' => '',
376
            'post_type' => $type
377
            ) 
378
        );
379
380
        $this->assertNotEmpty($output);
381
        $this->assertEquals(2, substr_count($output, 'class="page_item '));
382
        $this->assertContains('current_page_item', $output);
383
        $this->assertEquals(1, substr_count($output, 'current_page_item'));
384
385
        _unregister_post_type($type);
386
    }
387
388
    function test_exclude_tree() 
389
    {
390
        $post_id1 = self::factory()->post->create(array( 'post_type' => 'page' ));
391
        $post_id2 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $post_id1 ));
392
        $post_id3 = self::factory()->post->create(array( 'post_type' => 'page' ));
393
        $post_id4 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $post_id3 ));
394
395
        $all = get_pages();
396
397
        $this->assertCount(4, $all);
398
399
        $exclude1 = get_pages("exclude_tree=$post_id1");
400
        $this->assertCount(2, $exclude1);
401
402
        $exclude2 = get_pages(array( 'exclude_tree' => $post_id1 ));
403
        $this->assertCount(2, $exclude2);
404
405
        $exclude3 = get_pages(array( 'exclude_tree' => array( $post_id1 ) ));
406
        $this->assertCount(2, $exclude3);
407
408
        $exclude4 = get_pages(array( 'exclude_tree' => array( $post_id1, $post_id2 ) ));
409
        $this->assertCount(2, $exclude4);
410
411
        $exclude5 = get_pages(array( 'exclude_tree' => array( $post_id1, $post_id3 ) ));
412
        $this->assertCount(0, $exclude5);
413
414
        $post_id5 = self::factory()->post->create(array( 'post_type' => 'page' ));
415
        $post_id6 = self::factory()->post->create(array( 'post_type' => 'page', 'post_parent' => $post_id5 ));
416
417
        $exclude6 = get_pages(array( 'exclude_tree' => array( $post_id1, $post_id3 ) ));
418
        $this->assertCount(2, $exclude6);
419
    }
420
}
421