WP-API JS Client: Improve support for meta.

* Add/fix `getMeta`, `getMetas`, `setMeta` and `setMetas` helpers for models that support meta.
* Add tests for new helpers, verify meta support for `Posts`, `Comments`, `Tags` and `Users`.
* Include meta data in fixture generation and fixture file driving tests.

Fixes #41055.


git-svn-id: https://develop.svn.wordpress.org/trunk@41678 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein 2017-10-02 18:39:30 +00:00
parent ba07b22cb5
commit 10488d5fdc
4 changed files with 153 additions and 21 deletions

View File

@ -501,8 +501,49 @@
* Add a helper function to handle post Meta.
*/
MetaMixin = {
getMeta: function() {
return buildCollectionGetter( this, 'PostMeta', 'https://api.w.org/meta' );
/**
* Get meta by key for a post.
*
* @param {string} key The meta key.
*
* @return {object} The post meta value.
*/
getMeta: function( key ) {
var metas = this.get( 'meta' );
return metas[ key ];
},
/**
* Get all meta key/values for a post.
*
* @return {object} The post metas, as a key value pair object.
*/
getMetas: function() {
return this.get( 'meta' );
},
/**
* Set a group of meta key/values for a post.
*
* @param {object} meta The post meta to set, as key/value pairs.
*/
setMetas: function( meta ) {
var metas = this.get( 'meta' );
_.extend( metas, meta );
this.set( 'meta', metas );
},
/**
* Set a single meta value for a post, by key.
*
* @param {string} key The meta key.
* @param {object} value The meta value.
*/
setMeta: function( key, value ) {
var metas = this.get( 'meta' );
metas[ key ] = value;
this.set( 'meta', metas );
}
},
@ -734,8 +775,8 @@
model = model.extend( CategoriesMixin );
}
// Add the MetaMixin for models that support meta collections.
if ( ! _.isUndefined( loadingObjects.collections[ modelClassName + 'Meta' ] ) ) {
// Add the MetaMixin for models that support meta.
if ( ! _.isUndefined( model.prototype.args.meta ) ) {
model = model.extend( MetaMixin );
}

View File

@ -145,6 +145,7 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
'post_excerpt' => 'REST API Client Fixture: Post',
'post_author' => 0,
) );
wp_update_post( array(
'ID' => $post_id,
'post_content' => 'Updated post content.',
@ -195,6 +196,24 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
'comment_author_email' => 'lights@example.org',
'comment_author_url' => 'http://lights.example.org/',
) );
$meta_args = array(
'sanitize_callback' => 'sanitize_my_meta_key',
'auth_callback' => '__return_true',
'type' => 'string',
'description' => 'Test meta key',
'single' => true,
'show_in_rest' => true,
);
// Set up meta.
register_meta( 'user', 'meta_key', $meta_args );
update_user_meta( 1, 'meta_key', 'meta_value' ); // Always use the first user.
register_meta( 'post', 'meta_key', $meta_args );
update_post_meta( $post_id, 'meta_key', 'meta_value' );
register_meta( 'comment', 'meta_key', $meta_args );
update_comment_meta( $comment_id, 'meta_key', 'meta_value' );
register_meta( 'term', 'meta_key', $meta_args );
update_term_meta( $tag_id, 'meta_key', 'meta_value' );
// Generate route data for subsequent QUnit tests.
$routes_to_generate_data = array(

View File

@ -3560,7 +3560,9 @@ mockedApiResponse.PostsCollection = [
"sticky": false,
"template": "",
"format": "standard",
"meta": [],
"meta": {
"meta_key": "meta_value"
},
"categories": [
1
],
@ -3651,7 +3653,9 @@ mockedApiResponse.PostModel = {
"sticky": false,
"template": "",
"format": "standard",
"meta": [],
"meta": {
"meta_key": "meta_value"
},
"categories": [
1
],
@ -3745,7 +3749,9 @@ mockedApiResponse.PagesCollection = [
"comment_status": "closed",
"ping_status": "closed",
"template": "",
"meta": [],
"meta": {
"meta_key": ""
},
"_links": {
"self": [
{
@ -3820,7 +3826,9 @@ mockedApiResponse.PageModel = {
"comment_status": "closed",
"ping_status": "closed",
"template": "",
"meta": []
"meta": {
"meta_key": ""
}
};
mockedApiResponse.pageRevisions = [
@ -3899,7 +3907,9 @@ mockedApiResponse.MediaCollection = [
"comment_status": "open",
"ping_status": "closed",
"template": "",
"meta": [],
"meta": {
"meta_key": ""
},
"description": {
"rendered": "<p class=\"attachment\"><!-- <a...><img.../></a> --></p>"
},
@ -3958,7 +3968,9 @@ mockedApiResponse.MediaModel = {
"comment_status": "open",
"ping_status": "closed",
"template": "",
"meta": [],
"meta": {
"meta_key": ""
},
"description": {
"rendered": "<p class=\"attachment\"><!-- <a...><img.../></a> --></p>"
},
@ -4242,7 +4254,9 @@ mockedApiResponse.CategoriesCollection = [
"slug": "uncategorized",
"taxonomy": "category",
"parent": 0,
"meta": [],
"meta": {
"meta_key": ""
},
"_links": {
"self": [
{
@ -4284,7 +4298,9 @@ mockedApiResponse.CategoryModel = {
"slug": "uncategorized",
"taxonomy": "category",
"parent": 0,
"meta": []
"meta": {
"meta_key": ""
}
};
mockedApiResponse.TagsCollection = [
@ -4296,7 +4312,9 @@ mockedApiResponse.TagsCollection = [
"name": "REST API Client Fixture: Tag",
"slug": "restapi-client-fixture-tag",
"taxonomy": "post_tag",
"meta": [],
"meta": {
"meta_key": "meta_value"
},
"_links": {
"self": [
{
@ -4337,7 +4355,9 @@ mockedApiResponse.TagModel = {
"name": "REST API Client Fixture: Tag",
"slug": "restapi-client-fixture-tag",
"taxonomy": "post_tag",
"meta": []
"meta": {
"meta_key": "meta_value"
}
};
mockedApiResponse.UsersCollection = [
@ -4353,7 +4373,9 @@ mockedApiResponse.UsersCollection = [
"48": "http://0.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=48&d=mm&r=g",
"96": "http://0.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=96&d=mm&r=g"
},
"meta": [],
"meta": {
"meta_key": "meta_value"
},
"_links": {
"self": [
{
@ -4379,7 +4401,9 @@ mockedApiResponse.UsersCollection = [
"48": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=48&d=mm&r=g",
"96": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
},
"meta": [],
"meta": {
"meta_key": ""
},
"_links": {
"self": [
{
@ -4407,7 +4431,9 @@ mockedApiResponse.UserModel = {
"48": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=48&d=mm&r=g",
"96": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
},
"meta": []
"meta": {
"meta_key": ""
}
};
mockedApiResponse.me = {
@ -4422,7 +4448,9 @@ mockedApiResponse.me = {
"48": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=48&d=mm&r=g",
"96": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
},
"meta": []
"meta": {
"meta_key": ""
}
};
mockedApiResponse.CommentsCollection = [
@ -4446,7 +4474,9 @@ mockedApiResponse.CommentsCollection = [
"48": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=48&d=mm&r=g",
"96": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=96&d=mm&r=g"
},
"meta": [],
"meta": {
"meta_key": "meta_value"
},
"_links": {
"self": [
{
@ -4489,7 +4519,9 @@ mockedApiResponse.CommentModel = {
"48": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=48&d=mm&r=g",
"96": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=96&d=mm&r=g"
},
"meta": []
"meta": {
"meta_key": "meta_value"
}
};
mockedApiResponse.settings = {

View File

@ -1,4 +1,4 @@
/* global wp */
/* global wp, JSON */
( function( QUnit ) {
module( 'wpapi' );
@ -381,4 +381,44 @@
} );
} );
var theModelTypesWithMeta = [
'Posts',
'Comments',
'Tags',
'Users'
];
_.each( theModelTypesWithMeta, function( modelType ) {
// Test post meta.
wp.api.loadPromise.done( function() {
QUnit.test( 'Check meta support for ' + modelType + '.', function( assert ) {
var theModels = new wp.api.collections[ modelType ]();
theModels.fetch().done( function() {
// Get the main endpoint.
var endpoint = theModels.at(0);
// Verify the meta object returned correctly from `getMetas()`.
assert.equal( JSON.stringify( endpoint.getMetas() ), '{"meta_key":"meta_value"}', 'Full meta key/values object should be readable.' );
// Verify single meta returned correctly from `getMeta()`
assert.equal( endpoint.getMeta( 'meta_key' ), 'meta_value', 'Single meta should be readable by key.' );
// Verify setting meta values with `setMetas()`.
endpoint.setMetas( { 'test_key':'test_value' } );
assert.equal( endpoint.getMeta( 'test_key' ), 'test_value', 'Multiple meta should be writable via setMetas.' );
// Verify setting a single meta value with `setMeta()`.
endpoint.setMeta( 'test_key2', 'test_value2' );
assert.equal( endpoint.getMeta( 'test_key2' ), 'test_value2', 'Single meta should be writable via setMeta.' );
} );
} );
} );
} );
} )( window.QUnit );