Skip to content

Commit

Permalink
Disable the Preview button when post type isn't viewable (#6232)
Browse files Browse the repository at this point in the history
* Add tests for `viewable` REST API param

* Minor corrections to test and docs

* Disable the Preview button when post type isn't viewable

* Fix coding style issue

* Add e2e assertion for the preview button
  • Loading branch information
danielbachhuber authored and youknowriad committed Apr 18, 2018
1 parent e50e200 commit d6d950a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
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

0 comments on commit d6d950a

Please sign in to comment.