forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththePost.php
More file actions
411 lines (361 loc) · 12.4 KB
/
thePost.php
File metadata and controls
411 lines (361 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
/**
* @group query
* @covers WP_Query::the_post
*/
class Tests_Query_ThePost extends WP_UnitTestCase {
/**
* Author IDs created for shared fixtures.
*
* @var int[]
*/
public static $author_ids = array();
/**
* Post parent ID created for shared fixtures.
*
* @var int
*/
public static $page_parent_id = 0;
/**
* Post child IDs created for shared fixtures.
*
* @var int[]
*/
public static $page_child_ids = array();
/**
* Create the shared fixtures.
*
* @param WP_UnitTest_Factory $factory Factory object.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$author_ids = $factory->user->create_many( 5, array( 'role' => 'author' ) );
self::$page_parent_id = $factory->post->create( array( 'post_type' => 'page' ) );
// Create child pages.
foreach ( self::$author_ids as $author_id ) {
self::$page_child_ids[] = $factory->post->create(
array(
'post_type' => 'page',
'post_parent' => self::$page_parent_id,
'post_author' => $author_id,
)
);
}
}
/**
* Ensure custom 'fields' values are respected.
*
* @ticket 56992
*/
public function test_wp_query_respects_custom_fields_values() {
global $wpdb;
add_filter(
'posts_fields',
function ( $fields, $query ) {
global $wpdb;
if ( $query->get( 'fields' ) === 'custom' ) {
$fields = "$wpdb->posts.ID,$wpdb->posts.post_author";
}
return $fields;
},
10,
2
);
$query = new WP_Query(
array(
'fields' => 'custom',
'post_type' => 'page',
'post__in' => self::$page_child_ids,
)
);
$this->assertNotEmpty( $query->posts, 'The query is expected to return results' );
$this->assertSame( $query->get( 'fields' ), 'custom', 'The WP_Query class is expected to use the custom fields value' );
$this->assertStringContainsString( "$wpdb->posts.ID,$wpdb->posts.post_author", $query->request, 'The database query is expected to use the custom fields value' );
}
/**
* Ensure custom 'fields' populates the global post in the loop.
*
* @ticket 56992
*/
public function test_wp_query_with_custom_fields_value_populates_the_global_post() {
global $wpdb;
add_filter(
'posts_fields',
function ( $fields, $query ) {
global $wpdb;
if ( $query->get( 'fields' ) === 'custom' ) {
$fields = "$wpdb->posts.ID,$wpdb->posts.post_author";
}
return $fields;
},
10,
2
);
$query = new WP_Query(
array(
'fields' => 'custom',
'post_type' => 'page',
'post__in' => self::$page_child_ids,
'orderby' => 'id',
'order' => 'ASC',
)
);
$query->the_post();
// Get the global post and specific post.
$global_post = get_post();
$specific_post = get_post( self::$page_child_ids[0], ARRAY_A );
$this->assertSameSetsWithIndex( $specific_post, $global_post->to_array(), 'The global post is expected to be fully populated.' );
$this->assertNotEmpty( get_the_title(), 'The title is expected to be populated.' );
$this->assertNotEmpty( get_the_content(), 'The content is expected to be populated.' );
$this->assertNotEmpty( get_the_excerpt(), 'The excerpt is expected to be populated.' );
}
/**
* Ensure that a secondary loop populates the global post completely regardless of the fields parameter.
*
* @ticket 56992
*
* @dataProvider data_the_loop_fields
*
* @param string $fields Fields parameter for use in the query.
*/
public function test_the_loop_populates_the_global_post_completely( $fields ) {
$query = new WP_Query(
array(
'fields' => $fields,
'post_type' => 'page',
'page_id' => self::$page_child_ids[0],
)
);
$this->assertNotEmpty( $query->posts, 'The query is expected to return results' );
// Start the loop.
$query->the_post();
// Get the global post and specific post.
$global_post = get_post();
$specific_post = get_post( self::$page_child_ids[0], ARRAY_A );
$this->assertSameSetsWithIndex( $specific_post, $global_post->to_array(), 'The global post is expected to be fully populated.' );
$this->assertNotEmpty( get_the_title(), 'The title is expected to be populated.' );
$this->assertNotEmpty( get_the_content(), 'The content is expected to be populated.' );
$this->assertNotEmpty( get_the_excerpt(), 'The excerpt is expected to be populated.' );
}
/**
* Ensure that a secondary loop primes the post cache completely regardless of the fields parameter.
*
* @ticket 56992
*
* @dataProvider data_the_loop_fields
*
* @param string $fields Fields parameter for use in the query.
* @param int $expected_queries Expected number of queries when starting the loop.
*/
public function test_the_loop_primes_the_post_cache( $fields, $expected_queries ) {
$query = new WP_Query(
array(
'fields' => $fields,
'post_type' => 'page',
'post__in' => self::$page_child_ids,
)
);
// Start the loop.
$start_queries = get_num_queries();
$query->the_post();
$end_queries = get_num_queries();
/*
* Querying complete posts: 2 queries.
* 1. User meta data.
* 2. User data.
*
* Querying partial posts: 4 queries.
* 1. Post objects
* 2. Post meta data.
* 3. User meta data.
* 4. User data.
*/
$this->assertSame( $expected_queries, $end_queries - $start_queries, "Starting the loop should make $expected_queries db queries." );
// Complete the loop.
$start_queries = get_num_queries();
while ( $query->have_posts() ) {
$query->the_post();
}
$end_queries = get_num_queries();
$this->assertSame( 0, $end_queries - $start_queries, 'The cache is expected to be primed by the loop.' );
}
/**
* Ensure that a secondary loop primes the author cache completely regardless of the fields parameter.
*
* @ticket 56992
*
* @dataProvider data_the_loop_fields
*
* @param string $fields Fields parameter for use in the query.
* @param int $expected_queries Expected number of queries when starting the loop.
*/
public function test_the_loop_primes_the_author_cache( $fields, $expected_queries ) {
$query = new WP_Query(
array(
'fields' => $fields,
'post_type' => 'page',
'post__in' => self::$page_child_ids,
)
);
// Start the loop.
$start_queries = get_num_queries();
$query->the_post();
$end_queries = get_num_queries();
/*
* Querying complete posts: 2 queries.
* 1. User meta data.
* 2. User data.
*
* Querying partial posts: 4 queries.
* 1. Post objects
* 2. Post meta data.
* 3. User meta data.
* 4. User data.
*/
$this->assertSame( $expected_queries, $end_queries - $start_queries, "Starting the loop should make $expected_queries db queries." );
// Complete the loop.
$start_queries = get_num_queries();
while ( $query->have_posts() ) {
$query->the_post();
get_the_author();
}
$end_queries = get_num_queries();
$this->assertSame( 0, $end_queries - $start_queries, 'The cache is expected to be primed by the loop.' );
}
/**
* Data provider for:
* - test_the_loop_populates_the_global_post_completely,
* - test_the_loop_primes_the_post_cache, and,
* - test_the_loop_primes_the_author_cache.
*
* @return array[]
*/
public function data_the_loop_fields() {
return array(
'all fields' => array( 'all', 2 ),
'all fields (empty fields)' => array( '', 2 ),
'post IDs' => array( 'ids', 4 ),
'post ids and parent' => array( 'id=>parent', 4 ),
);
}
/**
* Ensure draft content is shown for post previews and permalinks for logged in users.
*
* @ticket 56992
*/
public function test_post_preview_links_draft_posts() {
$user_id = self::$author_ids[0];
wp_set_current_user( $user_id );
$draft_post = $this->factory()->post->create(
array(
'post_status' => 'draft',
'post_author' => $user_id,
'post_content' => 'ticket 56992',
)
);
// Ensure the global post is populated with the draft content for the preview link.
$this->go_to( get_preview_post_link( $draft_post ) );
if ( have_posts() ) {
the_post();
}
$this->assertSame( 'ticket 56992', get_the_content(), 'Preview link should show draft content to logged in user' );
// Ensure the global post is populated with the draft content for the permalink.
$this->go_to( get_permalink( $draft_post ) );
if ( have_posts() ) {
the_post();
}
$this->assertSame( 'ticket 56992', get_the_content(), 'Permalink should show draft content to logged in user' );
// Ensure the global post is not populated with the draft content for the preview link when logged out.
wp_set_current_user( 0 );
$this->go_to( get_preview_post_link( $draft_post ) );
if ( have_posts() ) {
the_post();
}
$this->assertEmpty( get_the_content(), 'Preview link should not show draft content to logged out users' );
// Ensure the global post is not populated with the draft content for the permalink when logged out.
$this->go_to( get_permalink( $draft_post ) );
if ( have_posts() ) {
the_post();
}
$this->assertEmpty( get_the_content(), 'Permalink should not show draft content to logged out users' );
}
/**
* Ensure autosave content is shown for post previews.
*
* @ticket 56992
*/
public function test_post_preview_links_autosaves() {
$user_id = self::$author_ids[0];
wp_set_current_user( $user_id );
$published_post = $this->factory()->post->create(
array(
'post_status' => 'publish',
'post_author' => $user_id,
'post_content' => 'ticket 56992',
)
);
// Create an autosave for the published post.
$autosave = get_post( $published_post, ARRAY_A );
$autosave['post_ID'] = $published_post;
$autosave['post_content'] = 'ticket 56992 edited';
wp_create_post_autosave( $autosave );
// Set up the preview $_GET parameters.
$nonce = wp_create_nonce( 'post_preview_' . $published_post );
$query_args['preview_id'] = $published_post;
$query_args['preview_nonce'] = $nonce;
$post_preview_link = get_preview_post_link( $published_post, $query_args );
/*
* Set up the GET parameters for the preview link.
*
* _show_post_preview() checks the $_GET super global for preview
* and nonce parameters. It needs to run prior to the global query
* being set up in WP_Query (via $this->go_to()), so the preview
* parameters are created here to ensure _show_post_preview()
* runs correctly.
*/
$_GET['preview_id'] = $published_post;
$_GET['preview_nonce'] = $nonce;
_show_post_preview();
// Ensure the global post is populated with the autosave content for the preview link.
$this->go_to( $post_preview_link );
if ( have_posts() ) {
the_post();
}
$this->assertSame( 'ticket 56992 edited', get_the_content(), 'Preview link should show autosave content to logged in user' );
// Ensure the global post is populated with the published content for the permalink.
$this->go_to( get_permalink( $published_post ) );
if ( have_posts() ) {
the_post();
}
$this->assertSame( 'ticket 56992', get_the_content(), 'Permalink should show published content to logged in user' );
wp_set_current_user( 0 );
// New user, new nonce; set up the preview $_GET parameters.
$nonce = wp_create_nonce( 'post_preview_' . $published_post );
$query_args['preview_id'] = $published_post;
$query_args['preview_nonce'] = $nonce;
$post_preview_link = get_preview_post_link( $published_post, $query_args );
/*
* Set up the GET parameters for the preview link.
*
* _show_post_preview() checks the $_GET super global for preview
* and nonce parameters. It needs to run prior to the global query
* being set up in WP_Query (via $this->go_to()), so the preview
* parameters are created here to ensure _show_post_preview()
* runs correctly.
*/
$_GET['preview_id'] = $published_post;
$_GET['preview_nonce'] = $nonce;
_show_post_preview();
// Ensure the global post is not populated with the draft content for the preview link when logged out.
$this->go_to( $post_preview_link );
if ( have_posts() ) {
the_post();
}
$this->assertSame( 'ticket 56992', get_the_content(), 'Preview link should show published content to logged out users' );
// Ensure the global post is not populated with the draft content for the permalink when logged out.
$this->go_to( get_permalink( $published_post ) );
if ( have_posts() ) {
the_post();
}
$this->assertSame( 'ticket 56992', get_the_content(), 'Permalink should show published content to logged out users' );
}
}