| 2558 | /** |
| 2559 | * Verify that updating a post with a `null` date or date_gmt results in a reset post, where all |
| 2560 | * date values are equal (date, date_gmt, date_modified and date_modofied_gmt) in the API response. |
| 2561 | * In the database, the post_date_gmt field is reset to the default `0000-00-00 00:00:00`. |
| 2562 | * |
| 2563 | * @ticket 44975 |
| 2564 | */ |
| 2565 | public function test_rest_update_post_with_empty_date() { |
| 2566 | // Create a new test post. |
| 2567 | $post_id = $this->factory->post->create(); |
| 2568 | wp_set_current_user( self::$editor_id ); |
| 2569 | |
| 2570 | // Set the post date to the future. |
| 2571 | $future_date = '2919-07-29T18:00:00'; |
| 2572 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) ); |
| 2573 | $request->add_header( 'content-type', 'application/json' ); |
| 2574 | $params = $this->set_post_data( array( 'date_gmt' => $future_date, 'date' => $future_date,'title' => 'update', 'status' => 'draft' ) ); |
| 2575 | $request->set_body( wp_json_encode( $params ) ); |
| 2576 | $response = rest_get_server()->dispatch( $request ); |
| 2577 | $this->check_update_post_response( $response ); |
| 2578 | $new_data = $response->get_data(); |
| 2579 | |
| 2580 | // Verify the post is set to the future date. |
| 2581 | $this->assertEquals( $new_data['date_gmt'], $future_date ); |
| 2582 | $this->assertEquals( $new_data['date'], $future_date ); |
| 2583 | $this->assertNotEquals( $new_data['date_gmt'], $new_data['modified_gmt'] ); |
| 2584 | $this->assertNotEquals( $new_data['date'], $new_data['modified'] ); |
| 2585 | |
| 2586 | // Update post with a blank field (date or date_gmt). |
| 2587 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) ); |
| 2588 | $request->add_header( 'content-type', 'application/json' ); |
| 2589 | $params = $this->set_post_data( array( 'date_gmt' => null, 'title' => 'test', 'status' => 'draft' ) ); |
| 2590 | $request->set_body( wp_json_encode( $params ) ); |
| 2591 | $response = rest_get_server()->dispatch( $request ); |
| 2592 | |
| 2593 | // Verify the date field values are reset in the API response. |
| 2594 | $this->check_update_post_response( $response ); |
| 2595 | $new_data = $response->get_data(); |
| 2596 | $this->assertEquals( $new_data['date_gmt'], $new_data['date'] ); |
| 2597 | $this->assertNotEquals( $new_data['date_gmt'], $future_date ); |
| 2598 | |
| 2599 | $post = get_post( $post_id, 'ARRAY_A' ); |
| 2600 | $this->assertEquals( $post['post_date_gmt'], '0000-00-00 00:00:00' ); |
| 2601 | $this->assertNotEquals( $new_data['date_gmt'], $future_date ); |
| 2602 | $this->assertNotEquals( $new_data['date'], $future_date ); |
| 2603 | } |
| 2604 | |