Changeset 35436 for trunk/tests/phpunit/tests/oembed/controller.php
- Timestamp:
- 10/29/2015 10:50:13 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/oembed/controller.php
r35408 r35436 3 3 /** 4 4 * @group oembed 5 * @group restapi 5 6 */ 6 7 class Test_oEmbed_Controller extends WP_UnitTestCase { 8 /** 9 * @var WP_REST_Server 10 */ 11 protected $server; 12 13 public function setUp() { 14 parent::setUp(); 15 16 /** @var WP_REST_Server $wp_rest_server */ 17 global $wp_rest_server; 18 $this->server = $wp_rest_server = new Spy_REST_Server(); 19 20 do_action( 'rest_api_init', $this->server ); 21 } 22 23 function test_wp_oembed_ensure_format() { 24 $this->assertEquals( 'json', wp_oembed_ensure_format( 'json' ) ); 25 $this->assertEquals( 'xml', wp_oembed_ensure_format( 'xml' ) ); 26 $this->assertEquals( 'json', wp_oembed_ensure_format( 123 ) ); 27 $this->assertEquals( 'json', wp_oembed_ensure_format( 'random' ) ); 28 $this->assertEquals( 'json', wp_oembed_ensure_format( array() ) ); 29 } 30 31 function test_oembed_create_xml() { 32 $actual = _oembed_create_xml( array( 33 'foo' => 'bar', 34 'bar' => 'baz', 35 'ping' => 'pong', 36 ) ); 37 38 $expected = '<oembed><foo>bar</foo><bar>baz</bar><ping>pong</ping></oembed>'; 39 40 $this->assertStringEndsWith( $expected, trim( $actual ) ); 41 42 $actual = _oembed_create_xml( array( 43 'foo' => array( 44 'bar' => 'baz', 45 ), 46 'ping' => 'pong', 47 ) ); 48 49 $expected = '<oembed><foo><bar>baz</bar></foo><ping>pong</ping></oembed>'; 50 51 $this->assertStringEndsWith( $expected, trim( $actual ) ); 52 53 $actual = _oembed_create_xml( array( 54 'foo' => array( 55 'bar' => array( 56 'ping' => 'pong', 57 ), 58 ), 59 'hello' => 'world', 60 ) ); 61 62 $expected = '<oembed><foo><bar><ping>pong</ping></bar></foo><hello>world</hello></oembed>'; 63 64 $this->assertStringEndsWith( $expected, trim( $actual ) ); 65 66 $actual = _oembed_create_xml( array( 67 array( 68 'foo' => array( 69 'bar', 70 ), 71 ), 72 'helloworld', 73 ) ); 74 75 $expected = '<oembed><oembed><foo><oembed>bar</oembed></foo></oembed><oembed>helloworld</oembed></oembed>'; 76 77 $this->assertStringEndsWith( $expected, trim( $actual ) ); 78 } 79 80 public function test_route_availability() { 81 // Check the route was registered correctly. 82 $filtered_routes = $this->server->get_routes(); 83 $this->assertArrayHasKey( '/oembed/1.0/embed', $filtered_routes ); 84 $route = $filtered_routes['/oembed/1.0/embed']; 85 $this->assertCount( 1, $route ); 86 $this->assertArrayHasKey( 'callback', $route[0] ); 87 $this->assertArrayHasKey( 'methods', $route[0] ); 88 $this->assertArrayHasKey( 'args', $route[0] ); 89 } 90 91 function test_request_with_wrong_method() { 92 $request = new WP_REST_Request( 'POST', '/oembed/1.0/embed' ); 93 94 $response = $this->server->dispatch( $request ); 95 $data = $response->get_data(); 96 97 $this->assertEquals( 'rest_no_route', $data[0]['code'] ); 98 } 99 100 function test_request_without_url_param() { 101 $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' ); 102 103 $response = $this->server->dispatch( $request ); 104 $data = $response->get_data(); 105 106 $this->assertEquals( 'rest_missing_callback_param', $data[0]['code'] ); 107 $this->assertEquals( 'url', $data[0]['data']['params'][0] ); 108 } 109 7 110 function test_request_with_bad_url() { 8 $request = array( 9 'url' => '', 10 'format' => 'json', 11 'maxwidth' => 600, 12 ); 13 14 $legacy_controller = new WP_oEmbed_Controller(); 15 16 $this->assertEquals( get_status_header_desc( 404 ), $legacy_controller->dispatch( $request ) ); 111 $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' ); 112 $request->set_param( 'url', 'http://google.com/' ); 113 114 $response = $this->server->dispatch( $request ); 115 $data = $response->get_data(); 116 117 $this->assertEquals( 'oembed_invalid_url', $data[0]['code'] ); 118 } 119 120 function test_request_invalid_format() { 121 $post_id = $this->factory()->post->create(); 122 123 $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' ); 124 $request->set_param( 'url', get_permalink( $post_id ) ); 125 $request->set_param( 'format', 'random' ); 126 127 $response = $this->server->dispatch( $request ); 128 $data = $response->get_data(); 129 130 $this->assertInternalType( 'array', $data ); 131 $this->assertNotEmpty( $data ); 17 132 } 18 133 … … 26 141 ) ); 27 142 28 // WP_Query arguments. 29 $request = array( 30 'url' => get_permalink( $post->ID ), 31 'format' => 'json', 32 'maxwidth' => 400, 33 'callback' => '', 34 'oembed' => true, 35 ); 36 37 $legacy_controller = new WP_oEmbed_Controller(); 38 39 $data = json_decode( $legacy_controller->dispatch( $request ), true ); 40 41 $this->assertTrue( is_array( $data ) ); 143 $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' ); 144 $request->set_param( 'url', get_permalink( $post->ID ) ); 145 $request->set_param( 'maxwidth', 400 ); 146 147 $response = $this->server->dispatch( $request ); 148 $data = $response->get_data(); 149 150 $this->assertInternalType( 'array', $data ); 151 $this->assertNotEmpty( $data ); 42 152 43 153 $this->assertArrayHasKey( 'version', $data ); … … 60 170 } 61 171 62 function test_request_ jsonp() {172 function test_request_xml() { 63 173 $user = self::factory()->user->create_and_get( array( 64 174 'display_name' => 'John Doe', … … 69 179 ) ); 70 180 71 $request = array( 72 'url' => get_permalink( $post->ID ), 73 'format' => 'json', 74 'maxwidth' => 600, 75 'callback' => 'mycallback', 76 ); 77 78 $legacy_controller = new WP_oEmbed_Controller(); 79 80 $data = $legacy_controller->dispatch( $request ); 81 82 $this->assertEquals( 0, strpos( $data, '/**/mycallback(' ) ); 83 } 84 85 function test_request_jsonp_invalid_callback() { 86 $user = self::factory()->user->create_and_get( array( 87 'display_name' => 'John Doe', 88 ) ); 89 $post = self::factory()->post->create_and_get( array( 90 'post_author' => $user->ID, 91 'post_title' => 'Hello World', 92 ) ); 93 94 $request = array( 95 'url' => get_permalink( $post->ID ), 96 'format' => 'json', 97 'maxwidth' => 600, 98 'callback' => array( 'foo', 'bar' ), 99 ); 100 101 $legacy_controller = new WP_oEmbed_Controller(); 102 103 $data = $legacy_controller->dispatch( $request ); 104 105 $this->assertFalse( strpos( $data, '/**/' ) ); 106 } 107 108 function test_request_json_invalid_data() { 109 $request = array( 110 'callback' => '', 111 ); 112 113 $legacy_controller = new WP_oEmbed_Controller(); 114 115 $this->assertEquals( get_status_header_desc( 501 ), $legacy_controller->json_response( null, $request ) ); 116 $this->assertEquals( get_status_header_desc( 501 ), $legacy_controller->json_response( 123, $request ) ); 117 $this->assertEquals( get_status_header_desc( 501 ), $legacy_controller->json_response( array(), $request ) ); 118 } 119 120 function test_request_xml() { 121 $user = self::factory()->user->create_and_get( array( 122 'display_name' => 'John Doe', 123 ) ); 124 $post = self::factory()->post->create_and_get( array( 125 'post_author' => $user->ID, 126 'post_title' => 'Hello World', 127 ) ); 128 129 $request = array( 130 'url' => get_permalink( $post->ID ), 131 'format' => 'xml', 132 'maxwidth' => 400, 133 'callback' => '', 134 ); 135 136 $legacy_controller = new WP_oEmbed_Controller(); 137 138 $data = $legacy_controller->dispatch( $request ); 139 140 $data = simplexml_load_string( $data ); 141 $this->assertInstanceOf( 'SimpleXMLElement', $data ); 142 143 $data = (array) $data; 181 $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' ); 182 $request->set_param( 'url', get_permalink( $post->ID ) ); 183 $request->set_param( 'format', 'xml' ); 184 $request->set_param( 'maxwidth', 400 ); 185 186 $response = $this->server->dispatch( $request ); 187 $data = $response->get_data(); 188 189 $this->assertInternalType( 'array', $data ); 190 $this->assertNotEmpty( $data ); 144 191 145 192 $this->assertArrayHasKey( 'version', $data ); … … 162 209 } 163 210 164 function test_request_xml_invalid_data() {165 $legacy_controller = new WP_oEmbed_Controller();166 167 $this->assertEquals( get_status_header_desc( 501 ), $legacy_controller->xml_response( null ) );168 $this->assertEquals( get_status_header_desc( 501 ), $legacy_controller->xml_response( 123 ) );169 $this->assertEquals( get_status_header_desc( 501 ), $legacy_controller->xml_response( array() ) );170 }171 172 211 /** 173 212 * @group multisite … … 179 218 180 219 $child = self::factory()->blog->create(); 181 182 220 switch_to_blog( $child ); 183 221 … … 186 224 ) ); 187 225 188 $request = array( 189 'url' => get_permalink( $post->ID ), 190 'format' => 'json', 191 'maxwidth' => 600, 192 'callback' => '', 193 ); 194 195 $legacy_controller = new WP_oEmbed_Controller(); 196 197 $data = json_decode( $legacy_controller->dispatch( $request ), true ); 226 $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' ); 227 $request->set_param( 'url', get_permalink( $post->ID ) ); 228 $request->set_param( 'maxwidth', 400 ); 229 230 $response = $this->server->dispatch( $request ); 231 $data = $response->get_data(); 198 232 199 233 $this->assertInternalType( 'array', $data ); … … 203 237 } 204 238 239 function test_rest_pre_serve_request() { 240 $user = $this->factory()->user->create_and_get( array( 241 'display_name' => 'John Doe', 242 ) ); 243 $post = $this->factory()->post->create_and_get( array( 244 'post_author' => $user->ID, 245 'post_title' => 'Hello World', 246 ) ); 247 248 $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' ); 249 $request->set_param( 'url', get_permalink( $post->ID ) ); 250 $request->set_param( 'format', 'xml' ); 251 252 $response = $this->server->dispatch( $request ); 253 $output = get_echo( '_oembed_rest_pre_serve_request', array( true, $response, $request, $this->server ) ); 254 255 $xml = simplexml_load_string( $output ); 256 $this->assertInstanceOf( 'SimpleXMLElement', $xml ); 257 } 258 259 function test_rest_pre_serve_request_wrong_format() { 260 $post = $this->factory()->post->create_and_get(); 261 262 $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' ); 263 $request->set_param( 'url', get_permalink( $post->ID ) ); 264 $request->set_param( 'format', 'json' ); 265 266 $response = $this->server->dispatch( $request ); 267 268 $this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, $this->server ) ); 269 } 270 271 function test_rest_pre_serve_request_wrong_method() { 272 $post = $this->factory()->post->create_and_get(); 273 274 $request = new WP_REST_Request( 'HEAD', '/oembed/1.0/embed' ); 275 $request->set_param( 'url', get_permalink( $post->ID ) ); 276 $request->set_param( 'format', 'xml' ); 277 278 $response = $this->server->dispatch( $request ); 279 280 $this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, $this->server ) ); 281 } 282 205 283 function test_get_oembed_endpoint_url() { 206 $this->assertEquals( home_url() . '/? oembed=true', get_oembed_endpoint_url() );207 $this->assertEquals( home_url() . '/? oembed=true', get_oembed_endpoint_url( '', 'json' ) );208 $this->assertEquals( home_url() . '/? oembed=true', get_oembed_endpoint_url( '', 'xml' ) );209 210 $post_id = self::factory()->post->create();284 $this->assertEquals( home_url() . '/?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url() ); 285 $this->assertEquals( home_url() . '/?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url( '', 'json' ) ); 286 $this->assertEquals( home_url() . '/?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url( '', 'xml' ) ); 287 288 $post_id = $this->factory()->post->create(); 211 289 $url = get_permalink( $post_id ); 212 290 $url_encoded = urlencode( $url ); 213 291 214 $this->assertEquals( home_url() . '/?oembed=true&url=' . $url_encoded, get_oembed_endpoint_url( $url ) ); 215 $this->assertEquals( home_url() . '/?oembed=true&url=' . $url_encoded . '&format=xml', get_oembed_endpoint_url( $url, 'xml' ) ); 216 } 217 218 function test_wp_oembed_ensure_format() { 219 $this->assertEquals( 'json', wp_oembed_ensure_format( 'json' ) ); 220 $this->assertEquals( 'xml', wp_oembed_ensure_format( 'xml' ) ); 221 $this->assertEquals( 'json', wp_oembed_ensure_format( 123 ) ); 222 $this->assertEquals( 'json', wp_oembed_ensure_format( 'random' ) ); 223 $this->assertEquals( 'json', wp_oembed_ensure_format( array() ) ); 224 } 225 226 function test_oembed_create_xml() { 227 $actual = _oembed_create_xml( array( 228 'foo' => 'bar', 229 'bar' => 'baz', 230 'ping' => 'pong', 231 ) ); 232 233 $expected = '<oembed><foo>bar</foo><bar>baz</bar><ping>pong</ping></oembed>'; 234 235 $this->assertStringEndsWith( $expected, trim( $actual ) ); 236 237 $actual = _oembed_create_xml( array( 238 'foo' => array( 239 'bar' => 'baz', 240 ), 241 'ping' => 'pong', 242 ) ); 243 244 $expected = '<oembed><foo><bar>baz</bar></foo><ping>pong</ping></oembed>'; 245 246 $this->assertStringEndsWith( $expected, trim( $actual ) ); 247 248 $actual = _oembed_create_xml( array( 249 'foo' => array( 250 'bar' => array( 251 'ping' => 'pong', 252 ), 253 ), 254 'hello' => 'world', 255 ) ); 256 257 $expected = '<oembed><foo><bar><ping>pong</ping></bar></foo><hello>world</hello></oembed>'; 258 259 $this->assertStringEndsWith( $expected, trim( $actual ) ); 260 261 $actual = _oembed_create_xml( array( 262 array( 263 'foo' => array( 264 'bar', 265 ), 266 ), 267 'helloworld', 268 ) ); 269 270 $expected = '<oembed><oembed><foo><oembed>bar</oembed></foo></oembed><oembed>helloworld</oembed></oembed>'; 271 272 $this->assertStringEndsWith( $expected, trim( $actual ) ); 292 $this->assertEquals( home_url() . '/?rest_route=%2Foembed%2F1.0%2Fembed&url=' . $url_encoded, get_oembed_endpoint_url( $url ) ); 293 $this->assertEquals( home_url() . '/?rest_route=%2Foembed%2F1.0%2Fembed&url=' . $url_encoded . '&format=xml', get_oembed_endpoint_url( $url, 'xml' ) ); 294 } 295 296 function test_get_oembed_endpoint_url_pretty_permalinks() { 297 update_option( 'permalink_structure', '/%postname%' ); 298 299 $this->assertEquals( home_url() . '/wp-json/oembed/1.0/embed', get_oembed_endpoint_url() ); 300 $this->assertEquals( home_url() . '/wp-json/oembed/1.0/embed', get_oembed_endpoint_url( '', 'xml' ) ); 301 302 $post_id = $this->factory()->post->create(); 303 $url = get_permalink( $post_id ); 304 $url_encoded = urlencode( $url ); 305 306 $this->assertEquals( home_url() . '/wp-json/oembed/1.0/embed?url=' . $url_encoded, get_oembed_endpoint_url( $url ) ); 307 $this->assertEquals( home_url() . '/wp-json/oembed/1.0/embed?url=' . $url_encoded . '&format=xml', get_oembed_endpoint_url( $url, 'xml' ) ); 308 309 update_option( 'permalink_structure', '' ); 273 310 } 274 311 }
Note: See TracChangeset
for help on using the changeset viewer.