Make WordPress Core


Ignore:
Timestamp:
07/07/2020 03:20:34 AM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Add support for the uniqueItems keyword.

Props sorenbronsted.
Fixes #48821.

File:
1 edited

Legend:

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

    r48306 r48357  
    508508
    509509    /**
    510      * @ticket 48818
     510     * @ticket       48818
    511511     *
    512512     * @dataProvider data_required_property
     
    536536
    537537    /**
    538      * @ticket 48818
     538     * @ticket       48818
    539539     *
    540540     * @dataProvider data_required_property
     
    578578
    579579    /**
    580      * @ticket 48818
     580     * @ticket       48818
    581581     *
    582582     * @dataProvider data_required_nested_property
     
    611611
    612612    /**
    613      * @ticket 48818
     613     * @ticket       48818
    614614     *
    615615     * @dataProvider data_required_nested_property
     
    670670
    671671    /**
    672      * @ticket 48818
     672     * @ticket       48818
    673673     *
    674674     * @dataProvider data_required_deeply_nested_property
     
    710710
    711711    /**
    712      * @ticket 48818
     712     * @ticket       48818
    713713     *
    714714     * @dataProvider data_required_deeply_nested_property
     
    750750
    751751    /**
    752      * @ticket 48818
     752     * @ticket       48818
    753753     *
    754754     * @dataProvider data_required_deeply_nested_property
     
    906906        $this->assertWPError( rest_validate_value_from_schema( 'foobar', $schema ) );
    907907    }
     908
     909    /**
     910     * @ticket       48821
     911     *
     912     * @dataProvider data_unique_items
     913     */
     914    public function test_unique_items( $test, $suite ) {
     915        $test_description = $suite['description'] . ': ' . $test['description'];
     916        $message          = $test_description . ': ' . var_export( $test['data'], true );
     917
     918        $valid = rest_validate_value_from_schema( $test['data'], $suite['schema'] );
     919
     920        if ( $test['valid'] ) {
     921            $this->assertTrue( $valid, $message );
     922        } else {
     923            $this->assertWPError( $valid, $message );
     924        }
     925    }
     926
     927    public function data_unique_items() {
     928        $all_types = array( 'object', 'array', 'null', 'number', 'integer', 'boolean', 'string' );
     929
     930        // the following test suites is not supported at the moment
     931        $skip   = array(
     932            'uniqueItems with an array of items',
     933            'uniqueItems with an array of items and additionalItems=false',
     934            'uniqueItems=false with an array of items',
     935            'uniqueItems=false with an array of items and additionalItems=false',
     936        );
     937        $suites = json_decode( file_get_contents( __DIR__ . '/json_schema_test_suite/uniqueitems.json' ), true );
     938
     939        $tests = array();
     940
     941        foreach ( $suites as $suite ) {
     942            if ( in_array( $suite['description'], $skip, true ) ) {
     943                continue;
     944            }
     945            // type is required for our implementation
     946            if ( ! isset( $suite['schema']['type'] ) ) {
     947                $suite['schema']['type'] = 'array';
     948            }
     949            // items is required for our implementation
     950            if ( ! isset( $suite['schema']['items'] ) ) {
     951                $suite['schema']['items'] = array(
     952                    'type'  => $all_types,
     953                    'items' => array(
     954                        'type' => $all_types,
     955                    ),
     956                );
     957            }
     958            foreach ( $suite['tests'] as $test ) {
     959                $tests[] = array( $test, $suite );
     960            }
     961        }
     962
     963        return $tests;
     964    }
     965
     966    /**
     967     * @ticket 48821
     968     */
     969    public function test_unique_items_deep_objects() {
     970        $schema = array(
     971            'type'        => 'array',
     972            'uniqueItems' => true,
     973            'items'       => array(
     974                'type'       => 'object',
     975                'properties' => array(
     976                    'release' => array(
     977                        'type'       => 'object',
     978                        'properties' => array(
     979                            'name'    => array(
     980                                'type' => 'string',
     981                            ),
     982                            'version' => array(
     983                                'type' => 'string',
     984                            ),
     985                        ),
     986                    ),
     987                ),
     988            ),
     989        );
     990
     991        $data = array(
     992            array(
     993                'release' => array(
     994                    'name'    => 'Kirk',
     995                    'version' => '5.3',
     996                ),
     997            ),
     998            array(
     999                'release' => array(
     1000                    'version' => '5.3',
     1001                    'name'    => 'Kirk',
     1002                ),
     1003            ),
     1004        );
     1005
     1006        $this->assertWPError( rest_validate_value_from_schema( $data, $schema ) );
     1007
     1008        $data[0]['release']['version'] = '5.3.0';
     1009        $this->assertTrue( rest_validate_value_from_schema( $data, $schema ) );
     1010    }
     1011
     1012    /**
     1013     * @ticket 48821
     1014     */
     1015    public function test_unique_items_deep_arrays() {
     1016        $schema = array(
     1017            'type'        => 'array',
     1018            'uniqueItems' => true,
     1019            'items'       => array(
     1020                'type'  => 'array',
     1021                'items' => array(
     1022                    'type' => 'string',
     1023                ),
     1024            ),
     1025        );
     1026
     1027        $data = array(
     1028            array(
     1029                'Kirk',
     1030                'Jaco',
     1031            ),
     1032            array(
     1033                'Kirk',
     1034                'Jaco',
     1035            ),
     1036        );
     1037
     1038        $this->assertWPError( rest_validate_value_from_schema( $data, $schema ) );
     1039
     1040        $data[1] = array_reverse( $data[1] );
     1041        $this->assertTrue( rest_validate_value_from_schema( $data, $schema ) );
     1042    }
    9081043}
Note: See TracChangeset for help on using the changeset viewer.