REST API: Add QUnit tests for wp-api.js and PHPUnit fixture generation.
Add QUnit tests: verify that wp-api loads correctly, verify that the expected base models and collections exist and can be instantiated, verify that collections contain the correct models, verify that expected helper functions are in place for each collection. The QUnit tests rely on two fixture files: `tests/qunit/fixtures/wp-api-generated.js` contains the data response from each core endpoint and is generated by running the PHPUnit `restapi-jsclient` group. `tests/qunit/fixtures/wp-api.js` maps the generated data to endpoint routes, and overrides `Backbone.ajax` to mock the responses for the tests. Add PHPUnit tests in `tests/phpunit/tests/rest-api/rest-schema-setup.php`. First, verify that the API returns the expected routes via `server->get_routes()`. Then, the `test_build_wp_api_client_fixtures` test goes thru each endpoint and requests it from the API, tests that it returns data, and builds up the data for the mocked QUnit tests, saving the final results to `tests/qunit/fixtures/wp-api-generated.js`. Add a new grunt task `restapi-jsclient` which runs the phpunit side data generation and the qunit tests together. Props jnylen0, welcher, adamsilverstein, netweb, ocean90, rachelbaker. Merges [40058], [40061], [40065], [40066], [40077], and [40104] to the 4.7 branch. Fixes #39264. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@40116 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
9afe01b087
commit
dbf739bbaa
@ -439,6 +439,10 @@ module.exports = function(grunt) {
|
||||
'external-http': {
|
||||
cmd: 'phpunit',
|
||||
args: ['-c', 'phpunit.xml.dist', '--group', 'external-http']
|
||||
},
|
||||
'restapi-jsclient': {
|
||||
cmd: 'phpunit',
|
||||
args: ['-c', 'phpunit.xml.dist', '--group', 'restapi-jsclient']
|
||||
}
|
||||
},
|
||||
uglify: {
|
||||
@ -672,6 +676,11 @@ module.exports = function(grunt) {
|
||||
'jshint:media'
|
||||
] );
|
||||
|
||||
grunt.registerTask( 'restapi-jsclient', [
|
||||
'phpunit:restapi-jsclient',
|
||||
'qunit:compiled'
|
||||
] );
|
||||
|
||||
grunt.renameTask( 'watch', '_watch' );
|
||||
|
||||
grunt.registerTask( 'watch', function() {
|
||||
|
@ -18,8 +18,9 @@ $config_file_path .= '/wp-tests-config.php';
|
||||
*/
|
||||
global $wpdb, $current_site, $current_blog, $wp_rewrite, $shortcode_tags, $wp, $phpmailer, $wp_theme_directories;
|
||||
|
||||
if ( !is_readable( $config_file_path ) ) {
|
||||
die( "ERROR: wp-tests-config.php is missing! Please use wp-tests-config-sample.php to create a config file.\n" );
|
||||
if ( ! is_readable( $config_file_path ) ) {
|
||||
echo "ERROR: wp-tests-config.php is missing! Please use wp-tests-config-sample.php to create a config file.\n";
|
||||
exit( 1 );
|
||||
}
|
||||
require_once $config_file_path;
|
||||
require_once dirname( __FILE__ ) . '/functions.php';
|
||||
|
@ -10,13 +10,16 @@
|
||||
* @group restapi
|
||||
*/
|
||||
class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
|
||||
protected static $wp_meta_keys_saved;
|
||||
protected static $post_id;
|
||||
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$wp_meta_keys_saved = $GLOBALS['wp_meta_keys'];
|
||||
self::$post_id = $factory->post->create();
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
$GLOBALS['wp_meta_keys'] = self::$wp_meta_keys_saved;
|
||||
wp_delete_post( self::$post_id, true );
|
||||
}
|
||||
|
||||
|
404
tests/phpunit/tests/rest-api/rest-schema-setup.php
Normal file
404
tests/phpunit/tests/rest-api/rest-schema-setup.php
Normal file
@ -0,0 +1,404 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering schema initialization.
|
||||
*
|
||||
* Also generates the fixture data used by the wp-api.js QUnit tests.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST API
|
||||
*/
|
||||
|
||||
/**
|
||||
* @group restapi
|
||||
* @group restapi-jsclient
|
||||
*/
|
||||
class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
/** @var WP_REST_Server $wp_rest_server */
|
||||
global $wp_rest_server;
|
||||
$this->server = $wp_rest_server = new Spy_REST_Server;
|
||||
do_action( 'rest_api_init' );
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
parent::tearDown();
|
||||
remove_filter( 'rest_url', array( $this, 'test_rest_url_for_leading_slash' ), 10, 2 );
|
||||
/** @var WP_REST_Server $wp_rest_server */
|
||||
global $wp_rest_server;
|
||||
$wp_rest_server = null;
|
||||
}
|
||||
|
||||
public function test_expected_routes_in_schema() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertTrue( is_array( $routes ), '`get_routes` should return an array.' );
|
||||
$this->assertTrue( ! empty( $routes ), 'Routes should not be empty.' );
|
||||
|
||||
$routes = array_filter( array_keys( $routes ), array( $this, 'is_builtin_route' ) );
|
||||
|
||||
$expected_routes = array(
|
||||
'/',
|
||||
'/oembed/1.0',
|
||||
'/oembed/1.0/embed',
|
||||
'/wp/v2',
|
||||
'/wp/v2/posts',
|
||||
'/wp/v2/posts/(?P<id>[\\d]+)',
|
||||
'/wp/v2/posts/(?P<parent>[\\d]+)/revisions',
|
||||
'/wp/v2/posts/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)',
|
||||
'/wp/v2/pages',
|
||||
'/wp/v2/pages/(?P<id>[\\d]+)',
|
||||
'/wp/v2/pages/(?P<parent>[\\d]+)/revisions',
|
||||
'/wp/v2/pages/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)',
|
||||
'/wp/v2/media',
|
||||
'/wp/v2/media/(?P<id>[\\d]+)',
|
||||
'/wp/v2/types',
|
||||
'/wp/v2/types/(?P<type>[\\w-]+)',
|
||||
'/wp/v2/statuses',
|
||||
'/wp/v2/statuses/(?P<status>[\\w-]+)',
|
||||
'/wp/v2/taxonomies',
|
||||
'/wp/v2/taxonomies/(?P<taxonomy>[\\w-]+)',
|
||||
'/wp/v2/categories',
|
||||
'/wp/v2/categories/(?P<id>[\\d]+)',
|
||||
'/wp/v2/tags',
|
||||
'/wp/v2/tags/(?P<id>[\\d]+)',
|
||||
'/wp/v2/users',
|
||||
'/wp/v2/users/(?P<id>[\\d]+)',
|
||||
'/wp/v2/users/me',
|
||||
'/wp/v2/comments',
|
||||
'/wp/v2/comments/(?P<id>[\\d]+)',
|
||||
'/wp/v2/settings',
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected_routes, $routes );
|
||||
}
|
||||
|
||||
private function is_builtin_route( $route ) {
|
||||
return (
|
||||
'/' === $route ||
|
||||
preg_match( '#^/oembed/1\.0(/.+)?$#', $route ) ||
|
||||
preg_match( '#^/wp/v2(/.+)?$#', $route )
|
||||
);
|
||||
}
|
||||
|
||||
public function test_build_wp_api_client_fixtures() {
|
||||
// Set up data for individual endpoint responses. We need to specify
|
||||
// lots of different fields on these objects, otherwise the generated
|
||||
// fixture file will be different between runs of PHPUnit tests, which
|
||||
// is not desirable.
|
||||
|
||||
$administrator_id = $this->factory->user->create( array(
|
||||
'role' => 'administrator',
|
||||
'display_name' => 'REST API Client Fixture: User',
|
||||
'user_nicename' => 'restapiclientfixtureuser',
|
||||
'user_email' => 'administrator@example.org',
|
||||
) );
|
||||
wp_set_current_user( $administrator_id );
|
||||
|
||||
$post_id = $this->factory->post->create( array(
|
||||
'post_name' => 'restapi-client-fixture-post',
|
||||
'post_title' => 'REST API Client Fixture: Post',
|
||||
'post_content' => 'REST API Client Fixture: Post',
|
||||
'post_excerpt' => 'REST API Client Fixture: Post',
|
||||
'post_author' => 0,
|
||||
) );
|
||||
wp_update_post( array(
|
||||
'ID' => $post_id,
|
||||
'post_content' => 'Updated post content.',
|
||||
) );
|
||||
|
||||
$page_id = $this->factory->post->create( array(
|
||||
'post_type' => 'page',
|
||||
'post_name' => 'restapi-client-fixture-page',
|
||||
'post_title' => 'REST API Client Fixture: Page',
|
||||
'post_content' => 'REST API Client Fixture: Page',
|
||||
'post_excerpt' => 'REST API Client Fixture: Page',
|
||||
'post_date' => '2017-02-14 00:00:00',
|
||||
'post_date_gmt' => '2017-02-14 00:00:00',
|
||||
'post_author' => 0,
|
||||
) );
|
||||
wp_update_post( array(
|
||||
'ID' => $page_id,
|
||||
'post_content' => 'Updated page content.',
|
||||
) );
|
||||
|
||||
$tag_id = $this->factory->tag->create( array(
|
||||
'name' => 'REST API Client Fixture: Tag',
|
||||
'slug' => 'restapi-client-fixture-tag',
|
||||
'description' => 'REST API Client Fixture: Tag',
|
||||
) );
|
||||
|
||||
$media_id = $this->factory->attachment->create_object( '/tmp/canola.jpg', 0, array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
'post_excerpt' => 'A sample caption',
|
||||
'post_name' => 'restapi-client-fixture-attachment',
|
||||
'post_title' => 'REST API Client Fixture: Attachment',
|
||||
'post_date' => '2017-02-14 00:00:00',
|
||||
'post_date_gmt' => '2017-02-14 00:00:00',
|
||||
'post_author' => 0,
|
||||
) );
|
||||
|
||||
$comment_id = $this->factory->comment->create( array(
|
||||
'comment_approved' => 1,
|
||||
'comment_post_ID' => $post_id,
|
||||
'user_id' => 0,
|
||||
'comment_date' => '2017-02-14 00:00:00',
|
||||
'comment_date_gmt' => '2017-02-14 00:00:00',
|
||||
'comment_author' => 'Internet of something or other',
|
||||
'comment_author_email' => 'lights@example.org',
|
||||
'comment_author_url' => 'http://lights.example.org/',
|
||||
) );
|
||||
|
||||
// Generate route data for subsequent QUnit tests.
|
||||
$routes_to_generate_data = array(
|
||||
array(
|
||||
'route' => '/',
|
||||
'name' => 'Schema',
|
||||
),
|
||||
array(
|
||||
'route' => '/oembed/1.0',
|
||||
'name' => 'oembed',
|
||||
),
|
||||
array(
|
||||
'route' => '/oembed/1.0/embed',
|
||||
'name' => 'oembeds',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/posts',
|
||||
'name' => 'PostsCollection',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/posts/' . $post_id,
|
||||
'name' => 'PostModel',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/posts/' . $post_id . '/revisions',
|
||||
'name' => 'postRevisions',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/posts/' . $post_id . '/revisions/1',
|
||||
'name' => 'revision',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/pages',
|
||||
'name' => 'PagesCollection',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/pages/' . $page_id,
|
||||
'name' => 'PageModel',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/pages/'. $page_id . '/revisions',
|
||||
'name' => 'pageRevisions',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/pages/'. $page_id . '/revisions/1',
|
||||
'name' => 'pageRevision',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/media',
|
||||
'name' => 'MediaCollection',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/media/' . $media_id,
|
||||
'name' => 'MediaModel',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/types',
|
||||
'name' => 'TypesCollection',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/types/',
|
||||
'name' => 'TypeModel',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/statuses',
|
||||
'name' => 'StatusesCollection',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/statuses/publish',
|
||||
'name' => 'StatusModel',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/taxonomies',
|
||||
'name' => 'TaxonomiesCollection',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/taxonomies/category',
|
||||
'name' => 'TaxonomyModel',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/categories',
|
||||
'name' => 'CategoriesCollection',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/categories/1',
|
||||
'name' => 'CategoryModel',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/tags',
|
||||
'name' => 'TagsCollection',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/tags/' . $tag_id,
|
||||
'name' => 'TagModel',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/users',
|
||||
'name' => 'UsersCollection',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/users/' . $administrator_id,
|
||||
'name' => 'UserModel',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/users/me',
|
||||
'name' => 'me',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/comments',
|
||||
'name' => 'CommentsCollection',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/comments/1',
|
||||
'name' => 'CommentModel',
|
||||
),
|
||||
array(
|
||||
'route' => '/wp/v2/settings',
|
||||
'name' => 'settings',
|
||||
),
|
||||
);
|
||||
|
||||
$mocked_responses = "/**\n";
|
||||
$mocked_responses .= " * DO NOT EDIT\n";
|
||||
$mocked_responses .= " * Auto-generated by test_build_wp_api_client_fixtures\n";
|
||||
$mocked_responses .= " */\n";
|
||||
$mocked_responses .= "var mockedApiResponse = {};\n";
|
||||
$mocked_responses .= "/* jshint -W109 */\n";
|
||||
|
||||
foreach ( $routes_to_generate_data as $route ) {
|
||||
$request = new WP_REST_Request( 'GET', $route['route'] );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertTrue( ! empty( $data ), $route['name'] . ' route should return data.' );
|
||||
|
||||
if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) {
|
||||
$fixture = $this->normalize_fixture( $data, $route['name'] );
|
||||
$mocked_responses .= "\nmockedApiResponse." . $route['name'] . ' = '
|
||||
. json_encode( $fixture, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES )
|
||||
. ";\n";
|
||||
}
|
||||
}
|
||||
|
||||
if ( is_multisite() ) {
|
||||
echo "Skipping generation of API client fixtures in multisite mode.\n";
|
||||
} else if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
|
||||
echo "Skipping generation of API client fixtures due to unsupported JSON_* constants.\n";
|
||||
} else {
|
||||
// Save the route object for QUnit tests.
|
||||
$file = './tests/qunit/fixtures/wp-api-generated.js';
|
||||
file_put_contents( $file, $mocked_responses );
|
||||
}
|
||||
|
||||
// Clean up our test data.
|
||||
wp_delete_post( $post_id, true );
|
||||
wp_delete_post( $page_id, true );
|
||||
wp_delete_term( $tag_id, 'tags' );
|
||||
wp_delete_attachment( $media_id );
|
||||
wp_delete_comment( $comment_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* This array contains normalized versions of object IDs and other values
|
||||
* that can change depending on how PHPUnit is executed. For details on
|
||||
* how they were generated, see #39264.
|
||||
*/
|
||||
private static $fixture_replacements = array(
|
||||
'PostsCollection.0.id' => 3,
|
||||
'PostsCollection.0.guid.rendered' => 'http://example.org/?p=3',
|
||||
'PostsCollection.0.link' => 'http://example.org/?p=3',
|
||||
'PostsCollection.0._links.self.0.href' => 'http://example.org/?rest_route=/wp/v2/posts/3',
|
||||
'PostsCollection.0._links.replies.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fcomments&post=3',
|
||||
'PostsCollection.0._links.version-history.0.href' => 'http://example.org/?rest_route=/wp/v2/posts/3/revisions',
|
||||
'PostsCollection.0._links.wp:attachment.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3',
|
||||
'PostsCollection.0._links.wp:term.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fcategories&post=3',
|
||||
'PostsCollection.0._links.wp:term.1.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Ftags&post=3',
|
||||
'PostModel.id' => 3,
|
||||
'PostModel.guid.rendered' => 'http://example.org/?p=3',
|
||||
'PostModel.link' => 'http://example.org/?p=3',
|
||||
'postRevisions.0.author' => '2',
|
||||
'postRevisions.0.id' => 4,
|
||||
'postRevisions.0.parent' => 3,
|
||||
'postRevisions.0.slug' => '3-revision-v1',
|
||||
'postRevisions.0.guid.rendered' => 'http://example.org/?p=4',
|
||||
'postRevisions.0._links.parent.0.href' => 'http://example.org/?rest_route=/wp/v2/posts/3',
|
||||
'PagesCollection.0.id' => 5,
|
||||
'PagesCollection.0.guid.rendered' => 'http://example.org/?page_id=5',
|
||||
'PagesCollection.0.link' => 'http://example.org/?page_id=5',
|
||||
'PagesCollection.0._links.self.0.href' => 'http://example.org/?rest_route=/wp/v2/pages/5',
|
||||
'PagesCollection.0._links.replies.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fcomments&post=5',
|
||||
'PagesCollection.0._links.version-history.0.href' => 'http://example.org/?rest_route=/wp/v2/pages/5/revisions',
|
||||
'PagesCollection.0._links.wp:attachment.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5',
|
||||
'PageModel.id' => 5,
|
||||
'PageModel.guid.rendered' => 'http://example.org/?page_id=5',
|
||||
'PageModel.link' => 'http://example.org/?page_id=5',
|
||||
'pageRevisions.0.author' => '2',
|
||||
'pageRevisions.0.id' => 6,
|
||||
'pageRevisions.0.parent' => 5,
|
||||
'pageRevisions.0.slug' => '5-revision-v1',
|
||||
'pageRevisions.0.guid.rendered' => 'http://example.org/?p=6',
|
||||
'pageRevisions.0._links.parent.0.href' => 'http://example.org/?rest_route=/wp/v2/pages/5',
|
||||
'MediaCollection.0.id' => 7,
|
||||
'MediaCollection.0.guid.rendered' => 'http://example.org/?attachment_id=7',
|
||||
'MediaCollection.0.link' => 'http://example.org/?attachment_id=7',
|
||||
'MediaCollection.0._links.self.0.href' => 'http://example.org/?rest_route=/wp/v2/media/7',
|
||||
'MediaCollection.0._links.replies.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fcomments&post=7',
|
||||
'MediaModel.id' => 7,
|
||||
'MediaModel.guid.rendered' => 'http://example.org/?attachment_id=7',
|
||||
'MediaModel.link' => 'http://example.org/?attachment_id=7',
|
||||
'TagsCollection.0.id' => 2,
|
||||
'TagsCollection.0._links.self.0.href' => 'http://example.org/?rest_route=/wp/v2/tags/2',
|
||||
'TagsCollection.0._links.wp:post_type.0.href' => 'http://example.org/?rest_route=%2Fwp%2Fv2%2Fposts&tags=2',
|
||||
'TagModel.id' => 2,
|
||||
'UsersCollection.1.id' => 2,
|
||||
'UsersCollection.1.link' => 'http://example.org/?author=2',
|
||||
'UsersCollection.1._links.self.0.href' => 'http://example.org/?rest_route=/wp/v2/users/2',
|
||||
'UserModel.id' => 2,
|
||||
'UserModel.link' => 'http://example.org/?author=2',
|
||||
'me.id' => 2,
|
||||
'me.link' => 'http://example.org/?author=2',
|
||||
'CommentsCollection.0.id' => 2,
|
||||
'CommentsCollection.0.post' => 3,
|
||||
'CommentsCollection.0.link' => 'http://example.org/?p=3#comment-2',
|
||||
'CommentsCollection.0._links.self.0.href' => 'http://example.org/?rest_route=/wp/v2/comments/2',
|
||||
'CommentsCollection.0._links.up.0.href' => 'http://example.org/?rest_route=/wp/v2/posts/3',
|
||||
);
|
||||
|
||||
private function normalize_fixture( $data, $path ) {
|
||||
if ( isset( self::$fixture_replacements[ $path ] ) ) {
|
||||
return self::$fixture_replacements[ $path ];
|
||||
}
|
||||
|
||||
if ( ! is_array( $data ) ) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
foreach ( $data as $key => $value ) {
|
||||
if ( is_string( $value ) && (
|
||||
'date' === $key ||
|
||||
'date_gmt' === $key ||
|
||||
'modified' === $key ||
|
||||
'modified_gmt' === $key
|
||||
) ) {
|
||||
$data[ $key ] = '2017-02-14T00:00:00';
|
||||
} else {
|
||||
$data[ $key ] = $this->normalize_fixture( $value, "$path.$key" );
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
4307
tests/qunit/fixtures/wp-api-generated.js
Normal file
4307
tests/qunit/fixtures/wp-api-generated.js
Normal file
File diff suppressed because it is too large
Load Diff
55
tests/qunit/fixtures/wp-api.js
Normal file
55
tests/qunit/fixtures/wp-api.js
Normal file
@ -0,0 +1,55 @@
|
||||
/* global mockedApiResponse, Backbone */
|
||||
/**
|
||||
* @var mockedApiResponse defined in wp-api-generated.js
|
||||
*/
|
||||
|
||||
var pathToData = {
|
||||
'wp-json/wp/v2/': mockedApiResponse.Schema,
|
||||
'wp-json/wp/v2/categories': mockedApiResponse.CategoriesCollection,
|
||||
'wp-json/wp/v2/comments': mockedApiResponse.CommentsCollection,
|
||||
'wp-json/wp/v2/media': mockedApiResponse.MediaCollection,
|
||||
'wp-json/wp/v2/pages': mockedApiResponse.PagesCollection,
|
||||
'wp-json/wp/v2/posts': mockedApiResponse.PostsCollection,
|
||||
'wp-json/wp/v2/statuses': mockedApiResponse.StatusesCollection,
|
||||
'wp-json/wp/v2/tags': mockedApiResponse.TagsCollection,
|
||||
'wp-json/wp/v2/taxonomies': mockedApiResponse.TaxonomiesCollection,
|
||||
'wp-json/wp/v2/types': mockedApiResponse.TypesCollection,
|
||||
'wp-json/wp/v2/users': mockedApiResponse.UsersCollection,
|
||||
'wp-json/wp/v2/category': mockedApiResponse.CategoryModel,
|
||||
'wp-json/wp/v2/media1': mockedApiResponse.MediaModel,
|
||||
'wp-json/wp/v2/page': mockedApiResponse.PageModel,
|
||||
'wp-json/wp/v2/post': mockedApiResponse.PostModel,
|
||||
'wp-json/wp/v2/tag': mockedApiResponse.TagModel,
|
||||
'wp-json/wp/v2/user': mockedApiResponse.UserModel,
|
||||
'wp-json/wp/v2/taxonomy': mockedApiResponse.TaxonomyModel,
|
||||
'wp-json/wp/v2/status': mockedApiResponse.StatusModel,
|
||||
'wp-json/wp/v2/type': mockedApiResponse.TypeModel
|
||||
};
|
||||
|
||||
/**
|
||||
* Mock the ajax callbacks for our tests.
|
||||
*
|
||||
* @param {object} param The parameters sent to the ajax request.
|
||||
*
|
||||
* @return {Object} A jQuery defered object that resolves with the mapped data.
|
||||
*/
|
||||
Backbone.ajax = function ( param ) {
|
||||
|
||||
var data,
|
||||
request = param.url.replace( 'http://localhost/', '' );
|
||||
|
||||
if ( pathToData[ request ] ) {
|
||||
data = pathToData[ request ];
|
||||
}
|
||||
|
||||
// Call success handler.
|
||||
param.success( data );
|
||||
var deferred = jQuery.Deferred();
|
||||
|
||||
// Resolve the deferred with the mocked data
|
||||
deferred.resolve( data );
|
||||
|
||||
// Return the deferred promise that will resolve with the expected data.
|
||||
return deferred.promise();
|
||||
|
||||
};
|
@ -17,6 +17,11 @@
|
||||
}
|
||||
};
|
||||
</script>
|
||||
<script>
|
||||
var wpApiSettings = {
|
||||
'root': 'http://localhost/wp-json/'
|
||||
};
|
||||
</script>
|
||||
<script src="../../src/wp-includes/js/wp-util.js"></script>
|
||||
<script src="../../src/wp-includes/js/wp-a11y.js"></script>
|
||||
|
||||
@ -34,6 +39,8 @@
|
||||
<script src="fixtures/customize-settings.js"></script>
|
||||
<script src="fixtures/customize-menus.js"></script>
|
||||
<script src="fixtures/customize-widgets.js"></script>
|
||||
<script src="fixtures/wp-api-generated.js"></script>
|
||||
<script src="fixtures/wp-api.js"></script>
|
||||
</div>
|
||||
<p><a href="editor">TinyMCE tests</a></p>
|
||||
|
||||
@ -43,6 +50,7 @@
|
||||
<script src="../../src/wp-includes/js/customize-models.js"></script>
|
||||
<script src="../../src/wp-includes/js/shortcode.js"></script>
|
||||
<script src="../../src/wp-admin/js/customize-controls.js"></script>
|
||||
<script src="../../src/wp-includes/js/wp-api.js"></script>
|
||||
|
||||
<script type='text/javascript' src='../../src/wp-includes/js/jquery/ui/core.js'></script>
|
||||
<script type='text/javascript' src='../../src/wp-includes/js/jquery/ui/widget.js'></script>
|
||||
@ -61,6 +69,7 @@
|
||||
<script src="wp-admin/js/customize-base.js"></script>
|
||||
<script src="wp-admin/js/customize-header.js"></script>
|
||||
<script src="wp-includes/js/shortcode.js"></script>
|
||||
<script src="wp-includes/js/wp-api.js"></script>
|
||||
<script src="wp-admin/js/customize-controls.js"></script>
|
||||
<script src="wp-admin/js/customize-controls-utils.js"></script>
|
||||
<script src="wp-admin/js/customize-nav-menus.js"></script>
|
||||
|
195
tests/qunit/wp-includes/js/wp-api.js
Normal file
195
tests/qunit/wp-includes/js/wp-api.js
Normal file
@ -0,0 +1,195 @@
|
||||
/* global wp */
|
||||
( function( QUnit ) {
|
||||
module( 'wpapi' );
|
||||
|
||||
QUnit.test( 'API Loaded correctly', function( assert ) {
|
||||
var done = assert.async();
|
||||
assert.expect( 2 );
|
||||
|
||||
assert.ok( wp.api.loadPromise );
|
||||
|
||||
wp.api.loadPromise.done( function() {
|
||||
assert.ok( wp.api.models );
|
||||
done();
|
||||
} );
|
||||
|
||||
} );
|
||||
|
||||
// The list of collections we should check.
|
||||
var collectionClassNames = [
|
||||
'Categories',
|
||||
'Comments',
|
||||
'Media',
|
||||
'Pages',
|
||||
'Posts',
|
||||
'Statuses',
|
||||
'Tags',
|
||||
'Taxonomies',
|
||||
'Types',
|
||||
'Users'
|
||||
];
|
||||
|
||||
// Collections that should get helpers tested.
|
||||
var collectionHelperTests = [
|
||||
{
|
||||
'collectionType': 'Posts',
|
||||
'returnsModelType': 'post',
|
||||
'supportsMethods': {
|
||||
'getDate': 'getDate',
|
||||
'getRevisions': 'getRevisions',
|
||||
'getTags': 'getTags',
|
||||
'getCategories': 'getCategories',
|
||||
'getAuthorUser': 'getAuthorUser',
|
||||
'getFeaturedMedia': 'getFeaturedMedia'
|
||||
/*'getMeta': 'getMeta', currently not supported */
|
||||
}
|
||||
},
|
||||
{
|
||||
'collectionType': 'Pages',
|
||||
'returnsModelType': 'page',
|
||||
'supportsMethods': {
|
||||
'getDate': 'getDate',
|
||||
'getRevisions': 'getRevisions',
|
||||
'getAuthorUser': 'getAuthorUser',
|
||||
'getFeaturedMedia': 'getFeaturedMedia'
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
_.each( collectionClassNames, function( className ) {
|
||||
QUnit.test( 'Testing ' + className + ' collection.', function( assert ) {
|
||||
var done = assert.async();
|
||||
|
||||
wp.api.loadPromise.done( function() {
|
||||
var theCollection = new wp.api.collections[ className ]();
|
||||
assert.ok(
|
||||
theCollection,
|
||||
'We can instantiate wp.api.collections.' + className
|
||||
);
|
||||
theCollection.fetch().done( function() {
|
||||
assert.equal(
|
||||
1,
|
||||
theCollection.state.currentPage,
|
||||
'We should be on page 1 of the collection in ' + className
|
||||
);
|
||||
|
||||
// Should this collection have helper methods?
|
||||
var collectionHelperTest = _.findWhere( collectionHelperTests, { 'collectionType': className } );
|
||||
|
||||
// If we found a match, run the tests against it.
|
||||
if ( ! _.isUndefined( collectionHelperTest ) ) {
|
||||
|
||||
// Test the first returned model.
|
||||
var firstModel = theCollection.at( 0 );
|
||||
|
||||
// Is the model the right type?
|
||||
assert.equal(
|
||||
collectionHelperTest.returnsModelType,
|
||||
firstModel.get( 'type' ),
|
||||
'The wp.api.collections.' + className + ' is of type ' + collectionHelperTest.returnsModelType
|
||||
);
|
||||
|
||||
// Does the model have all of the expected supported methods?
|
||||
_.each( collectionHelperTest.supportsMethods, function( method ) {
|
||||
assert.equal(
|
||||
'function',
|
||||
typeof firstModel[ method ],
|
||||
className + '.' + method + ' is a function.'
|
||||
);
|
||||
} );
|
||||
}
|
||||
|
||||
// Trigger Qunit async completion.
|
||||
done();
|
||||
} );
|
||||
|
||||
} );
|
||||
|
||||
} );
|
||||
} );
|
||||
|
||||
// The list of models we should check.
|
||||
var modelsWithIdsClassNames = [
|
||||
'Category',
|
||||
'Media',
|
||||
'Page',
|
||||
'Post',
|
||||
'Tag',
|
||||
'User'
|
||||
];
|
||||
|
||||
_.each( modelsWithIdsClassNames, function( className ) {
|
||||
|
||||
QUnit.test( 'Checking ' + className + ' model.' , function( assert ) {
|
||||
var done = assert.async();
|
||||
|
||||
assert.expect( 2 );
|
||||
|
||||
wp.api.loadPromise.done( function() {
|
||||
var theModel = new wp.api.models[ className ]();
|
||||
assert.ok( theModel, 'We can instantiate wp.api.models.' + className );
|
||||
theModel.fetch().done( function( ) {
|
||||
var theModel2 = new wp.api.models[ className ]();
|
||||
theModel2.set( 'id', theModel.attributes[0].id );
|
||||
theModel2.fetch().done( function() {
|
||||
|
||||
// We were able to retrieve the model.
|
||||
assert.equal(
|
||||
theModel.attributes[0].id,
|
||||
theModel2.get( 'id' ) ,
|
||||
'We should be able to get a ' + className
|
||||
);
|
||||
|
||||
// Trigger Qunit async completion.
|
||||
done();
|
||||
} );
|
||||
} );
|
||||
|
||||
} );
|
||||
|
||||
} );
|
||||
} );
|
||||
|
||||
var modelsWithIndexes = [
|
||||
'Taxonomy',
|
||||
'Status',
|
||||
'Type'
|
||||
];
|
||||
|
||||
_.each( modelsWithIndexes, function( className ) {
|
||||
|
||||
QUnit.test( 'Testing ' + className + ' model.' , function( assert ) {
|
||||
var done = assert.async();
|
||||
|
||||
assert.expect( 2 );
|
||||
|
||||
wp.api.loadPromise.done( function( ) {
|
||||
|
||||
var theModel = new wp.api.models[ className ]();
|
||||
assert.ok( theModel, 'We can instantiate wp.api.models.' + className );
|
||||
theModel.fetch().done( function( ) {
|
||||
var theModel2 = new wp.api.models[ className ]();
|
||||
|
||||
if ( ! _.isUndefined( theModel.attributes[0] ) ) {
|
||||
theModel2.set( 'id', theModel.attributes[0].id );
|
||||
}
|
||||
|
||||
theModel2.fetch().done( function() {
|
||||
// We were able to retrieve the model.
|
||||
assert.notEqual(
|
||||
0,
|
||||
_.keys( theModel2.attributes ).length ,
|
||||
'We should be able to get a ' + className
|
||||
);
|
||||
|
||||
// Trigger Qunit async completion.
|
||||
done();
|
||||
} );
|
||||
} );
|
||||
|
||||
} );
|
||||
|
||||
} );
|
||||
} );
|
||||
|
||||
} )( window.QUnit );
|
Loading…
Reference in New Issue
Block a user