Make WordPress Core


Ignore:
Timestamp:
02/24/2017 09:58:07 PM (9 years ago)
Author:
SergeyBiryukov
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.

Props jnylen0.
Merges [40101] to the 4.7 branch.
Fixes #39256.

Location:
branches/4.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r39957 r40114  
    959959        }
    960960
     961        public function comment_dates_provider() {
     962                return array(
     963                        'set date without timezone' => array(
     964                                'params'   => array(
     965                                        'timezone_string' => 'America/New_York',
     966                                        'date'            => '2016-12-12T14:00:00',
     967                                ),
     968                                'results' => array(
     969                                        'date'            => '2016-12-12T14:00:00',
     970                                        'date_gmt'        => '2016-12-12T19:00:00',
     971                                ),
     972                        ),
     973                        'set date_gmt without timezone' => array(
     974                                'params'   => array(
     975                                        'timezone_string' => 'America/New_York',
     976                                        'date_gmt'        => '2016-12-12T19:00:00',
     977                                ),
     978                                'results' => array(
     979                                        'date'            => '2016-12-12T14:00:00',
     980                                        'date_gmt'        => '2016-12-12T19:00:00',
     981                                ),
     982                        ),
     983                        'set date with timezone' => array(
     984                                'params'   => array(
     985                                        'timezone_string' => 'America/New_York',
     986                                        'date'            => '2016-12-12T18:00:00-01:00',
     987                                ),
     988                                'results' => array(
     989                                        'date'            => '2016-12-12T14:00:00',
     990                                        'date_gmt'        => '2016-12-12T19:00:00',
     991                                ),
     992                        ),
     993                        'set date_gmt with timezone' => array(
     994                                'params'   => array(
     995                                        'timezone_string' => 'America/New_York',
     996                                        'date_gmt'        => '2016-12-12T18:00:00-01:00',
     997                                ),
     998                                'results' => array(
     999                                        'date'            => '2016-12-12T14:00:00',
     1000                                        'date_gmt'        => '2016-12-12T19:00:00',
     1001                                ),
     1002                        ),
     1003                );
     1004        }
     1005
     1006        /**
     1007         * @dataProvider comment_dates_provider
     1008         */
     1009        public function test_create_comment_date( $params, $results ) {
     1010                wp_set_current_user( self::$admin_id );
     1011                update_option( 'timezone_string', $params['timezone_string'] );
     1012
     1013                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1014                $request->set_param( 'content', 'not empty' );
     1015                $request->set_param( 'post', self::$post_id );
     1016                if ( isset( $params['date'] ) ) {
     1017                        $request->set_param( 'date', $params['date'] );
     1018                }
     1019                if ( isset( $params['date_gmt'] ) ) {
     1020                        $request->set_param( 'date_gmt', $params['date_gmt'] );
     1021                }
     1022                $response = $this->server->dispatch( $request );
     1023
     1024                update_option( 'timezone_string', '' );
     1025
     1026                $this->assertEquals( 201, $response->get_status() );
     1027                $data = $response->get_data();
     1028                $comment = get_comment( $data['id'] );
     1029
     1030                $this->assertEquals( $results['date'], $data['date'] );
     1031                $comment_date = str_replace( 'T', ' ', $results['date'] );
     1032                $this->assertEquals( $comment_date, $comment->comment_date );
     1033
     1034                $this->assertEquals( $results['date_gmt'], $data['date_gmt'] );
     1035                $comment_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
     1036                $this->assertEquals( $comment_date_gmt, $comment->comment_date_gmt );
     1037        }
     1038
    9611039        public function test_create_item_using_accepted_content_raw_value() {
    9621040                wp_set_current_user( self::$admin_id );
     
    19412019                $this->assertEquals( mysql_to_rfc3339( $updated->comment_date ), $comment['date'] );
    19422020                $this->assertEquals( '2014-11-07T10:14:25', $comment['date'] );
     2021        }
     2022
     2023        /**
     2024         * @dataProvider comment_dates_provider
     2025         */
     2026        public function test_update_comment_date( $params, $results ) {
     2027                wp_set_current_user( self::$editor_id );
     2028                update_option( 'timezone_string', $params['timezone_string'] );
     2029
     2030                $comment_id = $this->factory->comment->create();
     2031
     2032                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
     2033                if ( isset( $params['date'] ) ) {
     2034                        $request->set_param( 'date', $params['date'] );
     2035                }
     2036                if ( isset( $params['date_gmt'] ) ) {
     2037                        $request->set_param( 'date_gmt', $params['date_gmt'] );
     2038                }
     2039                $response = $this->server->dispatch( $request );
     2040
     2041                update_option( 'timezone_string', '' );
     2042
     2043                $this->assertEquals( 200, $response->get_status() );
     2044                $data = $response->get_data();
     2045                $comment = get_comment( $data['id'] );
     2046
     2047                $this->assertEquals( $results['date'], $data['date'] );
     2048                $comment_date = str_replace( 'T', ' ', $results['date'] );
     2049                $this->assertEquals( $comment_date, $comment->comment_date );
     2050
     2051                $this->assertEquals( $results['date_gmt'], $data['date_gmt'] );
     2052                $comment_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
     2053                $this->assertEquals( $comment_date_gmt, $comment->comment_date_gmt );
    19432054        }
    19442055
Note: See TracChangeset for help on using the changeset viewer.