forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupPostdata.php
More file actions
441 lines (369 loc) · 10.6 KB
/
setupPostdata.php
File metadata and controls
441 lines (369 loc) · 10.6 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* @group query
* @covers ::setup_postdata
*/
class Tests_Query_SetupPostdata extends WP_UnitTestCase {
protected $global_keys = array( 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );
protected $global_data = array();
protected $pages_global;
public function setUp() {
parent::setUp();
return;
foreach ( $this->global_keys as $global_key ) {
if ( isset( $GLOBALS[ $global_key ] ) ) {
$this->global_data[ $global_key ] = $GLOBALS[ $global_key ];
unset( $GLOBALS[ $global_key ] );
} else {
$this->global_data[ $global_key ] = null;
}
}
}
public function test_id() {
$p = self::factory()->post->create_and_get();
setup_postdata( $p );
$this->assertNotEmpty( $p->ID );
$this->assertSame( $p->ID, $GLOBALS['id'] );
}
/**
* @ticket 30970
*/
public function test_setup_by_id() {
$p = self::factory()->post->create_and_get();
setup_postdata( $p->ID );
$this->assertSame( $p->ID, $GLOBALS['id'] );
}
/**
* @ticket 30970
*/
public function test_setup_by_fake_post() {
$fake = new stdClass;
$fake->ID = 98765;
setup_postdata( $fake->ID );
// Fails because there's no post with this ID.
$this->assertNotSame( $fake->ID, $GLOBALS['id'] );
}
/**
* @ticket 30970
*/
public function test_setup_by_postish_object() {
$p = self::factory()->post->create();
$post = new stdClass();
$post->ID = $p;
setup_postdata( $p );
$this->assertSame( $p, $GLOBALS['id'] );
}
public function test_authordata() {
$u = self::factory()->user->create_and_get();
$p = self::factory()->post->create_and_get(
array(
'post_author' => $u->ID,
)
);
setup_postdata( $p );
$this->assertNotEmpty( $GLOBALS['authordata'] );
$this->assertEquals( $u, $GLOBALS['authordata'] );
}
public function test_currentday() {
$p = self::factory()->post->create_and_get(
array(
'post_date' => '1980-09-09 06:30:00',
)
);
setup_postdata( $p );
$this->assertSame( '09.09.80', $GLOBALS['currentday'] );
}
public function test_currentmonth() {
$p = self::factory()->post->create_and_get(
array(
'post_date' => '1980-09-09 06:30:00',
)
);
setup_postdata( $p );
$this->assertSame( '09', $GLOBALS['currentmonth'] );
}
public function test_secondary_query_post_vars() {
$users = self::factory()->user->create_many( 2 );
$post1 = self::factory()->post->create_and_get(
array(
'post_author' => $users[0],
'post_date' => '2012-02-02 02:00:00',
)
);
$post2 = self::factory()->post->create_and_get(
array(
'post_author' => $users[1],
'post_date' => '2013-03-03 03:00:00',
)
);
$this->go_to( get_permalink( $post1 ) );
setup_postdata( $post1 );
// Main loop.
$this->assertSame( $post1->ID, $GLOBALS['id'] );
$this->assertEquals( get_userdata( $users[0] ), $GLOBALS['authordata'] );
$this->assertSame( '02.02.12', $GLOBALS['currentday'] );
$this->assertSame( '02', $GLOBALS['currentmonth'] );
// Secondary loop.
$q = new WP_Query(
array(
'posts_per_page' => 1,
)
);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
// Should refer to the current loop.
$this->assertSame( $post2->ID, $GLOBALS['id'] );
$this->assertEquals( get_userdata( $users[1] ), $GLOBALS['authordata'] );
$this->assertSame( '03.03.13', $GLOBALS['currentday'] );
$this->assertSame( '03', $GLOBALS['currentmonth'] );
}
}
wp_reset_postdata();
// Should be reset to main loop.
$this->assertSame( $post1->ID, $GLOBALS['id'] );
$this->assertEquals( get_userdata( $users[0] ), $GLOBALS['authordata'] );
$this->assertSame( '02.02.12', $GLOBALS['currentday'] );
$this->assertSame( '02', $GLOBALS['currentmonth'] );
}
public function test_single_page() {
$post = self::factory()->post->create_and_get(
array(
'post_content' => 'Page 0',
)
);
setup_postdata( $post );
$this->assertSame( 0, $GLOBALS['multipage'] );
$this->assertSame( 1, $GLOBALS['numpages'] );
$this->assertEquals( array( 'Page 0' ), $GLOBALS['pages'] );
}
public function test_multi_page() {
$post = self::factory()->post->create_and_get(
array(
'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3',
)
);
setup_postdata( $post );
$this->assertSame( 1, $GLOBALS['multipage'] );
$this->assertSame( 4, $GLOBALS['numpages'] );
$this->assertEquals( array( 'Page 0', 'Page 1', 'Page 2', 'Page 3' ), $GLOBALS['pages'] );
}
/**
* @ticket 16746
*/
public function test_nextpage_at_start_of_content() {
$post = self::factory()->post->create_and_get(
array(
'post_content' => '<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3',
)
);
setup_postdata( $post );
$this->assertSame( 1, $GLOBALS['multipage'] );
$this->assertSame( 3, $GLOBALS['numpages'] );
$this->assertEquals( array( 'Page 1', 'Page 2', 'Page 3' ), $GLOBALS['pages'] );
}
public function test_trim_nextpage_linebreaks() {
$post = self::factory()->post->create_and_get(
array(
'post_content' => "Page 0\n<!--nextpage-->\nPage 1\nhas a line break\n<!--nextpage-->Page 2<!--nextpage-->\n\nPage 3",
)
);
setup_postdata( $post );
$this->assertEquals( array( 'Page 0', "Page 1\nhas a line break", 'Page 2', "\nPage 3" ), $GLOBALS['pages'] );
}
/**
* @ticket 25349
*/
public function test_secondary_query_nextpage() {
$post1 = self::factory()->post->create(
array(
'post_content' => 'Post 1 Page 1<!--nextpage-->Post 1 Page 2',
)
);
$post2 = self::factory()->post->create(
array(
'post_content' => 'Post 2 Page 1<!--nextpage-->Post 2 Page 2',
)
);
$this->go_to( '/?p=' . $post1 );
setup_postdata( get_post( $post1 ) );
// Main loop.
$this->assertSame( array( 'Post 1 Page 1', 'Post 1 Page 2' ), $GLOBALS['pages'] );
// Secondary loop.
$q = new WP_Query(
array(
'post__in' => array( $post2 ),
)
);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
// Should refer to the current loop.
$this->assertSame( array( 'Post 2 Page 1', 'Post 2 Page 2' ), $GLOBALS['pages'] );
}
}
wp_reset_postdata();
// Should be reset to main loop.
$this->assertSame( array( 'Post 1 Page 1', 'Post 1 Page 2' ), $GLOBALS['pages'] );
}
public function test_page_from_wp_query() {
$page = self::factory()->post->create_and_get(
array(
'post_type' => 'page',
)
);
$this->go_to( '/?page=78' );
$GLOBALS['wp_query']->query_vars['page'] = 78;
setup_postdata( $page );
$this->assertSame( 78, $GLOBALS['page'] );
}
public function test_page_when_on_page() {
$page = self::factory()->post->create_and_get(
array(
'post_type' => 'page',
)
);
$this->go_to( get_permalink( $page ) );
setup_postdata( $page );
$this->assertSame( 1, $GLOBALS['page'] );
}
/**
* @ticket 20904
*/
public function test_secondary_query_page() {
$post = self::factory()->post->create_and_get();
$this->go_to( '/?page=3' );
setup_postdata( $post );
// Main loop.
$this->assertSame( 3, $GLOBALS['page'] );
// Secondary loop.
$posts = self::factory()->post->create_many( 5 );
$q = new WP_Query(
array(
'page' => 4,
'posts_per_page' => 1,
)
);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
// $page should refer to the current loop.
$this->assertSame( 4, $GLOBALS['page'] );
}
}
wp_reset_postdata();
// $page should be reset to main loop.
$this->assertSame( 3, $GLOBALS['page'] );
}
/**
* @ticket 20904
*/
public function test_more_when_on_setup_post() {
$post = self::factory()->post->create_and_get();
$this->go_to( get_permalink( $post ) );
setup_postdata( $post );
$this->assertSame( 1, $GLOBALS['more'] );
}
/**
* @ticket 20904
*
* $more should not be true when the set-up post is not the same as the current post.
*/
public function test_more_when_on_single() {
$post1 = self::factory()->post->create_and_get();
$post2 = self::factory()->post->create_and_get();
$this->go_to( get_permalink( $post1 ) );
setup_postdata( $post2 );
$this->assertTrue( empty( $GLOBALS['more'] ) );
}
/**
* @ticket 20904
*
* $more should not be true when the set-up post is not the same as the current page.
*/
public function test_more_when_on_page() {
$post = self::factory()->post->create_and_get();
$page = self::factory()->post->create_and_get(
array(
'post_type' => 'page',
)
);
$this->go_to( get_permalink( $page ) );
setup_postdata( $post );
$this->assertTrue( empty( $GLOBALS['more'] ) );
}
/**
* @ticket 20904
*/
public function test_more_when_on_feed() {
$post = self::factory()->post->create_and_get();
$this->go_to( '/?feed=rss' );
setup_postdata( $post );
$this->assertSame( 1, $GLOBALS['more'] );
}
/**
* @ticket 20904
* @ticket 25349
*/
public function test_secondary_query_more() {
$post = self::factory()->post->create_and_get();
$this->go_to( get_permalink( $post ) );
setup_postdata( $post );
// Main loop.
$this->assertSame( 1, $GLOBALS['more'] );
// Secondary loop.
$q = new WP_Query(
array(
'posts_per_page' => 1,
)
);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
// $more should refer to the current loop.
$this->assertTrue( empty( $GLOBALS['more'] ) );
}
}
wp_reset_postdata();
// $page should be reset to main loop.
$this->assertSame( 1, $GLOBALS['more'] );
}
/**
* @ticket 24330
*
* setup_postdata( $a_post ) followed by the_content() in a loop that does not update
* global $post should use the content of $a_post rather then the global post.
*/
function test_setup_postdata_loop() {
$post_id = self::factory()->post->create( array( 'post_content' => 'global post' ) );
$GLOBALS['wp_query']->post = $GLOBALS['post'] = get_post( $post_id );
$ids = self::factory()->post->create_many( 5 );
foreach ( $ids as $id ) {
$page = get_post( $id );
if ( $page ) {
setup_postdata( $page );
$content = get_echo( 'the_content', array() );
$this->assertEquals( $post_id, $GLOBALS['post']->ID );
$this->assertNotEquals( '<p>global post</p>', strip_ws( $content ) );
wp_reset_postdata();
}
}
}
/**
* @ticket 47114
*
* setup_postdata() should set the globals before `the_post` action is fired.
*/
public function test_the_post_action() {
$post = self::factory()->post->create_and_get();
add_action( 'the_post', array( $this, 'the_post_action_callback' ) );
setup_postdata( $post );
$this->assertEquals( $GLOBALS['pages'], $this->pages_global );
}
/**
* Helpers
*/
public function the_post_action_callback() {
$this->pages_global = $GLOBALS['pages'];
}
}