Make WordPress Core

Ticket #39256: 39256.4.diff

File 39256.4.diff, 17.2 KB (added by jnylen0, 8 years ago)

Deprecate rest_get_date_with_gmt; add tests for comments

  • src/wp-includes/rest-api.php

    diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php
    index d5d7638..6e0fa0b 100644
    a b function rest_parse_date( $date, $force_utc = false ) { 
    783783 * Retrieves a local date with its GMT equivalent, in MySQL datetime format.
    784784 *
    785785 * @since 4.4.0
     786 * @deprecated 4.7.2 Use rest_parse_date_with_utc()
    786787 *
    787788 * @see rest_parse_date()
    788789 *
    function rest_parse_date( $date, $force_utc = false ) { 
    792793 *                    null on failure.
    793794 */
    794795function rest_get_date_with_gmt( $date, $force_utc = false ) {
     796        _deprecated_function( __METHOD__, '4.7.2', 'rest_parse_date_with_utc' );
     797
    795798        $date = rest_parse_date( $date, $force_utc );
    796799
    797800        if ( empty( $date ) ) {
    function rest_get_date_with_gmt( $date, $force_utc = false ) { 
    805808}
    806809
    807810/**
     811 * Parses a date into both its local and UTC equivalent, in MySQL datetime format.
     812 *
     813 * @since 4.7.2
     814 *
     815 * @see rest_parse_date()
     816 *
     817 * @param string $date   RFC3339 timestamp.
     818 * @param bool   $is_utc Whether the provided date should be interpreted as UTC.
     819 * @return array|null Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s),
     820 *                    null on failure.
     821 */
     822function rest_parse_date_with_utc( $date, $is_utc = true ) {
     823        // Whether or not the original date actually has a timezone string
     824        // changes the way we need to do timezone conversion.  Store this info
     825        // before parsing the date, and use it later.
     826        $has_timezone = preg_match( '#(Z|[+-]\d{2}(:\d{2})?)$#', $date );
     827
     828        $date = rest_parse_date( $date );
     829
     830        if ( empty( $date ) ) {
     831                return null;
     832        }
     833
     834        // At this point $date could either be a local date (if we were passed a
     835        // *local* date without a timezone offset) or a UTC date (otherwise).
     836        // Timezone conversion needs to be handled differently between these two
     837        // cases.
     838        if ( ! $is_utc && ! $has_timezone ) {
     839                $local = date( 'Y-m-d H:i:s', $date );
     840                $utc = get_gmt_from_date( $local );
     841        } else {
     842                $utc = date( 'Y-m-d H:i:s', $date );
     843                $local = get_date_from_gmt( $utc );
     844        }
     845
     846        return array( $local, $utc );
     847}
     848
     849/**
    808850 * Returns a contextual HTTP error code for authorization failure.
    809851 *
    810852 * @since 4.7.0
  • src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
    index 117529d..6060c5d 100644
    a b class WP_REST_Comments_Controller extends WP_REST_Controller { 
    11231123                }
    11241124
    11251125                if ( ! empty( $request['date'] ) ) {
    1126                         $date_data = rest_get_date_with_gmt( $request['date'] );
     1126                        $date_data = rest_parse_date_with_utc( $request['date'], false );
    11271127
    11281128                        if ( ! empty( $date_data ) ) {
    11291129                                list( $prepared_comment['comment_date'], $prepared_comment['comment_date_gmt'] ) = $date_data;
    11301130                        }
    11311131                } elseif ( ! empty( $request['date_gmt'] ) ) {
    1132                         $date_data = rest_get_date_with_gmt( $request['date_gmt'], true );
     1132                        $date_data = rest_parse_date_with_utc( $request['date_gmt'], true );
    11331133
    11341134                        if ( ! empty( $date_data ) ) {
    11351135                                list( $prepared_comment['comment_date'], $prepared_comment['comment_date_gmt'] ) = $date_data;
  • 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 25e9c9a..f9e637f 100644
    a b class WP_REST_Posts_Controller extends WP_REST_Controller { 
    10001000
    10011001                // Post date.
    10021002                if ( ! empty( $schema['properties']['date'] ) && ! empty( $request['date'] ) ) {
    1003                         $date_data = rest_get_date_with_gmt( $request['date'] );
     1003                        $date_data = rest_parse_date_with_utc( $request['date'], false );
    10041004
    10051005                        if ( ! empty( $date_data ) ) {
    10061006                                list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data;
     1007                                $prepared_post->edit_date = true;
    10071008                        }
    10081009                } elseif ( ! empty( $schema['properties']['date_gmt'] ) && ! empty( $request['date_gmt'] ) ) {
    1009                         $date_data = rest_get_date_with_gmt( $request['date_gmt'], true );
     1010                        $date_data = rest_parse_date_with_utc( $request['date_gmt'], true );
    10101011
    10111012                        if ( ! empty( $date_data ) ) {
    10121013                                list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data;
     1014                                $prepared_post->edit_date = true;
    10131015                        }
    10141016                }
    10151017
  • tests/phpunit/tests/rest-api.php

    diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php
    index 69bf9a3..f303a01 100644
    a b class Tests_REST_API extends WP_UnitTestCase { 
    383383                $this->assertEquals( $valid, wp_check_jsonp_callback( $callback ) );
    384384        }
    385385
     386        public function rest_date_provider() {
     387                return array(
     388                        // Valid dates with timezones
     389                        array( '2017-01-16T11:30:00-05:00', gmmktime( 11, 30,  0,  1, 16, 2017 ) + 5 * HOUR_IN_SECONDS ),
     390                        array( '2017-01-16T11:30:00-05:30', gmmktime( 11, 30,  0,  1, 16, 2017 ) + 5.5 * HOUR_IN_SECONDS ),
     391                        array( '2017-01-16T11:30:00-05'   , gmmktime( 11, 30,  0,  1, 16, 2017 ) + 5 * HOUR_IN_SECONDS ),
     392                        array( '2017-01-16T11:30:00+05'   , gmmktime( 11, 30,  0,  1, 16, 2017 ) - 5 * HOUR_IN_SECONDS ),
     393                        array( '2017-01-16T11:30:00-00'   , gmmktime( 11, 30,  0,  1, 16, 2017 ) ),
     394                        array( '2017-01-16T11:30:00+00'   , gmmktime( 11, 30,  0,  1, 16, 2017 ) ),
     395                        array( '2017-01-16T11:30:00Z'     , gmmktime( 11, 30,  0,  1, 16, 2017 ) ),
     396
     397                        // Valid dates without timezones
     398                        array( '2017-01-16T11:30:00'      , gmmktime( 11, 30,  0,  1, 16, 2017 ) ),
     399
     400                        // Invalid dates (TODO: support parsing partial dates as ranges, see #38641)
     401                        array( '2017-01-16T11:30:00-5', false ),
     402                        array( '2017-01-16T11:30', false ),
     403                        array( '2017-01-16T11', false ),
     404                        array( '2017-01-16T', false ),
     405                        array( '2017-01-16', false ),
     406                        array( '2017-01', false ),
     407                        array( '2017', false ),
     408                );
     409        }
     410
     411        /**
     412         * @dataProvider rest_date_provider
     413         */
     414        public function test_rest_parse_date( $string, $value ) {
     415                $this->assertEquals( $value, rest_parse_date( $string ) );
     416        }
     417
     418        public function rest_date_force_utc_provider() {
     419                return array(
     420                        // Valid dates with timezones
     421                        array( '2017-01-16T11:30:00-05:00', gmmktime( 11, 30,  0,  1, 16, 2017 ) ),
     422                        array( '2017-01-16T11:30:00-05:30', gmmktime( 11, 30,  0,  1, 16, 2017 ) ),
     423                        array( '2017-01-16T11:30:00-05'   , gmmktime( 11, 30,  0,  1, 16, 2017 ) ),
     424                        array( '2017-01-16T11:30:00+05'   , gmmktime( 11, 30,  0,  1, 16, 2017 ) ),
     425                        array( '2017-01-16T11:30:00-00'   , gmmktime( 11, 30,  0,  1, 16, 2017 ) ),
     426                        array( '2017-01-16T11:30:00+00'   , gmmktime( 11, 30,  0,  1, 16, 2017 ) ),
     427                        array( '2017-01-16T11:30:00Z'     , gmmktime( 11, 30,  0,  1, 16, 2017 ) ),
     428
     429                        // Valid dates without timezones
     430                        array( '2017-01-16T11:30:00'      , gmmktime( 11, 30,  0,  1, 16, 2017 ) ),
     431
     432                        // Invalid dates (TODO: support parsing partial dates as ranges, see #38641)
     433                        array( '2017-01-16T11:30:00-5', false ),
     434                        array( '2017-01-16T11:30', false ),
     435                        array( '2017-01-16T11', false ),
     436                        array( '2017-01-16T', false ),
     437                        array( '2017-01-16', false ),
     438                        array( '2017-01', false ),
     439                        array( '2017', false ),
     440                );
     441        }
     442
     443        /**
     444         * @dataProvider rest_date_force_utc_provider
     445         */
     446        public function test_rest_parse_date_force_utc( $string, $value ) {
     447                $this->assertEquals( $value, rest_parse_date( $string, true ) );
     448        }
    386449}
  • tests/phpunit/tests/rest-api/rest-comments-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php
    index fea7878..502a6d2 100644
    a b class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    960960                $this->assertEquals( self::$post_id, $data['post'] );
    961961        }
    962962
     963        public function comment_dates_provider() {
     964                return array(
     965                        'set date without timezone' => array(
     966                                'params'   => array(
     967                                        'timezone_string' => 'America/New_York',
     968                                        'date'            => '2016-12-12T14:00:00',
     969                                ),
     970                                'results' => array(
     971                                        'date'            => '2016-12-12T14:00:00',
     972                                        'date_gmt'        => '2016-12-12T19:00:00',
     973                                ),
     974                        ),
     975                        'set date_gmt without timezone' => array(
     976                                'params'   => array(
     977                                        'timezone_string' => 'America/New_York',
     978                                        'date_gmt'        => '2016-12-12T19:00:00',
     979                                ),
     980                                'results' => array(
     981                                        'date'            => '2016-12-12T14:00:00',
     982                                        'date_gmt'        => '2016-12-12T19:00:00',
     983                                ),
     984                        ),
     985                        'set date with timezone' => array(
     986                                'params'   => array(
     987                                        'timezone_string' => 'America/New_York',
     988                                        'date'            => '2016-12-12T18:00:00-01:00',
     989                                ),
     990                                'results' => array(
     991                                        'date'            => '2016-12-12T14:00:00',
     992                                        'date_gmt'        => '2016-12-12T19:00:00',
     993                                ),
     994                        ),
     995                        'set date_gmt with timezone' => array(
     996                                'params'   => array(
     997                                        'timezone_string' => 'America/New_York',
     998                                        'date_gmt'        => '2016-12-12T18:00:00-01:00',
     999                                ),
     1000                                'results' => array(
     1001                                        'date'            => '2016-12-12T14:00:00',
     1002                                        'date_gmt'        => '2016-12-12T19:00:00',
     1003                                ),
     1004                        ),
     1005                );
     1006        }
     1007
     1008        /**
     1009         * @dataProvider comment_dates_provider
     1010         */
     1011        public function test_create_comment_date( $params, $results ) {
     1012                wp_set_current_user( self::$admin_id );
     1013                update_option( 'timezone_string', $params['timezone_string'] );
     1014
     1015                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1016                $request->set_param( 'content', 'not empty' );
     1017                $request->set_param( 'post', self::$post_id );
     1018                if ( isset( $params['date'] ) ) {
     1019                        $request->set_param( 'date', $params['date'] );
     1020                }
     1021                if ( isset( $params['date_gmt'] ) ) {
     1022                        $request->set_param( 'date_gmt', $params['date_gmt'] );
     1023                }
     1024                $response = $this->server->dispatch( $request );
     1025
     1026                update_option( 'timezone_string', '' );
     1027
     1028                $this->assertEquals( 201, $response->get_status() );
     1029                $data = $response->get_data();
     1030                $comment = get_comment( $data['id'] );
     1031
     1032                $this->assertEquals( $results['date'], $data['date'] );
     1033                $comment_date = str_replace( 'T', ' ', $results['date'] );
     1034                $this->assertEquals( $comment_date, $comment->comment_date );
     1035
     1036                $this->assertEquals( $results['date_gmt'], $data['date_gmt'] );
     1037                $comment_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
     1038                $this->assertEquals( $comment_date_gmt, $comment->comment_date_gmt );
     1039        }
     1040
    9631041        public function test_create_item_using_accepted_content_raw_value() {
    9641042                wp_set_current_user( self::$admin_id );
    9651043
    class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    19702048                $this->assertEquals( '2014-11-07T10:14:25', $comment['date'] );
    19712049        }
    19722050
     2051        /**
     2052         * @dataProvider comment_dates_provider
     2053         */
     2054        public function test_update_comment_date( $params, $results ) {
     2055                wp_set_current_user( self::$editor_id );
     2056                update_option( 'timezone_string', $params['timezone_string'] );
     2057
     2058                $comment_id = $this->factory->comment->create();
     2059
     2060                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
     2061                if ( isset( $params['date'] ) ) {
     2062                        $request->set_param( 'date', $params['date'] );
     2063                }
     2064                if ( isset( $params['date_gmt'] ) ) {
     2065                        $request->set_param( 'date_gmt', $params['date_gmt'] );
     2066                }
     2067                $response = $this->server->dispatch( $request );
     2068
     2069                update_option( 'timezone_string', '' );
     2070
     2071                $this->assertEquals( 200, $response->get_status() );
     2072                $data = $response->get_data();
     2073                $comment = get_comment( $data['id'] );
     2074
     2075                $this->assertEquals( $results['date'], $data['date'] );
     2076                $comment_date = str_replace( 'T', ' ', $results['date'] );
     2077                $this->assertEquals( $comment_date, $comment->comment_date );
     2078
     2079                $this->assertEquals( $results['date_gmt'], $data['date_gmt'] );
     2080                $comment_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
     2081                $this->assertEquals( $comment_date_gmt, $comment->comment_date_gmt );
     2082        }
     2083
    19732084        public function test_update_item_no_content() {
    19742085                $post_id = $this->factory->post->create();
    19752086
  • 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 ca16356..6943329 100644
    a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    11521152                $this->check_create_post_response( $response );
    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
    11571261         */
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    19852089                $this->assertEquals( date( 'Y-m-d', strtotime( $expected_modified ) ), date( 'Y-m-d', strtotime( $new_post->post_modified ) ) );
    19862090        }
    19872091
     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 );
     2124        }
     2125
    19882126        public function test_update_post_with_invalid_date() {
    19892127                wp_set_current_user( self::$editor_id );
    19902128