From 37dd81f889be38c81878fcba8ee948cc60e2c937 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 5 Mar 2019 08:48:22 +0100 Subject: [PATCH 1/2] Fix allowed_block_types regression --- packages/block-editor/src/store/defaults.js | 2 +- packages/e2e-tests/plugins/allowed-blocks.php | 20 +++++++++++ .../specs/plugins/allowed-blocks.test.js | 36 +++++++++++++++++++ .../editor/src/components/provider/index.js | 2 +- 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 packages/e2e-tests/plugins/allowed-blocks.php create mode 100644 packages/e2e-tests/specs/plugins/allowed-blocks.test.js diff --git a/packages/block-editor/src/store/defaults.js b/packages/block-editor/src/store/defaults.js index 3e61e459c53f6..728b500a51d59 100644 --- a/packages/block-editor/src/store/defaults.js +++ b/packages/block-editor/src/store/defaults.js @@ -17,7 +17,7 @@ export const PREFERENCES_DEFAULTS = { * disableCustomFontSizes boolean Whether or not the custom font sizes are disabled * imageSizes Array Available image sizes * maxWidth number Max width to constraint resizing - * blockTypes boolean|Array Allowed block types + * allowedBlockTypes boolean|Array Allowed block types * hasFixedToolbar boolean Whether or not the editor toolbar is fixed * focusMode boolean Whether the focus mode is enabled or not * styles Array Editor Styles diff --git a/packages/e2e-tests/plugins/allowed-blocks.php b/packages/e2e-tests/plugins/allowed-blocks.php new file mode 100644 index 0000000000000..ec21febe59189 --- /dev/null +++ b/packages/e2e-tests/plugins/allowed-blocks.php @@ -0,0 +1,20 @@ +post_type !== 'post' ) { + return $allowed_block_types; + } + return array( 'core/paragraph', 'core/image' ); +} + +add_filter( 'allowed_block_types', 'my_plugin_allowed_block_types', 10, 2 ); diff --git a/packages/e2e-tests/specs/plugins/allowed-blocks.test.js b/packages/e2e-tests/specs/plugins/allowed-blocks.test.js new file mode 100644 index 0000000000000..9e86e4ad01990 --- /dev/null +++ b/packages/e2e-tests/specs/plugins/allowed-blocks.test.js @@ -0,0 +1,36 @@ +/** + * WordPress dependencies + */ +import { + activatePlugin, + createNewPost, + deactivatePlugin, + searchForBlock, +} from '@wordpress/e2e-test-utils'; + +describe( 'Allowed Blocks Filter', () => { + beforeAll( async () => { + await activatePlugin( 'gutenberg-test-allowed-blocks' ); + } ); + + beforeEach( async () => { + await createNewPost(); + } ); + + afterAll( async () => { + await deactivatePlugin( 'gutenberg-test-allowed-blocks' ); + } ); + + it( 'should restrict the allowed blocks in the inserter', async () => { + // The paragraph block is available. + await searchForBlock( 'Paragraph' ); + const paragraphBlock = await page.$( `button[aria-label="Paragraph"]` ); + expect( paragraphBlock ).not.toBeNull(); + await paragraphBlock.click(); + + // The gallery block is not available. + await searchForBlock( 'Gallery' ); + const galleryBlock = await page.$( `button[aria-label="Gallery"]` ); + expect( galleryBlock ).toBeNull(); + } ); +} ); diff --git a/packages/editor/src/components/provider/index.js b/packages/editor/src/components/provider/index.js index 92e13d137ce54..8817eb5d4be3b 100644 --- a/packages/editor/src/components/provider/index.js +++ b/packages/editor/src/components/provider/index.js @@ -61,7 +61,7 @@ class EditorProvider extends Component { 'disableCustomFontSizes', 'imageSizes', 'maxWidth', - 'blockTypes', + 'allowedBlockTypes', 'hasFixedToolbar', 'focusMode', 'styles', From dc17306c4f36646a52fcadceb45df1f7b48bd46f Mon Sep 17 00:00:00 2001 From: Jorge Date: Tue, 5 Mar 2019 08:54:51 +0000 Subject: [PATCH 2/2] Apply required fixes for phpcs errors --- packages/e2e-tests/plugins/allowed-blocks.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/e2e-tests/plugins/allowed-blocks.php b/packages/e2e-tests/plugins/allowed-blocks.php index ec21febe59189..c3d30cb20c9bd 100644 --- a/packages/e2e-tests/plugins/allowed-blocks.php +++ b/packages/e2e-tests/plugins/allowed-blocks.php @@ -9,12 +9,16 @@ /** * Restrict the allowed blocks in the editor. + * + * @param Array $allowed_block_types An array of strings containing the previously allowed blocks. + * @param WP_Post $post The current post object. + * @return Array An array of strings containing the new allowed blocks after the filter is applied. */ function my_plugin_allowed_block_types( $allowed_block_types, $post ) { - if ( $post->post_type !== 'post' ) { - return $allowed_block_types; - } - return array( 'core/paragraph', 'core/image' ); + if ( 'post' !== $post->post_type ) { + return $allowed_block_types; + } + return array( 'core/paragraph', 'core/image' ); } add_filter( 'allowed_block_types', 'my_plugin_allowed_block_types', 10, 2 );