2017-02-24 19:39:39 +01:00
|
|
|
/* global mockedApiResponse, Backbone, jsWidgetsEndpointSchema */
|
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.
Fixes #39264.
git-svn-id: https://develop.svn.wordpress.org/trunk@40058 602fd350-edb4-49c9-b593-d223f7449a82
2017-02-14 05:22:32 +01:00
|
|
|
/**
|
|
|
|
* @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,
|
2017-02-24 19:39:39 +01:00
|
|
|
'wp-json/wp/v2/type': mockedApiResponse.TypeModel,
|
2017-07-20 22:25:28 +02:00
|
|
|
'wp-json/js-widgets/v1/': jsWidgetsEndpointSchema,
|
|
|
|
'wp-json/wp/v2/users/me': mockedApiResponse.me,
|
|
|
|
'wp-json/wp/v2/settings': mockedApiResponse.settings
|
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.
Fixes #39264.
git-svn-id: https://develop.svn.wordpress.org/trunk@40058 602fd350-edb4-49c9-b593-d223f7449a82
2017-02-14 05:22:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock the ajax callbacks for our tests.
|
|
|
|
*
|
|
|
|
* @param {object} param The parameters sent to the ajax request.
|
|
|
|
*
|
2017-04-02 21:40:37 +02:00
|
|
|
* @return {Object} A jQuery deferred object that resolves with the mapped data.
|
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.
Fixes #39264.
git-svn-id: https://develop.svn.wordpress.org/trunk@40058 602fd350-edb4-49c9-b593-d223f7449a82
2017-02-14 05:22:32 +01:00
|
|
|
*/
|
|
|
|
Backbone.ajax = function ( param ) {
|
|
|
|
|
|
|
|
var data,
|
2017-04-02 21:40:37 +02:00
|
|
|
request = param.url
|
|
|
|
.replace( 'http://remotehost/', '' )
|
|
|
|
.replace( 'http://localhost/', '' );
|
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.
Fixes #39264.
git-svn-id: https://develop.svn.wordpress.org/trunk@40058 602fd350-edb4-49c9-b593-d223f7449a82
2017-02-14 05:22:32 +01:00
|
|
|
|
|
|
|
if ( pathToData[ request ] ) {
|
|
|
|
data = pathToData[ request ];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call success handler.
|
|
|
|
param.success( data );
|
|
|
|
var deferred = jQuery.Deferred();
|
|
|
|
|
2020-01-29 01:43:23 +01:00
|
|
|
// Resolve the deferred with the mocked data.
|
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.
Fixes #39264.
git-svn-id: https://develop.svn.wordpress.org/trunk@40058 602fd350-edb4-49c9-b593-d223f7449a82
2017-02-14 05:22:32 +01:00
|
|
|
deferred.resolve( data );
|
|
|
|
|
|
|
|
// Return the deferred promise that will resolve with the expected data.
|
|
|
|
return deferred.promise();
|
|
|
|
|
|
|
|
};
|