Ticket #48363: 48363.diff
File 48363.diff, 4.4 KB (added by , 4 years ago) |
---|
-
src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php
diff --git a/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php b/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php index 411fc5460c..08de9b9cf4 100644
a b abstract class WP_REST_Meta_Fields { 430 430 $type = ! empty( $rest_args['schema']['type'] ) ? $rest_args['schema']['type'] : $type; 431 431 432 432 if ( null === $rest_args['schema']['default'] ) { 433 $rest_args['schema']['default'] = $this->get_default_for_type( $type );433 $rest_args['schema']['default'] = self::get_default_for_type( $type ); 434 434 } 435 435 436 436 $rest_args['schema'] = $this->default_additional_properties_to_false( $rest_args['schema'] ); … … abstract class WP_REST_Meta_Fields { 501 501 $schema = $args['schema']['items']; 502 502 } 503 503 504 if ( '' === $value ) { 505 $value = self::get_default_for_type( $schema['type'] ); 506 } 507 504 508 if ( is_wp_error( rest_validate_value_from_schema( $value, $schema ) ) ) { 505 509 return null; 506 510 } … … abstract class WP_REST_Meta_Fields { 566 570 * @param string $type The schema type. 567 571 * @return mixed 568 572 */ 569 protected function get_default_for_type( $type ) {573 protected static function get_default_for_type( $type ) { 570 574 switch ( $type ) { 571 575 case 'string': 572 576 return ''; -
tests/phpunit/tests/rest-api/rest-post-meta-fields.php
diff --git a/tests/phpunit/tests/rest-api/rest-post-meta-fields.php b/tests/phpunit/tests/rest-api/rest-post-meta-fields.php index 39b167e782..e24e7aca40 100644
a b class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 1979 1979 1980 1980 /** 1981 1981 * @ticket 43392 1982 * @dataProvider _dp_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable 1982 1983 */ 1983 public function test_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable( ) {1984 public function test_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable( $type, $stored, $expected ) { 1984 1985 register_post_meta( 1985 1986 'post', 1986 ' boolean',1987 'safe', 1987 1988 array( 1988 1989 'single' => true, 1989 1990 'show_in_rest' => true, 1990 'type' => 'boolean',1991 'type' => $type, 1991 1992 ) 1992 1993 ); 1993 1994 1994 update_post_meta( self::$post_id, ' boolean', 'true');1995 update_post_meta( self::$post_id, 'safe', $stored ); 1995 1996 1996 1997 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 1997 1998 $response = rest_get_server()->dispatch( $request ); 1998 1999 1999 $this->assertTrue( $response->get_data()['meta']['boolean'] ); 2000 $this->assertSame( $expected, $response->get_data()['meta']['safe'] ); 2001 } 2002 2003 public function _dp_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable() { 2004 return array( 2005 array( 'boolean', 'true', true ), 2006 array( 'boolean', 'false', false ), 2007 array( 'boolean', '1', true ), 2008 array( 'boolean', '0', false ), 2009 array( 'boolean', '', false ), 2010 array( 'integer', '', 0 ), 2011 array( 'integer', '1', 1 ), 2012 array( 'integer', '0', 0 ), 2013 array( 'number', '', 0.0 ), 2014 array( 'number', '1.1', 1.1 ), 2015 array( 'number', '0.0', 0.0 ), 2016 array( 'string', '', '' ), 2017 array( 'string', '1', '1' ), 2018 array( 'string', '0', '0' ), 2019 array( 'string', 'str', 'str' ), 2020 ); 2000 2021 } 2001 2022 2002 2023 /** … … class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 2598 2619 $this->assertSame( array( false, true ), get_post_meta( self::$post_id, 'items', true ) ); 2599 2620 } 2600 2621 2622 public function test_boolean_meta_update_to_false_stores_0() { 2623 $this->grant_write_permission(); 2624 2625 register_post_meta( 2626 'post', 2627 'boolean', 2628 array( 2629 'single' => true, 2630 'type' => 'boolean', 2631 'show_in_rest' => true, 2632 'sanitize_callback' => function( $value ) { 2633 return $value ? '1' : '0'; 2634 }, 2635 ) 2636 ); 2637 2638 update_post_meta( self::$post_id, 'boolean', 1 ); 2639 2640 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 2641 $request->set_body_params( 2642 array( 2643 'meta' => array( 2644 'boolean' => false, 2645 ), 2646 ) 2647 ); 2648 2649 $response = rest_get_server()->dispatch( $request ); 2650 $this->assertEquals( 200, $response->get_status() ); 2651 $this->assertEquals( '0', get_post_meta( self::$post_id, 'boolean', true ) ); 2652 } 2653 2601 2654 /** 2602 2655 * Internal function used to disable an insert query which 2603 2656 * will trigger a wpdb error for testing purposes.