-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Check for allowed blocks recursively in patterns #30366
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
Merged
david-szabo97
merged 20 commits into
trunk
from
fix/patterns-check-allowed-block-types-recursively
May 14, 2021
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
bd06b18
Check blocks recursively
david-szabo97 1d2a707
Return early in case when all blocks are allowed/disallowed
david-szabo97 109c98b
Update dependencies
david-szabo97 b0e1c35
Remove unused dependency
david-szabo97 5038470
Use correct innerBlocks property
david-szabo97 4cd5b4d
Remove IIFE
david-szabo97 0c36222
Extract function
david-szabo97 6380fd0
Add e2e tests
david-szabo97 d934014
Rework tests
david-szabo97 cf1eaa6
Remove scope
david-szabo97 c953c6f
Make tests faster and more consistent
david-szabo97 eec6c1d
Use stage one parsing only
david-szabo97 341b3ea
Rename function and don't export it
david-szabo97 f83e430
Add comment
david-szabo97 6513185
Rename properties and update comment
david-szabo97 854d9e9
Fix typo
david-szabo97 3109b0d
Update usages
david-szabo97 7e7d9c3
Fix wrong selector called
david-szabo97 fb6af97
Move selector call to usePatternsSetup
david-szabo97 70d80c2
Remove selector call
david-szabo97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add e2e tests
- Loading branch information
commit 6380fd0f92efcb236cab421f4b2a798d641ffd39
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
packages/e2e-tests/plugins/allowed-patterns-disable-blocks.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
| /** | ||
| * Plugin Name: Gutenberg Test Allowed Patterns Disable Blocks | ||
| * Plugin URI: https://github.com/WordPress/gutenberg | ||
| * Author: Gutenberg Team | ||
| * | ||
| * @package gutenberg-test-allowed-patterns-disable-blocks | ||
| */ | ||
|
|
||
| /** | ||
| * 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->post_type ) { | ||
| return $allowed_block_types; | ||
| } | ||
| return array( 'core/heading', 'core/columns', 'core/column', 'core/image', 'core/spacer' ); | ||
| } | ||
|
|
||
| add_filter( 'allowed_block_types', 'my_plugin_allowed_block_types', 10, 2 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
| /** | ||
| * Plugin Name: Gutenberg Test Allowed Patterns | ||
| * Plugin URI: https://github.com/WordPress/gutenberg | ||
| * Author: Gutenberg Team | ||
| * | ||
| * @package gutenberg-test-allowed-patterns | ||
| */ | ||
|
|
||
| register_block_pattern( | ||
| 'test-allowed-patterns/lone-heading', | ||
| array( | ||
| 'title' => 'Test: Single heading', | ||
| 'scope' => array( | ||
| 'inserter' => true, | ||
| ), | ||
| 'content' => '<!-- wp:heading --><h2>Hello!</h2><!-- /wp:heading -->', | ||
| ) | ||
| ); | ||
|
|
||
| register_block_pattern( | ||
| 'test-allowed-patterns/lone-paragraph', | ||
| array( | ||
| 'title' => 'Test: Single paragraph', | ||
| 'scope' => array( | ||
| 'inserter' => true, | ||
| ), | ||
| 'content' => '<!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph -->', | ||
| ) | ||
| ); | ||
|
|
||
| register_block_pattern( | ||
| 'test-allowed-patterns/paragraph-inside-group', | ||
| array( | ||
| 'title' => 'Test: Paragraph inside group', | ||
| 'scope' => array( | ||
david-szabo97 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'inserter' => true, | ||
| ), | ||
| 'content' => '<!-- wp:group --><div class="wp-block-group"><!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph --></div><!-- /wp:group -->', | ||
| ) | ||
| ); | ||
67 changes: 67 additions & 0 deletions
67
packages/e2e-tests/specs/editor/various/allowed-patterns.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { | ||
| activatePlugin, | ||
| createNewPost, | ||
| deactivatePlugin, | ||
| searchForPattern, | ||
| toggleGlobalBlockInserter, | ||
| } from '@wordpress/e2e-test-utils'; | ||
|
|
||
| const isPatternAvailable = async ( name ) => { | ||
| await searchForPattern( name ); | ||
| const elements = await page.$x( | ||
| `//div[@role = 'option']//div[contains(text(), '${ name }')]` | ||
| ); | ||
| const patternExists = elements.length > 0; | ||
| await toggleGlobalBlockInserter(); | ||
| return patternExists; | ||
| }; | ||
|
|
||
| const TEST_PATTERNS = [ | ||
| [ 'Test: Single heading', false ], | ||
| [ 'Test: Single paragraph', true ], | ||
| [ 'Test: Paragraph inside group', true ], | ||
| ]; | ||
|
|
||
| describe( 'Allowed Patterns', () => { | ||
| beforeAll( async () => { | ||
| await activatePlugin( 'gutenberg-test-allowed-patterns' ); | ||
| } ); | ||
| afterAll( async () => { | ||
| await deactivatePlugin( 'gutenberg-test-allowed-patterns' ); | ||
| } ); | ||
| beforeEach( async () => { | ||
| await createNewPost(); | ||
| } ); | ||
|
|
||
| describe( 'Disable blocks plugin disabled', () => { | ||
| it( 'should show test patterns', async () => { | ||
| for ( const [ patternName ] of TEST_PATTERNS ) { | ||
| expect( await isPatternAvailable( patternName ) ).toBe( true ); | ||
| } | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'Disable blocks plugin enabled', () => { | ||
| beforeAll( async () => { | ||
| await activatePlugin( | ||
| 'gutenberg-test-allowed-patterns-disable-blocks' | ||
| ); | ||
| } ); | ||
| afterAll( async () => { | ||
| await deactivatePlugin( | ||
| 'gutenberg-test-allowed-patterns-disable-blocks' | ||
| ); | ||
| } ); | ||
|
|
||
| it( 'should not show test patterns', async () => { | ||
| for ( const [ patternName, shouldBeAvailable ] of TEST_PATTERNS ) { | ||
| expect( await isPatternAvailable( patternName ) ).toBe( | ||
| shouldBeAvailable | ||
| ); | ||
| } | ||
| } ); | ||
| } ); | ||
| } ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.