Make WordPress Core


Ignore:
Timestamp:
02/24/2016 04:00:12 AM (11 years ago)
Author:
rmccue
Message:

REST API: Add WP_REST_Request::from_url()

Allows converting a REST URL into a Request object.

Props danielbachhuber.
Fixes #35803.

File:
1 edited

Legend:

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

    r35890 r36673  
    421421                return new WP_Error( 'some-error', 'This is not valid!' );
    422422        }
     423
     424        public function data_from_url() {
     425                return array(
     426                        array(
     427                                'permalink_structure' => '/%post_name%/',
     428                                'original_url'        => 'http://' . WP_TESTS_DOMAIN . '/wp-json/wp/v2/posts/1?foo=bar',
     429                        ),
     430                        array(
     431                                'permalink_structure' => '',
     432                                'original_url'        => 'http://' . WP_TESTS_DOMAIN . '/?rest_route=%2Fwp%2Fv2%2Fposts%2F1&foo=bar',
     433                        ),
     434                );
     435        }
     436
     437        /**
     438         * @dataProvider data_from_url
     439         */
     440        public function test_from_url( $permalink_structure, $original_url ) {
     441                update_option( 'permalink_structure', $permalink_structure );
     442                $url = add_query_arg( 'foo', 'bar', rest_url( '/wp/v2/posts/1' ) );
     443                $this->assertEquals( $original_url, $url );
     444                $request = WP_REST_Request::from_url( $url );
     445                $this->assertInstanceOf( 'WP_REST_Request', $request );
     446                $this->assertEquals( '/wp/v2/posts/1', $request->get_route() );
     447                $this->assertEqualSets( array(
     448                        'foo' => 'bar',
     449                ), $request->get_query_params() );
     450        }
     451
     452        /**
     453         * @dataProvider data_from_url
     454         */
     455        public function test_from_url_invalid( $permalink_structure ) {
     456                update_option( 'permalink_structure', $permalink_structure );
     457                $using_site = site_url( '/wp/v2/posts/1' );
     458                $request = WP_REST_Request::from_url( $using_site );
     459                $this->assertFalse( $request );
     460
     461                $using_home = home_url( '/wp/v2/posts/1' ) ;
     462                $request = WP_REST_Request::from_url( $using_home );
     463                $this->assertFalse( $request );
     464        }
    423465}
Note: See TracChangeset for help on using the changeset viewer.