- Timestamp:
- 02/24/2017 10:33:05 PM (8 years ago)
- Location:
- branches/4.7
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/4.7
- Property svn:mergeinfo changed
/trunk merged: 40058,40061,40065-40066,40077,40104
- Property svn:mergeinfo changed
-
branches/4.7/tests/phpunit/tests/rest-api/rest-schema-setup.php
r40058 r40116 37 37 $this->assertTrue( is_array( $routes ), '`get_routes` should return an array.' ); 38 38 $this->assertTrue( ! empty( $routes ), 'Routes should not be empty.' ); 39 40 $routes = array_filter( array_keys( $routes ), array( $this, 'is_builtin_route' ) ); 39 41 40 42 $expected_routes = array( … … 71 73 ); 72 74 73 $this->assertEquals( $expected_routes, array_keys( $routes ) ); 75 $this->assertEquals( $expected_routes, $routes ); 76 } 77 78 private function is_builtin_route( $route ) { 79 return ( 80 '/' === $route || 81 preg_match( '#^/oembed/1\.0(/.+)?$#', $route ) || 82 preg_match( '#^/wp/v2(/.+)?$#', $route ) 83 ); 74 84 } 75 85 76 86 public function test_build_wp_api_client_fixtures() { 77 // Set up for testing the individual endpoints. 78 // Set a current admin user. 79 $administrator = $this->factory->user->create( array( 80 'role' => 'administrator', 81 ) ); 82 wp_set_current_user( $administrator ); 83 84 // Set up data for endpoints. 85 $post_id = $this->factory->post->create(); 86 $page_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); 87 $tag_id = $this->factory->tag->create( array( 'name' => 'test' ) ); 87 // Set up data for individual endpoint responses. We need to specify 88 // lots of different fields on these objects, otherwise the generated 89 // fixture file will be different between runs of PHPUnit tests, which 90 // is not desirable. 91 92 $administrator_id = $this->factory->user->create( array( 93 'role' => 'administrator', 94 'display_name' => 'REST API Client Fixture: User', 95 'user_nicename' => 'restapiclientfixtureuser', 96 'user_email' => 'administrator@example.org', 97 ) ); 98 wp_set_current_user( $administrator_id ); 99 100 $post_id = $this->factory->post->create( array( 101 'post_name' => 'restapi-client-fixture-post', 102 'post_title' => 'REST API Client Fixture: Post', 103 'post_content' => 'REST API Client Fixture: Post', 104 'post_excerpt' => 'REST API Client Fixture: Post', 105 'post_author' => 0, 106 ) ); 107 wp_update_post( array( 108 'ID' => $post_id, 109 'post_content' => 'Updated post content.', 110 ) ); 111 112 $page_id = $this->factory->post->create( array( 113 'post_type' => 'page', 114 'post_name' => 'restapi-client-fixture-page', 115 'post_title' => 'REST API Client Fixture: Page', 116 'post_content' => 'REST API Client Fixture: Page', 117 'post_excerpt' => 'REST API Client Fixture: Page', 118 'post_date' => '2017-02-14 00:00:00', 119 'post_date_gmt' => '2017-02-14 00:00:00', 120 'post_author' => 0, 121 ) ); 122 wp_update_post( array( 123 'ID' => $page_id, 124 'post_content' => 'Updated page content.', 125 ) ); 126 127 $tag_id = $this->factory->tag->create( array( 128 'name' => 'REST API Client Fixture: Tag', 129 'slug' => 'restapi-client-fixture-tag', 130 'description' => 'REST API Client Fixture: Tag', 131 ) ); 132 88 133 $media_id = $this->factory->attachment->create_object( '/tmp/canola.jpg', 0, array( 89 134 'post_mime_type' => 'image/jpeg', 90 135 'post_excerpt' => 'A sample caption', 91 ) ); 92 wp_update_post( array( 'post_content' => 'Updated content.', 'ID' => $post_id ) ); 93 wp_update_post( array( 'post_content' => 'Updated content.', 'ID' => $page_id ) ); 136 'post_name' => 'restapi-client-fixture-attachment', 137 'post_title' => 'REST API Client Fixture: Attachment', 138 'post_date' => '2017-02-14 00:00:00', 139 'post_date_gmt' => '2017-02-14 00:00:00', 140 'post_author' => 0, 141 ) ); 142 94 143 $comment_id = $this->factory->comment->create( array( 95 'comment_approved' => 1, 96 'comment_post_ID' => $post_id, 97 'user_id' => 0, 144 'comment_approved' => 1, 145 'comment_post_ID' => $post_id, 146 'user_id' => 0, 147 'comment_date' => '2017-02-14 00:00:00', 148 'comment_date_gmt' => '2017-02-14 00:00:00', 149 'comment_author' => 'Internet of something or other', 150 'comment_author_email' => 'lights@example.org', 151 'comment_author_url' => 'http://lights.example.org/', 98 152 ) ); 99 153 … … 197 251 ), 198 252 array( 199 'route' => '/wp/v2/users/ 1',253 'route' => '/wp/v2/users/' . $administrator_id, 200 254 'name' => 'UserModel', 201 255 ), … … 218 272 ); 219 273 220 // Set up the mocked response and tell jshint to ignore the single quote json objects 221 $mocked_responses = "/*jshint -W109 */\n\nvar mockedApiResponse = {};\n\n"; 222 $mocked_responses .= "/**\n"; 274 $mocked_responses = "/**\n"; 223 275 $mocked_responses .= " * DO NOT EDIT\n"; 224 276 $mocked_responses .= " * Auto-generated by test_build_wp_api_client_fixtures\n"; 225 277 $mocked_responses .= " */\n"; 278 $mocked_responses .= "var mockedApiResponse = {};\n"; 279 $mocked_responses .= "/* jshint -W109 */\n"; 226 280 227 281 foreach ( $routes_to_generate_data as $route ) { … … 232 286 $this->assertTrue( ! empty( $data ), $route['name'] . ' route should return data.' ); 233 287 234 $mocked_responses .= 'mockedApiResponse.' . $route['name'] . ' = ' . wp_json_encode( $data ) . ";\n\n"; 288 if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) { 289 $fixture = $this->normalize_fixture( $data, $route['name'] ); 290 $mocked_responses .= "\nmockedApiResponse." . $route['name'] . ' = ' 291 . json_encode( $fixture, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) 292 . ";\n"; 293 } 235 294 } 236 295 237 // Save the route object for QUnit tests. 238 $file = './tests/qunit/fixtures/wp-api-generated.js'; 239 file_put_contents( $file, $mocked_responses ); 296 if ( is_multisite() ) { 297 echo "Skipping generation of API client fixtures in multisite mode.\n"; 298 } else if ( version_compare( PHP_VERSION, '5.4', '<' ) ) { 299 echo "Skipping generation of API client fixtures due to unsupported JSON_* constants.\n"; 300 } else { 301 // Save the route object for QUnit tests. 302 $file = './tests/qunit/fixtures/wp-api-generated.js'; 303 file_put_contents( $file, $mocked_responses ); 304 } 240 305 241 306 // Clean up our test data. … … 246 311 wp_delete_comment( $comment_id ); 247 312 } 313 314 /** 315 * This array contains normalized versions of object IDs and other values 316 * that can change depending on how PHPUnit is executed. For details on 317 * how they were generated, see #39264. 318 */ 319 private static $fixture_replacements = array( 320 'PostsCollection.0.id' => 3, 321 'PostsCollection.0.guid.rendered' => 'http://example.org/?p=3', 322 'PostsCollection.0.link' => 'http://example.org/?p=3', 323 'PostsCollection.0._links.self.0.href' => 'http://example.org/?rest_route=/wp/v2/posts/3', 324 'PostsCollection.0._links.replies.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fcomments&post=3', 325 'PostsCollection.0._links.version-history.0.href' => 'http://example.org/?rest_route=/wp/v2/posts/3/revisions', 326 'PostsCollection.0._links.wp:attachment.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3', 327 'PostsCollection.0._links.wp:term.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fcategories&post=3', 328 'PostsCollection.0._links.wp:term.1.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Ftags&post=3', 329 'PostModel.id' => 3, 330 'PostModel.guid.rendered' => 'http://example.org/?p=3', 331 'PostModel.link' => 'http://example.org/?p=3', 332 'postRevisions.0.author' => '2', 333 'postRevisions.0.id' => 4, 334 'postRevisions.0.parent' => 3, 335 'postRevisions.0.slug' => '3-revision-v1', 336 'postRevisions.0.guid.rendered' => 'http://example.org/?p=4', 337 'postRevisions.0._links.parent.0.href' => 'http://example.org/?rest_route=/wp/v2/posts/3', 338 'PagesCollection.0.id' => 5, 339 'PagesCollection.0.guid.rendered' => 'http://example.org/?page_id=5', 340 'PagesCollection.0.link' => 'http://example.org/?page_id=5', 341 'PagesCollection.0._links.self.0.href' => 'http://example.org/?rest_route=/wp/v2/pages/5', 342 'PagesCollection.0._links.replies.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fcomments&post=5', 343 'PagesCollection.0._links.version-history.0.href' => 'http://example.org/?rest_route=/wp/v2/pages/5/revisions', 344 'PagesCollection.0._links.wp:attachment.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5', 345 'PageModel.id' => 5, 346 'PageModel.guid.rendered' => 'http://example.org/?page_id=5', 347 'PageModel.link' => 'http://example.org/?page_id=5', 348 'pageRevisions.0.author' => '2', 349 'pageRevisions.0.id' => 6, 350 'pageRevisions.0.parent' => 5, 351 'pageRevisions.0.slug' => '5-revision-v1', 352 'pageRevisions.0.guid.rendered' => 'http://example.org/?p=6', 353 'pageRevisions.0._links.parent.0.href' => 'http://example.org/?rest_route=/wp/v2/pages/5', 354 'MediaCollection.0.id' => 7, 355 'MediaCollection.0.guid.rendered' => 'http://example.org/?attachment_id=7', 356 'MediaCollection.0.link' => 'http://example.org/?attachment_id=7', 357 'MediaCollection.0._links.self.0.href' => 'http://example.org/?rest_route=/wp/v2/media/7', 358 'MediaCollection.0._links.replies.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fcomments&post=7', 359 'MediaModel.id' => 7, 360 'MediaModel.guid.rendered' => 'http://example.org/?attachment_id=7', 361 'MediaModel.link' => 'http://example.org/?attachment_id=7', 362 'TagsCollection.0.id' => 2, 363 'TagsCollection.0._links.self.0.href' => 'http://example.org/?rest_route=/wp/v2/tags/2', 364 'TagsCollection.0._links.wp:post_type.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fposts&tags=2', 365 'TagModel.id' => 2, 366 'UsersCollection.1.id' => 2, 367 'UsersCollection.1.link' => 'http://example.org/?author=2', 368 'UsersCollection.1._links.self.0.href' => 'http://example.org/?rest_route=/wp/v2/users/2', 369 'UserModel.id' => 2, 370 'UserModel.link' => 'http://example.org/?author=2', 371 'me.id' => 2, 372 'me.link' => 'http://example.org/?author=2', 373 'CommentsCollection.0.id' => 2, 374 'CommentsCollection.0.post' => 3, 375 'CommentsCollection.0.link' => 'http://example.org/?p=3#comment-2', 376 'CommentsCollection.0._links.self.0.href' => 'http://example.org/?rest_route=/wp/v2/comments/2', 377 'CommentsCollection.0._links.up.0.href' => 'http://example.org/?rest_route=/wp/v2/posts/3', 378 ); 379 380 private function normalize_fixture( $data, $path ) { 381 if ( isset( self::$fixture_replacements[ $path ] ) ) { 382 return self::$fixture_replacements[ $path ]; 383 } 384 385 if ( ! is_array( $data ) ) { 386 return $data; 387 } 388 389 foreach ( $data as $key => $value ) { 390 if ( is_string( $value ) && ( 391 'date' === $key || 392 'date_gmt' === $key || 393 'modified' === $key || 394 'modified_gmt' === $key 395 ) ) { 396 $data[ $key ] = '2017-02-14T00:00:00'; 397 } else { 398 $data[ $key ] = $this->normalize_fixture( $value, "$path.$key" ); 399 } 400 } 401 402 return $data; 403 } 248 404 }
Note: See TracChangeset
for help on using the changeset viewer.