REST API: Add WP_REST_Request::from_url()

Allows converting a REST URL into a Request object.

Props danielbachhuber.
Fixes #35803.


git-svn-id: https://develop.svn.wordpress.org/trunk@36673 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan McCue
2016-02-24 04:00:12 +00:00
parent 6165ea0b98
commit 92d2be6a3d
2 changed files with 86 additions and 0 deletions

View File

@@ -420,4 +420,46 @@ class Tests_REST_Request extends WP_UnitTestCase {
public function _return_wp_error_on_validate_callback() {
return new WP_Error( 'some-error', 'This is not valid!' );
}
public function data_from_url() {
return array(
array(
'permalink_structure' => '/%post_name%/',
'original_url' => 'http://' . WP_TESTS_DOMAIN . '/wp-json/wp/v2/posts/1?foo=bar',
),
array(
'permalink_structure' => '',
'original_url' => 'http://' . WP_TESTS_DOMAIN . '/?rest_route=%2Fwp%2Fv2%2Fposts%2F1&foo=bar',
),
);
}
/**
* @dataProvider data_from_url
*/
public function test_from_url( $permalink_structure, $original_url ) {
update_option( 'permalink_structure', $permalink_structure );
$url = add_query_arg( 'foo', 'bar', rest_url( '/wp/v2/posts/1' ) );
$this->assertEquals( $original_url, $url );
$request = WP_REST_Request::from_url( $url );
$this->assertInstanceOf( 'WP_REST_Request', $request );
$this->assertEquals( '/wp/v2/posts/1', $request->get_route() );
$this->assertEqualSets( array(
'foo' => 'bar',
), $request->get_query_params() );
}
/**
* @dataProvider data_from_url
*/
public function test_from_url_invalid( $permalink_structure ) {
update_option( 'permalink_structure', $permalink_structure );
$using_site = site_url( '/wp/v2/posts/1' );
$request = WP_REST_Request::from_url( $using_site );
$this->assertFalse( $request );
$using_home = home_url( '/wp/v2/posts/1' ) ;
$request = WP_REST_Request::from_url( $using_home );
$this->assertFalse( $request );
}
}