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-comments-controller.php

    r39954 r40101  
    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 );
     
    19692047        $this->assertEquals( mysql_to_rfc3339( $updated->comment_date ), $comment['date'] );
    19702048        $this->assertEquals( '2014-11-07T10:14:25', $comment['date'] );
     2049    }
     2050
     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 );
    19712082    }
    19722083
Note: See TracChangeset for help on using the changeset viewer.