mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
REST API: Add support for arrays in schema validation and sanitization.
By allowing more fine-grained validation and sanitisation of endpoint args, we can ensure the correct data is being passed to endpoints. This can easily be extended to support new data types, such as CSV fields or objects. Props joehoyle, rachelbaker, pento. Fixes #38531. git-svn-id: https://develop.svn.wordpress.org/trunk@39046 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering schema validation and sanitization functionality.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST API
|
||||
*/
|
||||
|
||||
/**
|
||||
* @group restapi
|
||||
*/
|
||||
class WP_Test_REST_Schema_Validation extends WP_UnitTestCase {
|
||||
|
||||
public function test_type_number() {
|
||||
$schema = array(
|
||||
'type' => 'number',
|
||||
'minimum' => 1,
|
||||
'maximum' => 2,
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( 1, $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( 2, $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( 3, $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( true, $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_integer() {
|
||||
$schema = array(
|
||||
'type' => 'integer',
|
||||
'minimum' => 1,
|
||||
'maximum' => 2,
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( 1, $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( 2, $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( 3, $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( 1.1, $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_string() {
|
||||
$schema = array(
|
||||
'type' => 'string',
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( 'Hello :)', $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( '1', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( 1, $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( array(), $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_boolean() {
|
||||
$schema = array(
|
||||
'type' => 'boolean',
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( true, $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( false, $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( 1, $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( 0, $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( 'true', $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( 'false', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( 'no', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( 'yes', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( 1123, $schema ) );
|
||||
}
|
||||
|
||||
public function test_format_email() {
|
||||
$schema = array(
|
||||
'type' => 'string',
|
||||
'format' => 'email',
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( 'email@example.com', $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( 'a@b.c', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( 'email', $schema ) );
|
||||
}
|
||||
|
||||
public function test_format_date_time() {
|
||||
$schema = array(
|
||||
'type' => 'string',
|
||||
'format' => 'date-time',
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( '2016-06-30T05:43:21', $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( '2016-06-30T05:43:21Z', $schema ) );
|
||||
$this->assertTrue( rest_validate_value_from_schema( '2016-06-30T05:43:21+00:00', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( '20161027T163355Z', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( '2016', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( '2016-06-30', $schema ) );
|
||||
}
|
||||
|
||||
public function test_format_ipv4() {
|
||||
$schema = array(
|
||||
'type' => 'string',
|
||||
'format' => 'ipv4',
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( '127.0.0.1', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( '3333.3333.3333.3333', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( '1', $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_array() {
|
||||
$schema = array(
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'number',
|
||||
),
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( array( 1 ), $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( array( true ), $schema ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user