mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-03 16:50:13 +00:00
REST API: Pass "null" as the post date property to reset post to initial "floating" date value.
Props TimothyBlynJacobs, adamsilverstein, jnylen0, mnelson4. Fixes #44975. git-svn-id: https://develop.svn.wordpress.org/trunk@46249 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -2625,6 +2625,66 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
|
||||
$this->assertEquals( $params['excerpt'], $post->post_excerpt );
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that updating a post with a `null` date or date_gmt results in a reset post, where all
|
||||
* date values are equal (date, date_gmt, date_modified and date_modofied_gmt) in the API response.
|
||||
* In the database, the post_date_gmt field is reset to the default `0000-00-00 00:00:00`.
|
||||
*
|
||||
* @ticket 44975
|
||||
*/
|
||||
public function test_rest_update_post_with_empty_date() {
|
||||
// Create a new test post.
|
||||
$post_id = $this->factory->post->create();
|
||||
wp_set_current_user( self::$editor_id );
|
||||
|
||||
// Set the post date to the future.
|
||||
$future_date = '2919-07-29T18:00:00';
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
|
||||
$request->add_header( 'content-type', 'application/json' );
|
||||
$params = $this->set_post_data(
|
||||
array(
|
||||
'date_gmt' => $future_date,
|
||||
'date' => $future_date,
|
||||
'title' => 'update',
|
||||
'status' => 'draft',
|
||||
)
|
||||
);
|
||||
$request->set_body( wp_json_encode( $params ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->check_update_post_response( $response );
|
||||
$new_data = $response->get_data();
|
||||
|
||||
// Verify the post is set to the future date.
|
||||
$this->assertEquals( $new_data['date_gmt'], $future_date );
|
||||
$this->assertEquals( $new_data['date'], $future_date );
|
||||
$this->assertNotEquals( $new_data['date_gmt'], $new_data['modified_gmt'] );
|
||||
$this->assertNotEquals( $new_data['date'], $new_data['modified'] );
|
||||
|
||||
// Update post with a blank field (date or date_gmt).
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
|
||||
$request->add_header( 'content-type', 'application/json' );
|
||||
$params = $this->set_post_data(
|
||||
array(
|
||||
'date_gmt' => null,
|
||||
'title' => 'test',
|
||||
'status' => 'draft',
|
||||
)
|
||||
);
|
||||
$request->set_body( wp_json_encode( $params ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
||||
// Verify the date field values are reset in the API response.
|
||||
$this->check_update_post_response( $response );
|
||||
$new_data = $response->get_data();
|
||||
$this->assertEquals( $new_data['date_gmt'], $new_data['date'] );
|
||||
$this->assertNotEquals( $new_data['date_gmt'], $future_date );
|
||||
|
||||
$post = get_post( $post_id, 'ARRAY_A' );
|
||||
$this->assertEquals( $post['post_date_gmt'], '0000-00-00 00:00:00' );
|
||||
$this->assertNotEquals( $new_data['date_gmt'], $future_date );
|
||||
$this->assertNotEquals( $new_data['date'], $future_date );
|
||||
}
|
||||
|
||||
public function test_rest_update_post_raw() {
|
||||
wp_set_current_user( self::$editor_id );
|
||||
|
||||
|
||||
@@ -292,4 +292,59 @@ class WP_Test_REST_Schema_Sanitization extends WP_UnitTestCase {
|
||||
$this->assertEquals( 1.10, rest_sanitize_value_from_schema( 1.10, $schema ) );
|
||||
$this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) );
|
||||
}
|
||||
|
||||
public function test_nullable_date() {
|
||||
$schema = array(
|
||||
'type' => array( 'string', 'null' ),
|
||||
'format' => 'date-time',
|
||||
);
|
||||
|
||||
$this->assertNull( rest_sanitize_value_from_schema( null, $schema ) );
|
||||
$this->assertEquals( '2019-09-19T18:00:00', rest_sanitize_value_from_schema( '2019-09-19T18:00:00', $schema ) );
|
||||
$this->assertNull( rest_sanitize_value_from_schema( 'lalala', $schema ) );
|
||||
}
|
||||
|
||||
public function test_object_or_string() {
|
||||
$schema = array(
|
||||
'type' => array( 'object', 'string' ),
|
||||
'properties' => array(
|
||||
'raw' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals( 'My Value', rest_sanitize_value_from_schema( 'My Value', $schema ) );
|
||||
$this->assertEquals( array( 'raw' => 'My Value' ), rest_sanitize_value_from_schema( array( 'raw' => 'My Value' ), $schema ) );
|
||||
$this->assertNull( rest_sanitize_value_from_schema( array( 'raw' => 1 ), $schema ) );
|
||||
}
|
||||
|
||||
public function test_object_or_bool() {
|
||||
$schema = array(
|
||||
'type' => array( 'object', 'boolean' ),
|
||||
'properties' => array(
|
||||
'raw' => array(
|
||||
'type' => 'boolean',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertTrue( rest_sanitize_value_from_schema( true, $schema ) );
|
||||
$this->assertTrue( rest_sanitize_value_from_schema( '1', $schema ) );
|
||||
$this->assertTrue( rest_sanitize_value_from_schema( 1, $schema ) );
|
||||
|
||||
$this->assertFalse( rest_sanitize_value_from_schema( false, $schema ) );
|
||||
$this->assertFalse( rest_sanitize_value_from_schema( '0', $schema ) );
|
||||
$this->assertFalse( rest_sanitize_value_from_schema( 0, $schema ) );
|
||||
|
||||
$this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => true ), $schema ) );
|
||||
$this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => '1' ), $schema ) );
|
||||
$this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => 1 ), $schema ) );
|
||||
|
||||
$this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => false ), $schema ) );
|
||||
$this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => '0' ), $schema ) );
|
||||
$this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => 0 ), $schema ) );
|
||||
|
||||
$this->assertNull( rest_sanitize_value_from_schema( array( 'raw' => 'something non boolean' ), $schema ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,4 +296,36 @@ class WP_Test_REST_Schema_Validation extends WP_UnitTestCase {
|
||||
$this->assertTrue( rest_validate_value_from_schema( 1, $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( array(), $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_null() {
|
||||
$this->assertTrue( rest_validate_value_from_schema( null, array( 'type' => 'null' ) ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( '', array( 'type' => 'null' ) ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( 'null', array( 'type' => 'null' ) ) );
|
||||
}
|
||||
|
||||
public function test_nullable_date() {
|
||||
$schema = array(
|
||||
'type' => array( 'string', 'null' ),
|
||||
'format' => 'date-time',
|
||||
);
|
||||
|
||||
$this->assertTrue( rest_validate_value_from_schema( null, $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( '2019-09-19T18:00:00', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( 'some random string', $schema ) );
|
||||
}
|
||||
|
||||
public function test_object_or_string() {
|
||||
$schema = array(
|
||||
'type' => array( 'object', 'string' ),
|
||||
'properties' => array(
|
||||
'raw' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertTrue( rest_validate_value_from_schema( 'My Value', $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( array( 'raw' => 'My Value' ), $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( array( 'raw' => array( 'a list' ) ), $schema ) );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user