mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-01 11:14:36 +00:00
REST API: Only validate the format keyword if the type is a string.
This allows for using multi-type support with a string that has a format. For backwards compatibility support, the format validation will still apply if the type is not specified, or it is invalid. Two new doing it wrong notices are issued when omitting a type, or using an invalid type. Props ryotsun. Fixes #50189. git-svn-id: https://develop.svn.wordpress.org/trunk@48300 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -312,6 +312,8 @@ class WP_Test_REST_Schema_Sanitization extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
public function test_type_unknown() {
|
||||
$this->setExpectedIncorrectUsage( 'rest_sanitize_value_from_schema' );
|
||||
|
||||
$schema = array(
|
||||
'type' => 'lalala',
|
||||
);
|
||||
@@ -321,6 +323,8 @@ class WP_Test_REST_Schema_Sanitization extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
public function test_no_type() {
|
||||
$this->setExpectedIncorrectUsage( 'rest_sanitize_value_from_schema' );
|
||||
|
||||
$schema = array(
|
||||
'type' => null,
|
||||
);
|
||||
@@ -340,6 +344,44 @@ class WP_Test_REST_Schema_Sanitization extends WP_UnitTestCase {
|
||||
$this->assertNull( rest_sanitize_value_from_schema( 'lalala', $schema ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50189
|
||||
*/
|
||||
public function test_format_validation_is_skipped_if_non_string_type() {
|
||||
$schema = array(
|
||||
'type' => 'array',
|
||||
'format' => 'hex-color',
|
||||
);
|
||||
$this->assertEquals( array( '#fff' ), rest_sanitize_value_from_schema( '#fff', $schema ) );
|
||||
$this->assertEquals( array( '#qrst' ), rest_sanitize_value_from_schema( '#qrst', $schema ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50189
|
||||
*/
|
||||
public function test_format_validation_is_applied_if_missing_type() {
|
||||
$this->expectException( 'PHPUnit_Framework_Error_Notice' ); // For the undefined index.
|
||||
$this->setExpectedIncorrectUsage( 'rest_sanitize_value_from_schema' );
|
||||
|
||||
$schema = array( 'format' => 'hex-color' );
|
||||
$this->assertEquals( '#abc', rest_sanitize_value_from_schema( '#abc', $schema ) );
|
||||
$this->assertEquals( '', rest_sanitize_value_from_schema( '#jkl', $schema ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50189
|
||||
*/
|
||||
public function test_format_validation_is_applied_if_unknown_type() {
|
||||
$this->setExpectedIncorrectUsage( 'rest_sanitize_value_from_schema' );
|
||||
|
||||
$schema = array(
|
||||
'format' => 'hex-color',
|
||||
'type' => 'str',
|
||||
);
|
||||
$this->assertEquals( '#abc', rest_sanitize_value_from_schema( '#abc', $schema ) );
|
||||
$this->assertEquals( '', rest_sanitize_value_from_schema( '#jkl', $schema ) );
|
||||
}
|
||||
|
||||
public function test_object_or_string() {
|
||||
$schema = array(
|
||||
'type' => array( 'object', 'string' ),
|
||||
|
||||
@@ -137,6 +137,47 @@ class WP_Test_REST_Schema_Validation extends WP_UnitTestCase {
|
||||
$this->assertWPError( rest_validate_value_from_schema( 'FF01::101::2', $schema ) ); // Multicast, compressed.
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50189
|
||||
*/
|
||||
public function test_format_validation_is_skipped_if_non_string_type() {
|
||||
$schema = array(
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
'format' => 'email',
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( 'email@example.com', $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( 'email', $schema ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50189
|
||||
*/
|
||||
public function test_format_validation_is_applied_if_missing_type() {
|
||||
$this->expectException( 'PHPUnit_Framework_Error_Notice' ); // For the undefined index.
|
||||
$this->setExpectedIncorrectUsage( 'rest_validate_value_from_schema' );
|
||||
|
||||
$schema = array( 'format' => 'email' );
|
||||
$this->assertTrue( rest_validate_value_from_schema( 'email@example.com', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( 'email', $schema ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50189
|
||||
*/
|
||||
public function test_format_validation_is_applied_if_unknown_type() {
|
||||
$this->setExpectedIncorrectUsage( 'rest_validate_value_from_schema' );
|
||||
|
||||
$schema = array(
|
||||
'format' => 'email',
|
||||
'type' => 'str',
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( 'email@example.com', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( 'email', $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_array() {
|
||||
$schema = array(
|
||||
'type' => 'array',
|
||||
@@ -322,6 +363,8 @@ class WP_Test_REST_Schema_Validation extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
public function test_type_unknown() {
|
||||
$this->setExpectedIncorrectUsage( 'rest_validate_value_from_schema' );
|
||||
|
||||
$schema = array(
|
||||
'type' => 'lalala',
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user