Make WordPress Core


Ignore:
Timestamp:
07/04/2020 07:51:10 PM (4 years ago)
Author:
TimothyBlynJacobs
Message:

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.

File:
1 edited

Legend:

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

    r47753 r48300  
    313313
    314314    public function test_type_unknown() {
     315        $this->setExpectedIncorrectUsage( 'rest_sanitize_value_from_schema' );
     316
    315317        $schema = array(
    316318            'type' => 'lalala',
     
    322324
    323325    public function test_no_type() {
     326        $this->setExpectedIncorrectUsage( 'rest_sanitize_value_from_schema' );
     327
    324328        $schema = array(
    325329            'type' => null,
     
    339343        $this->assertEquals( '2019-09-19T18:00:00', rest_sanitize_value_from_schema( '2019-09-19T18:00:00', $schema ) );
    340344        $this->assertNull( rest_sanitize_value_from_schema( 'lalala', $schema ) );
     345    }
     346
     347    /**
     348     * @ticket 50189
     349     */
     350    public function test_format_validation_is_skipped_if_non_string_type() {
     351        $schema = array(
     352            'type'   => 'array',
     353            'format' => 'hex-color',
     354        );
     355        $this->assertEquals( array( '#fff' ), rest_sanitize_value_from_schema( '#fff', $schema ) );
     356        $this->assertEquals( array( '#qrst' ), rest_sanitize_value_from_schema( '#qrst', $schema ) );
     357    }
     358
     359    /**
     360     * @ticket 50189
     361     */
     362    public function test_format_validation_is_applied_if_missing_type() {
     363        $this->expectException( 'PHPUnit_Framework_Error_Notice' ); // For the undefined index.
     364        $this->setExpectedIncorrectUsage( 'rest_sanitize_value_from_schema' );
     365
     366        $schema = array( 'format' => 'hex-color' );
     367        $this->assertEquals( '#abc', rest_sanitize_value_from_schema( '#abc', $schema ) );
     368        $this->assertEquals( '', rest_sanitize_value_from_schema( '#jkl', $schema ) );
     369    }
     370
     371    /**
     372     * @ticket 50189
     373     */
     374    public function test_format_validation_is_applied_if_unknown_type() {
     375        $this->setExpectedIncorrectUsage( 'rest_sanitize_value_from_schema' );
     376
     377        $schema = array(
     378            'format' => 'hex-color',
     379            'type'   => 'str',
     380        );
     381        $this->assertEquals( '#abc', rest_sanitize_value_from_schema( '#abc', $schema ) );
     382        $this->assertEquals( '', rest_sanitize_value_from_schema( '#jkl', $schema ) );
    341383    }
    342384
Note: See TracChangeset for help on using the changeset viewer.