Make WordPress Core


Ignore:
Timestamp:
07/27/2022 02:59:12 PM (4 years 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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.