Tests: Replace use of $this->server with rest_get_server() for better memory recycling.

Props danielbachhuber.
Fixes #41641.

git-svn-id: https://develop.svn.wordpress.org/trunk@42724 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2018-02-21 16:24:30 +00:00
parent 05e88c58cd
commit 9e31509293
19 changed files with 1024 additions and 1012 deletions

View File

@@ -45,15 +45,18 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$this->server = $wp_rest_server = new Spy_REST_Server();
$wp_rest_server = new Spy_REST_Server;
do_action( 'rest_api_init', $wp_rest_server );
do_action( 'rest_api_init', $this->server );
add_filter( 'pre_http_request', array( $this, 'mock_embed_request' ), 10, 3 );
$this->request_count = 0;
}
public function tearDown() {
parent::tearDown();
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$wp_rest_server = null;
remove_filter( 'pre_http_request', array( $this, 'mock_embed_request' ), 10 );
}
@@ -179,7 +182,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
public function test_route_availability() {
// Check the route was registered correctly.
$filtered_routes = $this->server->get_routes();
$filtered_routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/oembed/1.0/embed', $filtered_routes );
$route = $filtered_routes['/oembed/1.0/embed'];
$this->assertCount( 1, $route );
@@ -200,7 +203,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
function test_request_with_wrong_method() {
$request = new WP_REST_Request( 'POST', '/oembed/1.0/embed' );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'rest_no_route', $data['code'] );
@@ -209,7 +212,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
function test_request_without_url_param() {
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'rest_missing_callback_param', $data['code'] );
@@ -220,7 +223,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
$request->set_param( 'url', 'http://google.com/' );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'oembed_invalid_url', $data['code'] );
@@ -233,7 +236,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request->set_param( 'url', get_permalink( $post_id ) );
$request->set_param( 'format', 'random' );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertInternalType( 'array', $data );
@@ -257,7 +260,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request->set_param( 'url', get_permalink( $post->ID ) );
$request->set_param( 'maxwidth', 400 );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertInternalType( 'array', $data );
@@ -300,7 +303,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request->set_param( 'url', home_url() );
$request->set_param( 'maxwidth', 400 );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertInternalType( 'array', $data );
@@ -345,7 +348,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request->set_param( 'format', 'xml' );
$request->set_param( 'maxwidth', 400 );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertInternalType( 'array', $data );
@@ -388,7 +391,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request->set_param( 'url', get_permalink( $post->ID ) );
$request->set_param( 'maxwidth', 400 );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertInternalType( 'array', $data );
@@ -414,8 +417,8 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request->set_param( 'url', get_permalink( $post->ID ) );
$request->set_param( 'format', 'xml' );
$response = $this->server->dispatch( $request );
$output = get_echo( '_oembed_rest_pre_serve_request', array( true, $response, $request, $this->server ) );
$response = rest_get_server()->dispatch( $request );
$output = get_echo( '_oembed_rest_pre_serve_request', array( true, $response, $request, rest_get_server() ) );
$xml = simplexml_load_string( $output );
$this->assertInstanceOf( 'SimpleXMLElement', $xml );
@@ -428,9 +431,9 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request->set_param( 'url', get_permalink( $post->ID ) );
$request->set_param( 'format', 'json' );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, $this->server ) );
$this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, rest_get_server() ) );
}
function test_rest_pre_serve_request_wrong_method() {
@@ -440,9 +443,9 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request->set_param( 'url', get_permalink( $post->ID ) );
$request->set_param( 'format', 'xml' );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, $this->server ) );
$this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, rest_get_server() ) );
}
function test_get_oembed_endpoint_url() {
@@ -477,7 +480,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
public function test_proxy_without_permission() {
// Test without a login.
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
@@ -485,7 +488,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
wp_set_current_user( self::$subscriber );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', self::INVALID_OEMBED_URL );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 403, $response->get_status() );
$data = $response->get_data();
@@ -496,7 +499,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
wp_set_current_user( self::$editor );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', self::INVALID_OEMBED_URL );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'oembed_invalid_url', $data['code'] );
@@ -506,7 +509,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
wp_set_current_user( self::$editor );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'type', 'xml' );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
}
@@ -518,12 +521,12 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request->set_param( 'maxwidth', 456 );
$request->set_param( 'maxheight', 789 );
$request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, $this->request_count );
// Subsequent request is cached and so it should not cause a request.
$this->server->dispatch( $request );
rest_get_server()->dispatch( $request );
$this->assertEquals( 1, $this->request_count );
// Rest with another user should also be cached.
@@ -533,7 +536,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) );
$request->set_param( 'maxwidth', 456 );
$request->set_param( 'maxheight', 789 );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 1, $this->request_count );
// Test data object.
@@ -554,7 +557,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', self::INVALID_OEMBED_URL );
$request->set_param( 'discover', 0 );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
$this->assertEquals( 0, $this->request_count );
}
@@ -565,7 +568,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
// For an unkown provider, a discovery request should happen.
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', self::INVALID_OEMBED_URL );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
$this->assertEquals( 1, $this->request_count );
}
@@ -576,7 +579,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$request->set_param( 'url', self::INVALID_OEMBED_URL );
$request->set_param( 'discover', 'notaboolean' );
$response = $this->server->dispatch( $request );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
$data = $response->get_data();