Make WordPress Core


Ignore:
Timestamp:
09/23/2019 05:24:58 PM (5 years ago)
Author:
kadamwhite
Message:

REST API: Pass "null" as the post date property to reset post to initial "floating" date value.

Props TimothyBlynJacobs, adamsilverstein, jnylen0, mnelson4.
Fixes #44975.

File:
1 edited

Legend:

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

    r44703 r46249  
    293293        $this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) );
    294294    }
     295
     296    public function test_nullable_date() {
     297        $schema = array(
     298            'type'   => array( 'string', 'null' ),
     299            'format' => 'date-time',
     300        );
     301
     302        $this->assertNull( rest_sanitize_value_from_schema( null, $schema ) );
     303        $this->assertEquals( '2019-09-19T18:00:00', rest_sanitize_value_from_schema( '2019-09-19T18:00:00', $schema ) );
     304        $this->assertNull( rest_sanitize_value_from_schema( 'lalala', $schema ) );
     305    }
     306
     307    public function test_object_or_string() {
     308        $schema = array(
     309            'type'       => array( 'object', 'string' ),
     310            'properties' => array(
     311                'raw' => array(
     312                    'type' => 'string',
     313                ),
     314            ),
     315        );
     316
     317        $this->assertEquals( 'My Value', rest_sanitize_value_from_schema( 'My Value', $schema ) );
     318        $this->assertEquals( array( 'raw' => 'My Value' ), rest_sanitize_value_from_schema( array( 'raw' => 'My Value' ), $schema ) );
     319        $this->assertNull( rest_sanitize_value_from_schema( array( 'raw' => 1 ), $schema ) );
     320    }
     321
     322    public function test_object_or_bool() {
     323        $schema = array(
     324            'type'       => array( 'object', 'boolean' ),
     325            'properties' => array(
     326                'raw' => array(
     327                    'type' => 'boolean',
     328                ),
     329            ),
     330        );
     331
     332        $this->assertTrue( rest_sanitize_value_from_schema( true, $schema ) );
     333        $this->assertTrue( rest_sanitize_value_from_schema( '1', $schema ) );
     334        $this->assertTrue( rest_sanitize_value_from_schema( 1, $schema ) );
     335
     336        $this->assertFalse( rest_sanitize_value_from_schema( false, $schema ) );
     337        $this->assertFalse( rest_sanitize_value_from_schema( '0', $schema ) );
     338        $this->assertFalse( rest_sanitize_value_from_schema( 0, $schema ) );
     339
     340        $this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => true ), $schema ) );
     341        $this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => '1' ), $schema ) );
     342        $this->assertEquals( array( 'raw' => true ), rest_sanitize_value_from_schema( array( 'raw' => 1 ), $schema ) );
     343
     344        $this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => false ), $schema ) );
     345        $this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => '0' ), $schema ) );
     346        $this->assertEquals( array( 'raw' => false ), rest_sanitize_value_from_schema( array( 'raw' => 0 ), $schema ) );
     347
     348        $this->assertNull( rest_sanitize_value_from_schema( array( 'raw' => 'something non boolean' ), $schema ) );
     349    }
    295350}
Note: See TracChangeset for help on using the changeset viewer.