Make WordPress Core


Ignore:
Timestamp:
09/23/2019 05:24:58 PM (7 years ago)
Author:
kadamwhite
Message:

REST API: Pass "null" as the post date property to reset post to initial "floating" date value.

Props TimothyBlynJacobs, adamsilverstein, jnylen0, mnelson4.
Fixes #44975.

File:
1 edited

Legend:

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

    r46184 r46249  
    26242624                $this->assertEquals( $params['content'], $post->post_content );
    26252625                $this->assertEquals( $params['excerpt'], $post->post_excerpt );
     2626        }
     2627
     2628        /**
     2629         * Verify that updating a post with a `null` date or date_gmt results in a reset post, where all
     2630         * date values are equal (date, date_gmt, date_modified and date_modofied_gmt) in the API response.
     2631         * In the database, the post_date_gmt field is reset to the default `0000-00-00 00:00:00`.
     2632         *
     2633         * @ticket 44975
     2634         */
     2635        public function test_rest_update_post_with_empty_date() {
     2636                // Create a new test post.
     2637                $post_id = $this->factory->post->create();
     2638                wp_set_current_user( self::$editor_id );
     2639
     2640                // Set the post date to the future.
     2641                $future_date = '2919-07-29T18:00:00';
     2642                $request     = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
     2643                $request->add_header( 'content-type', 'application/json' );
     2644                $params = $this->set_post_data(
     2645                        array(
     2646                                'date_gmt' => $future_date,
     2647                                'date'     => $future_date,
     2648                                'title'    => 'update',
     2649                                'status'   => 'draft',
     2650                        )
     2651                );
     2652                $request->set_body( wp_json_encode( $params ) );
     2653                $response = rest_get_server()->dispatch( $request );
     2654                $this->check_update_post_response( $response );
     2655                $new_data = $response->get_data();
     2656
     2657                // Verify the post is set to the future date.
     2658                $this->assertEquals( $new_data['date_gmt'], $future_date );
     2659                $this->assertEquals( $new_data['date'], $future_date );
     2660                $this->assertNotEquals( $new_data['date_gmt'], $new_data['modified_gmt'] );
     2661                $this->assertNotEquals( $new_data['date'], $new_data['modified'] );
     2662
     2663                // Update post with a blank field (date or date_gmt).
     2664                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
     2665                $request->add_header( 'content-type', 'application/json' );
     2666                $params = $this->set_post_data(
     2667                        array(
     2668                                'date_gmt' => null,
     2669                                'title'    => 'test',
     2670                                'status'   => 'draft',
     2671                        )
     2672                );
     2673                $request->set_body( wp_json_encode( $params ) );
     2674                $response = rest_get_server()->dispatch( $request );
     2675
     2676                // Verify the date field values are reset in the API response.
     2677                $this->check_update_post_response( $response );
     2678                $new_data = $response->get_data();
     2679                $this->assertEquals( $new_data['date_gmt'], $new_data['date'] );
     2680                $this->assertNotEquals( $new_data['date_gmt'], $future_date );
     2681
     2682                $post = get_post( $post_id, 'ARRAY_A' );
     2683                $this->assertEquals( $post['post_date_gmt'], '0000-00-00 00:00:00' );
     2684                $this->assertNotEquals( $new_data['date_gmt'], $future_date );
     2685                $this->assertNotEquals( $new_data['date'], $future_date );
    26262686        }
    26272687
Note: See TracChangeset for help on using the changeset viewer.