diff --git a/lib/taxonomies.js b/lib/taxonomies.js index ff86c62c..7637bccf 100644 --- a/lib/taxonomies.js +++ b/lib/taxonomies.js @@ -148,4 +148,18 @@ TaxonomiesRequest.prototype.parent = function( parentId ) { return this; }; +/** + * Specify the post for which to retrieve terms + * + * @method forPost + * @chainable + * @param {String|Number} post The ID of the post for which to retrieve terms + * @return {TaxonomiesRequest} The TaxonomiesRequest instance (for chaining) + */ +TaxonomiesRequest.prototype.forPost = function( postId ) { + this.param( 'post', postId ); + + return this; +}; + module.exports = TaxonomiesRequest; diff --git a/tests/integration/categories.js b/tests/integration/categories.js index 485d5684..d0f6c5a4 100644 --- a/tests/integration/categories.js +++ b/tests/integration/categories.js @@ -7,6 +7,7 @@ var SUCCESS = 'success'; // actually run. chai.use( require( 'chai-as-promised' ) ); var expect = chai.expect; +var _ = require( 'lodash' ); var WP = require( '../../' ); var WPRequest = require( '../../lib/shared/wp-request.js' ); @@ -309,4 +310,37 @@ describe( 'integration: categories()', function() { }); + describe( 'forPost()', function() { + + it( 'can be used to retrieve terms for a specific post', function() { + var postCategories; + var prom = wp.posts().perPage( 1 ).embed().get().then(function( posts ) { + var post = posts[ 0 ]; + // Find the categories for this post + postCategories = _.findWhere( post._embedded['https://api.w.org/term'], function( terms ) { + if ( terms.length && terms[ 0 ].taxonomy === 'category' ) { + return true; + } + }); + var postId = post.id; + return wp.categories().forPost( postId ); + }).then(function( categories ) { + expect( categories.length ).to.equal( postCategories.length ); + categories.forEach(function( cat, idx ) { + [ + 'id', + 'name', + 'slug', + 'taxonomy' + ].forEach(function( prop ) { + expect( cat[ prop ] ).to.equal( postCategories[ idx ][ prop ] ); + }); + }); + return SUCCESS; + }); + return expect( prom ).to.eventually.equal( SUCCESS ); + }); + + }); + }); diff --git a/tests/integration/posts.js b/tests/integration/posts.js index bcf36c34..9bf82b0f 100644 --- a/tests/integration/posts.js +++ b/tests/integration/posts.js @@ -280,7 +280,7 @@ describe( 'integration: posts()', function() { }).catch(function( err ) { expect( err ).to.be.an( 'object' ); expect( err ).to.have.property( 'status' ); - expect( err.status ).to.equal( 403 ); + expect( err.status ).to.equal( 401 ); return SUCCESS; }); return expect( prom ).to.eventually.equal( SUCCESS ); @@ -297,7 +297,7 @@ describe( 'integration: posts()', function() { }).catch(function( err ) { expect( err ).to.be.an( 'object' ); expect( err ).to.have.property( 'status' ); - expect( err.status ).to.equal( 403 ); + expect( err.status ).to.equal( 401 ); return SUCCESS; }); return expect( prom ).to.eventually.equal( SUCCESS ); diff --git a/tests/unit/lib/taxonomies.js b/tests/unit/lib/taxonomies.js index 72e1e7d1..2eb5f0c9 100644 --- a/tests/unit/lib/taxonomies.js +++ b/tests/unit/lib/taxonomies.js @@ -85,6 +85,11 @@ describe( 'wp.taxonomies', function() { expect( url ).to.equal( '/wp-json/wp/v2/categories?parent=42' ); }); + it( 'should permit specifying the parent for a collection of terms', function() { + var url = taxonomies.collection( 'categories' ).forPost( 1234 )._renderURI(); + expect( url ).to.equal( '/wp-json/wp/v2/categories?post=1234' ); + }); + }); });