Make WordPress Core

Ticket #40704: 40704.2.diff

File 40704.2.diff, 1.8 KB (added by jnylen0, 9 years ago)

Add unit test; remove unneeded continue

  • src/wp-includes/rest-api/class-wp-rest-server.php

    diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php
    index 2ec8ccd..e004844 100644
    a b class WP_REST_Server { 
    842842                $path   = $request->get_route();
    843843
    844844                foreach ( $this->get_routes() as $route => $handlers ) {
    845                         $match = preg_match( '@^' . $route . '$@i', $path, $args );
     845                        $match = preg_match( '@^' . $route . '$@i', $path, $matches );
    846846
    847847                        if ( ! $match ) {
    848848                                continue;
    849849                        }
    850850
     851                        $args = array();
     852                        foreach ( $matches as $param => $value ) {
     853                                if ( ! is_int( $param ) ) {
     854                                        $args[ $param ] = $value;
     855                                }
     856                        }
     857
    851858                        foreach ( $handlers as $handler ) {
    852859                                $callback  = $handler['callback'];
    853860                                $response = null;
  • tests/phpunit/tests/rest-api/rest-server.php

    diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php
    index 1cd9e68..cb75f4e 100644
    a b class Tests_REST_Server extends WP_Test_REST_TestCase { 
    162162                $this->assertEquals( 200, $response->get_status() );
    163163        }
    164164
     165        public function test_url_params_no_numeric_keys() {
     166
     167                $this->server->register_route( 'test', '/test/(?P<data>.*)', array(
     168                        array(
     169                                'methods'  => WP_REST_Server::READABLE,
     170                                'callback' => '__return_false',
     171                                'args'     => array(
     172                                        'data' => array(),
     173                                ),
     174                        ),
     175                ) );
     176
     177                $request = new WP_REST_Request( 'GET', '/test/some-value' );
     178                $this->server->dispatch( $request );
     179                $this->assertEquals( array( 'data' => 'some-value' ), $request->get_params() );
     180        }
     181
    165182        /**
    166183         * Pass a capability which the user does not have, this should
    167184         * result in a 403 error.