Make WordPress Core

Ticket #38883: 38883.2.diff

File 38883.2.diff, 9.4 KB (added by jnylen0, 8 years ago)

Corrected sign of timezone offset; added more tests; other minor cleanup

  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index 3648d9f..5b6cafd 100644
    a b class WP_REST_Posts_Controller extends WP_REST_Controller { 
    13911391                }
    13921392
    13931393                if ( ! empty( $schema['properties']['date_gmt'] ) ) {
    1394                         $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
     1394                        // For drafts, `post_date_gmt` may not be set, indicating that the
     1395                        // date of the draft should be updated each time it is saved (see
     1396                        // #38883).  In this case, shim the value based on the `post_date`
     1397                        // field with the site's timezone offset applied.
     1398                        if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
     1399                                $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_date ) - ( get_option( 'gmt_offset' ) * 3600 ) );
     1400                        } else {
     1401                                $post_date_gmt = $post->post_date_gmt;
     1402                        }
     1403                        $data['date_gmt'] = $this->prepare_date_response( $post_date_gmt );
    13951404                }
    13961405
    13971406                if ( ! empty( $schema['properties']['guid'] ) ) {
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    14071416                }
    14081417
    14091418                if ( ! empty( $schema['properties']['modified_gmt'] ) ) {
    1410                         $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
     1419                        // For drafts, `post_modified_gmt` may not be set (see
     1420                        // `post_date_gmt` comments above).  In this case, shim the value
     1421                        // based on the `post_modified` field with the site's timezone
     1422                        // offset applied.
     1423                        if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) {
     1424                                $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( get_option( 'gmt_offset' ) * 3600 ) );
     1425                        } else {
     1426                                $post_modified_gmt = $post->post_modified_gmt;
     1427                        }
     1428                        $data['modified_gmt'] = $this->prepare_date_response( $post_modified_gmt );
    14111429                }
    14121430
    14131431                if ( ! empty( $schema['properties']['password'] ) ) {
  • tests/phpunit/includes/testcase-rest-post-type-controller.php

    diff --git a/tests/phpunit/includes/testcase-rest-post-type-controller.php b/tests/phpunit/includes/testcase-rest-post-type-controller.php
    index 387d359..8c30d5b 100644
    a b abstract class WP_Test_REST_Post_Type_Controller_Testcase extends WP_Test_REST_C 
    1010                $this->assertEquals( $post->post_name, $data['slug'] );
    1111                $this->assertEquals( get_permalink( $post->ID ), $data['link'] );
    1212                if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
    13                         $this->assertNull( $data['date_gmt'] );
     13                        $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_date ) - ( get_option( 'gmt_offset' ) * 3600 ) );
     14                        $this->assertEquals( mysql_to_rfc3339( $post_date_gmt ), $data['date_gmt'] );
     15                } else {
     16                        $this->assertEquals( mysql_to_rfc3339( $post->post_date_gmt ), $data['date_gmt'] );
    1417                }
    1518                $this->assertEquals( mysql_to_rfc3339( $post->post_date ), $data['date'] );
    1619
    1720                if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) {
    18                         $this->assertNull( $data['modified_gmt'] );
     21                        $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $post->post_modified ) - ( get_option( 'gmt_offset' ) * 3600 ) );
     22                        $this->assertEquals( mysql_to_rfc3339( $post_modified_gmt ), $data['modified_gmt'] );
     23                } else {
     24                        $this->assertEquals( mysql_to_rfc3339( $post->post_modified_gmt ), $data['modified_gmt'] );
    1925                }
    2026                $this->assertEquals( mysql_to_rfc3339( $post->post_modified ), $data['modified'] );
    2127
    abstract class WP_Test_REST_Post_Type_Controller_Testcase extends WP_Test_REST_C 
    139145
    140146                if ( 'edit' === $context ) {
    141147                        $this->assertEquals( $post->guid, $data['guid']['raw'] );
    142 
    143                         if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
    144                                 $this->assertNull( $data['date_gmt'] );
    145                         } else {
    146                                 $this->assertEquals( mysql_to_rfc3339( $post->post_date_gmt ), $data['date_gmt'] );
    147                         }
    148 
    149                         if ( '0000-00-00 00:00:00' === $post->post_modified_gmt ) {
    150                                 $this->assertNull( $data['modified_gmt'] );
    151                         } else {
    152                                 $this->assertEquals( mysql_to_rfc3339( $post->post_modified_gmt ), $data['modified_gmt'] );
    153                         }
    154148                }
    155149
    156150                $taxonomies = wp_list_filter( get_object_taxonomies( $post->post_type, 'objects' ), array( 'show_in_rest' => true ) );
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
    index 6943329..b5c18a0 100644
    a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    12511251                $this->assertEquals( $post_date, $post->post_date );
    12521252
    12531253                $this->assertEquals( $results['date_gmt'], $data['date_gmt'] );
    1254                 // TODO expect null here for drafts (see https://core.trac.wordpress.org/ticket/5698#comment:14)
    12551254                $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
    12561255                $this->assertEquals( $post_date_gmt, $post->post_date_gmt );
    12571256        }
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    13501349
    13511350        public function test_create_post_as_contributor() {
    13521351                wp_set_current_user( self::$contributor_id );
     1352                update_option( 'timezone_string', 'America/Chicago' );
    13531353
    13541354                $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
    1355                 $params = $this->set_post_data(array(
     1355                $params = $this->set_post_data( array(
     1356                        // This results in a special `post_date_gmt` value of
     1357                        // '0000-00-00 00:00:00'.  See #38883.
    13561358                        'status' => 'pending',
    1357                 ));
     1359                ) );
    13581360
    13591361                $request->set_body_params( $params );
    13601362                $response = $this->server->dispatch( $request );
     1363                $this->assertEquals( 201, $response->get_status() );
     1364
     1365                $data = $response->get_data();
     1366                $post = get_post( $data['id'] );
     1367                $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1368                $this->assertNotEquals( '0000-00-00T00:00:00', $data['date_gmt'] );
     1369
    13611370                $this->check_create_post_response( $response );
     1371
     1372                update_option( 'timezone_string', '' );
    13621373        }
    13631374
    13641375        public function test_create_post_sticky() {
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    14311442                $new_post = get_post( $data['id'] );
    14321443                $this->assertEquals( 'draft', $data['status'] );
    14331444                $this->assertEquals( 'draft', $new_post->post_status );
    1434                 // Confirm dates are null
    1435                 $this->assertNull( $data['date_gmt'] );
    1436                 $this->assertNull( $data['modified_gmt'] );
     1445                // Confirm dates are shimmed for gmt_offset
     1446                $post_modified_gmt = date( 'Y-m-d H:i:s', strtotime( $new_post->post_modified ) + ( get_option( 'gmt_offset' ) * 3600 ) );
     1447                $post_date_gmt = date( 'Y-m-d H:i:s', strtotime( $new_post->post_date ) + ( get_option( 'gmt_offset' ) * 3600 ) );
     1448
     1449                $this->assertEquals( mysql_to_rfc3339( $post_modified_gmt ), $data['modified_gmt'] );
     1450                $this->assertEquals( mysql_to_rfc3339( $post_date_gmt ), $data['date_gmt'] );
    14371451        }
    14381452
    14391453        public function test_create_post_private() {
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    21182132                $this->assertEquals( $post_date, $post->post_date );
    21192133
    21202134                $this->assertEquals( $results['date_gmt'], $data['date_gmt'] );
    2121                 // TODO expect null here for drafts (see https://core.trac.wordpress.org/ticket/5698#comment:14)
    21222135                $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
    21232136                $this->assertEquals( $post_date_gmt, $post->post_date_gmt );
    21242137        }
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    21492162                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    21502163        }
    21512164
     2165        public function test_empty_post_date_gmt_shimmed_using_post_date() {
     2166                global $wpdb;
     2167
     2168                wp_set_current_user( self::$editor_id );
     2169                update_option( 'timezone_string', 'America/Chicago' );
     2170
     2171                // Need to set dates using wpdb directly because `wp_update_post` and
     2172                // `wp_insert_post` have additional validation on dates.
     2173                $post_id = $this->factory->post->create();
     2174                $wpdb->update(
     2175                        $wpdb->posts,
     2176                        array(
     2177                                'post_date'     => '2016-02-23 12:00:00',
     2178                                'post_date_gmt' => '0000-00-00 00:00:00',
     2179                        ),
     2180                        array(
     2181                                'ID' => $post_id,
     2182                        ),
     2183                        array( '%s', '%s' ),
     2184                        array( '%d' )
     2185                );
     2186                wp_cache_delete( $post_id, 'posts' );
     2187
     2188                $post = get_post( $post_id );
     2189                $this->assertEquals( $post->post_date,     '2016-02-23 12:00:00' );
     2190                $this->assertEquals( $post->post_date_gmt, '0000-00-00 00:00:00' );
     2191
     2192                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     2193                $response = $this->server->dispatch( $request );
     2194                $this->assertEquals( 200, $response->get_status() );
     2195                $data = $response->get_data();
     2196
     2197                $this->assertEquals( '2016-02-23T12:00:00', $data['date'] );
     2198                $this->assertEquals( '2016-02-23T18:00:00', $data['date_gmt'] );
     2199
     2200                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
     2201                $request->set_param( 'date', '2016-02-23T13:00:00' );
     2202                $response = $this->server->dispatch( $request );
     2203                $this->assertEquals( 200, $response->get_status() );
     2204                $data = $response->get_data();
     2205
     2206                $this->assertEquals( '2016-02-23T13:00:00', $data['date'] );
     2207                $this->assertEquals( '2016-02-23T19:00:00', $data['date_gmt'] );
     2208
     2209                $post = get_post( $post_id );
     2210                $this->assertEquals( $post->post_date,     '2016-02-23 13:00:00' );
     2211                $this->assertEquals( $post->post_date_gmt, '2016-02-23 19:00:00' );
     2212
     2213                update_option( 'timezone_string', '' );
     2214        }
     2215
    21522216        public function test_update_post_slug() {
    21532217                wp_set_current_user( self::$editor_id );
    21542218