Make WordPress Core


Ignore:
Timestamp:
02/21/2017 06:17:32 PM (8 years ago)
Author:
jnylen0
Message:

REST API: Fix multiple issues with setting dates of posts and comments.

This commit modifies the rest_get_date_with_gmt function to correctly parse local and UTC timestamps with or without timezone information.

It also ensures that the REST API can edit the dates of draft posts by setting the edit_date flag to wp_update_post.

Overall this commit ensures that post and comment dates can be set and updated as expected.

Fixes #39256.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r40037 r40101  
    11531153    }
    11541154
     1155    public function post_dates_provider() {
     1156        $all_statuses = array(
     1157            'draft',
     1158            'publish',
     1159            'future',
     1160            'pending',
     1161            'private',
     1162        );
     1163
     1164        $cases_short = array(
     1165            'set date without timezone' => array(
     1166                'statuses' => $all_statuses,
     1167                'params'   => array(
     1168                    'timezone_string' => 'America/New_York',
     1169                    'date'            => '2016-12-12T14:00:00',
     1170                ),
     1171                'results' => array(
     1172                    'date'            => '2016-12-12T14:00:00',
     1173                    'date_gmt'        => '2016-12-12T19:00:00',
     1174                ),
     1175            ),
     1176            'set date_gmt without timezone' => array(
     1177                'statuses' => $all_statuses,
     1178                'params'   => array(
     1179                    'timezone_string' => 'America/New_York',
     1180                    'date_gmt'        => '2016-12-12T19:00:00',
     1181                ),
     1182                'results' => array(
     1183                    'date'            => '2016-12-12T14:00:00',
     1184                    'date_gmt'        => '2016-12-12T19:00:00',
     1185                ),
     1186            ),
     1187            'set date with timezone' => array(
     1188                'statuses' => array( 'draft', 'publish' ),
     1189                'params'   => array(
     1190                    'timezone_string' => 'America/New_York',
     1191                    'date'            => '2016-12-12T18:00:00-01:00',
     1192                ),
     1193                'results' => array(
     1194                    'date'            => '2016-12-12T14:00:00',
     1195                    'date_gmt'        => '2016-12-12T19:00:00',
     1196                ),
     1197            ),
     1198            'set date_gmt with timezone' => array(
     1199                'statuses' => array( 'draft', 'publish' ),
     1200                'params'   => array(
     1201                    'timezone_string' => 'America/New_York',
     1202                    'date_gmt'        => '2016-12-12T18:00:00-01:00',
     1203                ),
     1204                'results' => array(
     1205                    'date'            => '2016-12-12T14:00:00',
     1206                    'date_gmt'        => '2016-12-12T19:00:00',
     1207                ),
     1208            ),
     1209        );
     1210
     1211        $cases = array();
     1212        foreach ( $cases_short as $description => $case ) {
     1213            foreach ( $case['statuses'] as $status ) {
     1214                $cases[ $description . ', status=' . $status ] = array(
     1215                    $status,
     1216                    $case['params'],
     1217                    $case['results'],
     1218                );
     1219            }
     1220        }
     1221
     1222        return $cases;
     1223    }
     1224
     1225    /**
     1226     * @dataProvider post_dates_provider
     1227     */
     1228    public function test_create_post_date( $status, $params, $results ) {
     1229        wp_set_current_user( self::$editor_id );
     1230        update_option( 'timezone_string', $params['timezone_string'] );
     1231
     1232        $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     1233        $request->set_param( 'status', $status );
     1234        $request->set_param( 'title', 'not empty' );
     1235        if ( isset( $params['date'] ) ) {
     1236            $request->set_param( 'date', $params['date'] );
     1237        }
     1238        if ( isset( $params['date_gmt'] ) ) {
     1239            $request->set_param( 'date_gmt', $params['date_gmt'] );
     1240        }
     1241        $response = $this->server->dispatch( $request );
     1242
     1243        update_option( 'timezone_string', '' );
     1244
     1245        $this->assertEquals( 201, $response->get_status() );
     1246        $data = $response->get_data();
     1247        $post = get_post( $data['id'] );
     1248
     1249        $this->assertEquals( $results['date'], $data['date'] );
     1250        $post_date = str_replace( 'T', ' ', $results['date'] );
     1251        $this->assertEquals( $post_date, $post->post_date );
     1252
     1253        $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)
     1255        $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
     1256        $this->assertEquals( $post_date_gmt, $post->post_date_gmt );
     1257    }
     1258
    11551259    /**
    11561260     * @ticket 38698
     
    19842088        $this->assertEquals( date( 'Y-m-d', strtotime( mysql_to_rfc3339( $expected_modified ) ) ), date( 'Y-m-d', strtotime( $data['modified'] ) ) );
    19852089        $this->assertEquals( date( 'Y-m-d', strtotime( $expected_modified ) ), date( 'Y-m-d', strtotime( $new_post->post_modified ) ) );
     2090    }
     2091
     2092    /**
     2093     * @dataProvider post_dates_provider
     2094     */
     2095    public function test_update_post_date( $status, $params, $results ) {
     2096        wp_set_current_user( self::$editor_id );
     2097        update_option( 'timezone_string', $params['timezone_string'] );
     2098
     2099        $post_id = $this->factory->post->create( array( 'post_status' => $status ) );
     2100
     2101        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
     2102        if ( isset( $params['date'] ) ) {
     2103            $request->set_param( 'date', $params['date'] );
     2104        }
     2105        if ( isset( $params['date_gmt'] ) ) {
     2106            $request->set_param( 'date_gmt', $params['date_gmt'] );
     2107        }
     2108        $response = $this->server->dispatch( $request );
     2109
     2110        update_option( 'timezone_string', '' );
     2111
     2112        $this->assertEquals( 200, $response->get_status() );
     2113        $data = $response->get_data();
     2114        $post = get_post( $data['id'] );
     2115
     2116        $this->assertEquals( $results['date'], $data['date'] );
     2117        $post_date = str_replace( 'T', ' ', $results['date'] );
     2118        $this->assertEquals( $post_date, $post->post_date );
     2119
     2120        $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)
     2122        $post_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
     2123        $this->assertEquals( $post_date_gmt, $post->post_date_gmt );
    19862124    }
    19872125
Note: See TracChangeset for help on using the changeset viewer.