Skip to content

Commit

Permalink
Check for nested navigation blocks at multiple levels and add unit te…
Browse files Browse the repository at this point in the history
…sts. Co-authored-by: Héctor <[email protected]>
  • Loading branch information
Ben Dwyer committed Jan 26, 2023
1 parent e703128 commit 575db6b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,21 +411,22 @@ function( $block ) {
}

/**
* Bail on rendering nested navigation blocks
* Returns true if the navigation block contains a nested navigation block.
*
* @param array $parsed_blocks the parsed blocks to be normalized.
* @return array the normalized parsed blocks.
* @return bool true if the navigation block contains a nested navigation block.
*/
function block_core_navigation_has_nested_core_navigation( $parsed_blocks ) {
$filtered = array_filter(
$parsed_blocks,
function( $block ) {
return $block['blockName'] === 'core/navigation';
foreach ( $parsed_blocks as $block ) {
if ( 'core/navigation' === $block['blockName'] ) {
return true;
}
);
if ( block_core_navigation_has_nested_core_navigation( $block['innerBlocks'] ) ) {
return true;
}
}

// Reset keys.
return count( $filtered ) > 0;
return false;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions phpunit/blocks/render-block-navigation-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,22 @@ public function test_block_core_navigation_get_post_ids_from_block_with_submenu(
$this->assertSameSetsWithIndex( array( 755, 789 ), $post_ids );
}

/**
* @covers ::gutenberg_block_core_navigation_has_nested_core_navigation
*/
public function test_gutenberg_block_core_navigation_has_nested_core_navigation() {
$parsed_blocks = parse_blocks( '<!-- wp:navigation /-->' );
$this->assertTrue( gutenberg_block_core_navigation_has_nested_core_navigation( $parsed_blocks ) );
}

public function test_gutenberg_block_core_navigation_has_nested_core_navigation_deep() {
$parsed_blocks = parse_blocks( '<!-- wp:group --><!-- /wp:group --><!-- wp:group --><!-- wp:group --><!-- wp:navigation /--><!-- /wp:group --><!-- /wp:group -->' );
$this->assertTrue( gutenberg_block_core_navigation_has_nested_core_navigation( $parsed_blocks ) );
}

public function test_gutenberg_block_core_navigation_has_nested_core_navigation_no_navigation() {
$parsed_blocks = parse_blocks( '<!-- wp:group --><!-- wp:group --><!-- /wp:group --><!-- /wp:group -->' );
$this->assertFalse( gutenberg_block_core_navigation_has_nested_core_navigation( $parsed_blocks ) );
}

}

0 comments on commit 575db6b

Please sign in to comment.