forked from WordPress/wordpress-develop
/
post.php
1485 lines (1232 loc) · 44.9 KB
/
post.php
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* test wp-includes/post.php
*
* @group post
*/
class Tests_Post extends WP_UnitTestCase {
protected static $editor_id;
protected static $grammarian_id;
public static function wpSetUpBeforeClass( $factory ) {
self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
add_role(
'grammarian',
'Grammarian',
array(
'read' => true,
'edit_posts' => true,
'edit_others_posts' => true,
'edit_published_posts' => true,
)
);
self::$grammarian_id = $factory->user->create( array( 'role' => 'grammarian' ) );
}
public static function wpTearDownAfterClass() {
remove_role( 'grammarian' );
}
function setUp(): void {
parent::setUp();
wp_set_current_user( self::$editor_id );
_set_cron_array( array() );
$this->post_ids = array();
}
/**
* Helper function: return the timestamp(s) of cron jobs for the specified hook and post.
*/
function _next_schedule_for_post( $hook, $id ) {
return wp_next_scheduled( 'publish_future_post', array( 0 => intval( $id ) ) );
}
/**
* Helper function, unsets current user globally.
*/
function _unset_current_user() {
global $current_user, $user_ID;
$current_user = null;
$user_ID = null;
}
/**
* Test simple valid behavior: insert and get a post.
*/
function test_vb_insert_get_delete() {
register_post_type( 'cpt', array( 'taxonomies' => array( 'post_tag', 'ctax' ) ) );
register_taxonomy( 'ctax', 'cpt' );
$post_types = array( 'post', 'cpt' );
foreach ( $post_types as $post_type ) {
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'tax_input' => array(
'post_tag' => 'tag1,tag2',
'ctax' => 'cterm1,cterm2',
),
'post_type' => $post_type,
);
// Insert a post and make sure the ID is OK.
$id = wp_insert_post( $post );
$this->assertTrue( is_numeric( $id ) );
$this->assertTrue( $id > 0 );
// Fetch the post and make sure it matches.
$out = get_post( $id );
$this->assertEquals( $post['post_content'], $out->post_content );
$this->assertEquals( $post['post_title'], $out->post_title );
$this->assertEquals( $post['post_status'], $out->post_status );
$this->assertEquals( $post['post_author'], $out->post_author );
// Test cache state.
$pcache = wp_cache_get( $id, 'posts' );
$this->assertInstanceOf( 'stdClass', $pcache );
$this->assertEquals( $id, $pcache->ID );
update_object_term_cache( $id, $post_type );
$tcache = wp_cache_get( $id, 'post_tag_relationships' );
$this->assertIsArray( $tcache );
$this->assertEquals( 2, count( $tcache ) );
$tcache = wp_cache_get( $id, 'ctax_relationships' );
if ( 'cpt' === $post_type ) {
$this->assertIsArray( $tcache );
$this->assertEquals( 2, count( $tcache ) );
} else {
$this->assertFalse( $tcache );
}
wp_delete_post( $id, true );
$this->assertFalse( wp_cache_get( $id, 'posts' ) );
$this->assertFalse( wp_cache_get( $id, 'post_tag_relationships' ) );
$this->assertFalse( wp_cache_get( $id, 'ctax_relationships' ) );
}
$GLOBALS['wp_taxonomies']['post_tag']->object_type = array( 'post' );
}
/**
* Insert a post with a future date, and make sure the status and cron schedule are correct.
*/
function test_vb_insert_future() {
$future_date = strtotime( '+1 day' );
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date ),
);
// Insert a post and make sure the ID is OK.
$id = wp_insert_post( $post );
$this->post_ids[] = $id;
// dmp( _get_cron_array() );
$this->assertTrue( is_numeric( $id ) );
$this->assertTrue( $id > 0 );
// Fetch the post and make sure it matches.
$out = get_post( $id );
$this->assertEquals( $post['post_content'], $out->post_content );
$this->assertEquals( $post['post_title'], $out->post_title );
$this->assertEquals( 'future', $out->post_status );
$this->assertEquals( $post['post_author'], $out->post_author );
$this->assertEquals( $post['post_date'], $out->post_date );
// There should be a publish_future_post hook scheduled on the future date.
$this->assertEquals( $future_date, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
}
/**
* Insert a post with a future date, and make sure the status and cron schedule are correct.
*/
function test_vb_insert_future_over_dst() {
// Some magic days - one DST one not.
$future_date_1 = strtotime( 'June 21st +1 year' );
$future_date_2 = strtotime( 'Jan 11th +1 year' );
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
);
// Insert a post and make sure the ID is OK.
$id = wp_insert_post( $post );
$this->post_ids[] = $id;
// Fetch the post and make sure has the correct date and status.
$out = get_post( $id );
$this->assertEquals( 'future', $out->post_status );
$this->assertEquals( $post['post_date'], $out->post_date );
// Check that there's a publish_future_post job scheduled at the right time.
$this->assertEquals( $future_date_1, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
// Now save it again with a date further in the future.
$post['ID'] = $id;
$post['post_date'] = strftime( '%Y-%m-%d %H:%M:%S', $future_date_2 );
$post['post_date_gmt'] = null;
wp_update_post( $post );
// Fetch the post again and make sure it has the new post_date.
$out = get_post( $id );
$this->assertEquals( 'future', $out->post_status );
$this->assertEquals( $post['post_date'], $out->post_date );
// And the correct date on the cron job.
$this->assertEquals( $future_date_2, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
}
/**
* Future post bug: posts get published at the wrong time if you edit the timestamp.
*
* @ticket 4710
*/
function test_vb_insert_future_edit_bug() {
$future_date_1 = strtotime( '+1 day' );
$future_date_2 = strtotime( '+2 day' );
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
);
// Insert a post and make sure the ID is OK.
$id = wp_insert_post( $post );
$this->post_ids[] = $id;
// Fetch the post and make sure has the correct date and status.
$out = get_post( $id );
$this->assertEquals( 'future', $out->post_status );
$this->assertEquals( $post['post_date'], $out->post_date );
// Check that there's a publish_future_post job scheduled at the right time.
$this->assertEquals( $future_date_1, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
// Now save it again with a date further in the future.
$post['ID'] = $id;
$post['post_date'] = strftime( '%Y-%m-%d %H:%M:%S', $future_date_2 );
$post['post_date_gmt'] = null;
wp_update_post( $post );
// Fetch the post again and make sure it has the new post_date.
$out = get_post( $id );
$this->assertEquals( 'future', $out->post_status );
$this->assertEquals( $post['post_date'], $out->post_date );
// And the correct date on the cron job.
$this->assertEquals( $future_date_2, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
}
/**
* Insert a draft post with a future date, and make sure no cron schedule is set.
*/
function test_vb_insert_future_draft() {
$future_date = strtotime( '+1 day' );
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'draft',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date ),
);
// Insert a post and make sure the ID is OK.
$id = wp_insert_post( $post );
$this->post_ids[] = $id;
// dmp( _get_cron_array() );
$this->assertTrue( is_numeric( $id ) );
$this->assertTrue( $id > 0 );
// Fetch the post and make sure it matches.
$out = get_post( $id );
$this->assertEquals( $post['post_content'], $out->post_content );
$this->assertEquals( $post['post_title'], $out->post_title );
$this->assertEquals( 'draft', $out->post_status );
$this->assertEquals( $post['post_author'], $out->post_author );
$this->assertEquals( $post['post_date'], $out->post_date );
// There should be a publish_future_post hook scheduled on the future date.
$this->assertEquals( false, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
}
/**
* Insert a future post, then edit and change it to draft, and make sure cron gets it right.
*/
function test_vb_insert_future_change_to_draft() {
$future_date_1 = strtotime( '+1 day' );
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
);
// Insert a post and make sure the ID is OK.
$id = wp_insert_post( $post );
$this->post_ids[] = $id;
// Fetch the post and make sure has the correct date and status.
$out = get_post( $id );
$this->assertEquals( 'future', $out->post_status );
$this->assertEquals( $post['post_date'], $out->post_date );
// Check that there's a publish_future_post job scheduled at the right time.
$this->assertEquals( $future_date_1, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
// Now save it again with status set to draft.
$post['ID'] = $id;
$post['post_status'] = 'draft';
wp_update_post( $post );
// Fetch the post again and make sure it has the new post_date.
$out = get_post( $id );
$this->assertEquals( 'draft', $out->post_status );
$this->assertEquals( $post['post_date'], $out->post_date );
// And the correct date on the cron job.
$this->assertEquals( false, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
}
/**
* Insert a future post, then edit and change the status, and make sure cron gets it right.
*/
function test_vb_insert_future_change_status() {
$future_date_1 = strtotime( '+1 day' );
$statuses = array( 'draft', 'static', 'object', 'attachment', 'inherit', 'pending' );
foreach ( $statuses as $status ) {
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
);
// Insert a post and make sure the ID is OK.
$id = wp_insert_post( $post );
$this->post_ids[] = $id;
// Fetch the post and make sure has the correct date and status.
$out = get_post( $id );
$this->assertEquals( 'future', $out->post_status );
$this->assertEquals( $post['post_date'], $out->post_date );
// Check that there's a publish_future_post job scheduled at the right time.
$this->assertEquals( $future_date_1, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
// Now save it again with status changed.
$post['ID'] = $id;
$post['post_status'] = $status;
wp_update_post( $post );
// Fetch the post again and make sure it has the new post_date.
$out = get_post( $id );
$this->assertEquals( $status, $out->post_status );
$this->assertEquals( $post['post_date'], $out->post_date );
// And the correct date on the cron job.
$this->assertEquals( false, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
}
}
/**
* Insert a draft post with a future date, and make sure no cron schedule is set.
*/
function test_vb_insert_future_private() {
$future_date = strtotime( '+1 day' );
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'private',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date ),
);
// Insert a post and make sure the ID is OK.
$id = wp_insert_post( $post );
$this->post_ids[] = $id;
// dmp( _get_cron_array() );
$this->assertTrue( is_numeric( $id ) );
$this->assertTrue( $id > 0 );
// Fetch the post and make sure it matches.
$out = get_post( $id );
$this->assertEquals( $post['post_content'], $out->post_content );
$this->assertEquals( $post['post_title'], $out->post_title );
$this->assertEquals( 'private', $out->post_status );
$this->assertEquals( $post['post_author'], $out->post_author );
$this->assertEquals( $post['post_date'], $out->post_date );
// There should be a publish_future_post hook scheduled on the future date.
$this->assertEquals( false, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
}
/**
* Insert a post with an invalid date, make sure it fails.
*
* @ticket 17180
*/
function test_vb_insert_invalid_date() {
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'public',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_date' => '2012-02-30 00:00:00',
);
// Test both return paths with or without WP_Error.
$insert_post = wp_insert_post( $post, true );
$this->assertWPError( $insert_post );
$this->assertEquals( 'invalid_date', $insert_post->get_error_code() );
$insert_post = wp_insert_post( $post );
$this->assertEquals( 0, $insert_post );
}
/**
* Insert a future post, then edit and change it to private, and make sure cron gets it right.
*/
function test_vb_insert_future_change_to_private() {
$future_date_1 = strtotime( '+1 day' );
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
);
// Insert a post and make sure the ID is OK.
$id = wp_insert_post( $post );
$this->post_ids[] = $id;
// Fetch the post and make sure has the correct date and status.
$out = get_post( $id );
$this->assertEquals( 'future', $out->post_status );
$this->assertEquals( $post['post_date'], $out->post_date );
// Check that there's a publish_future_post job scheduled at the right time.
$this->assertEquals( $future_date_1, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
// Now save it again with status set to draft.
$post['ID'] = $id;
$post['post_status'] = 'private';
wp_update_post( $post );
// Fetch the post again and make sure it has the new post_date.
$out = get_post( $id );
$this->assertEquals( 'private', $out->post_status );
$this->assertEquals( $post['post_date'], $out->post_date );
// And the correct date on the cron job.
$this->assertEquals( false, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
}
/**
* @ticket 5305
*/
public function test_wp_insert_post_should_not_allow_a_bare_numeric_slug_that_might_conflict_with_a_date_archive_when_generating_from_an_empty_post_title() {
$this->set_permalink_structure( '/%postname%/' );
$p = wp_insert_post(
array(
'post_title' => '',
'post_content' => 'test',
'post_status' => 'publish',
'post_type' => 'post',
)
);
$post = get_post( $p );
$this->set_permalink_structure();
$this->assertEquals( "$p-2", $post->post_name );
}
/**
* @ticket 5305
* @ticket 33392
*/
public function test_wp_insert_post_should_invalidate_post_cache_before_generating_guid_when_post_name_is_empty_and_is_generated_from_the_post_ID() {
register_post_type( 'wptests_pt' );
$p = wp_insert_post(
array(
'post_title' => '',
'post_type' => 'wptests_pt',
'post_status' => 'publish',
)
);
$post = get_post( $p );
$this->assertStringContainsString( 'wptests_pt=' . $p, $post->guid );
}
/**
* @ticket 20451
*/
public function test_wp_insert_post_with_meta_input() {
$post_id = wp_insert_post(
array(
'post_title' => '',
'post_content' => 'test',
'post_status' => 'publish',
'post_type' => 'post',
'meta_input' => array(
'hello' => 'world',
'foo' => 'bar',
),
)
);
$this->assertEquals( 'world', get_post_meta( $post_id, 'hello', true ) );
$this->assertEquals( 'bar', get_post_meta( $post_id, 'foo', true ) );
}
/**
* "When I delete a future post using wp_delete_post( $post->ID ) it does not update the cron correctly."
*
* @ticket 5364
*/
function test_delete_future_post_cron() {
$future_date = strtotime( '+1 day' );
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', $future_date ),
);
// Insert a post and make sure the ID is OK.
$id = wp_insert_post( $post );
$this->post_ids[] = $id;
// Check that there's a publish_future_post job scheduled at the right time.
$this->assertEquals( $future_date, $this->_next_schedule_for_post( 'publish_future_post', $id ) );
// Now delete the post and make sure the cron entry is removed.
wp_delete_post( $id );
$this->assertFalse( $this->_next_schedule_for_post( 'publish_future_post', $id ) );
}
/**
* Bug: permalink doesn't work if post title is empty.
*
* Might only fail if the post ID is greater than four characters.
*
* @ticket 5305
*/
function test_permalink_without_title() {
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => '',
'post_date' => '2007-10-31 06:15:00',
);
// Insert a post and make sure the ID is OK.
$id = wp_insert_post( $post );
$this->post_ids[] = $id;
$plink = get_permalink( $id );
// Permalink should include the post ID at the end.
$this->assertEquals( get_option( 'siteurl' ) . '/2007/10/31/' . $id . '/', $plink );
}
function test_wp_publish_post() {
$draft_id = self::factory()->post->create( array( 'post_status' => 'draft' ) );
$post = get_post( $draft_id );
$this->assertEquals( 'draft', $post->post_status );
wp_publish_post( $draft_id );
$post = get_post( $draft_id );
$this->assertEquals( 'publish', $post->post_status );
}
/**
* @ticket 22944
*/
function test_wp_insert_post_and_wp_publish_post_with_future_date() {
$future_date = gmdate( 'Y-m-d H:i:s', time() + 10000000 );
$post_id = self::factory()->post->create(
array(
'post_status' => 'publish',
'post_date' => $future_date,
)
);
$post = get_post( $post_id );
$this->assertEquals( 'future', $post->post_status );
$this->assertEquals( $future_date, $post->post_date );
wp_publish_post( $post_id );
$post = get_post( $post_id );
$this->assertEquals( 'publish', $post->post_status );
$this->assertEquals( $future_date, $post->post_date );
}
/**
* @ticket 48145
*/
function test_wp_insert_post_should_default_to_publish_if_post_date_is_within_59_seconds_from_current_time() {
$future_date = gmdate( 'Y-m-d H:i:s', time() + 59 );
$post_id = self::factory()->post->create(
array(
'post_date' => $future_date,
)
);
$post = get_post( $post_id );
$this->assertEquals( 'publish', $post->post_status );
$this->assertEquals( $future_date, $post->post_date );
}
/**
* @ticket 22944
*/
function test_publish_post_with_content_filtering() {
kses_remove_filters();
$post_id = wp_insert_post( array( 'post_title' => '<script>Test</script>' ) );
$post = get_post( $post_id );
$this->assertEquals( '<script>Test</script>', $post->post_title );
$this->assertEquals( 'draft', $post->post_status );
kses_init_filters();
wp_update_post(
array(
'ID' => $post->ID,
'post_status' => 'publish',
)
);
$post = get_post( $post->ID );
$this->assertEquals( 'Test', $post->post_title );
kses_remove_filters();
}
/**
* @ticket 22944
*/
function test_wp_publish_post_and_avoid_content_filtering() {
kses_remove_filters();
$post_id = wp_insert_post( array( 'post_title' => '<script>Test</script>' ) );
$post = get_post( $post_id );
$this->assertEquals( '<script>Test</script>', $post->post_title );
$this->assertEquals( 'draft', $post->post_status );
kses_init_filters();
wp_publish_post( $post->ID );
$post = get_post( $post->ID );
$this->assertEquals( '<script>Test</script>', $post->post_title );
kses_remove_filters();
}
/**
* @ticket 23708
*/
function test_get_post_ancestors_within_loop() {
global $post;
$parent_id = self::factory()->post->create();
$post = self::factory()->post->create_and_get( array( 'post_parent' => $parent_id ) );
$this->assertEquals( array( $parent_id ), get_post_ancestors( 0 ) );
}
/**
* @ticket 23474
*/
function test_update_invalid_post_id() {
$post_id = self::factory()->post->create( array( 'post_name' => 'get-page-uri-post-name' ) );
$post = get_post( $post_id, ARRAY_A );
$post['ID'] = 123456789;
$this->assertEquals( 0, wp_insert_post( $post ) );
$this->assertEquals( 0, wp_update_post( $post ) );
$this->assertInstanceOf( 'WP_Error', wp_insert_post( $post, true ) );
$this->assertInstanceOf( 'WP_Error', wp_update_post( $post, true ) );
}
function test_parse_post_content_single_page() {
global $multipage, $pages, $numpages;
$post_id = self::factory()->post->create( array( 'post_content' => 'Page 0' ) );
$post = get_post( $post_id );
setup_postdata( $post );
$this->assertEquals( 0, $multipage );
$this->assertCount( 1, $pages );
$this->assertEquals( 1, $numpages );
$this->assertEquals( array( 'Page 0' ), $pages );
}
function test_parse_post_content_multi_page() {
global $multipage, $pages, $numpages;
$post_id = self::factory()->post->create( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
$post = get_post( $post_id );
setup_postdata( $post );
$this->assertEquals( 1, $multipage );
$this->assertCount( 4, $pages );
$this->assertEquals( 4, $numpages );
$this->assertEquals( array( 'Page 0', 'Page 1', 'Page 2', 'Page 3' ), $pages );
}
function test_parse_post_content_remaining_single_page() {
global $multipage, $pages, $numpages;
$post_id = self::factory()->post->create( array( 'post_content' => 'Page 0' ) );
$post = get_post( $post_id );
setup_postdata( $post );
$this->assertEquals( 0, $multipage );
$this->assertCount( 1, $pages );
$this->assertEquals( 1, $numpages );
$this->assertEquals( array( 'Page 0' ), $pages );
}
function test_parse_post_content_remaining_multi_page() {
global $multipage, $pages, $numpages;
$post_id = self::factory()->post->create( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
$post = get_post( $post_id );
setup_postdata( $post );
$this->assertEquals( 1, $multipage );
$this->assertCount( 4, $pages );
$this->assertEquals( 4, $numpages );
$this->assertEquals( array( 'Page 0', 'Page 1', 'Page 2', 'Page 3' ), $pages );
}
/**
* @ticket 16746
*/
function test_parse_post_content_starting_with_nextpage() {
global $multipage, $pages, $numpages;
$post_id = self::factory()->post->create( array( 'post_content' => '<!--nextpage-->Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
$post = get_post( $post_id );
setup_postdata( $post );
$this->assertEquals( 1, $multipage );
$this->assertCount( 4, $pages );
$this->assertEquals( 4, $numpages );
$this->assertEquals( array( 'Page 0', 'Page 1', 'Page 2', 'Page 3' ), $pages );
}
/**
* @ticket 16746
*/
function test_parse_post_content_starting_with_nextpage_multi() {
global $multipage, $pages, $numpages;
$post_id = self::factory()->post->create( array( 'post_content' => '<!--nextpage-->Page 0' ) );
$post = get_post( $post_id );
setup_postdata( $post );
$this->assertEquals( 0, $multipage );
$this->assertCount( 1, $pages );
$this->assertEquals( 1, $numpages );
$this->assertEquals( array( 'Page 0' ), $pages );
}
/**
* @ticket 19373
*/
function test_insert_programmatic_sanitized() {
$this->_unset_current_user();
register_taxonomy( 'test_tax', 'post' );
$title = rand_str();
$post_data = array(
'post_author' => self::$editor_id,
'post_status' => 'public',
'post_content' => rand_str(),
'post_title' => $title,
'tax_input' => array(
'test_tax' => array( 'term', 'term2', 'term3' ),
),
);
$insert_post_id = wp_insert_post( $post_data, true, true );
$this->assertTrue( ( is_int( $insert_post_id ) && $insert_post_id > 0 ) );
$post = get_post( $insert_post_id );
$this->assertEquals( $post->post_author, self::$editor_id );
$this->assertEquals( $post->post_title, $title );
}
/**
* @ticket 24803
*/
function test_wp_count_posts() {
$post_type = rand_str( 20 );
register_post_type( $post_type );
self::factory()->post->create(
array(
'post_type' => $post_type,
'post_author' => self::$editor_id,
)
);
$count = wp_count_posts( $post_type, 'readable' );
$this->assertEquals( 1, $count->publish );
_unregister_post_type( $post_type );
$this->assertEquals( new stdClass, wp_count_posts( $post_type, 'readable' ) );
}
function test_wp_count_posts_filtered() {
$post_type = rand_str( 20 );
register_post_type( $post_type );
self::factory()->post->create_many(
3,
array(
'post_type' => $post_type,
'post_author' => self::$editor_id,
)
);
$count1 = wp_count_posts( $post_type, 'readable' );
$this->assertEquals( 3, $count1->publish );
add_filter( 'wp_count_posts', array( $this, 'filter_wp_count_posts' ) );
$count2 = wp_count_posts( $post_type, 'readable' );
$this->assertEquals( 2, $count2->publish );
remove_filter( 'wp_count_posts', array( $this, 'filter_wp_count_posts' ) );
}
function filter_wp_count_posts( $counts ) {
$counts->publish = 2;
return $counts;
}
function test_wp_count_posts_insert_invalidation() {
$post_ids = self::factory()->post->create_many( 3 );
$initial_counts = wp_count_posts();
$key = array_rand( $post_ids );
$_post = get_post( $post_ids[ $key ], ARRAY_A );
$_post['post_status'] = 'draft';
wp_insert_post( $_post );
$post = get_post( $post_ids[ $key ] );
$this->assertEquals( 'draft', $post->post_status );
$this->assertNotEquals( 'publish', $post->post_status );
$after_draft_counts = wp_count_posts();
$this->assertEquals( 1, $after_draft_counts->draft );
$this->assertEquals( 2, $after_draft_counts->publish );
$this->assertNotEquals( $initial_counts->publish, $after_draft_counts->publish );
}
function test_wp_count_posts_trash_invalidation() {
$post_ids = self::factory()->post->create_many( 3 );
$initial_counts = wp_count_posts();
$key = array_rand( $post_ids );
wp_trash_post( $post_ids[ $key ] );
$post = get_post( $post_ids[ $key ] );
$this->assertEquals( 'trash', $post->post_status );
$this->assertNotEquals( 'publish', $post->post_status );
$after_trash_counts = wp_count_posts();
$this->assertEquals( 1, $after_trash_counts->trash );
$this->assertEquals( 2, $after_trash_counts->publish );
$this->assertNotEquals( $initial_counts->publish, $after_trash_counts->publish );
}
/**
* @ticket 49685
*/
function test_wp_count_posts_status_changes_visible() {
self::factory()->post->create_many( 3 );
// Trigger a cache.
wp_count_posts();
register_post_status( 'test' );
$counts = wp_count_posts();
$this->assertTrue( isset( $counts->test ) );
$this->assertEquals( 0, $counts->test );
}
/**
* @ticket 13771
*/
function test_get_the_date_with_id_returns_correct_time() {
$post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
$this->assertEquals( 'March 1, 2014', get_the_date( 'F j, Y', $post_id ) );
}
/**
* @ticket 28310
*/
function test_get_the_date_returns_false_with_null_or_non_existing_post() {
$this->assertFalse( get_the_date() );
$this->assertFalse( get_the_date( 'F j, Y h:i:s' ) );
$this->assertFalse( get_the_date( '', 9 ) );
$this->assertFalse( get_the_date( 'F j, Y h:i:s', 9 ) );
}
/**
* @ticket 28310
*/
function test_get_the_time_with_id_returns_correct_time() {
$post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
$this->assertEquals( '16:35:00', get_the_time( 'H:i:s', $post_id ) );
}
/**
* @ticket 28310
*/
function test_get_the_time_returns_false_with_null_or_non_existing_post() {
$this->assertFalse( get_the_time() );
$this->assertFalse( get_the_time( 'h:i:s' ) );
$this->assertFalse( get_the_time( '', 9 ) );
$this->assertFalse( get_the_time( 'h:i:s', 9 ) );
}
/**
* @ticket 28310
*/
function test_get_post_time_with_id_returns_correct_time() {
$post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
$this->assertEquals( '16:35:00', get_post_time( 'H:i:s', false, $post_id ) );
}
/**
* @ticket 28310
*/
function test_get_post_time_returns_false_with_null_or_non_existing_post() {
$this->assertFalse( get_post_time() );
$this->assertFalse( get_post_time( 'h:i:s' ) );
$this->assertFalse( get_post_time( '', false, 9 ) );
$this->assertFalse( get_post_time( 'h:i:s', false, 9 ) );
}
/**
* @ticket 28310
*/
function test_get_post_modified_time_with_id_returns_correct_time() {
$post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
$this->assertEquals( '16:35:00', get_post_modified_time( 'H:i:s', false, $post_id ) );
}
/**
* @ticket 28310
*/
function test_get_post_modified_time_returns_false_with_null_or_non_existing_post() {
$this->assertFalse( get_post_modified_time() );
$this->assertFalse( get_post_modified_time( 'h:i:s' ) );
$this->assertFalse( get_post_modified_time( '', false, 9 ) );
$this->assertFalse( get_post_modified_time( 'h:i:s', false, 9 ) );
}
/**
* @ticket 28310
*/
function test_mysql2date_returns_false_with_no_date() {
$this->assertFalse( mysql2date( 'F j, Y H:i:s', '' ) );
}
/**
* @ticket 28310
*/
function test_mysql2date_returns_gmt_or_unix_timestamp() {
$this->assertEquals( '441013392', mysql2date( 'G', '1983-12-23 07:43:12' ) );
$this->assertEquals( '441013392', mysql2date( 'U', '1983-12-23 07:43:12' ) );
}
/**
* @ticket 25566
*/
function test_wp_tag_cloud_link_with_post_type() {
$post_type = 'new_post_type';
$tax = 'new_tag';
register_post_type( $post_type, array( 'taxonomies' => array( 'post_tag', $tax ) ) );
register_taxonomy( $tax, $post_type );
$post = self::factory()->post->create( array( 'post_type' => $post_type ) );
wp_set_object_terms( $post, rand_str(), $tax );
$wp_tag_cloud = wp_tag_cloud(
array(
'post_type' => $post_type,
'taxonomy' => $tax,