Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add e2e tests
  • Loading branch information
david-szabo97 committed May 12, 2021
commit 6380fd0f92efcb236cab421f4b2a798d641ffd39
24 changes: 24 additions & 0 deletions packages/e2e-tests/plugins/allowed-patterns-disable-blocks.php
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 );
41 changes: 41 additions & 0 deletions packages/e2e-tests/plugins/allowed-patterns.php
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(
'inserter' => true,
),
'content' => '<!-- wp:group --><div class="wp-block-group"><!-- wp:paragraph --><p>Hello!</p><!-- /wp:paragraph --></div><!-- /wp:group -->',
)
);
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
);
}
} );
} );
} );