Make WordPress Core

Ticket #51757: 51757.patch

File 51757.patch, 2.0 KB (added by ajlende, 5 years ago)
  • src/wp-includes/rest-api.php

    diff --git src/wp-includes/rest-api.php src/wp-includes/rest-api.php
    index 870bc6443c..fe111c92e6 100644
    function rest_get_allowed_schema_keywords() { 
    19081908                'uniqueItems',
    19091909                'anyOf',
    19101910                'oneOf',
     1911                'const',
    19111912        );
    19121913}
    19131914
    function rest_validate_value_from_schema( $value, $args, $param = '' ) { 
    21602161                }
    21612162        }
    21622163
     2164        if ( ! empty( $args['const'] ) ) {
     2165                if ( $value !== $args['const'] ) {
     2166                        /* translators: 1: Parameter, 2: The valid value. */
     2167                        return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not const %2$s.' ), $param, $args['const'] ) );
     2168                }
     2169        }
     2170
    21632171        if ( in_array( $args['type'], array( 'integer', 'number' ), true ) ) {
    21642172                if ( ! is_numeric( $value ) ) {
    21652173                        return new WP_Error(
  • tests/phpunit/tests/rest-api/rest-schema-validation.php

    diff --git tests/phpunit/tests/rest-api/rest-schema-validation.php tests/phpunit/tests/rest-api/rest-schema-validation.php
    index f33990ae59..c9cf4be7ed 100644
    class WP_Test_REST_Schema_Validation extends WP_UnitTestCase { 
    6262                $this->assertWPError( rest_validate_value_from_schema( 1123, $schema ) );
    6363        }
    6464
     65        /**
     66         * @ticket 51757
     67         */
     68        public function test_const() {
     69                $schema  = array(
     70                        'type'  => 'string',
     71                        'const' => 'ananas',
     72                );
     73                $valid   = 'ananas';
     74                $invalid = 'pineapple';
     75
     76                $this->assertTrue( rest_validate_value_from_schema( $valid, $schema ) );
     77                $this->assertWPError( rest_validate_value_from_schema( $invalid, $schema ) );
     78
     79                // By definition, const is functionally equivalent to an enum with a single value.
     80                // However, they produce different error messages, so the invalid isn't tested.
     81                $enum_schema = array(
     82                        'type'  => 'string',
     83                        'enum' => array( 'ananas' ),
     84                );
     85                $this->assertSame(
     86                        rest_validate_value_from_schema( $valid, $schema ),
     87                        rest_validate_value_from_schema( $valid, $enum_schema )
     88                );
     89        }
     90
    6591        public function test_format_email() {
    6692                $schema = array(
    6793                        'type'   => 'string',