Make WordPress Core

Changeset 53785


Ignore:
Timestamp:
07/27/2022 02:59:12 PM (22 months ago)
Author:
SergeyBiryukov
Message:

Tests: Move wp_insert_post() tests to their own file.

Now that there is a separate test class for wp_insert_post() tests, some of the pre-existing tests from the general Tests_Post class can be moved there.

Includes:

  • Removal of unnecessarily setting the current user to Editor for all tests.
  • Removal of unnecessarily setting the cron option to an empty array for all tests.

Follow-up to [496/tests], [36607], [53782], [53783].

See #55652.

Location:
trunk/tests/phpunit/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post.php

    r53783 r53785  
    3131    }
    3232
    33     public function set_up() {
    34         parent::set_up();
    35 
    36         wp_set_current_user( self::$editor_id );
    37         _set_cron_array( array() );
    38     }
    39 
    40     /**
    41      * Helper function: return the timestamp(s) of cron jobs for the specified hook and post.
    42      */
    43     private function next_schedule_for_post( $hook, $post_id ) {
    44         return wp_next_scheduled( 'publish_future_post', array( 0 => (int) $post_id ) );
    45     }
    46 
    47     /**
    48      * Helper function, unsets current user globally.
    49      */
    50     private function unset_current_user() {
    51         global $current_user, $user_ID;
    52 
    53         $current_user = null;
    54         $user_ID      = null;
    55     }
    56 
    57     /**
    58      * Test simple valid behavior: insert and get a post.
    59      *
    60      * @dataProvider data_vb_insert_get_delete
    61      */
    62     public function test_vb_insert_get_delete( $post_type ) {
    63         register_post_type(
    64             'cpt',
    65             array(
    66                 'taxonomies' => array( 'post_tag', 'ctax' ),
    67             )
    68         );
    69         register_taxonomy( 'ctax', 'cpt' );
    70 
    71         $data = array(
    72             'post_author'  => self::$editor_id,
    73             'post_status'  => 'publish',
    74             'post_content' => "{$post_type}_content",
    75             'post_title'   => "{$post_type}_title",
    76             'tax_input'    => array(
    77                 'post_tag' => 'tag1,tag2',
    78                 'ctax'     => 'cterm1,cterm2',
    79             ),
    80             'post_type'    => $post_type,
    81         );
    82 
    83         // Insert a post and make sure the ID is OK.
    84         $post_id = wp_insert_post( $data );
    85         $this->assertIsInt( $post_id );
    86         $this->assertGreaterThan( 0, $post_id );
    87 
    88         // Fetch the post and make sure it matches.
    89         $post = get_post( $post_id );
    90 
    91         $this->assertSame( $data['post_content'], $post->post_content );
    92         $this->assertSame( $data['post_title'], $post->post_title );
    93         $this->assertSame( $data['post_status'], $post->post_status );
    94         $this->assertEquals( $data['post_author'], $post->post_author );
    95 
    96         // Test cache state.
    97         $post_cache = wp_cache_get( $post_id, 'posts' );
    98         $this->assertInstanceOf( 'stdClass', $post_cache );
    99         $this->assertSame( $post_id, $post_cache->ID );
    100 
    101         update_object_term_cache( $post_id, $post_type );
    102         $term_cache = wp_cache_get( $post_id, 'post_tag_relationships' );
    103         $this->assertIsArray( $term_cache );
    104         $this->assertCount( 2, $term_cache );
    105 
    106         $term_cache = wp_cache_get( $post_id, 'ctax_relationships' );
    107         if ( 'cpt' === $post_type ) {
    108             $this->assertIsArray( $term_cache );
    109             $this->assertCount( 2, $term_cache );
    110         } else {
    111             $this->assertFalse( $term_cache );
    112         }
    113 
    114         wp_delete_post( $post_id, true );
    115 
    116         $this->assertFalse( wp_cache_get( $post_id, 'posts' ) );
    117         $this->assertFalse( wp_cache_get( $post_id, 'post_tag_relationships' ) );
    118         $this->assertFalse( wp_cache_get( $post_id, 'ctax_relationships' ) );
    119 
    120         $GLOBALS['wp_taxonomies']['post_tag']->object_type = array( 'post' );
    121     }
    122 
    123     public function data_vb_insert_get_delete() {
    124         $post_types = array( 'post', 'cpt' );
    125 
    126         return $this->text_array_to_dataprovider( $post_types );
    127     }
    128 
    129     /**
    130      * Insert a post with a future date, and make sure the status and cron schedule are correct.
    131      */
    132     public function test_vb_insert_future() {
    133         $future_date = strtotime( '+1 day' );
    134 
    135         $data = array(
    136             'post_author'  => self::$editor_id,
    137             'post_status'  => 'publish',
    138             'post_content' => 'content',
    139             'post_title'   => 'title',
    140             'post_date'    => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
    141         );
    142 
    143         // Insert a post and make sure the ID is OK.
    144         $post_id = wp_insert_post( $data );
    145         $this->assertIsInt( $post_id );
    146         $this->assertGreaterThan( 0, $post_id );
    147 
    148         // Fetch the post and make sure it matches.
    149         $post = get_post( $post_id );
    150 
    151         $this->assertSame( $data['post_content'], $post->post_content );
    152         $this->assertSame( $data['post_title'], $post->post_title );
    153         $this->assertSame( 'future', $post->post_status );
    154         $this->assertEquals( $data['post_author'], $post->post_author );
    155         $this->assertSame( $data['post_date'], $post->post_date );
    156 
    157         // There should be a publish_future_post hook scheduled on the future date.
    158         $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    159     }
    160 
    161     /**
    162      * Insert a post with a future date, and make sure the status and cron schedule are correct.
    163      */
    164     public function test_vb_insert_future_over_dst() {
    165         // Some magic days - one DST one not.
    166         $future_date_1 = strtotime( 'June 21st +1 year' );
    167         $future_date_2 = strtotime( 'Jan 11th +1 year' );
    168 
    169         $data = array(
    170             'post_author'  => self::$editor_id,
    171             'post_status'  => 'publish',
    172             'post_content' => 'content',
    173             'post_title'   => 'title',
    174             'post_date'    => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
    175         );
    176 
    177         // Insert a post and make sure the ID is OK.
    178         $post_id = wp_insert_post( $data );
    179 
    180         // Fetch the post and make sure has the correct date and status.
    181         $post = get_post( $post_id );
    182         $this->assertSame( 'future', $post->post_status );
    183         $this->assertSame( $data['post_date'], $post->post_date );
    184 
    185         // Check that there's a publish_future_post job scheduled at the right time.
    186         $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    187 
    188         // Now save it again with a date further in the future.
    189         $data['ID']            = $post_id;
    190         $data['post_date']     = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' );
    191         $data['post_date_gmt'] = null;
    192         wp_update_post( $data );
    193 
    194         // Fetch the post again and make sure it has the new post_date.
    195         $post = get_post( $post_id );
    196         $this->assertSame( 'future', $post->post_status );
    197         $this->assertSame( $data['post_date'], $post->post_date );
    198 
    199         // And the correct date on the cron job.
    200         $this->assertSame( $future_date_2, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    201     }
    202 
    203     /**
    204      * Future post bug: posts get published at the wrong time if you edit the timestamp.
    205      *
    206      * @ticket 4710
    207      */
    208     public function test_vb_insert_future_edit_bug() {
    209         $future_date_1 = strtotime( '+1 day' );
    210         $future_date_2 = strtotime( '+2 day' );
    211 
    212         $data = array(
    213             'post_author'  => self::$editor_id,
    214             'post_status'  => 'publish',
    215             'post_content' => 'content',
    216             'post_title'   => 'title',
    217             'post_date'    => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
    218         );
    219 
    220         // Insert a post and make sure the ID is OK.
    221         $post_id = wp_insert_post( $data );
    222 
    223         // Fetch the post and make sure has the correct date and status.
    224         $post = get_post( $post_id );
    225         $this->assertSame( 'future', $post->post_status );
    226         $this->assertSame( $data['post_date'], $post->post_date );
    227 
    228         // Check that there's a publish_future_post job scheduled at the right time.
    229         $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    230 
    231         // Now save it again with a date further in the future.
    232         $data['ID']            = $post_id;
    233         $data['post_date']     = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' );
    234         $data['post_date_gmt'] = null;
    235         wp_update_post( $data );
    236 
    237         // Fetch the post again and make sure it has the new post_date.
    238         $post = get_post( $post_id );
    239         $this->assertSame( 'future', $post->post_status );
    240         $this->assertSame( $data['post_date'], $post->post_date );
    241 
    242         // And the correct date on the cron job.
    243         $this->assertSame( $future_date_2, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    244     }
    245 
    246     /**
    247      * Insert a draft post with a future date, and make sure no cron schedule is set.
    248      */
    249     public function test_vb_insert_future_draft() {
    250         $future_date = strtotime( '+1 day' );
    251 
    252         $data = array(
    253             'post_author'  => self::$editor_id,
    254             'post_status'  => 'draft',
    255             'post_content' => 'content',
    256             'post_title'   => 'title',
    257             'post_date'    => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
    258         );
    259 
    260         // Insert a post and make sure the ID is OK.
    261         $post_id = wp_insert_post( $data );
    262         $this->assertIsInt( $post_id );
    263         $this->assertGreaterThan( 0, $post_id );
    264 
    265         // Fetch the post and make sure it matches.
    266         $post = get_post( $post_id );
    267 
    268         $this->assertSame( $data['post_content'], $post->post_content );
    269         $this->assertSame( $data['post_title'], $post->post_title );
    270         $this->assertSame( 'draft', $post->post_status );
    271         $this->assertEquals( $data['post_author'], $post->post_author );
    272         $this->assertSame( $data['post_date'], $post->post_date );
    273 
    274         // There should be a publish_future_post hook scheduled on the future date.
    275         $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    276 
    277     }
    278 
    279     /**
    280      * Insert a future post, then edit and change it to draft, and make sure cron gets it right.
    281      */
    282     public function test_vb_insert_future_change_to_draft() {
    283         $future_date_1 = strtotime( '+1 day' );
    284 
    285         $data = array(
    286             'post_author'  => self::$editor_id,
    287             'post_status'  => 'publish',
    288             'post_content' => 'content',
    289             'post_title'   => 'title',
    290             'post_date'    => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
    291         );
    292 
    293         // Insert a post and make sure the ID is OK.
    294         $post_id = wp_insert_post( $data );
    295 
    296         // Fetch the post and make sure has the correct date and status.
    297         $post = get_post( $post_id );
    298         $this->assertSame( 'future', $post->post_status );
    299         $this->assertSame( $data['post_date'], $post->post_date );
    300 
    301         // Check that there's a publish_future_post job scheduled at the right time.
    302         $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    303 
    304         // Now save it again with status set to draft.
    305         $data['ID']          = $post_id;
    306         $data['post_status'] = 'draft';
    307         wp_update_post( $data );
    308 
    309         // Fetch the post again and make sure it has the new post_date.
    310         $post = get_post( $post_id );
    311         $this->assertSame( 'draft', $post->post_status );
    312         $this->assertSame( $data['post_date'], $post->post_date );
    313 
    314         // And the correct date on the cron job.
    315         $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    316     }
    317 
    318     /**
    319      * Insert a future post, then edit and change the status, and make sure cron gets it right.
    320      *
    321      * @dataProvider data_vb_insert_future_change_status
    322      */
    323     public function test_vb_insert_future_change_status( $status ) {
    324         $future_date_1 = strtotime( '+1 day' );
    325 
    326         $data = array(
    327             'post_author'  => self::$editor_id,
    328             'post_status'  => 'publish',
    329             'post_content' => "{$status}_content",
    330             'post_title'   => "{$status}_title",
    331             'post_date'    => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
    332         );
    333 
    334         // Insert a post and make sure the ID is OK.
    335         $post_id = wp_insert_post( $data );
    336 
    337         // Fetch the post and make sure has the correct date and status.
    338         $post = get_post( $post_id );
    339         $this->assertSame( 'future', $post->post_status );
    340         $this->assertSame( $data['post_date'], $post->post_date );
    341 
    342         // Check that there's a publish_future_post job scheduled at the right time.
    343         $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    344 
    345         // Now save it again with status changed.
    346         $data['ID']          = $post_id;
    347         $data['post_status'] = $status;
    348         wp_update_post( $data );
    349 
    350         // Fetch the post again and make sure it has the new post_date.
    351         $post = get_post( $post_id );
    352         $this->assertSame( $status, $post->post_status );
    353         $this->assertSame( $data['post_date'], $post->post_date );
    354 
    355         // And the correct date on the cron job.
    356         $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    357     }
    358 
    359     public function data_vb_insert_future_change_status() {
    360         $statuses = array(
    361             'draft',
    362             'static',
    363             'object',
    364             'attachment',
    365             'inherit',
    366             'pending',
    367         );
    368 
    369         return $this->text_array_to_dataprovider( $statuses );
    370     }
    371 
    372     /**
    373      * Insert a draft post with a future date, and make sure no cron schedule is set.
    374      */
    375     public function test_vb_insert_future_private() {
    376         $future_date = strtotime( '+1 day' );
    377 
    378         $data = array(
    379             'post_author'  => self::$editor_id,
    380             'post_status'  => 'private',
    381             'post_content' => 'content',
    382             'post_title'   => 'title',
    383             'post_date'    => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
    384         );
    385 
    386         // Insert a post and make sure the ID is OK.
    387         $post_id = wp_insert_post( $data );
    388         $this->assertIsInt( $post_id );
    389         $this->assertGreaterThan( 0, $post_id );
    390 
    391         // Fetch the post and make sure it matches.
    392         $post = get_post( $post_id );
    393 
    394         $this->assertSame( $data['post_content'], $post->post_content );
    395         $this->assertSame( $data['post_title'], $post->post_title );
    396         $this->assertSame( 'private', $post->post_status );
    397         $this->assertEquals( $data['post_author'], $post->post_author );
    398         $this->assertSame( $data['post_date'], $post->post_date );
    399 
    400         // There should be a publish_future_post hook scheduled on the future date.
    401         $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    402     }
    403 
    404     /**
    405      * Insert a post with an invalid date, make sure it fails.
    406      *
    407      * @ticket 17180
    408      */
    409     public function test_vb_insert_invalid_date() {
    410         $data = array(
    411             'post_author'  => self::$editor_id,
    412             'post_status'  => 'publish',
    413             'post_content' => 'content',
    414             'post_title'   => 'title',
    415             'post_date'    => '2012-02-30 00:00:00',
    416         );
    417 
    418         // Test both return paths with or without WP_Error.
    419         $post_id = wp_insert_post( $data, true );
    420         $this->assertWPError( $post_id );
    421         $this->assertSame( 'invalid_date', $post_id->get_error_code() );
    422 
    423         $post_id = wp_insert_post( $data );
    424         $this->assertSame( 0, $post_id );
    425     }
    426 
    427     /**
    428      * Insert a future post, then edit and change it to private, and make sure cron gets it right.
    429      */
    430     public function test_vb_insert_future_change_to_private() {
    431         $future_date_1 = strtotime( '+1 day' );
    432 
    433         $data = array(
    434             'post_author'  => self::$editor_id,
    435             'post_status'  => 'publish',
    436             'post_content' => 'content',
    437             'post_title'   => 'title',
    438             'post_date'    => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
    439         );
    440 
    441         // Insert a post and make sure the ID is OK.
    442         $post_id = wp_insert_post( $data );
    443 
    444         // Fetch the post and make sure has the correct date and status.
    445         $post = get_post( $post_id );
    446         $this->assertSame( 'future', $post->post_status );
    447         $this->assertSame( $data['post_date'], $post->post_date );
    448 
    449         // Check that there's a publish_future_post job scheduled at the right time.
    450         $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    451 
    452         // Now save it again with status set to draft.
    453         $data['ID']          = $post_id;
    454         $data['post_status'] = 'private';
    455         wp_update_post( $data );
    456 
    457         // Fetch the post again and make sure it has the new post_date.
    458         $post = get_post( $post_id );
    459         $this->assertSame( 'private', $post->post_status );
    460         $this->assertSame( $data['post_date'], $post->post_date );
    461 
    462         // And the correct date on the cron job.
    463         $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    464     }
    465 
    466     /**
    467      * @ticket 5305
    468      */
    469     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() {
    470         $this->set_permalink_structure( '/%postname%/' );
    471 
    472         $post_id = wp_insert_post(
    473             array(
    474                 'post_title'   => '',
    475                 'post_content' => 'test',
    476                 'post_status'  => 'publish',
    477                 'post_type'    => 'post',
    478             )
    479         );
    480 
    481         $post = get_post( $post_id );
    482 
    483         $this->assertSame( "$post_id-2", $post->post_name );
    484     }
    485 
    486     /**
    487      * @ticket 5305
    488      * @ticket 33392
    489      */
    490     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() {
    491         register_post_type( 'wptests_pt' );
    492 
    493         $post_id = wp_insert_post(
    494             array(
    495                 'post_title'  => '',
    496                 'post_type'   => 'wptests_pt',
    497                 'post_status' => 'publish',
    498             )
    499         );
    500 
    501         $post = get_post( $post_id );
    502 
    503         $this->assertStringContainsString( 'wptests_pt=' . $post_id, $post->guid );
    504     }
    505 
    506     /**
    507      * @ticket 55877
    508      * @covers ::wp_insert_post
    509      */
    510     public function test_wp_insert_post_should_not_trigger_warning_for_pending_posts_with_unknown_cpt() {
    511         $post_id = wp_insert_post(
    512             array(
    513                 'post_title'  => 'title',
    514                 'post_type'   => 'unknown',
    515                 'post_status' => 'pending',
    516             )
    517         );
    518 
    519         $this->assertIsInt( $post_id );
    520         $this->assertGreaterThan( 0, $post_id );
    521     }
    522 
    523     /**
    524      * @ticket 20451
    525      */
    526     public function test_wp_insert_post_with_meta_input() {
    527         $post_id = wp_insert_post(
    528             array(
    529                 'post_title'   => '',
    530                 'post_content' => 'test',
    531                 'post_status'  => 'publish',
    532                 'post_type'    => 'post',
    533                 'meta_input'   => array(
    534                     'hello' => 'world',
    535                     'foo'   => 'bar',
    536                 ),
    537             )
    538         );
    539 
    540         $this->assertSame( 'world', get_post_meta( $post_id, 'hello', true ) );
    541         $this->assertSame( 'bar', get_post_meta( $post_id, 'foo', true ) );
    542     }
    543 
    544     /**
    545      * "When I delete a future post using wp_delete_post( $post->ID ) it does not update the cron correctly."
    546      *
    547      * @ticket 5364
    548      */
    549     public function test_delete_future_post_cron() {
    550         $future_date = strtotime( '+1 day' );
    551 
    552         $data = array(
    553             'post_author'  => self::$editor_id,
    554             'post_status'  => 'publish',
    555             'post_content' => 'content',
    556             'post_title'   => 'title',
    557             'post_date'    => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
    558         );
    559 
    560         // Insert a post and make sure the ID is OK.
    561         $post_id = wp_insert_post( $data );
    562 
    563         // Check that there's a publish_future_post job scheduled at the right time.
    564         $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    565 
    566         // Now delete the post and make sure the cron entry is removed.
    567         wp_delete_post( $post_id );
    568 
    569         $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    570     }
    571 
    572     /**
    573      * Bug: permalink doesn't work if post title is empty.
    574      *
    575      * Might only fail if the post ID is greater than four characters.
    576      *
    577      * @ticket 5305
    578      */
    579     public function test_permalink_without_title() {
    580         $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
    581 
    582         $data = array(
    583             'post_author'  => self::$editor_id,
    584             'post_status'  => 'publish',
    585             'post_content' => 'content',
    586             'post_title'   => '',
    587             'post_date'    => '2007-10-31 06:15:00',
    588         );
    589 
    590         // Insert a post and make sure the ID is OK.
    591         $post_id = wp_insert_post( $data );
    592 
    593         // Permalink should include the post ID at the end.
    594         $expected = get_option( 'siteurl' ) . '/2007/10/31/' . $post_id . '/';
    595         $this->assertSame( $expected, get_permalink( $post_id ) );
    596     }
    597 
    598     /**
    599      * @ticket 23708
    600      */
    601     public function test_get_post_ancestors_within_loop() {
    602         global $post;
    603 
    604         $parent_id = self::factory()->post->create();
    605         $post      = self::factory()->post->create_and_get(
    606             array(
    607                 'post_parent' => $parent_id,
    608             )
    609         );
    610 
    611         $this->assertSame( array( $parent_id ), get_post_ancestors( 0 ) );
    612     }
    613 
    614     /**
    615      * @ticket 23474
    616      */
    617     public function test_update_invalid_post_id() {
    618         $post_id = self::factory()->post->create();
    619         $post    = get_post( $post_id, ARRAY_A );
    620 
    621         $post['ID'] = 123456789;
    622 
    623         $this->assertSame( 0, wp_insert_post( $post ) );
    624         $this->assertSame( 0, wp_update_post( $post ) );
    625 
    626         $this->assertInstanceOf( 'WP_Error', wp_insert_post( $post, true ) );
    627         $this->assertInstanceOf( 'WP_Error', wp_update_post( $post, true ) );
    628 
    629     }
    630 
    63133    public function test_parse_post_content_single_page() {
    63234        global $multipage, $pages, $numpages;
     
    735137        $this->assertSame( 1, $numpages );
    736138        $this->assertSame( array( 'Page 0' ), $pages );
    737     }
    738 
    739     /**
    740      * @ticket 19373
    741      */
    742     public function test_insert_programmatic_sanitized() {
    743         $this->unset_current_user();
    744 
    745         register_taxonomy( 'test_tax', 'post' );
    746 
    747         $title = 'title';
    748         $data  = array(
    749             'post_author'  => self::$editor_id,
    750             'post_status'  => 'publish',
    751             'post_content' => 'content',
    752             'post_title'   => $title,
    753             'tax_input'    => array(
    754                 'test_tax' => array( 'term', 'term2', 'term3' ),
    755             ),
    756         );
    757 
    758         $post_id = wp_insert_post( $data, true, true );
    759         $this->assertIsInt( $post_id );
    760         $this->assertGreaterThan( 0, $post_id );
    761 
    762         $post = get_post( $post_id );
    763         $this->assertEquals( self::$editor_id, $post->post_author );
    764         $this->assertSame( $title, $post->post_title );
    765139    }
    766140
     
    879253        wp_set_object_terms( $post, 'foo', $tax );
    880254
     255        wp_set_current_user( self::$editor_id );
     256
    881257        $wp_tag_cloud = wp_tag_cloud(
    882258            array(
     
    937313
    938314    /**
    939      * @ticket 31168
    940      */
    941     public function test_wp_insert_post_default_comment_ping_status_open() {
    942         $post_id = self::factory()->post->create(
    943             array(
    944                 'post_author'  => self::$editor_id,
    945                 'post_status'  => 'publish',
    946                 'post_content' => 'content',
    947                 'post_title'   => 'title',
    948             )
    949         );
    950         $post    = get_post( $post_id );
    951 
    952         $this->assertSame( 'open', $post->comment_status );
    953         $this->assertSame( 'open', $post->ping_status );
    954     }
    955 
    956     /**
    957      * @ticket 31168
    958      */
    959     public function test_wp_insert_post_page_default_comment_ping_status_closed() {
    960         $post_id = self::factory()->post->create(
    961             array(
    962                 'post_author'  => self::$editor_id,
    963                 'post_status'  => 'publish',
    964                 'post_content' => 'content',
    965                 'post_title'   => 'title',
    966                 'post_type'    => 'page',
    967             )
    968         );
    969         $post    = get_post( $post_id );
    970 
    971         $this->assertSame( 'closed', $post->comment_status );
    972         $this->assertSame( 'closed', $post->ping_status );
    973     }
    974 
    975     /**
    976      * @ticket 31168
    977      */
    978     public function test_wp_insert_post_cpt_default_comment_ping_status_open() {
    979         $post_type = rand_str( 20 );
    980         register_post_type( $post_type, array( 'supports' => array( 'comments', 'trackbacks' ) ) );
    981 
    982         $post_id = self::factory()->post->create(
    983             array(
    984                 'post_author'  => self::$editor_id,
    985                 'post_status'  => 'publish',
    986                 'post_content' => rand_str(),
    987                 'post_title'   => rand_str(),
    988                 'post_type'    => $post_type,
    989             )
    990         );
    991         $post    = get_post( $post_id );
    992 
    993         _unregister_post_type( $post_type );
    994 
    995         $this->assertSame( 'open', $post->comment_status );
    996         $this->assertSame( 'open', $post->ping_status );
    997     }
    998 
    999     /**
    1000      * @ticket 31168
    1001      */
    1002     public function test_wp_insert_post_cpt_default_comment_ping_status_closed() {
    1003         $post_type = rand_str( 20 );
    1004         register_post_type( $post_type );
    1005 
    1006         $post_id = self::factory()->post->create(
    1007             array(
    1008                 'post_author'  => self::$editor_id,
    1009                 'post_status'  => 'publish',
    1010                 'post_content' => rand_str(),
    1011                 'post_title'   => rand_str(),
    1012                 'post_type'    => $post_type,
    1013             )
    1014         );
    1015         $post    = get_post( $post_id );
    1016 
    1017         _unregister_post_type( $post_type );
    1018 
    1019         $this->assertSame( 'closed', $post->comment_status );
    1020         $this->assertSame( 'closed', $post->ping_status );
    1021     }
    1022 
    1023     /**
    1024315     * If a post is sticky and is updated by a user that does not have the publish_post capability,
    1025316     * it should _stay_ sticky.
     
    1128419    }
    1129420
    1130     /**
    1131      * If a post is updated without providing a post_name param,
    1132      * a new slug should not be generated.
    1133      *
    1134      * @ticket 34865
    1135      */
    1136     public function test_post_updates_without_slug_provided() {
    1137         $post_id = self::factory()->post->create(
    1138             array(
    1139                 'post_title'  => 'Stuff',
    1140                 'post_status' => 'publish',
    1141             )
    1142         );
    1143 
    1144         $data = array(
    1145             'ID'         => $post_id,
    1146             'post_title' => 'Stuff and Things',
    1147         );
    1148 
    1149         wp_insert_post( $data );
    1150 
    1151         $updated_post = get_post( $post_id );
    1152         // Ensure changing the post_title didn't modify the post_name.
    1153         $this->assertSame( 'stuff', $updated_post->post_name );
    1154     }
    1155 
    1156     /**
    1157      * @ticket 32585
    1158      */
    1159     public function test_wp_insert_post_author_zero() {
    1160         $post_id = self::factory()->post->create( array( 'post_author' => 0 ) );
    1161 
    1162         $this->assertEquals( 0, get_post( $post_id )->post_author );
    1163     }
    1164 
    1165     /**
    1166      * @ticket 32585
    1167      */
    1168     public function test_wp_insert_post_author_null() {
    1169         $post_id = self::factory()->post->create( array( 'post_author' => null ) );
    1170 
    1171         $this->assertEquals( self::$editor_id, get_post( $post_id )->post_author );
    1172     }
    1173 
    1174     /**
    1175      * @ticket 15946
    1176      */
    1177     public function test_wp_insert_post_should_respect_post_date_gmt() {
    1178         $data = array(
    1179             'post_author'   => self::$editor_id,
    1180             'post_status'   => 'publish',
    1181             'post_content'  => 'content',
    1182             'post_title'    => 'title',
    1183             'post_date_gmt' => '2014-01-01 12:00:00',
    1184         );
    1185 
    1186         // Insert a post and make sure the ID is OK.
    1187         $post_id = wp_insert_post( $data );
    1188 
    1189         $post = get_post( $post_id );
    1190 
    1191         $this->assertSame( $data['post_content'], $post->post_content );
    1192         $this->assertSame( $data['post_title'], $post->post_title );
    1193         $this->assertEquals( $data['post_author'], $post->post_author );
    1194         $this->assertSame( get_date_from_gmt( $data['post_date_gmt'] ), $post->post_date );
    1195         $this->assertSame( $data['post_date_gmt'], $post->post_date_gmt );
    1196     }
    1197 
    1198421    public function test_wp_delete_post_reassign_hierarchical_post_type() {
    1199422        $grandparent_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
     
    1221444
    1222445    /**
    1223      * Test ensuring that the post_name (UUID) is preserved when wp_insert_post()/wp_update_post() is called.
    1224      *
    1225      * @see _wp_customize_changeset_filter_insert_post_data()
    1226      * @ticket 30937
    1227      */
    1228     public function test_wp_insert_post_for_customize_changeset_should_not_drop_post_name() {
    1229         $this->assertSame( 10, has_filter( 'wp_insert_post_data', '_wp_customize_changeset_filter_insert_post_data' ) );
    1230 
    1231         $changeset_data = array(
    1232             'blogname' => array(
    1233                 'value' => 'Hello World',
    1234             ),
    1235         );
    1236 
    1237         wp_set_current_user( $this->factory()->user->create( array( 'role' => 'contributor' ) ) );
    1238 
    1239         $uuid    = wp_generate_uuid4();
    1240         $post_id = wp_insert_post(
    1241             array(
    1242                 'post_type'    => 'customize_changeset',
    1243                 'post_name'    => strtoupper( $uuid ),
    1244                 'post_content' => wp_json_encode( $changeset_data ),
    1245             )
    1246         );
    1247         $this->assertSame( $uuid, get_post( $post_id )->post_name, 'Expected lower-case UUID4 to be inserted.' );
    1248         $this->assertSame( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) );
    1249 
    1250         $changeset_data['blogname']['value'] = 'Hola Mundo';
    1251         wp_update_post(
    1252             array(
    1253                 'ID'           => $post_id,
    1254                 'post_status'  => 'draft',
    1255                 'post_content' => wp_json_encode( $changeset_data ),
    1256             )
    1257         );
    1258         $this->assertSame( $uuid, get_post( $post_id )->post_name, 'Expected post_name to not have been dropped for drafts.' );
    1259         $this->assertSame( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) );
    1260 
    1261         $changeset_data['blogname']['value'] = 'Hallo Welt';
    1262         wp_update_post(
    1263             array(
    1264                 'ID'           => $post_id,
    1265                 'post_status'  => 'pending',
    1266                 'post_content' => wp_json_encode( $changeset_data ),
    1267             )
    1268         );
    1269         $this->assertSame( $uuid, get_post( $post_id )->post_name, 'Expected post_name to not have been dropped for pending.' );
    1270         $this->assertSame( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) );
    1271     }
    1272 
    1273     /**
    1274446     * Test ensuring that the post_slug can be filtered with a custom value short circuiting the built in
    1275447     * function that tries to create a unique name based on the post name.
     
    1296468    public function filter_pre_wp_unique_post_slug( $default, $slug, $post_ID, $post_status, $post_type, $post_parent ) {
    1297469        return 'override-slug-' . $post_type;
    1298     }
    1299 
    1300     /**
    1301      * @ticket 48113
    1302      */
    1303     public function test_insert_post_should_respect_date_floating_post_status_arg() {
    1304         register_post_status( 'floating', array( 'date_floating' => true ) );
    1305 
    1306         $post_id = self::factory()->post->create(
    1307             array(
    1308                 'post_status'   => 'floating',
    1309                 'post_date'     => null,
    1310                 'post_date_gmt' => null,
    1311             )
    1312         );
    1313 
    1314         $post = get_post( $post_id );
    1315         self::assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    1316     }
    1317 
    1318     /**
    1319      * @ticket 48113
    1320      */
    1321     public function test_insert_post_should_respect_date_floating_post_status_arg_not_set() {
    1322         register_post_status( 'not-floating', array( 'date_floating' => false ) );
    1323 
    1324         $post_id = self::factory()->post->create(
    1325             array(
    1326                 'post_status'   => 'floating',
    1327                 'post_date'     => null,
    1328                 'post_date_gmt' => null,
    1329             )
    1330         );
    1331 
    1332         $post = get_post( $post_id );
    1333         self::assertEqualsWithDelta(
    1334             strtotime( gmdate( 'Y-m-d H:i:s' ) ),
    1335             strtotime( $post->post_date_gmt ),
    1336             2,
    1337             'The dates should be equal'
    1338         );
    1339     }
    1340 
    1341     /**
    1342      * Test ensuring that wp_update_post() does not unintentionally modify post tags
    1343      * if the post has several tags with the same name but different slugs.
    1344      *
    1345      * Tags should only be modified if 'tags_input' parameter was explicitly provided,
    1346      * and is different from the existing tags.
    1347      *
    1348      * @ticket 45121
    1349      */
    1350     public function test_update_post_should_only_modify_post_tags_if_different_tags_input_was_provided() {
    1351         $tag_1 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_1' ) );
    1352         $tag_2 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_2' ) );
    1353         $tag_3 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_3' ) );
    1354 
    1355         $post_id = self::factory()->post->create(
    1356             array(
    1357                 'tags_input' => array( $tag_1['term_id'], $tag_2['term_id'] ),
    1358             )
    1359         );
    1360 
    1361         $post = get_post( $post_id );
    1362 
    1363         $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
    1364         $this->assertSameSets( array( $tag_1['term_id'], $tag_2['term_id'] ), $tags );
    1365 
    1366         wp_update_post( $post );
    1367 
    1368         $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
    1369         $this->assertSameSets( array( $tag_1['term_id'], $tag_2['term_id'] ), $tags );
    1370 
    1371         wp_update_post(
    1372             array(
    1373                 'ID'         => $post->ID,
    1374                 'tags_input' => array( $tag_2['term_id'], $tag_3['term_id'] ),
    1375             )
    1376         );
    1377 
    1378         $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
    1379         $this->assertSameSets( array( $tag_2['term_id'], $tag_3['term_id'] ), $tags );
    1380     }
    1381 
    1382     /**
    1383      * @ticket 52187
    1384      */
    1385     public function test_insert_empty_post_date() {
    1386         $post_date_gmt = '2020-12-29 10:11:45';
    1387         $invalid_date  = '2020-12-41 14:15:27';
    1388 
    1389         // Empty post_date_gmt with floating status
    1390         $post_id = self::factory()->post->create(
    1391             array(
    1392                 'post_status' => 'draft',
    1393             )
    1394         );
    1395         $post    = get_post( $post_id );
    1396         $this->assertEqualsWithDelta(
    1397             strtotime( gmdate( 'Y-m-d H:i:s' ) ),
    1398             strtotime( $post->post_date ),
    1399             2,
    1400             'The dates should be equal'
    1401         );
    1402         $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    1403 
    1404         $post_id = self::factory()->post->create(
    1405             array(
    1406                 'post_date_gmt' => '0000-00-00 00:00:00',
    1407                 'post_status'   => 'draft',
    1408             )
    1409         );
    1410         $post    = get_post( $post_id );
    1411         $this->assertEqualsWithDelta(
    1412             strtotime( gmdate( 'Y-m-d H:i:s' ) ),
    1413             strtotime( $post->post_date ),
    1414             2,
    1415             'The dates should be equal'
    1416         );
    1417         $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    1418 
    1419         // Empty post_date_gmt without floating status
    1420         $post_id = self::factory()->post->create(
    1421             array(
    1422                 'post_status' => 'publish',
    1423             )
    1424         );
    1425         $post    = get_post( $post_id );
    1426         $this->assertEqualsWithDelta(
    1427             strtotime( gmdate( 'Y-m-d H:i:s' ) ),
    1428             strtotime( $post->post_date ),
    1429             2,
    1430             'The dates should be equal'
    1431         );
    1432         $this->assertEqualsWithDelta(
    1433             strtotime( gmdate( 'Y-m-d H:i:s' ) ),
    1434             strtotime( get_gmt_from_date( $post->post_date ) ),
    1435             2,
    1436             'The dates should be equal'
    1437         );
    1438 
    1439         $post_id = self::factory()->post->create(
    1440             array(
    1441                 'post_date_gmt' => '0000-00-00 00:00:00',
    1442                 'post_status'   => 'publish',
    1443             )
    1444         );
    1445         $post    = get_post( $post_id );
    1446         $this->assertEqualsWithDelta(
    1447             strtotime( gmdate( 'Y-m-d H:i:s' ) ),
    1448             strtotime( $post->post_date ),
    1449             2,
    1450             'The dates should be equal'
    1451         );
    1452         $this->assertEqualsWithDelta(
    1453             strtotime( gmdate( 'Y-m-d H:i:s' ) ),
    1454             strtotime( get_gmt_from_date( $post->post_date ) ),
    1455             2,
    1456             'The dates should be equal'
    1457         );
    1458 
    1459         // Valid post_date_gmt
    1460         $post_id = self::factory()->post->create(
    1461             array(
    1462                 'post_date_gmt' => $post_date_gmt,
    1463             )
    1464         );
    1465         $post    = get_post( $post_id );
    1466         $this->assertSame( get_date_from_gmt( $post_date_gmt ), $post->post_date );
    1467         $this->assertSame( $post_date_gmt, $post->post_date_gmt );
    1468 
    1469         // Invalid post_date_gmt
    1470         $post_id = self::factory()->post->create(
    1471             array(
    1472                 'post_date_gmt' => $invalid_date,
    1473             )
    1474         );
    1475         $post    = get_post( $post_id );
    1476         $this->assertSame( '1970-01-01 00:00:00', $post->post_date );
    1477         $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    1478     }
    1479 
    1480     /**
    1481      * @ticket 52187
    1482      */
    1483     public function test_insert_valid_post_date() {
    1484         $post_date     = '2020-12-28 11:26:35';
    1485         $post_date_gmt = '2020-12-29 10:11:45';
    1486         $invalid_date  = '2020-12-41 14:15:27';
    1487 
    1488         // Empty post_date_gmt with floating status
    1489         $post_id = self::factory()->post->create(
    1490             array(
    1491                 'post_date'   => $post_date,
    1492                 'post_status' => 'draft',
    1493             )
    1494         );
    1495         $post    = get_post( $post_id );
    1496         $this->assertSame( $post_date, $post->post_date );
    1497         $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    1498 
    1499         $post_id = self::factory()->post->create(
    1500             array(
    1501                 'post_date'     => $post_date,
    1502                 'post_date_gmt' => '0000-00-00 00:00:00',
    1503                 'post_status'   => 'draft',
    1504             )
    1505         );
    1506         $post    = get_post( $post_id );
    1507         $this->assertSame( $post_date, $post->post_date );
    1508         $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    1509 
    1510         // Empty post_date_gmt without floating status
    1511         $post_id = self::factory()->post->create(
    1512             array(
    1513                 'post_date'   => $post_date,
    1514                 'post_status' => 'publish',
    1515             )
    1516         );
    1517         $post    = get_post( $post_id );
    1518         $this->assertSame( $post_date, $post->post_date );
    1519         $this->assertSame( get_gmt_from_date( $post_date ), $post->post_date_gmt );
    1520 
    1521         $post_id = self::factory()->post->create(
    1522             array(
    1523                 'post_date'     => $post_date,
    1524                 'post_date_gmt' => '0000-00-00 00:00:00',
    1525                 'post_status'   => 'publish',
    1526             )
    1527         );
    1528         $post    = get_post( $post_id );
    1529         $this->assertSame( $post_date, $post->post_date );
    1530         $this->assertSame( get_gmt_from_date( $post_date ), $post->post_date_gmt );
    1531 
    1532         // Valid post_date_gmt
    1533         $post_id = self::factory()->post->create(
    1534             array(
    1535                 'post_date'     => $post_date,
    1536                 'post_date_gmt' => $post_date_gmt,
    1537             )
    1538         );
    1539         $post    = get_post( $post_id );
    1540         $this->assertSame( $post_date, $post->post_date );
    1541         $this->assertSame( $post_date_gmt, $post->post_date_gmt );
    1542 
    1543         // Invalid post_date_gmt
    1544         $post_id = self::factory()->post->create(
    1545             array(
    1546                 'post_date'     => $post_date,
    1547                 'post_date_gmt' => $invalid_date,
    1548             )
    1549         );
    1550         $post    = get_post( $post_id );
    1551         $this->assertSame( $post_date, $post->post_date );
    1552         $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    1553     }
    1554 
    1555     /**
    1556      * @ticket 52187
    1557      */
    1558     public function test_insert_invalid_post_date() {
    1559         $post_date     = '2020-12-28 11:26:35';
    1560         $post_date_gmt = '2020-12-29 10:11:45';
    1561         $invalid_date  = '2020-12-41 14:15:27';
    1562 
    1563         // Empty post_date_gmt with floating status
    1564         $post_id = self::factory()->post->create(
    1565             array(
    1566                 'post_date'   => $invalid_date,
    1567                 'post_status' => 'draft',
    1568             )
    1569         );
    1570         $this->assertSame( 0, $post_id );
    1571 
    1572         $post_id = self::factory()->post->create(
    1573             array(
    1574                 'post_date'     => $invalid_date,
    1575                 'post_date_gmt' => '0000-00-00 00:00:00',
    1576                 'post_status'   => 'draft',
    1577             )
    1578         );
    1579         $this->assertSame( 0, $post_id );
    1580 
    1581         // Empty post_date_gmt without floating status
    1582         $post_id = self::factory()->post->create(
    1583             array(
    1584                 'post_date'   => $invalid_date,
    1585                 'post_status' => 'publish',
    1586             )
    1587         );
    1588         $this->assertSame( 0, $post_id );
    1589 
    1590         $post_id = self::factory()->post->create(
    1591             array(
    1592                 'post_date'     => $invalid_date,
    1593                 'post_date_gmt' => '0000-00-00 00:00:00',
    1594                 'post_status'   => 'publish',
    1595             )
    1596         );
    1597         $this->assertSame( 0, $post_id );
    1598 
    1599         // Valid post_date_gmt
    1600         $post_id = self::factory()->post->create(
    1601             array(
    1602                 'post_date'     => $invalid_date,
    1603                 'post_date_gmt' => $post_date_gmt,
    1604             )
    1605         );
    1606         $this->assertSame( 0, $post_id );
    1607 
    1608         // Invalid post_date_gmt
    1609         $post_id = self::factory()->post->create(
    1610             array(
    1611                 'post_date'     => $invalid_date,
    1612                 'post_date_gmt' => $invalid_date,
    1613             )
    1614         );
    1615         $this->assertSame( 0, $post_id );
    1616470    }
    1617471
  • trunk/tests/phpunit/tests/post/wpInsertPost.php

    r52010 r53785  
    33/**
    44 * @group post
     5 * @covers ::wp_insert_post
    56 */
    67class Tests_Post_wpInsertPost extends WP_UnitTestCase {
     
    89    protected static $user_ids = array(
    910        'administrator' => null,
     11        'editor'        => null,
    1012        'contributor'   => null,
    1113    );
     
    1820                )
    1921            ),
     22            'editor'        => $factory->user->create(
     23                array(
     24                    'role' => 'editor',
     25                )
     26            ),
    2027            'contributor'   => $factory->user->create(
    2128                array(
     
    6774
    6875    /**
     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    /**
    691224     * @ticket 11863
    701225     */
Note: See TracChangeset for help on using the changeset viewer.