Make WordPress Core


Ignore:
Timestamp:
10/01/2020 02:47:08 AM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support the patternProperties JSON Schema keyword.

Props yakimun.
Fixes #51024.

File:
1 edited

Legend:

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

    r49008 r49082  
    235235                $schema
    236236            )
     237        );
     238    }
     239
     240    /**
     241     * @ticket 51024
     242     *
     243     * @dataProvider data_type_object_pattern_properties
     244     *
     245     * @param array $pattern_properties
     246     * @param array $value
     247     * @param array $expected
     248     */
     249    public function test_type_object_pattern_properties( $pattern_properties, $value, $expected ) {
     250        $schema = array(
     251            'type'                 => 'object',
     252            'properties'           => array(
     253                'propA' => array( 'type' => 'string' ),
     254            ),
     255            'patternProperties'    => $pattern_properties,
     256            'additionalProperties' => false,
     257        );
     258
     259        $this->assertSame( $expected, rest_sanitize_value_from_schema( $value, $schema ) );
     260    }
     261
     262    /**
     263     * @return array
     264     */
     265    public function data_type_object_pattern_properties() {
     266        return array(
     267            array( array(), array(), array() ),
     268            array( array(), array( 'propA' => 'a' ), array( 'propA' => 'a' ) ),
     269            array(
     270                array(),
     271                array(
     272                    'propA' => 'a',
     273                    'propB' => 'b',
     274                ),
     275                array( 'propA' => 'a' ),
     276            ),
     277            array(
     278                array(
     279                    'propB' => array( 'type' => 'string' ),
     280                ),
     281                array( 'propA' => 'a' ),
     282                array( 'propA' => 'a' ),
     283            ),
     284            array(
     285                array(
     286                    'propB' => array( 'type' => 'string' ),
     287                ),
     288                array(
     289                    'propA' => 'a',
     290                    'propB' => 'b',
     291                ),
     292                array(
     293                    'propA' => 'a',
     294                    'propB' => 'b',
     295                ),
     296            ),
     297            array(
     298                array(
     299                    '.*C' => array( 'type' => 'string' ),
     300                ),
     301                array(
     302                    'propA' => 'a',
     303                    'propC' => 'c',
     304                ),
     305                array(
     306                    'propA' => 'a',
     307                    'propC' => 'c',
     308                ),
     309            ),
     310            array(
     311                array(
     312                    '[0-9]' => array( 'type' => 'integer' ),
     313                ),
     314                array(
     315                    'propA' => 'a',
     316                    'prop0' => '0',
     317                ),
     318                array(
     319                    'propA' => 'a',
     320                    'prop0' => 0,
     321                ),
     322            ),
     323            array(
     324                array(
     325                    '.+' => array( 'type' => 'string' ),
     326                ),
     327                array(
     328                    ''      => '',
     329                    'propA' => 'a',
     330                ),
     331                array( 'propA' => 'a' ),
     332            ),
    237333        );
    238334    }
Note: See TracChangeset for help on using the changeset viewer.