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

Fix: Regression: Template lock option is not taken into consideration #14390

Merged
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
56 changes: 56 additions & 0 deletions packages/e2e-tests/plugins/cpt-locking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Plugin Name: Gutenberg Test Plugin, CPT Locking
* Plugin URI: https://github.com/WordPress/gutenberg
* Author: Gutenberg Team
*
* @package gutenberg-test-cpt-locking
*/

/**
* Registers CPT's with 3 diffferent types of locking.
*/
function gutenberg_test_cpt_locking() {
$template = array(
array( 'core/image' ),
array(
'core/paragraph',
array(
'placeholder' => 'Add a description',
),
),
array( 'core/quote' ),
);
register_post_type(
'locked-all-post',
array(
'public' => true,
'label' => 'Locked All Post',
'show_in_rest' => true,
'template' => $template,
'template_lock' => 'all',
)
);
register_post_type(
'locked-insert-post',
array(
'public' => true,
'label' => 'Locked Insert Post',
'show_in_rest' => true,
'template' => $template,
'template_lock' => 'insert',
)
);
register_post_type(
'not-locked-post',
array(
'public' => true,
'label' => 'Not Locked Post',
'show_in_rest' => true,
'template' => $template,
'template_lock' => false,
)
);
}

add_action( 'init', 'gutenberg_test_cpt_locking' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`cpt locking template_lock false should allow blocks to be inserted 1`] = `
"<!-- wp:image -->
<figure class=\\"wp-block-image\\"><img alt=\\"\\"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph {\\"placeholder\\":\\"Add a description\\"} -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote -->

<!-- wp:list -->
<ul><li>List content</li></ul>
<!-- /wp:list -->"
`;

exports[`cpt locking template_lock false should allow blocks to be moved 1`] = `
"<!-- wp:paragraph {\\"placeholder\\":\\"Add a description\\"} -->
<p>p1</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class=\\"wp-block-image\\"><img alt=\\"\\"/></figure>
<!-- /wp:image -->

<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote -->"
`;

exports[`cpt locking template_lock false should allow blocks to be removed 1`] = `
"<!-- wp:image -->
<figure class=\\"wp-block-image\\"><img alt=\\"\\"/></figure>
<!-- /wp:image -->

<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote -->"
`;

exports[`cpt locking template_lock insert should allow blocks to be moved 1`] = `
"<!-- wp:paragraph {\\"placeholder\\":\\"Add a description\\"} -->
<p>p1</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class=\\"wp-block-image\\"><img alt=\\"\\"/></figure>
<!-- /wp:image -->

<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote -->"
`;
99 changes: 99 additions & 0 deletions packages/e2e-tests/specs/plugins/cpt-locking.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* WordPress dependencies
*/
import {
activatePlugin,
clickBlockToolbarButton,
createNewPost,
deactivatePlugin,
getEditedPostContent,
insertBlock,
} from '@wordpress/e2e-test-utils';

describe( 'cpt locking', () => {
beforeAll( async () => {
await activatePlugin( 'gutenberg-test-plugin-cpt-locking' );
} );

afterAll( async () => {
await deactivatePlugin( 'gutenberg-test-plugin-cpt-locking' );
} );

const shouldRemoveTheInserter = async () => {
expect(
await page.$( '.edit-post-header [aria-label="Add block"]' )
).toBeNull();
};

const shouldNotAllowBlocksToBeRemoved = async () => {
await page.type( '.editor-rich-text__editable.wp-block-paragraph', 'p1' );
await clickBlockToolbarButton( 'More options' );
expect(
await page.$x( '//button[contains(text(), "Remove Block")]' )
).toHaveLength( 0 );
};

const shouldAllowBlocksToBeMoved = async () => {
await page.click( '.editor-rich-text__editable.wp-block-paragraph' );
expect(
await page.$( 'button[aria-label="Move up"]' )
).not.toBeNull();
await page.click( 'button[aria-label="Move up"]' );
await page.type( '.editor-rich-text__editable.wp-block-paragraph', 'p1' );
expect( await getEditedPostContent() ).toMatchSnapshot();
};

describe( 'template_lock all', () => {
beforeEach( async () => {
await createNewPost( { postType: 'locked-all-post' } );
} );

it( 'should remove the inserter', shouldRemoveTheInserter );

it( 'should not allow blocks to be removed', shouldNotAllowBlocksToBeRemoved );

it( 'should not allow blocks to be moved', async () => {
await page.click( '.editor-rich-text__editable.wp-block-paragraph' );
expect(
await page.$( 'button[aria-label="Move up"]' )
).toBeNull();
} );
} );

describe( 'template_lock insert', () => {
beforeEach( async () => {
await createNewPost( { postType: 'locked-insert-post' } );
} );

it( 'should remove the inserter', shouldRemoveTheInserter );

it( 'should not allow blocks to be removed', shouldNotAllowBlocksToBeRemoved );

it( 'should allow blocks to be moved', shouldAllowBlocksToBeMoved );
} );

describe( 'template_lock false', () => {
beforeEach( async () => {
await createNewPost( { postType: 'not-locked-post' } );
} );

it( 'should allow blocks to be inserted', async () => {
expect(
await page.$( '.edit-post-header [aria-label="Add block"]' )
).not.toBeNull();
await insertBlock( 'List' );
await page.keyboard.type( 'List content' );
expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should allow blocks to be removed', async () => {
await page.type( '.editor-rich-text__editable.wp-block-paragraph', 'p1' );
await clickBlockToolbarButton( 'More options' );
const [ removeBlock ] = await page.$x( '//button[contains(text(), "Remove Block")]' );
await removeBlock.click();
expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should allow blocks to be moved', shouldAllowBlocksToBeMoved );
} );
} );
15 changes: 8 additions & 7 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,21 @@ class EditorProvider extends Component {
return {
...pick( settings, [
'alignWide',
'allowedBlockTypes',
'availableLegacyWidgets',
'bodyPlaceholder',
'colors',
'disableCustomColors',
'fontSizes',
'disableCustomFontSizes',
'imageSizes',
'maxWidth',
'allowedBlockTypes',
'focusMode',
'fontSizes',
'hasFixedToolbar',
'hasPermissionsToManageWidgets',
'focusMode',
'styles',
'imageSizes',
'isRTL',
'bodyPlaceholder',
'maxWidth',
'styles',
'templateLock',
'titlePlaceholder',
] ),
__experimentalMetaSource: {
Expand Down