WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/14/2016 04:35:35 PM (4 years ago)
Author:
joehoyle
Message:

REST API: Validate and Sanitize registered meta based off the schema.

With the addition of Array support in our schema validation functions, it's now possible to use these in the meta validation and sanitization steps. Also, this increases the test coverage of using registered via meta the API significantly.

Fixes #38531.
Props rachelbaker, tharsheblows.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php

    r39104 r39222  
    7777    }
    7878
     79    public function test_type_array_nested() {
     80        $schema = array(
     81            'type' => 'array',
     82            'items' => array(
     83                'type' => 'array',
     84                'items' => array(
     85                    'type' => 'number',
     86                ),
     87            ),
     88        );
     89        $this->assertEquals( array( array( 1 ), array( 2 ) ), rest_sanitize_value_from_schema( array( array( 1 ), array( 2 ) ), $schema ) );
     90        $this->assertEquals( array( array( 1 ), array( 2 ) ), rest_sanitize_value_from_schema( array( array( '1' ), array( '2' ) ), $schema ) );
     91    }
     92
    7993    public function test_type_array_as_csv() {
    8094        $schema = array(
     
    111125        $this->assertEquals( array( 'chicken', 'coleslaw' ), rest_sanitize_value_from_schema( 'chicken,coleslaw', $schema ) );
    112126    }
     127
     128    public function test_type_array_is_associative() {
     129        $schema = array(
     130            'type' => 'array',
     131            'items' => array(
     132                'type' => 'string',
     133            ),
     134        );
     135        $this->assertEquals( array( '1', '2' ), rest_sanitize_value_from_schema( array( 'first' => '1', 'second' => '2' ), $schema ) );
     136    }
     137
     138    public function test_type_unknown() {
     139        $schema = array(
     140            'type' => 'lalala',
     141        );
     142        $this->assertEquals( 'Best lyrics', rest_sanitize_value_from_schema( 'Best lyrics', $schema ) );
     143        $this->assertEquals( 1.10, rest_sanitize_value_from_schema( 1.10, $schema ) );
     144        $this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) );
     145    }
     146
     147    public function test_no_type() {
     148        $schema = array(
     149            'type' => null,
     150        );
     151        $this->assertEquals( 'Nothing', rest_sanitize_value_from_schema( 'Nothing', $schema ) );
     152        $this->assertEquals( 1.10, rest_sanitize_value_from_schema( 1.10, $schema ) );
     153        $this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) );
     154    }
    113155}
Note: See TracChangeset for help on using the changeset viewer.