Make WordPress Core


Ignore:
Timestamp:
07/26/2022 01:53:19 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Bring some modernization to wp_insert_post() tests.

Includes:

  • Using assertIsInt() instead of assertIsNumeric() for post IDs.
  • Using consistent variable names for post data and retrieved posts.
  • Removing added filters before performing assertions, not after.
  • Removing unused Tests_Post::$post_ids property.
  • Correcting the order of arguments in some assertions.
  • Converting some foreach() loops to data providers.
  • Wrapping long lines for better readability.

Follow-up to [13/tests], [14/tests], [167/tests], [496/tests], [1174/tests], [1246/tests], [1287/tests], [1307/tests], [1326/tests], [30510], [33261], [33630], [34762], [46279], [50012], [51438].

See #55652.

File:
1 edited

Legend:

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

    r53771 r53782  
    3636                wp_set_current_user( self::$editor_id );
    3737                _set_cron_array( array() );
    38 
    39                 $this->post_ids = array();
    4038        }
    4139
    … …  
    4341         * Helper function: return the timestamp(s) of cron jobs for the specified hook and post.
    4442         */
    45         private function next_schedule_for_post( $hook, $id ) {
    46                 return wp_next_scheduled( 'publish_future_post', array( 0 => (int) $id ) );
     43        private function next_schedule_for_post( $hook, $post_id ) {
     44                return wp_next_scheduled( 'publish_future_post', array( 0 => (int) $post_id ) );
    4745        }
    4846
    … …  
    5957        /**
    6058         * Test simple valid behavior: insert and get a post.
    61          */
    62         public function test_vb_insert_get_delete() {
    63                 register_post_type( 'cpt', array( 'taxonomies' => array( 'post_tag', 'ctax' ) ) );
     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                );
    6469                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() {
    65124                $post_types = array( 'post', 'cpt' );
    66125
    67                 foreach ( $post_types as $post_type ) {
    68                         $post = array(
    69                                 'post_author'  => self::$editor_id,
    70                                 'post_status'  => 'publish',
    71                                 'post_content' => "{$post_type}_content",
    72                                 'post_title'   => "{$post_type}_title",
    73                                 'tax_input'    => array(
    74                                         'post_tag' => 'tag1,tag2',
    75                                         'ctax'     => 'cterm1,cterm2',
    76                                 ),
    77                                 'post_type'    => $post_type,
    78                         );
    79 
    80                         // Insert a post and make sure the ID is OK.
    81                         $id = wp_insert_post( $post );
    82                         $this->assertIsNumeric( $id );
    83                         $this->assertGreaterThan( 0, $id );
    84 
    85                         // Fetch the post and make sure it matches.
    86                         $out = get_post( $id );
    87 
    88                         $this->assertSame( $post['post_content'], $out->post_content );
    89                         $this->assertSame( $post['post_title'], $out->post_title );
    90                         $this->assertSame( $post['post_status'], $out->post_status );
    91                         $this->assertEquals( $post['post_author'], $out->post_author );
    92 
    93                         // Test cache state.
    94                         $pcache = wp_cache_get( $id, 'posts' );
    95                         $this->assertInstanceOf( 'stdClass', $pcache );
    96                         $this->assertSame( $id, $pcache->ID );
    97 
    98                         update_object_term_cache( $id, $post_type );
    99                         $tcache = wp_cache_get( $id, 'post_tag_relationships' );
    100                         $this->assertIsArray( $tcache );
    101                         $this->assertCount( 2, $tcache );
    102 
    103                         $tcache = wp_cache_get( $id, 'ctax_relationships' );
    104                         if ( 'cpt' === $post_type ) {
    105                                 $this->assertIsArray( $tcache );
    106                                 $this->assertCount( 2, $tcache );
    107                         } else {
    108                                 $this->assertFalse( $tcache );
    109                         }
    110 
    111                         wp_delete_post( $id, true );
    112                         $this->assertFalse( wp_cache_get( $id, 'posts' ) );
    113                         $this->assertFalse( wp_cache_get( $id, 'post_tag_relationships' ) );
    114                         $this->assertFalse( wp_cache_get( $id, 'ctax_relationships' ) );
    115                 }
    116 
    117                 $GLOBALS['wp_taxonomies']['post_tag']->object_type = array( 'post' );
     126                return $this->text_array_to_dataprovider( $post_types );
    118127        }
    119128
    … …  
    124133                $future_date = strtotime( '+1 day' );
    125134
    126                 $post = array(
     135                $data = array(
    127136                        'post_author'  => self::$editor_id,
    128137                        'post_status'  => 'publish',
    … …  
    133142
    134143                // Insert a post and make sure the ID is OK.
    135                 $id               = wp_insert_post( $post );
    136                 $this->post_ids[] = $id;
    137                 // dmp( _get_cron_array() );
    138                 $this->assertIsNumeric( $id );
    139                 $this->assertGreaterThan( 0, $id );
     144                $post_id = wp_insert_post( $data );
     145                $this->assertIsInt( $post_id );
     146                $this->assertGreaterThan( 0, $post_id );
    140147
    141148                // Fetch the post and make sure it matches.
    142                 $out = get_post( $id );
    143 
    144                 $this->assertSame( $post['post_content'], $out->post_content );
    145                 $this->assertSame( $post['post_title'], $out->post_title );
    146                 $this->assertSame( 'future', $out->post_status );
    147                 $this->assertEquals( $post['post_author'], $out->post_author );
    148                 $this->assertSame( $post['post_date'], $out->post_date );
     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 );
    149156
    150157                // There should be a publish_future_post hook scheduled on the future date.
    151                 $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $id ) );
     158                $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    152159        }
    153160
    … …  
    160167                $future_date_2 = strtotime( 'Jan 11th +1 year' );
    161168
    162                 $post = array(
     169                $data = array(
    163170                        'post_author'  => self::$editor_id,
    164171                        'post_status'  => 'publish',
    … …  
    169176
    170177                // Insert a post and make sure the ID is OK.
    171                 $id               = wp_insert_post( $post );
    172                 $this->post_ids[] = $id;
     178                $post_id = wp_insert_post( $data );
    173179
    174180                // Fetch the post and make sure has the correct date and status.
    175                 $out = get_post( $id );
    176                 $this->assertSame( 'future', $out->post_status );
    177                 $this->assertSame( $post['post_date'], $out->post_date );
     181                $post = get_post( $post_id );
     182                $this->assertSame( 'future', $post->post_status );
     183                $this->assertSame( $data['post_date'], $post->post_date );
    178184
    179185                // Check that there's a publish_future_post job scheduled at the right time.
    180                 $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $id ) );
     186                $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    181187
    182188                // Now save it again with a date further in the future.
    183 
    184                 $post['ID']            = $id;
    185                 $post['post_date']     = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' );
    186                 $post['post_date_gmt'] = null;
    187                 wp_update_post( $post );
     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 );
    188193
    189194                // Fetch the post again and make sure it has the new post_date.
    190                 $out = get_post( $id );
    191                 $this->assertSame( 'future', $out->post_status );
    192                 $this->assertSame( $post['post_date'], $out->post_date );
     195                $post = get_post( $post_id );
     196                $this->assertSame( 'future', $post->post_status );
     197                $this->assertSame( $data['post_date'], $post->post_date );
    193198
    194199                // And the correct date on the cron job.
    195                 $this->assertSame( $future_date_2, $this->next_schedule_for_post( 'publish_future_post', $id ) );
     200                $this->assertSame( $future_date_2, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    196201        }
    197202
    … …  
    205210                $future_date_2 = strtotime( '+2 day' );
    206211
    207                 $post = array(
     212                $data = array(
    208213                        'post_author'  => self::$editor_id,
    209214                        'post_status'  => 'publish',
    … …  
    214219
    215220                // Insert a post and make sure the ID is OK.
    216                 $id               = wp_insert_post( $post );
    217                 $this->post_ids[] = $id;
     221                $post_id = wp_insert_post( $data );
    218222
    219223                // Fetch the post and make sure has the correct date and status.
    220                 $out = get_post( $id );
    221                 $this->assertSame( 'future', $out->post_status );
    222                 $this->assertSame( $post['post_date'], $out->post_date );
     224                $post = get_post( $post_id );
     225                $this->assertSame( 'future', $post->post_status );
     226                $this->assertSame( $data['post_date'], $post->post_date );
    223227
    224228                // Check that there's a publish_future_post job scheduled at the right time.
    225                 $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $id ) );
     229                $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    226230
    227231                // Now save it again with a date further in the future.
    228 
    229                 $post['ID']            = $id;
    230                 $post['post_date']     = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' );
    231                 $post['post_date_gmt'] = null;
    232                 wp_update_post( $post );
     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 );
    233236
    234237                // Fetch the post again and make sure it has the new post_date.
    235                 $out = get_post( $id );
    236                 $this->assertSame( 'future', $out->post_status );
    237                 $this->assertSame( $post['post_date'], $out->post_date );
     238                $post = get_post( $post_id );
     239                $this->assertSame( 'future', $post->post_status );
     240                $this->assertSame( $data['post_date'], $post->post_date );
    238241
    239242                // And the correct date on the cron job.
    240                 $this->assertSame( $future_date_2, $this->next_schedule_for_post( 'publish_future_post', $id ) );
     243                $this->assertSame( $future_date_2, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    241244        }
    242245
    … …  
    247250                $future_date = strtotime( '+1 day' );
    248251
    249                 $post = array(
     252                $data = array(
    250253                        'post_author'  => self::$editor_id,
    251254                        'post_status'  => 'draft',
    … …  
    256259
    257260                // Insert a post and make sure the ID is OK.
    258                 $id               = wp_insert_post( $post );
    259                 $this->post_ids[] = $id;
    260                 // dmp( _get_cron_array() );
    261                 $this->assertIsNumeric( $id );
    262                 $this->assertGreaterThan( 0, $id );
     261                $post_id = wp_insert_post( $data );
     262                $this->assertIsInt( $post_id );
     263                $this->assertGreaterThan( 0, $post_id );
    263264
    264265                // Fetch the post and make sure it matches.
    265                 $out = get_post( $id );
    266 
    267                 $this->assertSame( $post['post_content'], $out->post_content );
    268                 $this->assertSame( $post['post_title'], $out->post_title );
    269                 $this->assertSame( 'draft', $out->post_status );
    270                 $this->assertEquals( $post['post_author'], $out->post_author );
    271                 $this->assertSame( $post['post_date'], $out->post_date );
     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 );
    272273
    273274                // There should be a publish_future_post hook scheduled on the future date.
    274                 $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $id ) );
     275                $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    275276
    276277        }
    … …  
    282283                $future_date_1 = strtotime( '+1 day' );
    283284
    284                 $post = array(
     285                $data = array(
    285286                        'post_author'  => self::$editor_id,
    286287                        'post_status'  => 'publish',
    … …  
    291292
    292293                // Insert a post and make sure the ID is OK.
    293                 $id               = wp_insert_post( $post );
    294                 $this->post_ids[] = $id;
     294                $post_id = wp_insert_post( $data );
    295295
    296296                // Fetch the post and make sure has the correct date and status.
    297                 $out = get_post( $id );
    298                 $this->assertSame( 'future', $out->post_status );
    299                 $this->assertSame( $post['post_date'], $out->post_date );
     297                $post = get_post( $post_id );
     298                $this->assertSame( 'future', $post->post_status );
     299                $this->assertSame( $data['post_date'], $post->post_date );
    300300
    301301                // 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', $id ) );
     302                $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    303303
    304304                // Now save it again with status set to draft.
    305 
    306                 $post['ID']          = $id;
    307                 $post['post_status'] = 'draft';
    308                 wp_update_post( $post );
     305                $data['ID']          = $post_id;
     306                $data['post_status'] = 'draft';
     307                wp_update_post( $data );
    309308
    310309                // Fetch the post again and make sure it has the new post_date.
    311                 $out = get_post( $id );
    312                 $this->assertSame( 'draft', $out->post_status );
    313                 $this->assertSame( $post['post_date'], $out->post_date );
     310                $post = get_post( $post_id );
     311                $this->assertSame( 'draft', $post->post_status );
     312                $this->assertSame( $data['post_date'], $post->post_date );
    314313
    315314                // And the correct date on the cron job.
    316                 $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $id ) );
     315                $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    317316        }
    318317
    319318        /**
    320319         * Insert a future post, then edit and change the status, and make sure cron gets it right.
    321          */
    322         public function test_vb_insert_future_change_status() {
     320         *
     321         * @dataProvider data_vb_insert_future_change_status
     322         */
     323        public function test_vb_insert_future_change_status( $status ) {
    323324                $future_date_1 = strtotime( '+1 day' );
    324325
    325                 $statuses = array( 'draft', 'static', 'object', 'attachment', 'inherit', 'pending' );
    326 
    327                 foreach ( $statuses as $status ) {
    328                         $post = array(
    329                                 'post_author'  => self::$editor_id,
    330                                 'post_status'  => 'publish',
    331                                 'post_content' => "{$status}_content",
    332                                 'post_title'   => "{$status}_title",
    333                                 'post_date'    => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
    334                         );
    335 
    336                         // Insert a post and make sure the ID is OK.
    337                         $id               = wp_insert_post( $post );
    338                         $this->post_ids[] = $id;
    339 
    340                         // Fetch the post and make sure has the correct date and status.
    341                         $out = get_post( $id );
    342                         $this->assertSame( 'future', $out->post_status );
    343                         $this->assertSame( $post['post_date'], $out->post_date );
    344 
    345                         // Check that there's a publish_future_post job scheduled at the right time.
    346                         $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $id ) );
    347 
    348                         // Now save it again with status changed.
    349 
    350                         $post['ID']          = $id;
    351                         $post['post_status'] = $status;
    352                         wp_update_post( $post );
    353 
    354                         // Fetch the post again and make sure it has the new post_date.
    355                         $out = get_post( $id );
    356                         $this->assertSame( $status, $out->post_status );
    357                         $this->assertSame( $post['post_date'], $out->post_date );
    358 
    359                         // And the correct date on the cron job.
    360                         $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $id ) );
    361                 }
     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 );
    362370        }
    363371
    … …  
    368376                $future_date = strtotime( '+1 day' );
    369377
    370                 $post = array(
     378                $data = array(
    371379                        'post_author'  => self::$editor_id,
    372380                        'post_status'  => 'private',
    … …  
    377385
    378386                // Insert a post and make sure the ID is OK.
    379                 $id               = wp_insert_post( $post );
    380                 $this->post_ids[] = $id;
    381                 // dmp( _get_cron_array() );
    382                 $this->assertIsNumeric( $id );
    383                 $this->assertGreaterThan( 0, $id );
     387                $post_id = wp_insert_post( $data );
     388                $this->assertIsInt( $post_id );
     389                $this->assertGreaterThan( 0, $post_id );
    384390
    385391                // Fetch the post and make sure it matches.
    386                 $out = get_post( $id );
    387 
    388                 $this->assertSame( $post['post_content'], $out->post_content );
    389                 $this->assertSame( $post['post_title'], $out->post_title );
    390                 $this->assertSame( 'private', $out->post_status );
    391                 $this->assertEquals( $post['post_author'], $out->post_author );
    392                 $this->assertSame( $post['post_date'], $out->post_date );
     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 );
    393399
    394400                // There should be a publish_future_post hook scheduled on the future date.
    395                 $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $id ) );
     401                $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    396402        }
    397403
    … …  
    402408         */
    403409        public function test_vb_insert_invalid_date() {
    404                 $post = array(
     410                $data = array(
    405411                        'post_author'  => self::$editor_id,
    406412                        'post_status'  => 'publish',
    … …  
    411417
    412418                // Test both return paths with or without WP_Error.
    413                 $insert_post = wp_insert_post( $post, true );
    414                 $this->assertWPError( $insert_post );
    415                 $this->assertSame( 'invalid_date', $insert_post->get_error_code() );
    416 
    417                 $insert_post = wp_insert_post( $post );
    418                 $this->assertSame( 0, $insert_post );
     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 );
    419425        }
    420426
    … …  
    425431                $future_date_1 = strtotime( '+1 day' );
    426432
    427                 $post = array(
     433                $data = array(
    428434                        'post_author'  => self::$editor_id,
    429435                        'post_status'  => 'publish',
    … …  
    434440
    435441                // Insert a post and make sure the ID is OK.
    436                 $id               = wp_insert_post( $post );
    437                 $this->post_ids[] = $id;
     442                $post_id = wp_insert_post( $data );
    438443
    439444                // Fetch the post and make sure has the correct date and status.
    440                 $out = get_post( $id );
    441                 $this->assertSame( 'future', $out->post_status );
    442                 $this->assertSame( $post['post_date'], $out->post_date );
     445                $post = get_post( $post_id );
     446                $this->assertSame( 'future', $post->post_status );
     447                $this->assertSame( $data['post_date'], $post->post_date );
    443448
    444449                // Check that there's a publish_future_post job scheduled at the right time.
    445                 $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $id ) );
     450                $this->assertSame( $future_date_1, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    446451
    447452                // Now save it again with status set to draft.
    448 
    449                 $post['ID']          = $id;
    450                 $post['post_status'] = 'private';
    451                 wp_update_post( $post );
     453                $data['ID']          = $post_id;
     454                $data['post_status'] = 'private';
     455                wp_update_post( $data );
    452456
    453457                // Fetch the post again and make sure it has the new post_date.
    454                 $out = get_post( $id );
    455                 $this->assertSame( 'private', $out->post_status );
    456                 $this->assertSame( $post['post_date'], $out->post_date );
     458                $post = get_post( $post_id );
     459                $this->assertSame( 'private', $post->post_status );
     460                $this->assertSame( $data['post_date'], $post->post_date );
    457461
    458462                // And the correct date on the cron job.
    459                 $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $id ) );
     463                $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    460464        }
    461465
    … …  
    466470                $this->set_permalink_structure( '/%postname%/' );
    467471
    468                 $p = wp_insert_post(
     472                $post_id = wp_insert_post(
    469473                        array(
    470474                                'post_title'   => '',
    … …  
    475479                );
    476480
    477                 $post = get_post( $p );
    478 
    479                 $this->set_permalink_structure();
    480 
    481                 $this->assertSame( "$p-2", $post->post_name );
     481                $post = get_post( $post_id );
     482
     483                $this->assertSame( "$post_id-2", $post->post_name );
    482484        }
    483485
    … …  
    489491                register_post_type( 'wptests_pt' );
    490492
    491                 $p = wp_insert_post(
     493                $post_id = wp_insert_post(
    492494                        array(
    493495                                'post_title'  => '',
    … …  
    497499                );
    498500
    499                 $post = get_post( $p );
    500 
    501                 $this->assertStringContainsString( 'wptests_pt=' . $p, $post->guid );
     501                $post = get_post( $post_id );
     502
     503                $this->assertStringContainsString( 'wptests_pt=' . $post_id, $post->guid );
    502504        }
    503505
    … …  
    515517                );
    516518
    517                 $this->assertIsNumeric( $post_id );
     519                $this->assertIsInt( $post_id );
    518520                $this->assertGreaterThan( 0, $post_id );
    519521        }
    … …  
    548550                $future_date = strtotime( '+1 day' );
    549551
    550                 $post = array(
     552                $data = array(
    551553                        'post_author'  => self::$editor_id,
    552554                        'post_status'  => 'publish',
    … …  
    557559
    558560                // Insert a post and make sure the ID is OK.
    559                 $id               = wp_insert_post( $post );
    560                 $this->post_ids[] = $id;
     561                $post_id = wp_insert_post( $data );
    561562
    562563                // Check that there's a publish_future_post job scheduled at the right time.
    563                 $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $id ) );
     564                $this->assertSame( $future_date, $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    564565
    565566                // Now delete the post and make sure the cron entry is removed.
    566                 wp_delete_post( $id );
    567 
    568                 $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $id ) );
     567                wp_delete_post( $post_id );
     568
     569                $this->assertFalse( $this->next_schedule_for_post( 'publish_future_post', $post_id ) );
    569570        }
    570571
    … …  
    579580                $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
    580581
    581                 $post = array(
     582                $data = array(
    582583                        'post_author'  => self::$editor_id,
    583584                        'post_status'  => 'publish',
    … …  
    588589
    589590                // Insert a post and make sure the ID is OK.
    590                 $id               = wp_insert_post( $post );
    591                 $this->post_ids[] = $id;
    592 
    593                 $plink = get_permalink( $id );
     591                $post_id = wp_insert_post( $data );
    594592
    595593                // Permalink should include the post ID at the end.
    596                 $this->assertSame( get_option( 'siteurl' ) . '/2007/10/31/' . $id . '/', $plink );
     594                $expected = get_option( 'siteurl' ) . '/2007/10/31/' . $post_id . '/';
     595                $this->assertSame( $expected, get_permalink( $post_id ) );
    597596        }
    598597
    599598        public function test_wp_publish_post() {
    600                 $draft_id = self::factory()->post->create( array( 'post_status' => 'draft' ) );
     599                $draft_id = self::factory()->post->create(
     600                        array(
     601                                'post_status' => 'draft',
     602                        )
     603                );
    601604
    602605                $post = get_post( $draft_id );
    … …  
    604607
    605608                wp_publish_post( $draft_id );
     609
    606610                $post = get_post( $draft_id );
    607 
    608611                $this->assertSame( 'publish', $post->post_status );
    609612        }
    … …  
    626629
    627630                wp_publish_post( $post_id );
    628                 $post = get_post( $post_id );
    629 
     631
     632                $post = get_post( $post_id );
    630633                $this->assertSame( 'publish', $post->post_status );
    631634                $this->assertSame( $future_date, $post->post_date );
    … …  
    654657                kses_remove_filters();
    655658
    656                 $post_id = wp_insert_post( array( 'post_title' => '<script>Test</script>' ) );
     659                $post_id = wp_insert_post(
     660                        array(
     661                                'post_title' => '<script>Test</script>',
     662                        )
     663                );
    657664                $post    = get_post( $post_id );
    658665                $this->assertSame( '<script>Test</script>', $post->post_title );
    … …  
    667674                        )
    668675                );
     676
     677                kses_remove_filters();
     678
    669679                $post = get_post( $post->ID );
    670680                $this->assertSame( 'Test', $post->post_title );
    671 
    672                 kses_remove_filters();
    673681        }
    674682
    … …  
    679687                kses_remove_filters();
    680688
    681                 $post_id = wp_insert_post( array( 'post_title' => '<script>Test</script>' ) );
     689                $post_id = wp_insert_post(
     690                        array(
     691                                'post_title' => '<script>Test</script>',
     692                        )
     693                );
    682694                $post    = get_post( $post_id );
    683695                $this->assertSame( '<script>Test</script>', $post->post_title );
    … …  
    687699
    688700                wp_publish_post( $post->ID );
     701
     702                kses_remove_filters();
     703
    689704                $post = get_post( $post->ID );
    690705                $this->assertSame( '<script>Test</script>', $post->post_title );
    691 
    692                 kses_remove_filters();
    693706        }
    694707
    … …  
    698711        public function test_get_post_ancestors_within_loop() {
    699712                global $post;
     713
    700714                $parent_id = self::factory()->post->create();
    701                 $post      = self::factory()->post->create_and_get( array( 'post_parent' => $parent_id ) );
     715                $post      = self::factory()->post->create_and_get(
     716                        array(
     717                                'post_parent' => $parent_id,
     718                        )
     719                );
     720
    702721                $this->assertSame( array( $parent_id ), get_post_ancestors( 0 ) );
    703722        }
    … …  
    707726         */
    708727        public function test_update_invalid_post_id() {
    709                 $post_id = self::factory()->post->create( array( 'post_name' => 'get-page-uri-post-name' ) );
     728                $post_id = self::factory()->post->create();
    710729                $post    = get_post( $post_id, ARRAY_A );
    711730
    … …  
    722741        public function test_parse_post_content_single_page() {
    723742                global $multipage, $pages, $numpages;
    724                 $post_id = self::factory()->post->create( array( 'post_content' => 'Page 0' ) );
     743
     744                $post_id = self::factory()->post->create(
     745                        array(
     746                                'post_content' => 'Page 0',
     747                        )
     748                );
    725749                $post    = get_post( $post_id );
    726750                setup_postdata( $post );
     751
    727752                $this->assertSame( 0, $multipage );
    728753                $this->assertCount( 1, $pages );
    … …  
    733758        public function test_parse_post_content_multi_page() {
    734759                global $multipage, $pages, $numpages;
    735                 $post_id = self::factory()->post->create( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
     760
     761                $post_id = self::factory()->post->create(
     762                        array(
     763                                'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3',
     764                        )
     765                );
    736766                $post    = get_post( $post_id );
    737767                setup_postdata( $post );
     768
    738769                $this->assertSame( 1, $multipage );
    739770                $this->assertCount( 4, $pages );
    … …  
    744775        public function test_parse_post_content_remaining_single_page() {
    745776                global $multipage, $pages, $numpages;
    746                 $post_id = self::factory()->post->create( array( 'post_content' => 'Page 0' ) );
     777
     778                $post_id = self::factory()->post->create(
     779                        array(
     780                                'post_content' => 'Page 0',
     781                        )
     782                );
    747783                $post    = get_post( $post_id );
    748784                setup_postdata( $post );
     785
    749786                $this->assertSame( 0, $multipage );
    750787                $this->assertCount( 1, $pages );
    … …  
    755792        public function test_parse_post_content_remaining_multi_page() {
    756793                global $multipage, $pages, $numpages;
    757                 $post_id = self::factory()->post->create( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
     794
     795                $post_id = self::factory()->post->create(
     796                        array(
     797                                'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3',
     798                        )
     799                );
    758800                $post    = get_post( $post_id );
    759801                setup_postdata( $post );
     802
    760803                $this->assertSame( 1, $multipage );
    761804                $this->assertCount( 4, $pages );
    … …  
    769812        public function test_parse_post_content_starting_with_nextpage() {
    770813                global $multipage, $pages, $numpages;
    771                 $post_id = self::factory()->post->create( array( 'post_content' => '<!--nextpage-->Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
     814
     815                $post_id = self::factory()->post->create(
     816                        array(
     817                                'post_content' => '<!--nextpage-->Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3',
     818                        )
     819                );
    772820                $post    = get_post( $post_id );
    773821                setup_postdata( $post );
     822
    774823                $this->assertSame( 1, $multipage );
    775824                $this->assertCount( 4, $pages );
    … …  
    783832        public function test_parse_post_content_starting_with_nextpage_multi() {
    784833                global $multipage, $pages, $numpages;
    785                 $post_id = self::factory()->post->create( array( 'post_content' => '<!--nextpage-->Page 0' ) );
     834
     835                $post_id = self::factory()->post->create(
     836                        array(
     837                                'post_content' => '<!--nextpage-->Page 0',
     838                        )
     839                );
    786840                $post    = get_post( $post_id );
    787841                setup_postdata( $post );
     842
    788843                $this->assertSame( 0, $multipage );
    789844                $this->assertCount( 1, $pages );
    … …  
    800855                register_taxonomy( 'test_tax', 'post' );
    801856
    802                 $title          = 'title';
    803                 $post_data      = array(
     857                $title = 'title';
     858                $data  = array(
    804859                        'post_author'  => self::$editor_id,
    805860                        'post_status'  => 'publish',
    … …  
    810865                        ),
    811866                );
    812                 $insert_post_id = wp_insert_post( $post_data, true, true );
    813                 $this->assertIsInt( $insert_post_id );
    814                 $this->assertGreaterThan( 0, $insert_post_id );
    815 
    816                 $post = get_post( $insert_post_id );
    817                 $this->assertEquals( $post->post_author, self::$editor_id );
    818                 $this->assertSame( $post->post_title, $title );
     867
     868                $post_id = wp_insert_post( $data, true, true );
     869                $this->assertIsInt( $post_id );
     870                $this->assertGreaterThan( 0, $post_id );
     871
     872                $post = get_post( $post_id );
     873                $this->assertEquals( self::$editor_id, $post->post_author );
     874                $this->assertSame( $title, $post->post_title );
    819875        }
    820876
    … …  
    825881                $post_type = rand_str( 20 );
    826882                register_post_type( $post_type );
     883
    827884                self::factory()->post->create(
    828885                        array(
    … …  
    831888                        )
    832889                );
     890
    833891                $count = wp_count_posts( $post_type, 'readable' );
    834892                $this->assertEquals( 1, $count->publish );
     893
    835894                _unregister_post_type( $post_type );
    836                 $this->assertEquals( new stdClass, wp_count_posts( $post_type, 'readable' ) );
     895                $count = wp_count_posts( $post_type, 'readable' );
     896                $this->assertEquals( new stdClass, $count );
    837897        }
    838898
    … …  
    840900                $post_type = rand_str( 20 );
    841901                register_post_type( $post_type );
     902
    842903                self::factory()->post->create_many(
    843904                        3,
    … …  
    847908                        )
    848909                );
     910
    849911                $count1 = wp_count_posts( $post_type, 'readable' );
    850912                $this->assertEquals( 3, $count1->publish );
     913
    851914                add_filter( 'wp_count_posts', array( $this, 'filter_wp_count_posts' ) );
    852 
    853915                $count2 = wp_count_posts( $post_type, 'readable' );
     916                remove_filter( 'wp_count_posts', array( $this, 'filter_wp_count_posts' ) );
    854917                $this->assertEquals( 2, $count2->publish );
    855 
    856                 remove_filter( 'wp_count_posts', array( $this, 'filter_wp_count_posts' ) );
    857918        }
    858919
    … …  
    866927                $initial_counts = wp_count_posts();
    867928
    868                 $key                  = array_rand( $post_ids );
    869                 $_post                = get_post( $post_ids[ $key ], ARRAY_A );
     929                $key   = array_rand( $post_ids );
     930                $_post = get_post( $post_ids[ $key ], ARRAY_A );
     931
    870932                $_post['post_status'] = 'draft';
    871933                wp_insert_post( $_post );
     934
    872935                $post = get_post( $post_ids[ $key ] );
    873936                $this->assertSame( 'draft', $post->post_status );
    … …  
    10261089                $post_type = rand_str( 20 );
    10271090                register_post_type( $post_type, array( 'supports' => array( 'comments', 'trackbacks' ) ) );
     1091
    10281092                $post_id = self::factory()->post->create(
    10291093                        array(
    … …  
    10371101                $post    = get_post( $post_id );
    10381102
     1103                _unregister_post_type( $post_type );
     1104
    10391105                $this->assertSame( 'open', $post->comment_status );
    10401106                $this->assertSame( 'open', $post->ping_status );
    1041                 _unregister_post_type( $post_type );
    10421107        }
    10431108
    … …  
    10481113                $post_type = rand_str( 20 );
    10491114                register_post_type( $post_type );
     1115
    10501116                $post_id = self::factory()->post->create(
    10511117                        array(
    … …  
    10591125                $post    = get_post( $post_id );
    10601126
     1127                _unregister_post_type( $post_type );
     1128
    10611129                $this->assertSame( 'closed', $post->comment_status );
    10621130                $this->assertSame( 'closed', $post->ping_status );
    1063                 _unregister_post_type( $post_type );
    10641131        }
    10651132
    … …  
    11601227                stick_post( $post_id );
    11611228                $this->assertTrue( is_sticky( $post_id ) );
     1229
    11621230                unstick_post( $post_id );
    11631231                $this->assertFalse( is_sticky( $post_id ) );
    … …  
    12181286         */
    12191287        public function test_wp_insert_post_should_respect_post_date_gmt() {
    1220                 $post = array(
     1288                $data = array(
    12211289                        'post_author'   => self::$editor_id,
    12221290                        'post_status'   => 'publish',
    … …  
    12271295
    12281296                // Insert a post and make sure the ID is OK.
    1229                 $id = wp_insert_post( $post );
    1230 
    1231                 $out = get_post( $id );
    1232 
    1233                 $this->assertSame( $post['post_content'], $out->post_content );
    1234                 $this->assertSame( $post['post_title'], $out->post_title );
    1235                 $this->assertEquals( $post['post_author'], $out->post_author );
    1236                 $this->assertSame( get_date_from_gmt( $post['post_date_gmt'] ), $out->post_date );
    1237                 $this->assertSame( $post['post_date_gmt'], $out->post_date_gmt );
     1297                $post_id = wp_insert_post( $data );
     1298
     1299                $post = get_post( $post_id );
     1300
     1301                $this->assertSame( $data['post_content'], $post->post_content );
     1302                $this->assertSame( $data['post_title'], $post->post_title );
     1303                $this->assertEquals( $data['post_author'], $post->post_author );
     1304                $this->assertSame( get_date_from_gmt( $data['post_date_gmt'] ), $post->post_date );
     1305                $this->assertSame( $data['post_date_gmt'], $post->post_date_gmt );
    12381306        }
    12391307
    … …  
    12521320                        )
    12531321                );
     1322
    12541323                $this->assertSame( $parent_page_id, get_post( $page_id )->post_parent );
     1324
    12551325                wp_delete_post( $parent_page_id, true );
    12561326                $this->assertSame( $grandparent_page_id, get_post( $page_id )->post_parent );
     1327
    12571328                wp_delete_post( $grandparent_page_id, true );
    12581329                $this->assertSame( 0, get_post( $page_id )->post_parent );
    … …  
    12661337         */
    12671338        public function test_wp_insert_post_for_customize_changeset_should_not_drop_post_name() {
    1268 
    12691339                $this->assertSame( 10, has_filter( 'wp_insert_post_data', '_wp_customize_changeset_filter_insert_post_data' ) );
    12701340
    … …  
    13711441
    13721442                $post = get_post( $post_id );
    1373                 self::assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date_gmt ), 2, 'The dates should be equal' );
     1443                self::assertEqualsWithDelta(
     1444                        strtotime( gmdate( 'Y-m-d H:i:s' ) ),
     1445                        strtotime( $post->post_date_gmt ),
     1446                        2,
     1447                        'The dates should be equal'
     1448                );
    13741449        }
    13751450
    … …  
    14291504                );
    14301505                $post    = get_post( $post_id );
    1431                 $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
     1506                $this->assertEqualsWithDelta(
     1507                        strtotime( gmdate( 'Y-m-d H:i:s' ) ),
     1508                        strtotime( $post->post_date ),
     1509                        2,
     1510                        'The dates should be equal'
     1511                );
    14321512                $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    14331513
    … …  
    14391519                );
    14401520                $post    = get_post( $post_id );
    1441                 $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
     1521                $this->assertEqualsWithDelta(
     1522                        strtotime( gmdate( 'Y-m-d H:i:s' ) ),
     1523                        strtotime( $post->post_date ),
     1524                        2,
     1525                        'The dates should be equal'
     1526                );
    14421527                $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    14431528
    … …  
    14491534                );
    14501535                $post    = get_post( $post_id );
    1451                 $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
    1452                 $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( get_gmt_from_date( $post->post_date ) ), 2, 'The dates should be equal' );
     1536                $this->assertEqualsWithDelta(
     1537                        strtotime( gmdate( 'Y-m-d H:i:s' ) ),
     1538                        strtotime( $post->post_date ),
     1539                        2,
     1540                        'The dates should be equal'
     1541                );
     1542                $this->assertEqualsWithDelta(
     1543                        strtotime( gmdate( 'Y-m-d H:i:s' ) ),
     1544                        strtotime( get_gmt_from_date( $post->post_date ) ),
     1545                        2,
     1546                        'The dates should be equal'
     1547                );
    14531548
    14541549                $post_id = self::factory()->post->create(
    … …  
    14591554                );
    14601555                $post    = get_post( $post_id );
    1461                 $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
    1462                 $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( get_gmt_from_date( $post->post_date ) ), 2, 'The dates should be equal' );
     1556                $this->assertEqualsWithDelta(
     1557                        strtotime( gmdate( 'Y-m-d H:i:s' ) ),
     1558                        strtotime( $post->post_date ),
     1559                        2,
     1560                        'The dates should be equal'
     1561                );
     1562                $this->assertEqualsWithDelta(
     1563                        strtotime( gmdate( 'Y-m-d H:i:s' ) ),
     1564                        strtotime( get_gmt_from_date( $post->post_date ) ),
     1565                        2,
     1566                        'The dates should be equal'
     1567                );
    14631568
    14641569                // Valid post_date_gmt
    … …  
    16301735
    16311736                $resolved_post_date = wp_resolve_post_date();
    1632                 $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $resolved_post_date ), 2, 'The dates should be equal' );
     1737                $this->assertEqualsWithDelta(
     1738                        strtotime( gmdate( 'Y-m-d H:i:s' ) ),
     1739                        strtotime( $resolved_post_date ),
     1740                        2,
     1741                        'The dates should be equal'
     1742                );
    16331743
    16341744                $resolved_post_date = wp_resolve_post_date( '', $post_date_gmt );
Note: See TracChangeset for help on using the changeset viewer.