Make WordPress Core


Ignore:
Timestamp:
09/07/2020 02:35:52 AM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Extract WP_REST_Controller::get_endpoint_args_for_item_schema() to a standalone function.

This method is useful whenever a JSON Schema needs to be converted to a format suitable for argument validation with WP_REST_Request. Moving the logic into a standalone function allows developers to use it outside of the WP_REST_Controller context.

Props pentatonicfunk.
Fixes #50876.

File:
1 edited

Legend:

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

    r48939 r48951  
    223223    }
    224224
     225    /**
     226     * @ticket 50876
     227     */
     228    public function test_get_endpoint_args_for_item_schema() {
     229        $controller = new WP_REST_Test_Controller();
     230        $args       = $controller->get_endpoint_args_for_item_schema();
     231
     232        $this->assertArrayHasKey( 'somestring', $args );
     233        $this->assertArrayHasKey( 'someinteger', $args );
     234        $this->assertArrayHasKey( 'someboolean', $args );
     235        $this->assertArrayHasKey( 'someurl', $args );
     236        $this->assertArrayHasKey( 'somedate', $args );
     237        $this->assertArrayHasKey( 'someemail', $args );
     238        $this->assertArrayHasKey( 'somehex', $args );
     239        $this->assertArrayHasKey( 'someuuid', $args );
     240        $this->assertArrayHasKey( 'someenum', $args );
     241        $this->assertArrayHasKey( 'someargoptions', $args );
     242        $this->assertArrayHasKey( 'somedefault', $args );
     243        $this->assertArrayHasKey( 'somearray', $args );
     244        $this->assertArrayHasKey( 'someobject', $args );
     245    }
     246
    225247    public function test_get_endpoint_args_for_item_schema_description() {
    226248        $controller = new WP_REST_Test_Controller();
    227         $args       = $controller->get_endpoint_args_for_item_schema();
    228         $this->assertSame( 'A pretty string.', $args['somestring']['description'] );
     249        $args       = rest_get_endpoint_args_for_schema( $controller->get_item_schema() );
     250
     251        $this->assertEquals( 'A pretty string.', $args['somestring']['description'] );
    229252        $this->assertFalse( isset( $args['someinteger']['description'] ) );
    230253    }
     
    233256
    234257        $controller = new WP_REST_Test_Controller();
    235         $args       = $controller->get_endpoint_args_for_item_schema();
     258        $args       = rest_get_endpoint_args_for_schema( $controller->get_item_schema() );
    236259
    237260        $this->assertFalse( $args['someargoptions']['required'] );
     
    242265
    243266        $controller = new WP_REST_Test_Controller();
    244 
    245         $args = $controller->get_endpoint_args_for_item_schema();
     267        $args       = rest_get_endpoint_args_for_schema( $controller->get_item_schema() );
    246268
    247269        $this->assertSame( 'a', $args['somedefault']['default'] );
     
    254276
    255277        $controller = new WP_REST_Test_Controller();
    256         $args       = $controller->get_endpoint_args_for_item_schema();
     278        $args       = rest_get_endpoint_args_for_schema( $controller->get_item_schema() );
    257279
    258280        foreach ( array( 'minLength', 'maxLength', 'pattern' ) as $property ) {
Note: See TracChangeset for help on using the changeset viewer.