Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/taxonomies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
34 changes: 34 additions & 0 deletions tests/integration/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -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 );
});

});

});
4 changes: 2 additions & 2 deletions tests/integration/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/lib/taxonomies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
});

});

});