Make WordPress Core

Changeset 53315


Ignore:
Timestamp:
04/29/2022 04:53:55 PM (4 years ago)
Author:
johnbillion
Message:

REST API: Correct the schema for the wp/v2/block-directory/search endpoint.

Props zieladam, TimothyBlynJacobs, spacedmonkey, johnbillion

Fixes #53621

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php

    r52582 r53315  
    240240                'rating'              => array(
    241241                    'description' => __( 'The star rating of the block.' ),
    242                     'type'        => 'integer',
     242                    'type'        => 'number',
    243243                    'context'     => array( 'view' ),
    244244                ),
     
    250250                'active_installs'     => array(
    251251                    'description' => __( 'The number sites that have activated this block.' ),
    252                     'type'        => 'string',
     252                    'type'        => 'integer',
    253253                    'context'     => array( 'view' ),
    254254                ),
    255255                'author_block_rating' => array(
    256256                    'description' => __( 'The average rating of blocks published by the same author.' ),
    257                     'type'        => 'integer',
     257                    'type'        => 'number',
    258258                    'context'     => array( 'view' ),
    259259                ),
  • trunk/tests/phpunit/tests/rest-api/rest-block-directory-controller.php

    r52146 r53315  
    211211        $this->assertArrayHasKey( 'last_updated', $properties );
    212212        $this->assertArrayHasKey( 'humanized_updated', $properties );
     213    }
     214
     215    /**
     216     * @ticket 53621
     217     */
     218    public function test_get_items_response_conforms_to_schema() {
     219        wp_set_current_user( self::$admin_id );
     220        $plugin = $this->get_mock_plugin();
     221
     222        // Fetch the block directory schema.
     223        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/block-directory/search' );
     224        $schema  = rest_get_server()->dispatch( $request )->get_data()['schema'];
     225
     226        add_filter(
     227            'plugins_api',
     228            static function () use ( $plugin ) {
     229                return (object) array(
     230                    'info'    =>
     231                        array(
     232                            'page'    => 1,
     233                            'pages'   => 1,
     234                            'results' => 1,
     235                        ),
     236                    'plugins' => array(
     237                        $plugin,
     238                    ),
     239                );
     240            }
     241        );
     242
     243        // Fetch a block plugin.
     244        $request = new WP_REST_Request( 'GET', '/wp/v2/block-directory/search' );
     245        $request->set_query_params( array( 'term' => 'cache' ) );
     246
     247        $result = rest_get_server()->dispatch( $request );
     248        $data   = $result->get_data();
     249
     250        $valid = rest_validate_value_from_schema( $data[0], $schema );
     251
     252        $this->assertNotWPError( $valid );
    213253    }
    214254
Note: See TracChangeset for help on using the changeset viewer.