Changeset 53785 for trunk/tests/phpunit/tests/post/wpInsertPost.php
- Timestamp:
- 07/27/2022 02:59:12 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/post/wpInsertPost.php
r52010 r53785 3 3 /** 4 4 * @group post 5 * @covers ::wp_insert_post 5 6 */ 6 7 class Tests_Post_wpInsertPost extends WP_UnitTestCase { … … 8 9 protected static $user_ids = array( 9 10 'administrator' => null, 11 'editor' => null, 10 12 'contributor' => null, 11 13 ); … … 18 20 ) 19 21 ), 22 'editor' => $factory->user->create( 23 array( 24 'role' => 'editor', 25 ) 26 ), 20 27 'contributor' => $factory->user->create( 21 28 array( … … 67 74 68 75 /** 76 * Helper function: return the timestamp(s) of cron jobs for the specified hook and post. 77 */ 78 private function next_schedule_for_post( $hook, $post_id ) { 79 return wp_next_scheduled( 'publish_future_post', array( 0 => (int) $post_id ) ); 80 } 81 82 /** 83 * Helper function, unsets current user globally. 84 */ 85 private function unset_current_user() { 86 global $current_user, $user_ID; 87 88 $current_user = null; 89 $user_ID = null; 90 } 91 92 /** 93 * Test simple valid behavior: insert and get a post. 94 * 95 * @dataProvider data_vb_insert_get_delete 96 */ 97 public function test_vb_insert_get_delete( $post_type ) { 98 register_post_type( 99 'cpt', 100 array( 101 'taxonomies' => array( 'post_tag', 'ctax' ), 102 ) 103 ); 104 register_taxonomy( 'ctax', 'cpt' ); 105 106 wp_set_current_user( self::$user_ids['editor'] ); 107 108 $data = array( 109 'post_author' => self::$user_ids['editor'], 110 'post_status' => 'publish', 111 'post_content' => "{$post_type}_content", 112 'post_title' => "{$post_type}_title", 113 'tax_input' => array( 114 'post_tag' => 'tag1,tag2', 115 'ctax' => 'cterm1,cterm2', 116 ), 117 'post_type' => $post_type, 118 ); 119 120 // Insert a post and make sure the ID is OK. 121 $post_id = wp_insert_post( $data ); 122 $this->assertIsInt( $post_id ); 123 $this->assertGreaterThan( 0, $post_id ); 124 125 // Fetch the post and make sure it matches. 126 $post = get_post( $post_id ); 127 128 $this->assertSame( $data['post_content'], $post->post_content ); 129 $this->assertSame( $data['post_title'], $post->post_title ); 130 $this->assertSame( $data['post_status'], $post->post_status ); 131 $this->assertEquals( $data['post_author'], $post->post_author ); 132 133 // Test cache state. 134 $post_cache = wp_cache_get( $post_id, 'posts' ); 135 $this->assertInstanceOf( 'stdClass', $post_cache ); 136 $this->assertSame( $post_id, $post_cache->ID ); 137 138 update_object_term_cache( $post_id, $post_type ); 139 $term_cache = wp_cache_get( $post_id, 'post_tag_relationships' ); 140 $this->assertIsArray( $term_cache ); 141 $this->assertCount( 2, $term_cache ); 142 143 $term_cache = wp_cache_get( $post_id, 'ctax_relationships' ); 144 if ( 'cpt' === $post_type ) { 145 $this->assertIsArray( $term_cache ); 146 $this->assertCount( 2, $term_cache ); 147 } else { 148 $this->assertFalse( $term_cache ); 149 } 150 151 wp_delete_post( $post_id, true ); 152 153 $this->assertFalse( wp_cache_get( $post_id, 'posts' ) ); 154 $this->assertFalse( wp_cache_get( $post_id, 'post_tag_relationships' ) ); 155 $this->assertFalse( wp_cache_get( $post_id, 'ctax_relationships' ) ); 156 157 $GLOBALS['wp_taxonomies']['post_tag']->object_type = array( 'post' ); 158 } 159 160 public function data_vb_insert_get_delete() { 161 $post_types = array( 'post', 'cpt' ); 162 163 return $this->text_array_to_dataprovider( $post_types ); 164 } 165 166 /** 167 * Insert a post with a future date, and make sure the status and cron schedule are correct. 168 */ 169 public function test_vb_insert_future() { 170 $future_date = strtotime( '+1 day' ); 171 172 $data = array( 173 'post_author' => self::$user_ids['editor'], 174 'post_status' => 'publish', 175 'post_content' => 'content', 176 'post_title' => 'title', 177 'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ), 178 ); 179 180 // Insert a post and make sure the ID is OK. 181 $post_id = wp_insert_post( $data ); 182 $this->assertIsInt( $post_id ); 183 $this->assertGreaterThan( 0, $post_id ); 184 185 // Fetch the post and make sure it matches. 186 $post = get_post( $post_id ); 187 188 $this->assertSame( $data['post_content'], $post->post_content ); 189 $this->assertSame( $data['post_title'], $post->post_title ); 190 $this->assertSame( 'future', $post->post_status ); 191 $this->assertEquals( $data['post_author'], $post->post_author ); 192 $this->assertSame( $data['post_date'], $post->post_date ); 193 194 // There should be a publish_future_post hook scheduled on the future date. 195 $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 196 } 197 198 /** 199 * Insert a post with a future date, and make sure the status and cron schedule are correct. 200 */ 201 public function test_vb_insert_future_over_dst() { 202 // Some magic days - one DST one not. 203 $future_date_1 = strtotime( 'June 21st +1 year' ); 204 $future_date_2 = strtotime( 'Jan 11th +1 year' ); 205 206 $data = array( 207 'post_author' => self::$user_ids['editor'], 208 'post_status' => 'publish', 209 'post_content' => 'content', 210 'post_title' => 'title', 211 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), 212 ); 213 214 // Insert a post and make sure the ID is OK. 215 $post_id = wp_insert_post( $data ); 216 217 // Fetch the post and make sure has the correct date and status. 218 $post = get_post( $post_id ); 219 $this->assertSame( 'future', $post->post_status ); 220 $this->assertSame( $data['post_date'], $post->post_date ); 221 222 // Check that there's a publish_future_post job scheduled at the right time. 223 $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 224 225 // Now save it again with a date further in the future. 226 $data['ID'] = $post_id; 227 $data['post_date'] = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' ); 228 $data['post_date_gmt'] = null; 229 wp_update_post( $data ); 230 231 // Fetch the post again and make sure it has the new post_date. 232 $post = get_post( $post_id ); 233 $this->assertSame( 'future', $post->post_status ); 234 $this->assertSame( $data['post_date'], $post->post_date ); 235 236 // And the correct date on the cron job. 237 $this->assertSame( $future_date_2, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 238 } 239 240 /** 241 * Future post bug: posts get published at the wrong time if you edit the timestamp. 242 * 243 * @ticket 4710 244 */ 245 public function test_vb_insert_future_edit_bug() { 246 $future_date_1 = strtotime( '+1 day' ); 247 $future_date_2 = strtotime( '+2 day' ); 248 249 $data = array( 250 'post_author' => self::$user_ids['editor'], 251 'post_status' => 'publish', 252 'post_content' => 'content', 253 'post_title' => 'title', 254 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), 255 ); 256 257 // Insert a post and make sure the ID is OK. 258 $post_id = wp_insert_post( $data ); 259 260 // Fetch the post and make sure has the correct date and status. 261 $post = get_post( $post_id ); 262 $this->assertSame( 'future', $post->post_status ); 263 $this->assertSame( $data['post_date'], $post->post_date ); 264 265 // Check that there's a publish_future_post job scheduled at the right time. 266 $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 267 268 // Now save it again with a date further in the future. 269 $data['ID'] = $post_id; 270 $data['post_date'] = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' ); 271 $data['post_date_gmt'] = null; 272 wp_update_post( $data ); 273 274 // Fetch the post again and make sure it has the new post_date. 275 $post = get_post( $post_id ); 276 $this->assertSame( 'future', $post->post_status ); 277 $this->assertSame( $data['post_date'], $post->post_date ); 278 279 // And the correct date on the cron job. 280 $this->assertSame( $future_date_2, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 281 } 282 283 /** 284 * Insert a draft post with a future date, and make sure no cron schedule is set. 285 */ 286 public function test_vb_insert_future_draft() { 287 $future_date = strtotime( '+1 day' ); 288 289 $data = array( 290 'post_author' => self::$user_ids['editor'], 291 'post_status' => 'draft', 292 'post_content' => 'content', 293 'post_title' => 'title', 294 'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ), 295 ); 296 297 // Insert a post and make sure the ID is OK. 298 $post_id = wp_insert_post( $data ); 299 $this->assertIsInt( $post_id ); 300 $this->assertGreaterThan( 0, $post_id ); 301 302 // Fetch the post and make sure it matches. 303 $post = get_post( $post_id ); 304 305 $this->assertSame( $data['post_content'], $post->post_content ); 306 $this->assertSame( $data['post_title'], $post->post_title ); 307 $this->assertSame( 'draft', $post->post_status ); 308 $this->assertEquals( $data['post_author'], $post->post_author ); 309 $this->assertSame( $data['post_date'], $post->post_date ); 310 311 // There should be a publish_future_post hook scheduled on the future date. 312 $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 313 314 } 315 316 /** 317 * Insert a future post, then edit and change it to draft, and make sure cron gets it right. 318 */ 319 public function test_vb_insert_future_change_to_draft() { 320 $future_date_1 = strtotime( '+1 day' ); 321 322 $data = array( 323 'post_author' => self::$user_ids['editor'], 324 'post_status' => 'publish', 325 'post_content' => 'content', 326 'post_title' => 'title', 327 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), 328 ); 329 330 // Insert a post and make sure the ID is OK. 331 $post_id = wp_insert_post( $data ); 332 333 // Fetch the post and make sure has the correct date and status. 334 $post = get_post( $post_id ); 335 $this->assertSame( 'future', $post->post_status ); 336 $this->assertSame( $data['post_date'], $post->post_date ); 337 338 // Check that there's a publish_future_post job scheduled at the right time. 339 $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 340 341 // Now save it again with status set to draft. 342 $data['ID'] = $post_id; 343 $data['post_status'] = 'draft'; 344 wp_update_post( $data ); 345 346 // Fetch the post again and make sure it has the new post_date. 347 $post = get_post( $post_id ); 348 $this->assertSame( 'draft', $post->post_status ); 349 $this->assertSame( $data['post_date'], $post->post_date ); 350 351 // And the correct date on the cron job. 352 $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 353 } 354 355 /** 356 * Insert a future post, then edit and change the status, and make sure cron gets it right. 357 * 358 * @dataProvider data_vb_insert_future_change_status 359 */ 360 public function test_vb_insert_future_change_status( $status ) { 361 $future_date_1 = strtotime( '+1 day' ); 362 363 $data = array( 364 'post_author' => self::$user_ids['editor'], 365 'post_status' => 'publish', 366 'post_content' => "{$status}_content", 367 'post_title' => "{$status}_title", 368 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), 369 ); 370 371 // Insert a post and make sure the ID is OK. 372 $post_id = wp_insert_post( $data ); 373 374 // Fetch the post and make sure has the correct date and status. 375 $post = get_post( $post_id ); 376 $this->assertSame( 'future', $post->post_status ); 377 $this->assertSame( $data['post_date'], $post->post_date ); 378 379 // Check that there's a publish_future_post job scheduled at the right time. 380 $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 381 382 // Now save it again with status changed. 383 $data['ID'] = $post_id; 384 $data['post_status'] = $status; 385 wp_update_post( $data ); 386 387 // Fetch the post again and make sure it has the new post_date. 388 $post = get_post( $post_id ); 389 $this->assertSame( $status, $post->post_status ); 390 $this->assertSame( $data['post_date'], $post->post_date ); 391 392 // And the correct date on the cron job. 393 $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 394 } 395 396 public function data_vb_insert_future_change_status() { 397 $statuses = array( 398 'draft', 399 'static', 400 'object', 401 'attachment', 402 'inherit', 403 'pending', 404 ); 405 406 return $this->text_array_to_dataprovider( $statuses ); 407 } 408 409 /** 410 * Insert a draft post with a future date, and make sure no cron schedule is set. 411 */ 412 public function test_vb_insert_future_private() { 413 $future_date = strtotime( '+1 day' ); 414 415 $data = array( 416 'post_author' => self::$user_ids['editor'], 417 'post_status' => 'private', 418 'post_content' => 'content', 419 'post_title' => 'title', 420 'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ), 421 ); 422 423 // Insert a post and make sure the ID is OK. 424 $post_id = wp_insert_post( $data ); 425 $this->assertIsInt( $post_id ); 426 $this->assertGreaterThan( 0, $post_id ); 427 428 // Fetch the post and make sure it matches. 429 $post = get_post( $post_id ); 430 431 $this->assertSame( $data['post_content'], $post->post_content ); 432 $this->assertSame( $data['post_title'], $post->post_title ); 433 $this->assertSame( 'private', $post->post_status ); 434 $this->assertEquals( $data['post_author'], $post->post_author ); 435 $this->assertSame( $data['post_date'], $post->post_date ); 436 437 // There should be a publish_future_post hook scheduled on the future date. 438 $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 439 } 440 441 /** 442 * Insert a post with an invalid date, make sure it fails. 443 * 444 * @ticket 17180 445 */ 446 public function test_vb_insert_invalid_date() { 447 $data = array( 448 'post_author' => self::$user_ids['editor'], 449 'post_status' => 'publish', 450 'post_content' => 'content', 451 'post_title' => 'title', 452 'post_date' => '2012-02-30 00:00:00', 453 ); 454 455 // Test both return paths with or without WP_Error. 456 $post_id = wp_insert_post( $data, true ); 457 $this->assertWPError( $post_id ); 458 $this->assertSame( 'invalid_date', $post_id->get_error_code() ); 459 460 $post_id = wp_insert_post( $data ); 461 $this->assertSame( 0, $post_id ); 462 } 463 464 /** 465 * Insert a future post, then edit and change it to private, and make sure cron gets it right. 466 */ 467 public function test_vb_insert_future_change_to_private() { 468 $future_date_1 = strtotime( '+1 day' ); 469 470 $data = array( 471 'post_author' => self::$user_ids['editor'], 472 'post_status' => 'publish', 473 'post_content' => 'content', 474 'post_title' => 'title', 475 'post_date' => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ), 476 ); 477 478 // Insert a post and make sure the ID is OK. 479 $post_id = wp_insert_post( $data ); 480 481 // Fetch the post and make sure has the correct date and status. 482 $post = get_post( $post_id ); 483 $this->assertSame( 'future', $post->post_status ); 484 $this->assertSame( $data['post_date'], $post->post_date ); 485 486 // Check that there's a publish_future_post job scheduled at the right time. 487 $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 488 489 // Now save it again with status set to draft. 490 $data['ID'] = $post_id; 491 $data['post_status'] = 'private'; 492 wp_update_post( $data ); 493 494 // Fetch the post again and make sure it has the new post_date. 495 $post = get_post( $post_id ); 496 $this->assertSame( 'private', $post->post_status ); 497 $this->assertSame( $data['post_date'], $post->post_date ); 498 499 // And the correct date on the cron job. 500 $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 501 } 502 503 /** 504 * @ticket 5305 505 */ 506 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() { 507 $this->set_permalink_structure( '/%postname%/' ); 508 509 $post_id = wp_insert_post( 510 array( 511 'post_title' => '', 512 'post_content' => 'test', 513 'post_status' => 'publish', 514 'post_type' => 'post', 515 ) 516 ); 517 518 $post = get_post( $post_id ); 519 520 $this->assertSame( "$post_id-2", $post->post_name ); 521 } 522 523 /** 524 * @ticket 5305 525 * @ticket 33392 526 */ 527 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() { 528 register_post_type( 'wptests_pt' ); 529 530 $post_id = wp_insert_post( 531 array( 532 'post_title' => '', 533 'post_type' => 'wptests_pt', 534 'post_status' => 'publish', 535 ) 536 ); 537 538 $post = get_post( $post_id ); 539 540 $this->assertStringContainsString( 'wptests_pt=' . $post_id, $post->guid ); 541 } 542 543 /** 544 * @ticket 55877 545 * @covers ::wp_insert_post 546 */ 547 public function test_wp_insert_post_should_not_trigger_warning_for_pending_posts_with_unknown_cpt() { 548 $post_id = wp_insert_post( 549 array( 550 'post_title' => 'title', 551 'post_type' => 'unknown', 552 'post_status' => 'pending', 553 ) 554 ); 555 556 $this->assertIsInt( $post_id ); 557 $this->assertGreaterThan( 0, $post_id ); 558 } 559 560 /** 561 * @ticket 20451 562 */ 563 public function test_wp_insert_post_with_meta_input() { 564 $post_id = wp_insert_post( 565 array( 566 'post_title' => '', 567 'post_content' => 'test', 568 'post_status' => 'publish', 569 'post_type' => 'post', 570 'meta_input' => array( 571 'hello' => 'world', 572 'foo' => 'bar', 573 ), 574 ) 575 ); 576 577 $this->assertSame( 'world', get_post_meta( $post_id, 'hello', true ) ); 578 $this->assertSame( 'bar', get_post_meta( $post_id, 'foo', true ) ); 579 } 580 581 /** 582 * "When I delete a future post using wp_delete_post( $post->ID ) it does not update the cron correctly." 583 * 584 * @ticket 5364 585 * @covers ::wp_delete_post 586 */ 587 public function test_delete_future_post_cron() { 588 $future_date = strtotime( '+1 day' ); 589 590 $data = array( 591 'post_author' => self::$user_ids['editor'], 592 'post_status' => 'publish', 593 'post_content' => 'content', 594 'post_title' => 'title', 595 'post_date' => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ), 596 ); 597 598 // Insert a post and make sure the ID is OK. 599 $post_id = wp_insert_post( $data ); 600 601 // Check that there's a publish_future_post job scheduled at the right time. 602 $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 603 604 // Now delete the post and make sure the cron entry is removed. 605 wp_delete_post( $post_id ); 606 607 $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) ); 608 } 609 610 /** 611 * Bug: permalink doesn't work if post title is empty. 612 * 613 * Might only fail if the post ID is greater than four characters. 614 * 615 * @ticket 5305 616 */ 617 public function test_permalink_without_title() { 618 $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); 619 620 $data = array( 621 'post_author' => self::$user_ids['editor'], 622 'post_status' => 'publish', 623 'post_content' => 'content', 624 'post_title' => '', 625 'post_date' => '2007-10-31 06:15:00', 626 ); 627 628 // Insert a post and make sure the ID is OK. 629 $post_id = wp_insert_post( $data ); 630 631 // Permalink should include the post ID at the end. 632 $expected = get_option( 'siteurl' ) . '/2007/10/31/' . $post_id . '/'; 633 $this->assertSame( $expected, get_permalink( $post_id ) ); 634 } 635 636 /** 637 * @ticket 23708 638 */ 639 public function test_get_post_ancestors_within_loop() { 640 global $post; 641 642 $parent_id = self::factory()->post->create(); 643 $post = self::factory()->post->create_and_get( 644 array( 645 'post_parent' => $parent_id, 646 ) 647 ); 648 649 $this->assertSame( array( $parent_id ), get_post_ancestors( 0 ) ); 650 } 651 652 /** 653 * @ticket 23474 654 * @covers ::wp_update_post 655 */ 656 public function test_update_invalid_post_id() { 657 $post_id = self::factory()->post->create(); 658 $post = get_post( $post_id, ARRAY_A ); 659 660 $post['ID'] = 123456789; 661 662 $this->assertSame( 0, wp_insert_post( $post ) ); 663 $this->assertSame( 0, wp_update_post( $post ) ); 664 665 $this->assertInstanceOf( 'WP_Error', wp_insert_post( $post, true ) ); 666 $this->assertInstanceOf( 'WP_Error', wp_update_post( $post, true ) ); 667 668 } 669 670 /** 671 * @ticket 19373 672 */ 673 public function test_insert_programmatic_sanitized() { 674 $this->unset_current_user(); 675 676 register_taxonomy( 'test_tax', 'post' ); 677 678 $title = 'title'; 679 $data = array( 680 'post_author' => self::$user_ids['editor'], 681 'post_status' => 'publish', 682 'post_content' => 'content', 683 'post_title' => $title, 684 'tax_input' => array( 685 'test_tax' => array( 'term', 'term2', 'term3' ), 686 ), 687 ); 688 689 $post_id = wp_insert_post( $data, true, true ); 690 $this->assertIsInt( $post_id ); 691 $this->assertGreaterThan( 0, $post_id ); 692 693 $post = get_post( $post_id ); 694 $this->assertEquals( self::$user_ids['editor'], $post->post_author ); 695 $this->assertSame( $title, $post->post_title ); 696 } 697 698 /** 699 * @ticket 31168 700 */ 701 public function test_wp_insert_post_default_comment_ping_status_open() { 702 $post_id = self::factory()->post->create( 703 array( 704 'post_author' => self::$user_ids['editor'], 705 'post_status' => 'publish', 706 'post_content' => 'content', 707 'post_title' => 'title', 708 ) 709 ); 710 $post = get_post( $post_id ); 711 712 $this->assertSame( 'open', $post->comment_status ); 713 $this->assertSame( 'open', $post->ping_status ); 714 } 715 716 /** 717 * @ticket 31168 718 */ 719 public function test_wp_insert_post_page_default_comment_ping_status_closed() { 720 $post_id = self::factory()->post->create( 721 array( 722 'post_author' => self::$user_ids['editor'], 723 'post_status' => 'publish', 724 'post_content' => 'content', 725 'post_title' => 'title', 726 'post_type' => 'page', 727 ) 728 ); 729 $post = get_post( $post_id ); 730 731 $this->assertSame( 'closed', $post->comment_status ); 732 $this->assertSame( 'closed', $post->ping_status ); 733 } 734 735 /** 736 * @ticket 31168 737 */ 738 public function test_wp_insert_post_cpt_default_comment_ping_status_open() { 739 $post_type = rand_str( 20 ); 740 register_post_type( $post_type, array( 'supports' => array( 'comments', 'trackbacks' ) ) ); 741 742 $post_id = self::factory()->post->create( 743 array( 744 'post_author' => self::$user_ids['editor'], 745 'post_status' => 'publish', 746 'post_content' => rand_str(), 747 'post_title' => rand_str(), 748 'post_type' => $post_type, 749 ) 750 ); 751 $post = get_post( $post_id ); 752 753 _unregister_post_type( $post_type ); 754 755 $this->assertSame( 'open', $post->comment_status ); 756 $this->assertSame( 'open', $post->ping_status ); 757 } 758 759 /** 760 * @ticket 31168 761 */ 762 public function test_wp_insert_post_cpt_default_comment_ping_status_closed() { 763 $post_type = rand_str( 20 ); 764 register_post_type( $post_type ); 765 766 $post_id = self::factory()->post->create( 767 array( 768 'post_author' => self::$user_ids['editor'], 769 'post_status' => 'publish', 770 'post_content' => rand_str(), 771 'post_title' => rand_str(), 772 'post_type' => $post_type, 773 ) 774 ); 775 $post = get_post( $post_id ); 776 777 _unregister_post_type( $post_type ); 778 779 $this->assertSame( 'closed', $post->comment_status ); 780 $this->assertSame( 'closed', $post->ping_status ); 781 } 782 783 /** 784 * If a post is updated without providing a post_name param, 785 * a new slug should not be generated. 786 * 787 * @ticket 34865 788 */ 789 public function test_post_updates_without_slug_provided() { 790 $post_id = self::factory()->post->create( 791 array( 792 'post_title' => 'Stuff', 793 'post_status' => 'publish', 794 ) 795 ); 796 797 $data = array( 798 'ID' => $post_id, 799 'post_title' => 'Stuff and Things', 800 ); 801 802 wp_insert_post( $data ); 803 804 $updated_post = get_post( $post_id ); 805 // Ensure changing the post_title didn't modify the post_name. 806 $this->assertSame( 'stuff', $updated_post->post_name ); 807 } 808 809 /** 810 * @ticket 32585 811 */ 812 public function test_wp_insert_post_author_zero() { 813 $post_id = self::factory()->post->create( array( 'post_author' => 0 ) ); 814 815 $this->assertEquals( 0, get_post( $post_id )->post_author ); 816 } 817 818 /** 819 * @ticket 32585 820 */ 821 public function test_wp_insert_post_author_null() { 822 wp_set_current_user( self::$user_ids['editor'] ); 823 824 $post_id = self::factory()->post->create( array( 'post_author' => null ) ); 825 826 $this->assertEquals( self::$user_ids['editor'], get_post( $post_id )->post_author ); 827 } 828 829 /** 830 * @ticket 15946 831 */ 832 public function test_wp_insert_post_should_respect_post_date_gmt() { 833 $data = array( 834 'post_author' => self::$user_ids['editor'], 835 'post_status' => 'publish', 836 'post_content' => 'content', 837 'post_title' => 'title', 838 'post_date_gmt' => '2014-01-01 12:00:00', 839 ); 840 841 // Insert a post and make sure the ID is OK. 842 $post_id = wp_insert_post( $data ); 843 844 $post = get_post( $post_id ); 845 846 $this->assertSame( $data['post_content'], $post->post_content ); 847 $this->assertSame( $data['post_title'], $post->post_title ); 848 $this->assertEquals( $data['post_author'], $post->post_author ); 849 $this->assertSame( get_date_from_gmt( $data['post_date_gmt'] ), $post->post_date ); 850 $this->assertSame( $data['post_date_gmt'], $post->post_date_gmt ); 851 } 852 853 /** 854 * Test ensuring that the post_name (UUID) is preserved when wp_insert_post()/wp_update_post() is called. 855 * 856 * @see _wp_customize_changeset_filter_insert_post_data() 857 * @ticket 30937 858 */ 859 public function test_wp_insert_post_for_customize_changeset_should_not_drop_post_name() { 860 $this->assertSame( 10, has_filter( 'wp_insert_post_data', '_wp_customize_changeset_filter_insert_post_data' ) ); 861 862 $changeset_data = array( 863 'blogname' => array( 864 'value' => 'Hello World', 865 ), 866 ); 867 868 wp_set_current_user( self::$user_ids['contributor'] ); 869 870 $uuid = wp_generate_uuid4(); 871 $post_id = wp_insert_post( 872 array( 873 'post_type' => 'customize_changeset', 874 'post_name' => strtoupper( $uuid ), 875 'post_content' => wp_json_encode( $changeset_data ), 876 ) 877 ); 878 $this->assertSame( $uuid, get_post( $post_id )->post_name, 'Expected lower-case UUID4 to be inserted.' ); 879 $this->assertSame( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) ); 880 881 $changeset_data['blogname']['value'] = 'Hola Mundo'; 882 wp_update_post( 883 array( 884 'ID' => $post_id, 885 'post_status' => 'draft', 886 'post_content' => wp_json_encode( $changeset_data ), 887 ) 888 ); 889 $this->assertSame( $uuid, get_post( $post_id )->post_name, 'Expected post_name to not have been dropped for drafts.' ); 890 $this->assertSame( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) ); 891 892 $changeset_data['blogname']['value'] = 'Hallo Welt'; 893 wp_update_post( 894 array( 895 'ID' => $post_id, 896 'post_status' => 'pending', 897 'post_content' => wp_json_encode( $changeset_data ), 898 ) 899 ); 900 $this->assertSame( $uuid, get_post( $post_id )->post_name, 'Expected post_name to not have been dropped for pending.' ); 901 $this->assertSame( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) ); 902 } 903 904 /** 905 * @ticket 48113 906 */ 907 public function test_insert_post_should_respect_date_floating_post_status_arg() { 908 register_post_status( 'floating', array( 'date_floating' => true ) ); 909 910 $post_id = self::factory()->post->create( 911 array( 912 'post_status' => 'floating', 913 'post_date' => null, 914 'post_date_gmt' => null, 915 ) 916 ); 917 918 $post = get_post( $post_id ); 919 self::assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); 920 } 921 922 /** 923 * @ticket 48113 924 */ 925 public function test_insert_post_should_respect_date_floating_post_status_arg_not_set() { 926 register_post_status( 'not-floating', array( 'date_floating' => false ) ); 927 928 $post_id = self::factory()->post->create( 929 array( 930 'post_status' => 'floating', 931 'post_date' => null, 932 'post_date_gmt' => null, 933 ) 934 ); 935 936 $post = get_post( $post_id ); 937 self::assertEqualsWithDelta( 938 strtotime( gmdate( 'Y-m-d H:i:s' ) ), 939 strtotime( $post->post_date_gmt ), 940 2, 941 'The dates should be equal' 942 ); 943 } 944 945 /** 946 * Test ensuring that wp_update_post() does not unintentionally modify post tags 947 * if the post has several tags with the same name but different slugs. 948 * 949 * Tags should only be modified if 'tags_input' parameter was explicitly provided, 950 * and is different from the existing tags. 951 * 952 * @ticket 45121 953 * @covers ::wp_update_post 954 */ 955 public function test_update_post_should_only_modify_post_tags_if_different_tags_input_was_provided() { 956 $tag_1 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_1' ) ); 957 $tag_2 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_2' ) ); 958 $tag_3 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_3' ) ); 959 960 $post_id = self::factory()->post->create( 961 array( 962 'tags_input' => array( $tag_1['term_id'], $tag_2['term_id'] ), 963 ) 964 ); 965 966 $post = get_post( $post_id ); 967 968 $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); 969 $this->assertSameSets( array( $tag_1['term_id'], $tag_2['term_id'] ), $tags ); 970 971 wp_update_post( $post ); 972 973 $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); 974 $this->assertSameSets( array( $tag_1['term_id'], $tag_2['term_id'] ), $tags ); 975 976 wp_update_post( 977 array( 978 'ID' => $post->ID, 979 'tags_input' => array( $tag_2['term_id'], $tag_3['term_id'] ), 980 ) 981 ); 982 983 $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); 984 $this->assertSameSets( array( $tag_2['term_id'], $tag_3['term_id'] ), $tags ); 985 } 986 987 /** 988 * @ticket 52187 989 */ 990 public function test_insert_empty_post_date() { 991 $post_date_gmt = '2020-12-29 10:11:45'; 992 $invalid_date = '2020-12-41 14:15:27'; 993 994 // Empty post_date_gmt with floating status 995 $post_id = self::factory()->post->create( 996 array( 997 'post_status' => 'draft', 998 ) 999 ); 1000 $post = get_post( $post_id ); 1001 $this->assertEqualsWithDelta( 1002 strtotime( gmdate( 'Y-m-d H:i:s' ) ), 1003 strtotime( $post->post_date ), 1004 2, 1005 'The dates should be equal' 1006 ); 1007 $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); 1008 1009 $post_id = self::factory()->post->create( 1010 array( 1011 'post_date_gmt' => '0000-00-00 00:00:00', 1012 'post_status' => 'draft', 1013 ) 1014 ); 1015 $post = get_post( $post_id ); 1016 $this->assertEqualsWithDelta( 1017 strtotime( gmdate( 'Y-m-d H:i:s' ) ), 1018 strtotime( $post->post_date ), 1019 2, 1020 'The dates should be equal' 1021 ); 1022 $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); 1023 1024 // Empty post_date_gmt without floating status 1025 $post_id = self::factory()->post->create( 1026 array( 1027 'post_status' => 'publish', 1028 ) 1029 ); 1030 $post = get_post( $post_id ); 1031 $this->assertEqualsWithDelta( 1032 strtotime( gmdate( 'Y-m-d H:i:s' ) ), 1033 strtotime( $post->post_date ), 1034 2, 1035 'The dates should be equal' 1036 ); 1037 $this->assertEqualsWithDelta( 1038 strtotime( gmdate( 'Y-m-d H:i:s' ) ), 1039 strtotime( get_gmt_from_date( $post->post_date ) ), 1040 2, 1041 'The dates should be equal' 1042 ); 1043 1044 $post_id = self::factory()->post->create( 1045 array( 1046 'post_date_gmt' => '0000-00-00 00:00:00', 1047 'post_status' => 'publish', 1048 ) 1049 ); 1050 $post = get_post( $post_id ); 1051 $this->assertEqualsWithDelta( 1052 strtotime( gmdate( 'Y-m-d H:i:s' ) ), 1053 strtotime( $post->post_date ), 1054 2, 1055 'The dates should be equal' 1056 ); 1057 $this->assertEqualsWithDelta( 1058 strtotime( gmdate( 'Y-m-d H:i:s' ) ), 1059 strtotime( get_gmt_from_date( $post->post_date ) ), 1060 2, 1061 'The dates should be equal' 1062 ); 1063 1064 // Valid post_date_gmt 1065 $post_id = self::factory()->post->create( 1066 array( 1067 'post_date_gmt' => $post_date_gmt, 1068 ) 1069 ); 1070 $post = get_post( $post_id ); 1071 $this->assertSame( get_date_from_gmt( $post_date_gmt ), $post->post_date ); 1072 $this->assertSame( $post_date_gmt, $post->post_date_gmt ); 1073 1074 // Invalid post_date_gmt 1075 $post_id = self::factory()->post->create( 1076 array( 1077 'post_date_gmt' => $invalid_date, 1078 ) 1079 ); 1080 $post = get_post( $post_id ); 1081 $this->assertSame( '1970-01-01 00:00:00', $post->post_date ); 1082 $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); 1083 } 1084 1085 /** 1086 * @ticket 52187 1087 */ 1088 public function test_insert_valid_post_date() { 1089 $post_date = '2020-12-28 11:26:35'; 1090 $post_date_gmt = '2020-12-29 10:11:45'; 1091 $invalid_date = '2020-12-41 14:15:27'; 1092 1093 // Empty post_date_gmt with floating status 1094 $post_id = self::factory()->post->create( 1095 array( 1096 'post_date' => $post_date, 1097 'post_status' => 'draft', 1098 ) 1099 ); 1100 $post = get_post( $post_id ); 1101 $this->assertSame( $post_date, $post->post_date ); 1102 $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); 1103 1104 $post_id = self::factory()->post->create( 1105 array( 1106 'post_date' => $post_date, 1107 'post_date_gmt' => '0000-00-00 00:00:00', 1108 'post_status' => 'draft', 1109 ) 1110 ); 1111 $post = get_post( $post_id ); 1112 $this->assertSame( $post_date, $post->post_date ); 1113 $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); 1114 1115 // Empty post_date_gmt without floating status 1116 $post_id = self::factory()->post->create( 1117 array( 1118 'post_date' => $post_date, 1119 'post_status' => 'publish', 1120 ) 1121 ); 1122 $post = get_post( $post_id ); 1123 $this->assertSame( $post_date, $post->post_date ); 1124 $this->assertSame( get_gmt_from_date( $post_date ), $post->post_date_gmt ); 1125 1126 $post_id = self::factory()->post->create( 1127 array( 1128 'post_date' => $post_date, 1129 'post_date_gmt' => '0000-00-00 00:00:00', 1130 'post_status' => 'publish', 1131 ) 1132 ); 1133 $post = get_post( $post_id ); 1134 $this->assertSame( $post_date, $post->post_date ); 1135 $this->assertSame( get_gmt_from_date( $post_date ), $post->post_date_gmt ); 1136 1137 // Valid post_date_gmt 1138 $post_id = self::factory()->post->create( 1139 array( 1140 'post_date' => $post_date, 1141 'post_date_gmt' => $post_date_gmt, 1142 ) 1143 ); 1144 $post = get_post( $post_id ); 1145 $this->assertSame( $post_date, $post->post_date ); 1146 $this->assertSame( $post_date_gmt, $post->post_date_gmt ); 1147 1148 // Invalid post_date_gmt 1149 $post_id = self::factory()->post->create( 1150 array( 1151 'post_date' => $post_date, 1152 'post_date_gmt' => $invalid_date, 1153 ) 1154 ); 1155 $post = get_post( $post_id ); 1156 $this->assertSame( $post_date, $post->post_date ); 1157 $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt ); 1158 } 1159 1160 /** 1161 * @ticket 52187 1162 */ 1163 public function test_insert_invalid_post_date() { 1164 $post_date = '2020-12-28 11:26:35'; 1165 $post_date_gmt = '2020-12-29 10:11:45'; 1166 $invalid_date = '2020-12-41 14:15:27'; 1167 1168 // Empty post_date_gmt with floating status 1169 $post_id = self::factory()->post->create( 1170 array( 1171 'post_date' => $invalid_date, 1172 'post_status' => 'draft', 1173 ) 1174 ); 1175 $this->assertSame( 0, $post_id ); 1176 1177 $post_id = self::factory()->post->create( 1178 array( 1179 'post_date' => $invalid_date, 1180 'post_date_gmt' => '0000-00-00 00:00:00', 1181 'post_status' => 'draft', 1182 ) 1183 ); 1184 $this->assertSame( 0, $post_id ); 1185 1186 // Empty post_date_gmt without floating status 1187 $post_id = self::factory()->post->create( 1188 array( 1189 'post_date' => $invalid_date, 1190 'post_status' => 'publish', 1191 ) 1192 ); 1193 $this->assertSame( 0, $post_id ); 1194 1195 $post_id = self::factory()->post->create( 1196 array( 1197 'post_date' => $invalid_date, 1198 'post_date_gmt' => '0000-00-00 00:00:00', 1199 'post_status' => 'publish', 1200 ) 1201 ); 1202 $this->assertSame( 0, $post_id ); 1203 1204 // Valid post_date_gmt 1205 $post_id = self::factory()->post->create( 1206 array( 1207 'post_date' => $invalid_date, 1208 'post_date_gmt' => $post_date_gmt, 1209 ) 1210 ); 1211 $this->assertSame( 0, $post_id ); 1212 1213 // Invalid post_date_gmt 1214 $post_id = self::factory()->post->create( 1215 array( 1216 'post_date' => $invalid_date, 1217 'post_date_gmt' => $invalid_date, 1218 ) 1219 ); 1220 $this->assertSame( 0, $post_id ); 1221 } 1222 1223 /** 69 1224 * @ticket 11863 70 1225 */
Note: See TracChangeset
for help on using the changeset viewer.