Skip to content

Commit

Permalink
Fix: Crash when creating a hierarchical post without title (#19936)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta authored Jan 29, 2020
1 parent 6c8d945 commit 0bda7c4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/e2e-tests/plugins/custom-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,23 @@ function public_queryable_true_public_true_cpt() {
);
}
add_action( 'init', 'public_queryable_true_public_true_cpt' );

/**
* Registers a custom post type that is hierarchical and does not supports the title attribute.
*/
function hierarchical_without_title_cpt() {
register_post_type(
'hierar-no-title',
array(
'public' => true,
'label' => 'Hierarchical No Title',
'show_in_rest' => true,
'hierarchical' => true,
'supports' => array( 'page-attributes', 'editor', 'thumbnail', 'comments', 'post-formats' ),
'show_ui' => true,
'show_in_menu' => true,
)
);
}
add_action( 'init', 'hierarchical_without_title_cpt' );

56 changes: 56 additions & 0 deletions packages/e2e-tests/specs/editor/plugins/custom-post-types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* WordPress dependencies
*/
import {
activatePlugin,
createNewPost,
deactivatePlugin,
publishPost,
findSidebarPanelWithTitle,
} from '@wordpress/e2e-test-utils';

const openPageAttributesPanel = async () => {
const openButton = await findSidebarPanelWithTitle( 'Page Attributes' );

// Get the classes from the panel
const buttonClassName = await ( await openButton.getProperty( 'className' ) ).jsonValue();

// Open the panel if needed.
if ( -1 === buttonClassName.indexOf( 'is-opened' ) ) {
await openButton.click();
}
};
const SELECT_OPTION_SELECTOR = '.editor-page-attributes__parent option:nth-child(2)';

describe( 'Test Custom Post Types', () => {
beforeAll( async () => {
await activatePlugin( 'gutenberg-test-custom-post-types' );
} );

afterAll( async () => {
await deactivatePlugin( 'gutenberg-test-custom-post-types' );
} );

it( 'It should be able to create an hierarchical post without title support', async () => {
// Create a parent post.
await createNewPost( { postType: 'hierar-no-title' } );
await page.click( '.block-editor-writing-flow' );
await page.keyboard.type( 'Parent Post' );
await publishPost();
// Create a post that is a child of the previously created post.
await createNewPost( { postType: 'hierar-no-title' } );
await openPageAttributesPanel();
await page.waitForSelector( SELECT_OPTION_SELECTOR );
const optionToSelect = await page.$( SELECT_OPTION_SELECTOR );
const valueToSelect = await ( await optionToSelect.getProperty( 'value' ) ).jsonValue();
await page.select( '.editor-page-attributes__parent select', valueToSelect );
await page.click( '.block-editor-writing-flow' );
await page.keyboard.type( 'Child Post' );
await publishPost();
// Reload the child post and verify it is still correctly selected as a child post.
await page.reload();
await page.waitForSelector( SELECT_OPTION_SELECTOR );
const selectedValue = await page.$eval( '.editor-page-attributes__parent select', ( el ) => el.value );
expect( selectedValue ).toEqual( valueToSelect );
} );
} );
2 changes: 1 addition & 1 deletion packages/editor/src/components/page-attributes/parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function PageAttributesParent( { parent, postType, items, onUpdateParent
const pagesTree = buildTermsTree( pageItems.map( ( item ) => ( {
id: item.id,
parent: item.parent,
name: item.title.raw ? item.title.raw : `#${ item.id } (${ __( 'no title' ) })`,
name: ( item.title && item.title.raw ) ? item.title.raw : `#${ item.id } (${ __( 'no title' ) })`,
} ) ) );
return (
<TreeSelect
Expand Down

0 comments on commit 0bda7c4

Please sign in to comment.