Skip to content

Commit

Permalink
Fix error when using a navigation block that returns an empty fallbac…
Browse files Browse the repository at this point in the history
…k result (#56629)

* Fix fatal error when using a navigation block fallback that returns an empty result

* Fix get_inner_blocks_from_unstable_location function

* Use an empty WP_Block_List as a return value

* Add a unit test for get_inner_blocks_from_navigation_post
  • Loading branch information
talldan authored Dec 11, 2023
1 parent 823d086 commit b6860f0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private static function get_inner_blocks_html( $attributes, $inner_blocks ) {
private static function get_inner_blocks_from_navigation_post( $attributes ) {
$navigation_post = get_post( $attributes['ref'] );
if ( ! isset( $navigation_post ) ) {
return '';
return new WP_Block_List( array(), $attributes );
}

// Only published posts are valid. If this is changed then a corresponding change
Expand Down Expand Up @@ -214,7 +214,7 @@ private static function get_inner_blocks_from_fallback( $attributes ) {

// Fallback my have been filtered so do basic test for validity.
if ( empty( $fallback_blocks ) || ! is_array( $fallback_blocks ) ) {
return '';
return new WP_Block_List( array(), $attributes );
}

return new WP_Block_List( $fallback_blocks, $attributes );
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function block_core_navigation_sort_menu_items_by_parent_id( $menu_items ) {
function block_core_navigation_get_inner_blocks_from_unstable_location( $attributes ) {
$menu_items = block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] );
if ( empty( $menu_items ) ) {
return '';
return new WP_Block_List( array(), $attributes );
}

$menu_items_by_parent_id = block_core_navigation_sort_menu_items_by_parent_id( $menu_items );
Expand Down
19 changes: 19 additions & 0 deletions phpunit/class-wp-navigation-block-renderer-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,23 @@ public function test_gutenberg_get_markup_for_inner_block_site_title() {
$expected = '<li class="wp-block-navigation-item"><h1 class="wp-block-site-title"><a href="http://' . WP_TESTS_DOMAIN . '" target="_self" rel="home">Test Blog</a></h1></li>';
$this->assertEquals( $expected, $result );
}

/**
* Test that the `get_inner_blocks_from_navigation_post` method returns an empty block list for a non-existent post.
*
* @group navigation-renderer
*
* @covers WP_Navigation_Block_Renderer::get_inner_blocks_from_navigation_post
*/
public function test_gutenberg_get_inner_blocks_from_navigation_post_returns_empty_block_list() {
$reflection = new ReflectionClass( 'WP_Navigation_Block_Renderer' );
$method = $reflection->getMethod( 'get_inner_blocks_from_navigation_post' );
$method->setAccessible( true );
$attributes = array( 'ref' => 0 );

$actual = $method->invoke( $reflection, $attributes );
$expected = new WP_Block_List( array(), $attributes );
$this->assertEquals( $actual, $expected );
$this->assertCount( 0, $actual );
}
}

1 comment on commit b6860f0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in b6860f0.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7165485510
📝 Reported issues:

Please sign in to comment.