Skip to content

Commit

Permalink
Chrome: Hide post Author if the user don't have the right capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Sep 6, 2017
1 parent 23a8ac6 commit 1251c92
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
5 changes: 3 additions & 2 deletions editor/sidebar/post-author/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ export class PostAuthor extends Component {
}

render() {
const { postAuthor, instanceId, user } = this.props;
const authors = this.getAuthors();
if ( authors.length < 2 ) {
if ( ! user.data || ! user.data.capabilities.publish_posts || authors.length < 2 ) {
return null;
}

const { postAuthor, instanceId } = this.props;
const selectId = 'post-author-selector-' + instanceId;

// Disable reason: A select with an onchange throws a warning
Expand Down Expand Up @@ -90,6 +90,7 @@ const applyConnect = connect(
const applyWithAPIData = withAPIData( () => {
return {
users: '/wp/v2/users?context=edit&per_page=100',
user: '/wp/v2/users/me?context=edit',
};
} );

Expand Down
30 changes: 24 additions & 6 deletions editor/sidebar/post-author/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,25 @@ describe( 'PostAuthor', () => {
],
};

const user = {
data: {
capabilities: {
publish_posts: true,
},
},
};

describe( '#getAuthors()', () => {
it( 'returns empty array on unknown users', () => {
const wrapper = shallow( <PostAuthor users={ {} } /> );
const wrapper = shallow( <PostAuthor users={ {} } user={ user } /> );

const authors = wrapper.instance().getAuthors();

expect( authors ).toEqual( [] );
} );

it( 'filters users to authors', () => {
const wrapper = shallow( <PostAuthor users={ users } /> );
const wrapper = shallow( <PostAuthor users={ users } user={ user } /> );

const authors = wrapper.instance().getAuthors();

Expand All @@ -54,30 +62,39 @@ describe( 'PostAuthor', () => {
} );

describe( '#render()', () => {
it( 'should not render anything if the user doesn\'t have the right capabilities', () => {
let wrapper = shallow( <PostAuthor users={ users } user={ {} } /> );
expect( wrapper.type() ).toBe( null );
wrapper = shallow( <PostAuthor users={ users } user={
{ data: { capabilities: { publish_posts: false } } }
} /> );
expect( wrapper.type() ).toBe( null );
} );

it( 'should not render anything if users unknown', () => {
const wrapper = shallow( <PostAuthor users={ {} } /> );
const wrapper = shallow( <PostAuthor users={ {} } user={ user } /> );

expect( wrapper.type() ).toBe( null );
} );

it( 'should not render anything if single user', () => {
const wrapper = shallow(
<PostAuthor users={ { data: users.data.slice( 0, 1 ) } } />
<PostAuthor users={ { data: users.data.slice( 0, 1 ) } } user={ user } />
);

expect( wrapper.type() ).toBe( null );
} );

it( 'should not render anything if single filtered user', () => {
const wrapper = shallow(
<PostAuthor users={ { data: users.data.slice( 0, 2 ) } } />
<PostAuthor users={ { data: users.data.slice( 0, 2 ) } } user={ user } />
);

expect( wrapper.type() ).toBe( null );
} );

it( 'should render select control', () => {
const wrapper = shallow( <PostAuthor users={ users } /> );
const wrapper = shallow( <PostAuthor users={ users } user={ user } /> );

expect( wrapper.find( 'select' ).length ).not.toBe( 0 );
} );
Expand All @@ -87,6 +104,7 @@ describe( 'PostAuthor', () => {
const wrapper = shallow(
<PostAuthor
users={ users }
user={ user }
onUpdateAuthor={ onUpdateAuthor } />
);

Expand Down

0 comments on commit 1251c92

Please sign in to comment.