Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable the Preview button when post type isn't viewable #6232

Merged
merged 5 commits into from
Apr 18, 2018
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
13 changes: 12 additions & 1 deletion editor/components/post-preview-button/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
import { Component, compose } from '@wordpress/element';
import { Button } from '@wordpress/components';
import { Button, ifCondition } from '@wordpress/components';
import { _x } from '@wordpress/i18n';
import { withSelect, withDispatch } from '@wordpress/data';

Expand Down Expand Up @@ -117,16 +122,22 @@ export default compose( [
isEditedPostNew,
isEditedPostSaveable,
} = select( 'core/editor' );
const {
getPostType,
} = select( 'core' );
const postType = getPostType( getEditedPostAttribute( 'type' ) );
return {
postId: getCurrentPostId(),
link: getEditedPostPreviewLink(),
isDirty: isEditedPostDirty(),
isNew: isEditedPostNew(),
isSaveable: isEditedPostSaveable(),
isViewable: get( postType, 'viewable', false ),
modified: getEditedPostAttribute( 'modified' ),
};
} ),
withDispatch( ( dispatch )=>( {
autosave: dispatch( 'core/editor' ).autosave,
} ) ),
ifCondition( ( { isViewable } ) => isViewable ),
] )( PostPreviewButton );
23 changes: 23 additions & 0 deletions phpunit/class-gutenberg-rest-api-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,27 @@ function test_visibility_field_without_context() {

$this->assertFalse( isset( $result['visibility'] ) );
}

/**
* Should return an extra viewable field on response when in edit context.
*/
function test_viewable_field() {
wp_set_current_user( $this->administrator );
$request = new WP_REST_Request( 'GET', '/wp/v2/types/post' );
$request->set_param( 'context', 'edit' );
$response = rest_do_request( $request );
$result = $response->get_data();
$this->assertTrue( isset( $result['viewable'] ) );
$this->assertTrue( $result['viewable'] );
}

/**
* Should not return viewable field without context set.
*/
function test_viewable_field_without_context() {
$request = new WP_REST_Request( 'GET', '/wp/v2/types/post' );
$response = rest_do_request( $request );
$result = $response->get_data();
$this->assertFalse( isset( $result['viewable'] ) );
}
}
2 changes: 2 additions & 0 deletions test/e2e/specs/hello.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe( 'hello', () => {
expect( page.url() ).toEqual( expect.stringContaining( 'post-new.php' ) );
const title = await page.$( '[placeholder="Add title"]' );
expect( title ).not.toBeNull();
const postPreviewButton = await page.$( '.editor-post-preview.button' );
expect( postPreviewButton ).not.toBeNull();
} );

it( 'Should have no history', async () => {
Expand Down