Make WordPress Core


Ignore:
Timestamp:
09/27/2020 07:01:18 PM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support the multipleOf JSON Schema keyword.

Props yakimun.
Fixes #51022.

File:
1 edited

Legend:

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

    r49053 r49063  
    434434                $this->assertWPError( $error );
    435435                $this->assertSame( 'param must be between 10 (inclusive) and 20 (inclusive)', $error->get_error_message() );
     436        }
     437
     438        /**
     439         * @ticket 51022
     440         *
     441         * @dataProvider data_multiply_of
     442         *
     443         * @param int|float $value
     444         * @param int|float $divisor
     445         * @param bool      $expected
     446         */
     447        public function test_numeric_multiple_of( $value, $divisor, $expected ) {
     448                $schema = array(
     449                        'type'       => 'number',
     450                        'multipleOf' => $divisor,
     451                );
     452
     453                $result = rest_validate_value_from_schema( $value, $schema );
     454
     455                if ( $expected ) {
     456                        $this->assertTrue( $result );
     457                } else {
     458                        $this->assertWPError( $result );
     459                }
     460        }
     461
     462        public function data_multiply_of() {
     463                return array(
     464                        array( 0, 2, true ),
     465                        array( 4, 2, true ),
     466                        array( 3, 1.5, true ),
     467                        array( 2.4, 1.2, true ),
     468                        array( 1, 2, false ),
     469                        array( 2, 1.5, false ),
     470                        array( 2.1, 1.5, false ),
     471                );
    436472        }
    437473
Note: See TracChangeset for help on using the changeset viewer.