From 1e5412adce71cbe22712a3a00e4b391ad654e416 Mon Sep 17 00:00:00 2001 From: scruffian Date: Wed, 1 Mar 2023 10:56:14 +0000 Subject: [PATCH 01/37] Navigation: Always create a fallback navigation menu on every page load --- .../src/navigation/edit/index.js | 66 ------------------- .../block-library/src/navigation/index.php | 34 +++++++--- 2 files changed, 26 insertions(+), 74 deletions(-) diff --git a/packages/block-library/src/navigation/edit/index.js b/packages/block-library/src/navigation/edit/index.js index 027b3b87a0a8a7..10055770dcf856 100644 --- a/packages/block-library/src/navigation/edit/index.js +++ b/packages/block-library/src/navigation/edit/index.js @@ -41,7 +41,6 @@ import { } from '@wordpress/components'; import { __, sprintf } from '@wordpress/i18n'; import { speak } from '@wordpress/a11y'; -import { createBlock, getBlockType } from '@wordpress/blocks'; import { close, Icon } from '@wordpress/icons'; /** @@ -308,71 +307,6 @@ function Navigation( { // that automatically saves the menu as an entity when changes are made to the inner blocks. const hasUnsavedBlocks = hasUncontrolledInnerBlocks && ! isEntityAvailable; - useEffect( () => { - if ( - ref || - ! hasResolvedNavigationMenus || - isConvertingClassicMenu || - fallbackNavigationMenus?.length > 0 || - hasUnsavedBlocks - ) { - return; - } - - // If there's non fallback navigation menus and - // a classic menu with a `primary` location or slug, - // then create a new navigation menu based on it. - // Otherwise, use the most recently created classic menu. - if ( classicMenus?.length ) { - const primaryMenus = classicMenus.filter( - ( classicMenu ) => - classicMenu.locations.includes( 'primary' ) || - classicMenu.slug === 'primary' - ); - - if ( primaryMenus.length ) { - convertClassicMenu( - primaryMenus[ 0 ].id, - primaryMenus[ 0 ].name, - 'publish' - ); - } else { - classicMenus.sort( ( a, b ) => { - return b.id - a.id; - } ); - convertClassicMenu( - classicMenus[ 0 ].id, - classicMenus[ 0 ].name, - 'publish' - ); - } - } else { - // If there are no fallback navigation menus and no classic menus, - // then create a new navigation menu. - - // Check that we have a page-list block type. - let defaultBlocks = []; - if ( getBlockType( 'core/page-list' ) ) { - defaultBlocks = [ createBlock( 'core/page-list' ) ]; - } - - createNavigationMenu( - 'Navigation', // TODO - use the template slug in future - defaultBlocks, - 'publish' - ); - } - }, [ - hasResolvedNavigationMenus, - hasUnsavedBlocks, - classicMenus, - convertClassicMenu, - createNavigationMenu, - fallbackNavigationMenus?.length, - isConvertingClassicMenu, - ref, - ] ); - const navRef = useRef(); // The standard HTML5 tag for the block wrapper. diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 5ec43046d6618f..9c7a1603086fdd 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -463,15 +463,14 @@ function block_core_navigation_get_default_pages_fallback() { } /** - * Retrieves the appropriate fallback to be used on the front of the - * site when there is no menu assigned to the Nav block. - * - * This aims to mirror how the fallback mechanic for wp_nav_menu works. - * See https://developer.wordpress.org/reference/functions/wp_nav_menu/#more-information. - * - * @return array the array of blocks to be used as a fallback. + * Creates the appropriate fallback to be used on the front of the site. */ -function block_core_navigation_get_fallback_blocks() { +function block_core_navigation_create_fallback() { + $should_skip = apply_filters( 'block_core_navigation_skip_fallback', false ); + if ( $should_skip ) { + return; + } + // Get the most recently published Navigation post. $navigation_post = block_core_navigation_get_most_recently_published_navigation(); @@ -485,6 +484,21 @@ function block_core_navigation_get_fallback_blocks() { if ( ! $navigation_post ) { $navigation_post = block_core_navigation_get_default_pages_fallback(); } +} + + +/** + * Retrieves the appropriate fallback to be used on the front of the + * site when there is no menu assigned to the Nav block. + * + * This aims to mirror how the fallback mechanic for wp_nav_menu works. + * See https://developer.wordpress.org/reference/functions/wp_nav_menu/#more-information. + * + * @return array the array of blocks to be used as a fallback. + */ +function block_core_navigation_get_fallback_blocks() { + // Get the most recently published Navigation post. + $navigation_post = block_core_navigation_get_most_recently_published_navigation(); // Use the first non-empty Navigation as fallback, there should always be one. if ( $navigation_post ) { @@ -899,3 +913,7 @@ function block_core_navigation_typographic_presets_backcompatibility( $parsed_bl } add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' ); + +// We have to aply this action on both init and admin init, as the code loads after init when in wp-admin. +add_action( 'init', 'block_core_navigation_create_fallback' ); +add_action( 'admin_init', 'block_core_navigation_create_fallback' ); From 78ae0b1f9ac6a704d39898239255896aa9d847b0 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 14:47:05 +0000 Subject: [PATCH 02/37] Add basic unit tests --- .../block-library/src/navigation/index.php | 2 + phpunit/navigation-block-fallback-test.php | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 phpunit/navigation-block-fallback-test.php diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 9c7a1603086fdd..506b05d522dc6e 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -484,6 +484,8 @@ function block_core_navigation_create_fallback() { if ( ! $navigation_post ) { $navigation_post = block_core_navigation_get_default_pages_fallback(); } + + return $navigation_post; } diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php new file mode 100644 index 00000000000000..c4f26a1e377b84 --- /dev/null +++ b/phpunit/navigation-block-fallback-test.php @@ -0,0 +1,42 @@ +assertEquals( $fallback->post_type, 'wp_navigation' ); + $this->assertEquals( $fallback->post_title, 'Navigation' ); + $this->assertEquals( $fallback->post_content, '' ); + $this->assertEquals( $fallback->post_status, 'publish' ); + } + + public function test_skip_if_filter_returns_truthy() { + add_filter( 'block_core_navigation_skip_fallback', '__return_true' ); + + $actual = gutenberg_block_core_navigation_create_fallback(); + + $this->assertEquals( $actual, null ); + + remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); + } +} + + From 83d1ac3200774fbf5450d873ce481e0bcb5ba63e Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 15:13:14 +0000 Subject: [PATCH 03/37] Fix Classic Meny fallback to use Menu title not slug --- packages/block-library/src/navigation/index.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 506b05d522dc6e..82fc92d1758e48 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -344,6 +344,7 @@ function block_core_navigation_maybe_use_classic_menu_fallback() { return; } + // Create a new navigation menu from the classic menu. $wp_insert_post_result = wp_insert_post( array( @@ -356,6 +357,9 @@ function block_core_navigation_maybe_use_classic_menu_fallback() { true // So that we can check whether the result is an error. ); + + + if ( is_wp_error( $wp_insert_post_result ) ) { return; } From d68c93a6679b9abad7895fb13f1ebd0afa8f9899 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 15:13:22 +0000 Subject: [PATCH 04/37] Add tests for Classic Menu --- phpunit/navigation-block-fallback-test.php | 69 +++++++++++++++++++++- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index c4f26a1e377b84..ae821ef92dd78c 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -6,6 +6,15 @@ */ class Tests_Block_Navigation_Fallbacks extends WP_UnitTestCase { + + /** + * @var WP_Post + */ + protected static $navigation_post; + + + + public function set_up() { parent::set_up(); switch_theme( 'emptytheme' ); @@ -16,10 +25,12 @@ public static function wpSetUpBeforeClass() { } public static function wpTearDownAfterClass() { - + if ( ! empty( self::$post_with_comments_disabled->ID ) ) { + wp_delete_post( self::$navigation_post->ID, true ); + } } - public function test_creates_fallback_with_page_list_by_default() { + public function test_creates_fallback_navigation_with_page_list_by_default() { $fallback = gutenberg_block_core_navigation_create_fallback(); $this->assertEquals( $fallback->post_type, 'wp_navigation' ); @@ -28,11 +39,63 @@ public function test_creates_fallback_with_page_list_by_default() { $this->assertEquals( $fallback->post_status, 'publish' ); } - public function test_skip_if_filter_returns_truthy() { + public function test_gets_fallback_navigation_with_existing_navigation_menu_if_found() { + + self::$navigation_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_navigation', + 'post_title' => 'Existing Navigation Menu', + 'post_content' => '', + ) + ); + + $fallback = gutenberg_block_core_navigation_create_fallback(); + + $this->assertEquals( $fallback->post_title, self::$navigation_post->post_title ); + $this->assertEquals( $fallback->post_type, self::$navigation_post->post_type ); + $this->assertEquals( $fallback->post_content, self::$navigation_post->post_content ); + $this->assertEquals( $fallback->post_status, self::$navigation_post->post_status ); + } + + public function test_gets_fallback_navigation_with_existing_classic_menu_if_found() { + + $menu_id = wp_create_nav_menu( 'Existing Classic Menu' ); + + wp_update_nav_menu_item( + $menu_id, + 0, + array( + 'menu-item-title' => 'Classic Menu Item 1', + 'menu-item-url' => '/classic-menu-item-1', + 'menu-item-status' => 'publish', + ) + ); + + $fallback = gutenberg_block_core_navigation_create_fallback(); + + $this->assertEquals( 'Existing Classic Menu', $fallback->post_title, ); + $this->assertEquals( 'wp_navigation', $fallback->post_type, ); + $this->assertEquals( 'publish', $fallback->post_status ); + + // Assert that the fallback contains a navigation-link block. + $this->assertStringContainsString( '' ); - $this->assertEquals( $fallback->post_status, 'publish' ); - } public function test_gets_fallback_navigation_with_existing_navigation_menu_if_found() { @@ -90,6 +83,15 @@ public function test_gets_fallback_navigation_with_existing_classic_menu_if_foun wp_delete_nav_menu( $menu_id ); } + public function test_creates_fallback_navigation_with_page_list_by_default() { + $fallback = gutenberg_block_core_navigation_create_fallback(); + + $this->assertEquals( 'wp_navigation', $fallback->post_type ); + $this->assertEquals( 'Navigation', $fallback->post_title, ); + $this->assertEquals( '', $fallback->post_content ); + $this->assertEquals( 'publish', $fallback->post_status ); + } + public function test_should_skip_if_filter_returns_truthy() { add_filter( 'block_core_navigation_skip_fallback', '__return_true' ); From 5e254c134ab74d4e4e7fdc1c0fcfed0a568328c4 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 15:16:50 +0000 Subject: [PATCH 06/37] Cleanup test file --- phpunit/navigation-block-fallback-test.php | 32 ++++------------------ 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index 83294caad36b20..39a46b60fb3ada 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -7,34 +7,14 @@ class Tests_Block_Navigation_Fallbacks extends WP_UnitTestCase { - /** - * @var WP_Post - */ - protected static $navigation_post; - - - - public function set_up() { parent::set_up(); switch_theme( 'emptytheme' ); } - public static function wpSetUpBeforeClass() { - - } - - public static function wpTearDownAfterClass() { - if ( ! empty( self::$post_with_comments_disabled->ID ) ) { - wp_delete_post( self::$navigation_post->ID, true ); - } - } - - - public function test_gets_fallback_navigation_with_existing_navigation_menu_if_found() { - self::$navigation_post = self::factory()->post->create_and_get( + $navigation_post = self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', 'post_title' => 'Existing Navigation Menu', @@ -44,10 +24,10 @@ public function test_gets_fallback_navigation_with_existing_navigation_menu_if_f $fallback = gutenberg_block_core_navigation_create_fallback(); - $this->assertEquals( $fallback->post_title, self::$navigation_post->post_title ); - $this->assertEquals( $fallback->post_type, self::$navigation_post->post_type ); - $this->assertEquals( $fallback->post_content, self::$navigation_post->post_content ); - $this->assertEquals( $fallback->post_status, self::$navigation_post->post_status ); + $this->assertEquals( $fallback->post_title, $navigation_post->post_title ); + $this->assertEquals( $fallback->post_type, $navigation_post->post_type ); + $this->assertEquals( $fallback->post_content, $navigation_post->post_content ); + $this->assertEquals( $fallback->post_status, $navigation_post->post_status ); } public function test_gets_fallback_navigation_with_existing_classic_menu_if_found() { @@ -97,7 +77,7 @@ public function test_should_skip_if_filter_returns_truthy() { $actual = gutenberg_block_core_navigation_create_fallback(); - // Asserr no fallback is created. + // Assert no fallback is created. $this->assertEquals( $actual, null ); remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); From 238d956026ac053957d93ef43923b1536841c4f0 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 15:22:45 +0000 Subject: [PATCH 07/37] Add checks for total Navigation Posts created --- phpunit/navigation-block-fallback-test.php | 48 +++++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index 39a46b60fb3ada..729a8162c8b479 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -12,22 +12,49 @@ public function set_up() { switch_theme( 'emptytheme' ); } + public function get_navigations_in_database() { + $navs_in_db = new WP_Query( + array( + 'post_type' => 'wp_navigation', + 'post_status' => 'publish', + 'posts_per_page' => -1, + 'orderby' => 'date', + 'order' => 'DESC', + ) + ); + + return $navs_in_db->posts ?? array(); + } + public function test_gets_fallback_navigation_with_existing_navigation_menu_if_found() { - $navigation_post = self::factory()->post->create_and_get( + $navigation_post_1 = self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', - 'post_title' => 'Existing Navigation Menu', + 'post_title' => 'Existing Navigation Menu 1', 'post_content' => '', ) ); + $navigation_post_2 = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_navigation', + 'post_title' => 'Existing Navigation Menu 2', + 'post_content' => '', + ) + ); + + $most_recently_published_nav = $navigation_post_2; + $fallback = gutenberg_block_core_navigation_create_fallback(); - $this->assertEquals( $fallback->post_title, $navigation_post->post_title ); - $this->assertEquals( $fallback->post_type, $navigation_post->post_type ); - $this->assertEquals( $fallback->post_content, $navigation_post->post_content ); - $this->assertEquals( $fallback->post_status, $navigation_post->post_status ); + $this->assertEquals( $fallback->post_title, $most_recently_published_nav->post_title ); + $this->assertEquals( $fallback->post_type, $most_recently_published_nav->post_type ); + $this->assertEquals( $fallback->post_content, $most_recently_published_nav->post_content ); + $this->assertEquals( $fallback->post_status, $most_recently_published_nav->post_status ); + + $navs_in_db = $this->get_navigations_in_database(); + $this->assertCount( 2, $navs_in_db ); } public function test_gets_fallback_navigation_with_existing_classic_menu_if_found() { @@ -59,6 +86,9 @@ public function test_gets_fallback_navigation_with_existing_classic_menu_if_foun // Assert that fallback post_content contains the expected menu item url. $this->assertStringContainsString( '"url":"/classic-menu-item-1"', $fallback->post_content ); + $navs_in_db = $this->get_navigations_in_database(); + $this->assertCount( 1, $navs_in_db ); + // Cleanup. wp_delete_nav_menu( $menu_id ); } @@ -70,6 +100,9 @@ public function test_creates_fallback_navigation_with_page_list_by_default() { $this->assertEquals( 'Navigation', $fallback->post_title, ); $this->assertEquals( '', $fallback->post_content ); $this->assertEquals( 'publish', $fallback->post_status ); + + $navs_in_db = $this->get_navigations_in_database(); + $this->assertCount( 1, $navs_in_db ); } public function test_should_skip_if_filter_returns_truthy() { @@ -80,6 +113,9 @@ public function test_should_skip_if_filter_returns_truthy() { // Assert no fallback is created. $this->assertEquals( $actual, null ); + $navs_in_db = $this->get_navigations_in_database(); + $this->assertCount( 0, $navs_in_db ); + remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); } } From ef16a4ce7e245927b500b2502daabeb85cda4bcb Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 15:26:48 +0000 Subject: [PATCH 08/37] Add test to cover prefering existing Nav Menu over Classic Menu --- phpunit/navigation-block-fallback-test.php | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index 729a8162c8b479..fbbabb372c35d7 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -105,6 +105,34 @@ public function test_creates_fallback_navigation_with_page_list_by_default() { $this->assertCount( 1, $navs_in_db ); } + public function test_creates_fallback_from_existing_navigation_menu_even_if_classic_menu_exists() { + + // Create a Navigation Post + $navigation_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_navigation', + 'post_title' => 'Existing Navigation Menu', + 'post_content' => '', + ) + ); + + // Also create a Classic Menu - this should be ignored. + $menu_id = wp_create_nav_menu( 'Existing Classic Menu' ); + + $fallback = gutenberg_block_core_navigation_create_fallback(); + + $this->assertEquals( $fallback->post_title, $navigation_post->post_title ); + $this->assertEquals( $fallback->post_type, $navigation_post->post_type ); + $this->assertEquals( $fallback->post_content, $navigation_post->post_content ); + $this->assertEquals( $fallback->post_status, $navigation_post->post_status ); + + $navs_in_db = $this->get_navigations_in_database(); + $this->assertCount( 1, $navs_in_db ); + + // Cleanup. + wp_delete_nav_menu( $menu_id ); + } + public function test_should_skip_if_filter_returns_truthy() { add_filter( 'block_core_navigation_skip_fallback', '__return_true' ); From 4c8f78d5a7c716283cdbc751109eef343afcfd5e Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 15:31:38 +0000 Subject: [PATCH 09/37] Rename and improve function comment --- packages/block-library/src/navigation/index.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 82fc92d1758e48..a453fc9f9668bb 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -436,11 +436,12 @@ function block_core_navigation_block_contains_core_navigation( $inner_blocks ) { } /** - * Create and returns a navigation menu containing a page-list as a fallback. + * Create and returns a navigation menu containing default fallback content. + * (ideally a page-list). * * @return array the newly created navigation menu. */ -function block_core_navigation_get_default_pages_fallback() { +function block_core_navigation_get_default_fallback() { $registry = WP_Block_Type_Registry::get_instance(); // If `core/page-list` is not registered then use empty blocks. @@ -486,7 +487,7 @@ function block_core_navigation_create_fallback() { // If there are no navigation posts then default to a list of Pages. if ( ! $navigation_post ) { - $navigation_post = block_core_navigation_get_default_pages_fallback(); + $navigation_post = block_core_navigation_get_default_fallback(); } return $navigation_post; From fc8991d90182d9fce730a28d3d2ed6e6222f7d73 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 15:57:19 +0000 Subject: [PATCH 10/37] Add tests for retrieving fallback blocks --- phpunit/navigation-block-fallback-test.php | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index fbbabb372c35d7..a7bc0ecd0967d6 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -146,6 +146,86 @@ public function test_should_skip_if_filter_returns_truthy() { remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); } + + public function test_should_return_empty_fallback_blocks_when_no_navigations_exist() { + $fallback_blocks = gutenberg_block_core_navigation_get_fallback_blocks(); + + $this->assertIsArray( $fallback_blocks ); + $this->assertEmpty( $fallback_blocks ); + } + + public function test_should_return_blocks_from_most_recently_created_navigation() { + + $navigation_post_1 = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_navigation', + 'post_title' => 'Existing Navigation Menu 1', + 'post_content' => '', + ) + ); + + $navigation_post_2 = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_navigation', + 'post_title' => 'Existing Navigation Menu 2', + 'post_content' => '', + ) + ); + + $most_recently_published_nav = $navigation_post_2; + + $fallback_blocks = gutenberg_block_core_navigation_get_fallback_blocks(); + + $block = $fallback_blocks[0]; + + // Check blocks match most recently Navigation Post data. + $this->assertEquals( $block['blockName'], 'core/navigation-link' ); + $this->assertEquals( $block['attrs']['label'], 'Hello world' ); + $this->assertEquals( $block['attrs']['url'], '/hello-world' ); + + } + + public function test_should_return_empty_array_if_most_recently_created_navigation_is_empty() { + + $navigation_post_2 = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_navigation', + 'post_title' => 'Existing Navigation Menu', + 'post_content' => '', // empty. + ) + ); + + $fallback_blocks = gutenberg_block_core_navigation_get_fallback_blocks(); + + $this->assertIsArray( $fallback_blocks ); + $this->assertEmpty( $fallback_blocks ); + } + + public function test_should_return_filtered_blocks_fallback_is_filtered() { + + function use_site_logo() { + return parse_blocks( '' ); + } + + add_filter( 'block_core_navigation_render_fallback', 'use_site_logo' ); + + $navigation_post_2 = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_navigation', + 'post_title' => 'Existing Navigation Menu', + 'post_content' => '', + ) + ); + + $fallback_blocks = gutenberg_block_core_navigation_get_fallback_blocks(); + + $block = $fallback_blocks[0]; + + // Check blocks match most recently Navigation Post data. + $this->assertEquals( $block['blockName'], 'core/site-logo' ); + + remove_filter( 'block_core_navigation_render_fallback', 'use_site_logo' ); + } } From ff1258223644a5f7b623f4262ea12f5716ac3564 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 15:59:28 +0000 Subject: [PATCH 11/37] Ensure different post_content for different menus --- phpunit/navigation-block-fallback-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index a7bc0ecd0967d6..e3ab3538cc5db3 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -40,7 +40,7 @@ public function test_gets_fallback_navigation_with_existing_navigation_menu_if_f array( 'post_type' => 'wp_navigation', 'post_title' => 'Existing Navigation Menu 2', - 'post_content' => '', + 'post_content' => '', ) ); From c20ec7bba446a22de4631701c5dbcd3fc000c8e1 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 16:14:26 +0000 Subject: [PATCH 12/37] Test filtering empty blocks --- phpunit/navigation-block-fallback-test.php | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index e3ab3538cc5db3..b5cbff0cea9d16 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -201,6 +201,36 @@ public function test_should_return_empty_array_if_most_recently_created_navigati $this->assertEmpty( $fallback_blocks ); } + public function test_should_filter_out_empty_blocks_from_fallbacks() { + + $navigation_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_navigation', + 'post_title' => 'Existing Navigation Menu', + 'post_content' => ' ', + ) + ); + + $fallback_blocks = gutenberg_block_core_navigation_get_fallback_blocks(); + + $first_block = $fallback_blocks[0]; + + // There should only be a single page list block and no "null" blocks. + $this->assertCount( 1, $fallback_blocks ); + $this->assertEquals( $first_block['blockName'], 'core/page-list' ); + + // Check that no "empty" blocks exist with a blockName of 'null'. + // If the block parser encounters whitespace it will return a block with a blockName of null. + // This is an intentional feature, but is undesirable for our fallbacks. + $null_blocks = array_filter( + $fallback_blocks, + function( $block ) { + return $block['blockName'] === null; + } + ); + $this->assertEmpty( $null_blocks ); + } + public function test_should_return_filtered_blocks_fallback_is_filtered() { function use_site_logo() { From 1138e30aebff79d0b7a64f9542258d98a6009757 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 16:44:49 +0000 Subject: [PATCH 13/37] Fix parse error in unit tests --- phpunit/navigation-block-fallback-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index b5cbff0cea9d16..58c7cc35bef14c 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -74,7 +74,7 @@ public function test_gets_fallback_navigation_with_existing_classic_menu_if_foun $fallback = gutenberg_block_core_navigation_create_fallback(); $this->assertEquals( 'Existing Classic Menu', $fallback->post_title, ); - $this->assertEquals( 'wp_navigation', $fallback->post_type, ); + $this->assertEquals( 'wp_navigation', $fallback->post_type ); $this->assertEquals( 'publish', $fallback->post_status ); // Assert that the fallback contains a navigation-link block. From 1a82aafd41a2fdac034bb4aaa6cf61da754cd537 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 16:47:00 +0000 Subject: [PATCH 14/37] Fix PHP CS warnings --- phpunit/navigation-block-fallback-test.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index 58c7cc35bef14c..7ca8ab69d40c04 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -28,7 +28,7 @@ public function get_navigations_in_database() { public function test_gets_fallback_navigation_with_existing_navigation_menu_if_found() { - $navigation_post_1 = self::factory()->post->create_and_get( + self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', 'post_title' => 'Existing Navigation Menu 1', @@ -36,7 +36,7 @@ public function test_gets_fallback_navigation_with_existing_navigation_menu_if_f ) ); - $navigation_post_2 = self::factory()->post->create_and_get( + $most_recently_published_nav = self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', 'post_title' => 'Existing Navigation Menu 2', @@ -44,8 +44,6 @@ public function test_gets_fallback_navigation_with_existing_navigation_menu_if_f ) ); - $most_recently_published_nav = $navigation_post_2; - $fallback = gutenberg_block_core_navigation_create_fallback(); $this->assertEquals( $fallback->post_title, $most_recently_published_nav->post_title ); @@ -107,7 +105,7 @@ public function test_creates_fallback_navigation_with_page_list_by_default() { public function test_creates_fallback_from_existing_navigation_menu_even_if_classic_menu_exists() { - // Create a Navigation Post + // Create a Navigation Post. $navigation_post = self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', @@ -156,7 +154,7 @@ public function test_should_return_empty_fallback_blocks_when_no_navigations_exi public function test_should_return_blocks_from_most_recently_created_navigation() { - $navigation_post_1 = self::factory()->post->create_and_get( + self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', 'post_title' => 'Existing Navigation Menu 1', @@ -164,7 +162,7 @@ public function test_should_return_blocks_from_most_recently_created_navigation( ) ); - $navigation_post_2 = self::factory()->post->create_and_get( + $most_recently_published_nav = self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', 'post_title' => 'Existing Navigation Menu 2', @@ -172,8 +170,6 @@ public function test_should_return_blocks_from_most_recently_created_navigation( ) ); - $most_recently_published_nav = $navigation_post_2; - $fallback_blocks = gutenberg_block_core_navigation_get_fallback_blocks(); $block = $fallback_blocks[0]; @@ -187,7 +183,7 @@ public function test_should_return_blocks_from_most_recently_created_navigation( public function test_should_return_empty_array_if_most_recently_created_navigation_is_empty() { - $navigation_post_2 = self::factory()->post->create_and_get( + self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', 'post_title' => 'Existing Navigation Menu', @@ -239,7 +235,7 @@ function use_site_logo() { add_filter( 'block_core_navigation_render_fallback', 'use_site_logo' ); - $navigation_post_2 = self::factory()->post->create_and_get( + self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', 'post_title' => 'Existing Navigation Menu', From a31eec487e961fcfeda1cee463853c55e796b1c3 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 1 Mar 2023 18:20:32 +0000 Subject: [PATCH 15/37] Fix more PHPCS warnings --- phpunit/navigation-block-fallback-test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index 7ca8ab69d40c04..2a5c47544b92f9 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -23,7 +23,7 @@ public function get_navigations_in_database() { ) ); - return $navs_in_db->posts ?? array(); + return $navs_in_db->posts ? $navs_in_db->posts : array(); } public function test_gets_fallback_navigation_with_existing_navigation_menu_if_found() { @@ -71,7 +71,7 @@ public function test_gets_fallback_navigation_with_existing_classic_menu_if_foun $fallback = gutenberg_block_core_navigation_create_fallback(); - $this->assertEquals( 'Existing Classic Menu', $fallback->post_title, ); + $this->assertEquals( 'Existing Classic Menu', $fallback->post_title ); $this->assertEquals( 'wp_navigation', $fallback->post_type ); $this->assertEquals( 'publish', $fallback->post_status ); From ec6d2ad4d3def62954d9e58b23d437fb19eba609 Mon Sep 17 00:00:00 2001 From: scruffian Date: Thu, 2 Mar 2023 09:24:24 +0000 Subject: [PATCH 16/37] fix linting issues' --- packages/block-library/src/navigation/index.php | 4 ---- phpunit/navigation-block-fallback-test.php | 11 +++++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index a453fc9f9668bb..e59a11b11c8b88 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -344,7 +344,6 @@ function block_core_navigation_maybe_use_classic_menu_fallback() { return; } - // Create a new navigation menu from the classic menu. $wp_insert_post_result = wp_insert_post( array( @@ -357,9 +356,6 @@ function block_core_navigation_maybe_use_classic_menu_fallback() { true // So that we can check whether the result is an error. ); - - - if ( is_wp_error( $wp_insert_post_result ) ) { return; } diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index 2a5c47544b92f9..d845cede188a78 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -95,7 +95,7 @@ public function test_creates_fallback_navigation_with_page_list_by_default() { $fallback = gutenberg_block_core_navigation_create_fallback(); $this->assertEquals( 'wp_navigation', $fallback->post_type ); - $this->assertEquals( 'Navigation', $fallback->post_title, ); + $this->assertEquals( 'Navigation', $fallback->post_title ); $this->assertEquals( '', $fallback->post_content ); $this->assertEquals( 'publish', $fallback->post_status ); @@ -154,6 +154,7 @@ public function test_should_return_empty_fallback_blocks_when_no_navigations_exi public function test_should_return_blocks_from_most_recently_created_navigation() { + // Create a fallback navigation. self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', @@ -162,7 +163,8 @@ public function test_should_return_blocks_from_most_recently_created_navigation( ) ); - $most_recently_published_nav = self::factory()->post->create_and_get( + // Create another fallback navigation. + self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', 'post_title' => 'Existing Navigation Menu 2', @@ -199,7 +201,8 @@ public function test_should_return_empty_array_if_most_recently_created_navigati public function test_should_filter_out_empty_blocks_from_fallbacks() { - $navigation_post = self::factory()->post->create_and_get( + // Create a fallback navigation. + self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', 'post_title' => 'Existing Navigation Menu', @@ -221,7 +224,7 @@ public function test_should_filter_out_empty_blocks_from_fallbacks() { $null_blocks = array_filter( $fallback_blocks, function( $block ) { - return $block['blockName'] === null; + return null === $block['blockName']; } ); $this->assertEmpty( $null_blocks ); From 5665b743c671b2a97b5df383d0c2d4858098842b Mon Sep 17 00:00:00 2001 From: scruffian Date: Thu, 2 Mar 2023 16:53:55 +0000 Subject: [PATCH 17/37] change the hooks we create the fallback on --- packages/block-library/src/navigation/index.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index e59a11b11c8b88..e21aba816b36bb 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -572,6 +572,9 @@ function block_core_navigation_from_block_get_post_ids( $block ) { * @return string Returns the post content with the legacy widget added. */ function render_block_core_navigation( $attributes, $content, $block ) { + // First things first, let's create the fallback. + block_core_navigation_create_fallback(); + static $seen_menu_names = array(); // Flag used to indicate whether the rendered output is considered to be @@ -917,6 +920,5 @@ function block_core_navigation_typographic_presets_backcompatibility( $parsed_bl add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' ); -// We have to aply this action on both init and admin init, as the code loads after init when in wp-admin. -add_action( 'init', 'block_core_navigation_create_fallback' ); -add_action( 'admin_init', 'block_core_navigation_create_fallback' ); +// We apply this action on render_block_data, so that we generate a navigation in the dashboard. +add_action( 'render_block_data', 'block_core_navigation_create_fallback' ); From e13b4f379f8627362795eda83f4b244bac6d6306 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 3 Mar 2023 10:25:07 +0000 Subject: [PATCH 18/37] Move hooks to run on one time operations and ensure only run for block themes --- packages/block-library/src/navigation/index.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index e21aba816b36bb..ed601b84f6da8f 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -472,6 +472,11 @@ function block_core_navigation_create_fallback() { return; } + // Don't create a fallback if the theme is not a block theme. + if ( ! wp_is_block_theme() ) { + return; + } + // Get the most recently published Navigation post. $navigation_post = block_core_navigation_get_most_recently_published_navigation(); @@ -488,6 +493,9 @@ function block_core_navigation_create_fallback() { return $navigation_post; } +// Run on switching Theme and when installing WP for the first time. +add_action( 'switch_theme', 'block_core_navigation_create_fallback' ); +add_action( 'wp_install', 'block_core_navigation_create_fallback' ); /** @@ -572,8 +580,6 @@ function block_core_navigation_from_block_get_post_ids( $block ) { * @return string Returns the post content with the legacy widget added. */ function render_block_core_navigation( $attributes, $content, $block ) { - // First things first, let's create the fallback. - block_core_navigation_create_fallback(); static $seen_menu_names = array(); @@ -920,5 +926,5 @@ function block_core_navigation_typographic_presets_backcompatibility( $parsed_bl add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' ); -// We apply this action on render_block_data, so that we generate a navigation in the dashboard. -add_action( 'render_block_data', 'block_core_navigation_create_fallback' ); + + From da7f46d2f28441543881c52537ce6373773f239b Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 3 Mar 2023 10:36:06 +0000 Subject: [PATCH 19/37] Fix tests --- phpunit/navigation-block-fallback-test.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index d845cede188a78..47935c086bf7a5 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -9,7 +9,13 @@ class Tests_Block_Navigation_Fallbacks extends WP_UnitTestCase { public function set_up() { parent::set_up(); + // Navigation menu will be created on Theme switch. Therefore in order to test + // the behaviour of `gutenberg_block_core_navigation_create_fallback` + // the auto-creation of a fallback must be disabled for this initial + // theme switch. + add_filter( 'block_core_navigation_skip_fallback', '__return_true' ); switch_theme( 'emptytheme' ); + remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); } public function get_navigations_in_database() { From a5a36514b8406254e3b38bffb19e64e6db87221e Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 3 Mar 2023 10:40:44 +0000 Subject: [PATCH 20/37] Add tests for theme switch hook auto-creation --- phpunit/navigation-block-fallback-test.php | 40 +++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index 47935c086bf7a5..29c23e08019865 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -18,7 +18,7 @@ public function set_up() { remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); } - public function get_navigations_in_database() { + private function get_navigations_in_database() { $navs_in_db = new WP_Query( array( 'post_type' => 'wp_navigation', @@ -32,6 +32,42 @@ public function get_navigations_in_database() { return $navs_in_db->posts ? $navs_in_db->posts : array(); } + public function test_should_auto_create_navigation_menu_on_theme_switch() { + // Remove any existing disabling filters. + remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); + + // Should trigger creation of Navigation Menu if one does not already exist. + switch_theme( 'emptytheme' ); + + $navs_in_db = $this->get_navigations_in_database(); + $this->assertCount( 1, $navs_in_db ); + } + + public function test_should_not_auto_create_navigation_menu_on_theme_switch_if_one_already_exists() { + + self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_navigation', + 'post_title' => 'Existing Navigation Menu', + 'post_content' => '', + ) + ); + + // Remove any existing disabling filters. + remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); + + // Should trigger creation of Navigation Menu if one does not already exist. + switch_theme( 'emptytheme' ); + + $navs_in_db = $this->get_navigations_in_database(); + + // There should still only be one Navigation Menu. + $this->assertCount( 1, $navs_in_db ); + + // The existing Navigation Menu should be unchanged. + $this->assertEquals( 'Existing Navigation Menu', $navs_in_db[0]->post_title ); + } + public function test_gets_fallback_navigation_with_existing_navigation_menu_if_found() { self::factory()->post->create_and_get( @@ -261,6 +297,8 @@ function use_site_logo() { remove_filter( 'block_core_navigation_render_fallback', 'use_site_logo' ); } + + } From 84bd74628a3ff0caea1056eb26ff744a0c8ffe91 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 3 Mar 2023 10:56:26 +0000 Subject: [PATCH 21/37] Check that there are no menus before Theme switch test --- phpunit/navigation-block-fallback-test.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index 29c23e08019865..49e0ac74f2e680 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -36,6 +36,9 @@ public function test_should_auto_create_navigation_menu_on_theme_switch() { // Remove any existing disabling filters. remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); + $navs_in_db = $this->get_navigations_in_database(); + $this->assertCount( 0, $navs_in_db ); + // Should trigger creation of Navigation Menu if one does not already exist. switch_theme( 'emptytheme' ); @@ -45,6 +48,7 @@ public function test_should_auto_create_navigation_menu_on_theme_switch() { public function test_should_not_auto_create_navigation_menu_on_theme_switch_if_one_already_exists() { + // Pre-add a Navigation Menu to simulate when a user already has a menu. self::factory()->post->create_and_get( array( 'post_type' => 'wp_navigation', From f6dab81c8b7de774ee50d302a23b816d8ad475f8 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 09:48:01 +0000 Subject: [PATCH 22/37] Add test for wp_install --- phpunit/navigation-block-fallback-test.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index 49e0ac74f2e680..d58a736a577d0a 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -32,6 +32,18 @@ private function get_navigations_in_database() { return $navs_in_db->posts ? $navs_in_db->posts : array(); } + private function mock_wp_install() { + $user = get_current_user(); + do_action( 'wp_install', $user ); + } + + public function test_should_auto_create_navigation_menu_on_wp_install() { + $this->mock_wp_install(); + + $navs_in_db = $this->get_navigations_in_database(); + $this->assertCount( 1, $navs_in_db ); + } + public function test_should_auto_create_navigation_menu_on_theme_switch() { // Remove any existing disabling filters. remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); From 641a0ed23884a637998ec21fe0075abeab333115 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 09:51:13 +0000 Subject: [PATCH 23/37] Update comment to indicate page list is not an ideal scenario --- packages/block-library/src/navigation/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index ed601b84f6da8f..af1ad0b42848ad 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -433,7 +433,7 @@ function block_core_navigation_block_contains_core_navigation( $inner_blocks ) { /** * Create and returns a navigation menu containing default fallback content. - * (ideally a page-list). + * (a page-list if registered). * * @return array the newly created navigation menu. */ From 02c135399f26d395fe7b1c201e7668e78f81ed7f Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 11:50:18 +0000 Subject: [PATCH 24/37] Add test to validate menus not created when switching to a block Theme --- phpunit/navigation-block-fallback-test.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index d58a736a577d0a..377247ff8777a4 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -58,6 +58,18 @@ public function test_should_auto_create_navigation_menu_on_theme_switch() { $this->assertCount( 1, $navs_in_db ); } + public function test_should_not_auto_create_navigation_menu_on_theme_switch_to_classic_theme() { + + $navs_in_db = $this->get_navigations_in_database(); + $this->assertCount( 0, $navs_in_db ); + + // Should trigger creation of Navigation Menu if one does not already exist. + switch_theme( 'twentytwenty' ); + + $navs_in_db = $this->get_navigations_in_database(); + $this->assertCount( 0, $navs_in_db ); + } + public function test_should_not_auto_create_navigation_menu_on_theme_switch_if_one_already_exists() { // Pre-add a Navigation Menu to simulate when a user already has a menu. From 11ab9db0c23c2922323ea948fc9caf13508fb7ee Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 11:59:12 +0000 Subject: [PATCH 25/37] Revert whitespace changes Co-authored-by: Dave Smith <444434+getdave@users.noreply.github.com> --- packages/block-library/src/navigation/index.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index af1ad0b42848ad..050d0735743e6d 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -580,7 +580,6 @@ function block_core_navigation_from_block_get_post_ids( $block ) { * @return string Returns the post content with the legacy widget added. */ function render_block_core_navigation( $attributes, $content, $block ) { - static $seen_menu_names = array(); // Flag used to indicate whether the rendered output is considered to be @@ -925,6 +924,3 @@ function block_core_navigation_typographic_presets_backcompatibility( $parsed_bl } add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' ); - - - From 3a64b4d5256a346a4a5ea34141ca53e24cba11b5 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 14:34:18 +0000 Subject: [PATCH 26/37] Apply PHPUnit annotations from review Co-authored-by: Anton Vlasenko <43744263+anton-vlasenko@users.noreply.github.com> --- phpunit/navigation-block-fallback-test.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index 377247ff8777a4..cb943e9dcc54a5 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -37,6 +37,9 @@ private function mock_wp_install() { do_action( 'wp_install', $user ); } + /** + * @covers ::block_core_navigation_get_default_fallback + */ public function test_should_auto_create_navigation_menu_on_wp_install() { $this->mock_wp_install(); @@ -49,13 +52,13 @@ public function test_should_auto_create_navigation_menu_on_theme_switch() { remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); $navs_in_db = $this->get_navigations_in_database(); - $this->assertCount( 0, $navs_in_db ); + $this->assertCount( 0, $navs_in_db, 'Navigation Menu should not exist before running the tests.' ); // Should trigger creation of Navigation Menu if one does not already exist. switch_theme( 'emptytheme' ); $navs_in_db = $this->get_navigations_in_database(); - $this->assertCount( 1, $navs_in_db ); + $this->assertCount( 1, $navs_in_db, 'No Navigation Menu was found.' ); } public function test_should_not_auto_create_navigation_menu_on_theme_switch_to_classic_theme() { From d59cde34936547d2c4e9738234c8a3ec09feeea6 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 14:40:00 +0000 Subject: [PATCH 27/37] Remove unnecessary return and annotation function --- packages/block-library/src/navigation/index.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 050d0735743e6d..a1567bcb6818fb 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -464,7 +464,17 @@ function block_core_navigation_get_default_fallback() { } /** - * Creates the appropriate fallback to be used on the front of the site. + * Creates an appropriate fallback Navigation Menu (`wp_navigation` Post) + * to be used by the Navigation block. + * + * Behaviour: + * 1. If there is a Navigation menu already then use it. + * 2. If there is a Classic menu then convert it to a Navigation menu. + * 3. If there is no Navigation menu and no Classic menu then create a default menu. + * + * The fallback menu is only created if the theme is a block theme. + * + * @return void */ function block_core_navigation_create_fallback() { $should_skip = apply_filters( 'block_core_navigation_skip_fallback', false ); @@ -490,8 +500,6 @@ function block_core_navigation_create_fallback() { if ( ! $navigation_post ) { $navigation_post = block_core_navigation_get_default_fallback(); } - - return $navigation_post; } // Run on switching Theme and when installing WP for the first time. add_action( 'switch_theme', 'block_core_navigation_create_fallback' ); From a9c724869f790cb0f9e4ff8f382bdf868e765eb5 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 14:58:13 +0000 Subject: [PATCH 28/37] Try adding messages to all assertions --- phpunit/navigation-block-fallback-test.php | 81 ++++++++++------------ 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index cb943e9dcc54a5..10c9e424883ff3 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -44,7 +44,7 @@ public function test_should_auto_create_navigation_menu_on_wp_install() { $this->mock_wp_install(); $navs_in_db = $this->get_navigations_in_database(); - $this->assertCount( 1, $navs_in_db ); + $this->assertCount( 1, $navs_in_db, 'No Navigation Menu was found.' ); } public function test_should_auto_create_navigation_menu_on_theme_switch() { @@ -52,7 +52,7 @@ public function test_should_auto_create_navigation_menu_on_theme_switch() { remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); $navs_in_db = $this->get_navigations_in_database(); - $this->assertCount( 0, $navs_in_db, 'Navigation Menu should not exist before running the tests.' ); + $this->assertCount( 0, $navs_in_db, 'Navigation Menu should not exist before switching Theme.' ); // Should trigger creation of Navigation Menu if one does not already exist. switch_theme( 'emptytheme' ); @@ -64,13 +64,13 @@ public function test_should_auto_create_navigation_menu_on_theme_switch() { public function test_should_not_auto_create_navigation_menu_on_theme_switch_to_classic_theme() { $navs_in_db = $this->get_navigations_in_database(); - $this->assertCount( 0, $navs_in_db ); + $this->assertCount( 0, $navs_in_db, 'Navigation Menu should not exist before switching Theme.' ); // Should trigger creation of Navigation Menu if one does not already exist. switch_theme( 'twentytwenty' ); $navs_in_db = $this->get_navigations_in_database(); - $this->assertCount( 0, $navs_in_db ); + $this->assertCount( 0, $navs_in_db, 'A Navigation Menu should not exist.' ); } public function test_should_not_auto_create_navigation_menu_on_theme_switch_if_one_already_exists() { @@ -93,10 +93,10 @@ public function test_should_not_auto_create_navigation_menu_on_theme_switch_if_o $navs_in_db = $this->get_navigations_in_database(); // There should still only be one Navigation Menu. - $this->assertCount( 1, $navs_in_db ); + $this->assertCount( 1, $navs_in_db, 'A single Navigation Menu should exist.' ); // The existing Navigation Menu should be unchanged. - $this->assertEquals( 'Existing Navigation Menu', $navs_in_db[0]->post_title ); + $this->assertEquals( 'Existing Navigation Menu', $navs_in_db[0]->post_title, 'The title of the Navigation Menu should match the existing Navigation Menu' ); } public function test_gets_fallback_navigation_with_existing_navigation_menu_if_found() { @@ -119,13 +119,13 @@ public function test_gets_fallback_navigation_with_existing_navigation_menu_if_f $fallback = gutenberg_block_core_navigation_create_fallback(); - $this->assertEquals( $fallback->post_title, $most_recently_published_nav->post_title ); - $this->assertEquals( $fallback->post_type, $most_recently_published_nav->post_type ); - $this->assertEquals( $fallback->post_content, $most_recently_published_nav->post_content ); - $this->assertEquals( $fallback->post_status, $most_recently_published_nav->post_status ); + $this->assertEquals( $fallback->post_title, $most_recently_published_nav->post_title, 'The title of the fallback Navigation Menu should match the title of the most recently published Navigation Menu.' ); + $this->assertEquals( $fallback->post_type, $most_recently_published_nav->post_type, 'The post type of the fallback Navigation Menu should match the post type of the most recently published Navigation Menu.' ); + $this->assertEquals( $fallback->post_content, $most_recently_published_nav->post_content, 'The contents of the fallback Navigation Menu should match the contents of the most recently published Navigation Menu.' ); + $this->assertEquals( $fallback->post_status, $most_recently_published_nav->post_status, 'The status of the fallback Navigation Menu should match the status of the most recently published Navigation Menu.' ); $navs_in_db = $this->get_navigations_in_database(); - $this->assertCount( 2, $navs_in_db ); + $this->assertCount( 2, $navs_in_db, '2 Navigation Menus should exist.' ); } public function test_gets_fallback_navigation_with_existing_classic_menu_if_found() { @@ -144,21 +144,19 @@ public function test_gets_fallback_navigation_with_existing_classic_menu_if_foun $fallback = gutenberg_block_core_navigation_create_fallback(); - $this->assertEquals( 'Existing Classic Menu', $fallback->post_title ); - $this->assertEquals( 'wp_navigation', $fallback->post_type ); - $this->assertEquals( 'publish', $fallback->post_status ); + $this->assertEquals( 'Existing Classic Menu', $fallback->post_title, 'The title of the fallback Navigation Menu should match the name of the Classic menu.' ); // Assert that the fallback contains a navigation-link block. - $this->assertStringContainsString( '', $fallback->post_content ); - $this->assertEquals( 'publish', $fallback->post_status ); + $this->assertEquals( 'wp_navigation', $fallback->post_type, 'The fallback Navigation Menu should be of the expected Post type.' ); + $this->assertEquals( 'Navigation', $fallback->post_title, 'The fallback Navigation Menu should be have the expected title.' ); + $this->assertEquals( '', $fallback->post_content, 'The fallback Navigation Menu should contain a Page List block.' ); + $this->assertEquals( 'publish', $fallback->post_status, 'The fallback Navigation Menu should be published.' ); $navs_in_db = $this->get_navigations_in_database(); - $this->assertCount( 1, $navs_in_db ); + $this->assertCount( 1, $navs_in_db, 'A single Navigation Menu should exist.' ); } public function test_creates_fallback_from_existing_navigation_menu_even_if_classic_menu_exists() { @@ -192,13 +190,13 @@ public function test_creates_fallback_from_existing_navigation_menu_even_if_clas $fallback = gutenberg_block_core_navigation_create_fallback(); - $this->assertEquals( $fallback->post_title, $navigation_post->post_title ); - $this->assertEquals( $fallback->post_type, $navigation_post->post_type ); - $this->assertEquals( $fallback->post_content, $navigation_post->post_content ); - $this->assertEquals( $fallback->post_status, $navigation_post->post_status ); + $this->assertEquals( $fallback->post_title, $navigation_post->post_title, 'The title of the fallback Navigation Menu should match that of the existing Navigation Menu.' ); + $this->assertEquals( $fallback->post_type, $navigation_post->post_type, 'The post type of the fallback Navigation Menu should match that of the existing Navigation Menu.' ); + $this->assertEquals( $fallback->post_content, $navigation_post->post_content, 'The contents of the fallback Navigation Menu should match that of the existing Navigation Menu.' ); + $this->assertEquals( $fallback->post_status, $navigation_post->post_status, 'The status of the fallback Navigation Menu should match that of the existing Navigation Menu.' ); $navs_in_db = $this->get_navigations_in_database(); - $this->assertCount( 1, $navs_in_db ); + $this->assertCount( 1, $navs_in_db, 'A single Navigation Menu should exist.' ); // Cleanup. wp_delete_nav_menu( $menu_id ); @@ -209,11 +207,8 @@ public function test_should_skip_if_filter_returns_truthy() { $actual = gutenberg_block_core_navigation_create_fallback(); - // Assert no fallback is created. - $this->assertEquals( $actual, null ); - $navs_in_db = $this->get_navigations_in_database(); - $this->assertCount( 0, $navs_in_db ); + $this->assertCount( 0, $navs_in_db, 'No Navigation Menus should have been created.' ); remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); } @@ -221,8 +216,8 @@ public function test_should_skip_if_filter_returns_truthy() { public function test_should_return_empty_fallback_blocks_when_no_navigations_exist() { $fallback_blocks = gutenberg_block_core_navigation_get_fallback_blocks(); - $this->assertIsArray( $fallback_blocks ); - $this->assertEmpty( $fallback_blocks ); + $this->assertIsArray( $fallback_blocks, 'Fallback blocks should be an array.' ); + $this->assertEmpty( $fallback_blocks, 'Fallback blocks should be empty.' ); } public function test_should_return_blocks_from_most_recently_created_navigation() { @@ -250,9 +245,9 @@ public function test_should_return_blocks_from_most_recently_created_navigation( $block = $fallback_blocks[0]; // Check blocks match most recently Navigation Post data. - $this->assertEquals( $block['blockName'], 'core/navigation-link' ); - $this->assertEquals( $block['attrs']['label'], 'Hello world' ); - $this->assertEquals( $block['attrs']['url'], '/hello-world' ); + $this->assertEquals( $block['blockName'], 'core/navigation-link', '1st fallback block should match expected type.' ); + $this->assertEquals( $block['attrs']['label'], 'Hello world', '1st fallback block\'s label should match.' ); + $this->assertEquals( $block['attrs']['url'], '/hello-world', '1st fallback block\'s url should match.' ); } @@ -268,8 +263,8 @@ public function test_should_return_empty_array_if_most_recently_created_navigati $fallback_blocks = gutenberg_block_core_navigation_get_fallback_blocks(); - $this->assertIsArray( $fallback_blocks ); - $this->assertEmpty( $fallback_blocks ); + $this->assertIsArray( $fallback_blocks, 'Fallback blocks should be an array.' ); + $this->assertEmpty( $fallback_blocks, 'Fallback blocks should be empty.' ); } public function test_should_filter_out_empty_blocks_from_fallbacks() { @@ -288,8 +283,8 @@ public function test_should_filter_out_empty_blocks_from_fallbacks() { $first_block = $fallback_blocks[0]; // There should only be a single page list block and no "null" blocks. - $this->assertCount( 1, $fallback_blocks ); - $this->assertEquals( $first_block['blockName'], 'core/page-list' ); + $this->assertCount( 1, $fallback_blocks, 'Fallback blocks should contain a single block.' ); + $this->assertEquals( $first_block['blockName'], 'core/page-list', 'Fallback block should be a page list.' ); // Check that no "empty" blocks exist with a blockName of 'null'. // If the block parser encounters whitespace it will return a block with a blockName of null. @@ -300,7 +295,7 @@ function( $block ) { return null === $block['blockName']; } ); - $this->assertEmpty( $null_blocks ); + $this->assertEmpty( $null_blocks, 'Fallback blocks should not contain any null blocks.' ); } public function test_should_return_filtered_blocks_fallback_is_filtered() { @@ -324,7 +319,7 @@ function use_site_logo() { $block = $fallback_blocks[0]; // Check blocks match most recently Navigation Post data. - $this->assertEquals( $block['blockName'], 'core/site-logo' ); + $this->assertEquals( $block['blockName'], 'core/site-logo', '1st fallback block should match expected type.' ); remove_filter( 'block_core_navigation_render_fallback', 'use_site_logo' ); } From e0d8d7ccf7b35c3590c72e5d2111fa7ec84a6394 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 15:02:49 +0000 Subject: [PATCH 29/37] Add @covers annotation --- phpunit/navigation-block-fallback-test.php | 56 ++++++++++++++++++---- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index 10c9e424883ff3..a3ba654a8af975 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -38,7 +38,7 @@ private function mock_wp_install() { } /** - * @covers ::block_core_navigation_get_default_fallback + * @covers ::block_core_navigation_create_fallback */ public function test_should_auto_create_navigation_menu_on_wp_install() { $this->mock_wp_install(); @@ -47,6 +47,9 @@ public function test_should_auto_create_navigation_menu_on_wp_install() { $this->assertCount( 1, $navs_in_db, 'No Navigation Menu was found.' ); } + /** + * @covers ::block_core_navigation_create_fallback + */ public function test_should_auto_create_navigation_menu_on_theme_switch() { // Remove any existing disabling filters. remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); @@ -61,6 +64,9 @@ public function test_should_auto_create_navigation_menu_on_theme_switch() { $this->assertCount( 1, $navs_in_db, 'No Navigation Menu was found.' ); } + /** + * @covers ::block_core_navigation_create_fallback + */ public function test_should_not_auto_create_navigation_menu_on_theme_switch_to_classic_theme() { $navs_in_db = $this->get_navigations_in_database(); @@ -73,6 +79,9 @@ public function test_should_not_auto_create_navigation_menu_on_theme_switch_to_c $this->assertCount( 0, $navs_in_db, 'A Navigation Menu should not exist.' ); } + /** + * @covers ::block_core_navigation_create_fallback + */ public function test_should_not_auto_create_navigation_menu_on_theme_switch_if_one_already_exists() { // Pre-add a Navigation Menu to simulate when a user already has a menu. @@ -99,7 +108,10 @@ public function test_should_not_auto_create_navigation_menu_on_theme_switch_if_o $this->assertEquals( 'Existing Navigation Menu', $navs_in_db[0]->post_title, 'The title of the Navigation Menu should match the existing Navigation Menu' ); } - public function test_gets_fallback_navigation_with_existing_navigation_menu_if_found() { + /** + * @covers ::block_core_navigation_create_fallback + */ + public function test_creates_fallback_navigation_with_existing_navigation_menu_if_found() { self::factory()->post->create_and_get( array( @@ -128,7 +140,10 @@ public function test_gets_fallback_navigation_with_existing_navigation_menu_if_f $this->assertCount( 2, $navs_in_db, '2 Navigation Menus should exist.' ); } - public function test_gets_fallback_navigation_with_existing_classic_menu_if_found() { + /** + * @covers ::block_core_navigation_create_fallback + */ + public function test_creates_fallback_navigation_with_existing_classic_menu_if_found() { $menu_id = wp_create_nav_menu( 'Existing Classic Menu' ); @@ -162,6 +177,9 @@ public function test_gets_fallback_navigation_with_existing_classic_menu_if_foun wp_delete_nav_menu( $menu_id ); } + /** + * @covers ::block_core_navigation_create_fallback + */ public function test_creates_fallback_navigation_with_page_list_by_default() { $fallback = gutenberg_block_core_navigation_create_fallback(); @@ -174,6 +192,9 @@ public function test_creates_fallback_navigation_with_page_list_by_default() { $this->assertCount( 1, $navs_in_db, 'A single Navigation Menu should exist.' ); } + /** + * @covers ::block_core_navigation_create_fallback + */ public function test_creates_fallback_from_existing_navigation_menu_even_if_classic_menu_exists() { // Create a Navigation Post. @@ -202,6 +223,9 @@ public function test_creates_fallback_from_existing_navigation_menu_even_if_clas wp_delete_nav_menu( $menu_id ); } + /** + * @covers ::block_core_navigation_create_fallback + */ public function test_should_skip_if_filter_returns_truthy() { add_filter( 'block_core_navigation_skip_fallback', '__return_true' ); @@ -213,14 +237,21 @@ public function test_should_skip_if_filter_returns_truthy() { remove_filter( 'block_core_navigation_skip_fallback', '__return_true' ); } - public function test_should_return_empty_fallback_blocks_when_no_navigations_exist() { + + /** + * @covers ::gutenberg_block_core_navigation_get_fallback_blocks + */ + public function test_should_get_fallback_blocks_when_no_navigations_exist() { $fallback_blocks = gutenberg_block_core_navigation_get_fallback_blocks(); $this->assertIsArray( $fallback_blocks, 'Fallback blocks should be an array.' ); $this->assertEmpty( $fallback_blocks, 'Fallback blocks should be empty.' ); } - public function test_should_return_blocks_from_most_recently_created_navigation() { + /** + * @covers ::gutenberg_block_core_navigation_get_fallback_blocks + */ + public function test_should_get_blocks_from_most_recently_created_navigation() { // Create a fallback navigation. self::factory()->post->create_and_get( @@ -251,7 +282,10 @@ public function test_should_return_blocks_from_most_recently_created_navigation( } - public function test_should_return_empty_array_if_most_recently_created_navigation_is_empty() { + /** + * @covers ::gutenberg_block_core_navigation_get_fallback_blocks + */ + public function test_should_get_empty_array_if_most_recently_created_navigation_is_empty() { self::factory()->post->create_and_get( array( @@ -267,6 +301,9 @@ public function test_should_return_empty_array_if_most_recently_created_navigati $this->assertEmpty( $fallback_blocks, 'Fallback blocks should be empty.' ); } + /** + * @covers ::gutenberg_block_core_navigation_get_fallback_blocks + */ public function test_should_filter_out_empty_blocks_from_fallbacks() { // Create a fallback navigation. @@ -298,7 +335,10 @@ function( $block ) { $this->assertEmpty( $null_blocks, 'Fallback blocks should not contain any null blocks.' ); } - public function test_should_return_filtered_blocks_fallback_is_filtered() { + /** + * @covers ::gutenberg_block_core_navigation_get_fallback_blocks + */ + public function test_should_get_filtered_blocks_if_fallback_is_filtered() { function use_site_logo() { return parse_blocks( '' ); @@ -323,8 +363,6 @@ function use_site_logo() { remove_filter( 'block_core_navigation_render_fallback', 'use_site_logo' ); } - - } From dff16827dea0ff6f77e8dd2d3e31dea9229b6ce4 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 15:11:21 +0000 Subject: [PATCH 30/37] Apply correct annotations to test class --- phpunit/navigation-block-fallback-test.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/navigation-block-fallback-test.php index a3ba654a8af975..402e2e48ea5867 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/navigation-block-fallback-test.php @@ -2,7 +2,8 @@ /** * Tests Fallback Behavior for Navigation block * - * @package WordPress + * @package Gutenberg + * @subpackage block-library */ class Tests_Block_Navigation_Fallbacks extends WP_UnitTestCase { From 8604f516ed063ef33c71b182ffe9ab147d0b806b Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 15:14:07 +0000 Subject: [PATCH 31/37] Rename test file and add group annotation --- ...t.php => class-block-library-navigation-fallbacks-test.php} | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) rename phpunit/{navigation-block-fallback-test.php => class-block-library-navigation-fallbacks-test.php} (99%) diff --git a/phpunit/navigation-block-fallback-test.php b/phpunit/class-block-library-navigation-fallbacks-test.php similarity index 99% rename from phpunit/navigation-block-fallback-test.php rename to phpunit/class-block-library-navigation-fallbacks-test.php index 402e2e48ea5867..733c85b49c55d4 100644 --- a/phpunit/navigation-block-fallback-test.php +++ b/phpunit/class-block-library-navigation-fallbacks-test.php @@ -4,9 +4,10 @@ * * @package Gutenberg * @subpackage block-library + * @group Navigation block */ -class Tests_Block_Navigation_Fallbacks extends WP_UnitTestCase { +class Block_Library_Navigation_Fallbacks_Test extends WP_UnitTestCase { public function set_up() { parent::set_up(); From 249fd749bcbe1a855ec5b559f93954d4ddf70df3 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 15:19:21 +0000 Subject: [PATCH 32/37] Fix PHPCS unused variable --- phpunit/class-block-library-navigation-fallbacks-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/class-block-library-navigation-fallbacks-test.php b/phpunit/class-block-library-navigation-fallbacks-test.php index 733c85b49c55d4..6870d7144997a1 100644 --- a/phpunit/class-block-library-navigation-fallbacks-test.php +++ b/phpunit/class-block-library-navigation-fallbacks-test.php @@ -231,7 +231,7 @@ public function test_creates_fallback_from_existing_navigation_menu_even_if_clas public function test_should_skip_if_filter_returns_truthy() { add_filter( 'block_core_navigation_skip_fallback', '__return_true' ); - $actual = gutenberg_block_core_navigation_create_fallback(); + gutenberg_block_core_navigation_create_fallback(); $navs_in_db = $this->get_navigations_in_database(); $this->assertCount( 0, $navs_in_db, 'No Navigation Menus should have been created.' ); From 2b2795e386244a15ad95d927198830a4c66a994e Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 17:32:40 +0000 Subject: [PATCH 33/37] Normalise assertion Co-authored-by: Anton Vlasenko <43744263+anton-vlasenko@users.noreply.github.com> --- phpunit/class-block-library-navigation-fallbacks-test.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/phpunit/class-block-library-navigation-fallbacks-test.php b/phpunit/class-block-library-navigation-fallbacks-test.php index 6870d7144997a1..486ad9acf2eaff 100644 --- a/phpunit/class-block-library-navigation-fallbacks-test.php +++ b/phpunit/class-block-library-navigation-fallbacks-test.php @@ -246,8 +246,7 @@ public function test_should_skip_if_filter_returns_truthy() { public function test_should_get_fallback_blocks_when_no_navigations_exist() { $fallback_blocks = gutenberg_block_core_navigation_get_fallback_blocks(); - $this->assertIsArray( $fallback_blocks, 'Fallback blocks should be an array.' ); - $this->assertEmpty( $fallback_blocks, 'Fallback blocks should be empty.' ); + $this->assertSame( array(), $fallback_blocks, 'Fallback blocks should be an empty array.' ); } /** From 61f9a8e659ef8e143c0bce96d54e208c76611c02 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 20:23:10 +0000 Subject: [PATCH 34/37] Fix tests --- .../block-library/src/navigation/index.php | 31 ++++++++++++------- ...lock-library-navigation-fallbacks-test.php | 12 +++---- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index a1567bcb6818fb..9936de9eaec451 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -476,8 +476,9 @@ function block_core_navigation_get_default_fallback() { * * @return void */ -function block_core_navigation_create_fallback() { +function block_core_navigation_create_and_get_fallback() { $should_skip = apply_filters( 'block_core_navigation_skip_fallback', false ); + if ( $should_skip ) { return; } @@ -487,23 +488,29 @@ function block_core_navigation_create_fallback() { return; } - // Get the most recently published Navigation post. - $navigation_post = block_core_navigation_get_most_recently_published_navigation(); + // Get the most recently published Navigation menu. + $navigation_menu = block_core_navigation_get_most_recently_published_navigation(); - // If there are no navigation posts then try to find a classic menu - // and convert it into a block based navigation menu. - if ( ! $navigation_post ) { - $navigation_post = block_core_navigation_maybe_use_classic_menu_fallback(); + if ( $navigation_menu ) { + return $navigation_menu; } - // If there are no navigation posts then default to a list of Pages. - if ( ! $navigation_post ) { - $navigation_post = block_core_navigation_get_default_fallback(); + // If there are no Navigation menus then try to find a Classic menu + // and convert it into a Navigation menu. + + $navigation_menu = block_core_navigation_maybe_use_classic_menu_fallback(); + + if ( $navigation_menu ) { + return $navigation_menu; } + + // If there are no navigation posts then default to a list of Pages. + return block_core_navigation_get_default_fallback(); + } // Run on switching Theme and when installing WP for the first time. -add_action( 'switch_theme', 'block_core_navigation_create_fallback' ); -add_action( 'wp_install', 'block_core_navigation_create_fallback' ); +add_action( 'switch_theme', 'block_core_navigation_create_and_get_fallback' ); +add_action( 'wp_install', 'block_core_navigation_create_and_get_fallback' ); /** diff --git a/phpunit/class-block-library-navigation-fallbacks-test.php b/phpunit/class-block-library-navigation-fallbacks-test.php index 486ad9acf2eaff..1bebfb6c7702ea 100644 --- a/phpunit/class-block-library-navigation-fallbacks-test.php +++ b/phpunit/class-block-library-navigation-fallbacks-test.php @@ -12,7 +12,7 @@ class Block_Library_Navigation_Fallbacks_Test extends WP_UnitTestCase { public function set_up() { parent::set_up(); // Navigation menu will be created on Theme switch. Therefore in order to test - // the behaviour of `gutenberg_block_core_navigation_create_fallback` + // the behaviour of `gutenberg_block_core_navigation_create_and_get_fallback` // the auto-creation of a fallback must be disabled for this initial // theme switch. add_filter( 'block_core_navigation_skip_fallback', '__return_true' ); @@ -131,7 +131,7 @@ public function test_creates_fallback_navigation_with_existing_navigation_menu_i ) ); - $fallback = gutenberg_block_core_navigation_create_fallback(); + $fallback = gutenberg_block_core_navigation_create_and_get_fallback(); $this->assertEquals( $fallback->post_title, $most_recently_published_nav->post_title, 'The title of the fallback Navigation Menu should match the title of the most recently published Navigation Menu.' ); $this->assertEquals( $fallback->post_type, $most_recently_published_nav->post_type, 'The post type of the fallback Navigation Menu should match the post type of the most recently published Navigation Menu.' ); @@ -159,7 +159,7 @@ public function test_creates_fallback_navigation_with_existing_classic_menu_if_f ) ); - $fallback = gutenberg_block_core_navigation_create_fallback(); + $fallback = gutenberg_block_core_navigation_create_and_get_fallback(); $this->assertEquals( 'Existing Classic Menu', $fallback->post_title, 'The title of the fallback Navigation Menu should match the name of the Classic menu.' ); @@ -183,7 +183,7 @@ public function test_creates_fallback_navigation_with_existing_classic_menu_if_f * @covers ::block_core_navigation_create_fallback */ public function test_creates_fallback_navigation_with_page_list_by_default() { - $fallback = gutenberg_block_core_navigation_create_fallback(); + $fallback = gutenberg_block_core_navigation_create_and_get_fallback(); $this->assertEquals( 'wp_navigation', $fallback->post_type, 'The fallback Navigation Menu should be of the expected Post type.' ); $this->assertEquals( 'Navigation', $fallback->post_title, 'The fallback Navigation Menu should be have the expected title.' ); @@ -211,7 +211,7 @@ public function test_creates_fallback_from_existing_navigation_menu_even_if_clas // Also create a Classic Menu - this should be ignored. $menu_id = wp_create_nav_menu( 'Existing Classic Menu' ); - $fallback = gutenberg_block_core_navigation_create_fallback(); + $fallback = gutenberg_block_core_navigation_create_and_get_fallback(); $this->assertEquals( $fallback->post_title, $navigation_post->post_title, 'The title of the fallback Navigation Menu should match that of the existing Navigation Menu.' ); $this->assertEquals( $fallback->post_type, $navigation_post->post_type, 'The post type of the fallback Navigation Menu should match that of the existing Navigation Menu.' ); @@ -231,7 +231,7 @@ public function test_creates_fallback_from_existing_navigation_menu_even_if_clas public function test_should_skip_if_filter_returns_truthy() { add_filter( 'block_core_navigation_skip_fallback', '__return_true' ); - gutenberg_block_core_navigation_create_fallback(); + gutenberg_block_core_navigation_create_and_get_fallback(); $navs_in_db = $this->get_navigations_in_database(); $this->assertCount( 0, $navs_in_db, 'No Navigation Menus should have been created.' ); From 0c8bbe227a05d47e23ed15a8ecb888715d9e523b Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 20:29:47 +0000 Subject: [PATCH 35/37] Refactor to avoid requiring a return value from the hooked creation function --- .../block-library/src/navigation/index.php | 13 ++++++------ ...lock-library-navigation-fallbacks-test.php | 20 +++++++++++++------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 9936de9eaec451..585477cfb667a0 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -476,7 +476,7 @@ function block_core_navigation_get_default_fallback() { * * @return void */ -function block_core_navigation_create_and_get_fallback() { +function block_core_navigation_create_fallback() { $should_skip = apply_filters( 'block_core_navigation_skip_fallback', false ); if ( $should_skip ) { @@ -492,7 +492,7 @@ function block_core_navigation_create_and_get_fallback() { $navigation_menu = block_core_navigation_get_most_recently_published_navigation(); if ( $navigation_menu ) { - return $navigation_menu; + return; } // If there are no Navigation menus then try to find a Classic menu @@ -501,16 +501,15 @@ function block_core_navigation_create_and_get_fallback() { $navigation_menu = block_core_navigation_maybe_use_classic_menu_fallback(); if ( $navigation_menu ) { - return $navigation_menu; + return; } // If there are no navigation posts then default to a list of Pages. - return block_core_navigation_get_default_fallback(); - + block_core_navigation_get_default_fallback(); } // Run on switching Theme and when installing WP for the first time. -add_action( 'switch_theme', 'block_core_navigation_create_and_get_fallback' ); -add_action( 'wp_install', 'block_core_navigation_create_and_get_fallback' ); +add_action( 'switch_theme', 'block_core_navigation_create_fallback' ); +add_action( 'wp_install', 'block_core_navigation_create_fallback' ); /** diff --git a/phpunit/class-block-library-navigation-fallbacks-test.php b/phpunit/class-block-library-navigation-fallbacks-test.php index 1bebfb6c7702ea..8568ac967daf65 100644 --- a/phpunit/class-block-library-navigation-fallbacks-test.php +++ b/phpunit/class-block-library-navigation-fallbacks-test.php @@ -12,7 +12,7 @@ class Block_Library_Navigation_Fallbacks_Test extends WP_UnitTestCase { public function set_up() { parent::set_up(); // Navigation menu will be created on Theme switch. Therefore in order to test - // the behaviour of `gutenberg_block_core_navigation_create_and_get_fallback` + // the behaviour of `gutenberg_block_core_navigation_create_fallback` // the auto-creation of a fallback must be disabled for this initial // theme switch. add_filter( 'block_core_navigation_skip_fallback', '__return_true' ); @@ -131,7 +131,9 @@ public function test_creates_fallback_navigation_with_existing_navigation_menu_i ) ); - $fallback = gutenberg_block_core_navigation_create_and_get_fallback(); + gutenberg_block_core_navigation_create_fallback(); + + $fallback = $this->get_navigations_in_database()[0]; $this->assertEquals( $fallback->post_title, $most_recently_published_nav->post_title, 'The title of the fallback Navigation Menu should match the title of the most recently published Navigation Menu.' ); $this->assertEquals( $fallback->post_type, $most_recently_published_nav->post_type, 'The post type of the fallback Navigation Menu should match the post type of the most recently published Navigation Menu.' ); @@ -159,7 +161,9 @@ public function test_creates_fallback_navigation_with_existing_classic_menu_if_f ) ); - $fallback = gutenberg_block_core_navigation_create_and_get_fallback(); + gutenberg_block_core_navigation_create_fallback(); + + $fallback = $this->get_navigations_in_database()[0]; $this->assertEquals( 'Existing Classic Menu', $fallback->post_title, 'The title of the fallback Navigation Menu should match the name of the Classic menu.' ); @@ -183,7 +187,9 @@ public function test_creates_fallback_navigation_with_existing_classic_menu_if_f * @covers ::block_core_navigation_create_fallback */ public function test_creates_fallback_navigation_with_page_list_by_default() { - $fallback = gutenberg_block_core_navigation_create_and_get_fallback(); + gutenberg_block_core_navigation_create_fallback(); + + $fallback = $this->get_navigations_in_database()[0]; $this->assertEquals( 'wp_navigation', $fallback->post_type, 'The fallback Navigation Menu should be of the expected Post type.' ); $this->assertEquals( 'Navigation', $fallback->post_title, 'The fallback Navigation Menu should be have the expected title.' ); @@ -211,7 +217,9 @@ public function test_creates_fallback_from_existing_navigation_menu_even_if_clas // Also create a Classic Menu - this should be ignored. $menu_id = wp_create_nav_menu( 'Existing Classic Menu' ); - $fallback = gutenberg_block_core_navigation_create_and_get_fallback(); + gutenberg_block_core_navigation_create_fallback(); + + $fallback = $this->get_navigations_in_database()[0]; $this->assertEquals( $fallback->post_title, $navigation_post->post_title, 'The title of the fallback Navigation Menu should match that of the existing Navigation Menu.' ); $this->assertEquals( $fallback->post_type, $navigation_post->post_type, 'The post type of the fallback Navigation Menu should match that of the existing Navigation Menu.' ); @@ -231,7 +239,7 @@ public function test_creates_fallback_from_existing_navigation_menu_even_if_clas public function test_should_skip_if_filter_returns_truthy() { add_filter( 'block_core_navigation_skip_fallback', '__return_true' ); - gutenberg_block_core_navigation_create_and_get_fallback(); + gutenberg_block_core_navigation_create_fallback(); $navs_in_db = $this->get_navigations_in_database(); $this->assertCount( 0, $navs_in_db, 'No Navigation Menus should have been created.' ); From 3f164c799b7c548239304545c203990db9bdcb90 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 20:44:45 +0000 Subject: [PATCH 36/37] Refactor creation functions to SRP. --- .../block-library/src/navigation/index.php | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 585477cfb667a0..b51a4f279eb93d 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -329,22 +329,26 @@ function block_core_navigation_get_classic_menu_fallback_blocks( $classic_nav_me * * @return array the normalized parsed blocks. */ -function block_core_navigation_maybe_use_classic_menu_fallback() { - // See if we have a classic menu. +function block_core_navigation_create_classic_menu_fallback() { + // See if we have a Classic menu. $classic_nav_menu = block_core_navigation_get_classic_menu_fallback(); + // Exit early if one does not exist. if ( ! $classic_nav_menu ) { - return; + return new WP_Error( 'no_classic_menu', __( 'No classic menu exists to convert to a block.' ) ); } - // If we have a classic menu then convert it to blocks. + // If we have a Classic menu then convert it to blocks. $classic_nav_menu_blocks = block_core_navigation_get_classic_menu_fallback_blocks( $classic_nav_menu ); + // If it's empty then exit early. if ( empty( $classic_nav_menu_blocks ) ) { - return; + return new WP_Error( 'no_classic_menu', __( 'Classic menu is empty or could not be converted to blocks.' ) ); } - // Create a new navigation menu from the classic menu. + $return_errors = true; + + // Create a new Navigation menu from the Classic menu blocks. $wp_insert_post_result = wp_insert_post( array( 'post_content' => $classic_nav_menu_blocks, @@ -353,15 +357,12 @@ function block_core_navigation_maybe_use_classic_menu_fallback() { 'post_status' => 'publish', 'post_type' => 'wp_navigation', ), - true // So that we can check whether the result is an error. + $return_errors // So that we can check whether the result is an error. ); - if ( is_wp_error( $wp_insert_post_result ) ) { - return; - } - // Fetch the most recently published navigation which will be the classic one created above. - return block_core_navigation_get_most_recently_published_navigation(); + return $wp_insert_post_result; + } /** @@ -437,7 +438,7 @@ function block_core_navigation_block_contains_core_navigation( $inner_blocks ) { * * @return array the newly created navigation menu. */ -function block_core_navigation_get_default_fallback() { +function block_core_navigation_create_default_fallback() { $registry = WP_Block_Type_Registry::get_instance(); // If `core/page-list` is not registered then use empty blocks. @@ -455,12 +456,7 @@ function block_core_navigation_get_default_fallback() { true // So that we can check whether the result is an error. ); - if ( is_wp_error( $wp_insert_post_result ) ) { - return; - } - - // Fetch the most recently published navigation which will be the default one created above. - return block_core_navigation_get_most_recently_published_navigation(); + return $wp_insert_post_result; } /** @@ -491,21 +487,23 @@ function block_core_navigation_create_fallback() { // Get the most recently published Navigation menu. $navigation_menu = block_core_navigation_get_most_recently_published_navigation(); + // If there is already a Navigation menu then exit. if ( $navigation_menu ) { return; } // If there are no Navigation menus then try to find a Classic menu - // and convert it into a Navigation menu. - - $navigation_menu = block_core_navigation_maybe_use_classic_menu_fallback(); + // and attempt to convert it into a Navigation menu. + $navigation_menu = block_core_navigation_create_classic_menu_fallback(); - if ( $navigation_menu ) { + // If the conversion + creation was successful then exit. + if ( ! is_wp_error( $navigation_menu ) ) { return; } - // If there are no navigation posts then default to a list of Pages. - block_core_navigation_get_default_fallback(); + // If the Classic menu process did not result in a post + // then default to creating a default fallback Navigaiton menu. + block_core_navigation_create_default_fallback(); } // Run on switching Theme and when installing WP for the first time. add_action( 'switch_theme', 'block_core_navigation_create_fallback' ); From 7722010e0a9aadb6d1afc34dbca595d21e6c7146 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 6 Mar 2023 20:50:49 +0000 Subject: [PATCH 37/37] Improve doc blocks --- .../block-library/src/navigation/index.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index b51a4f279eb93d..f6facb0f72c71a 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -325,9 +325,10 @@ function block_core_navigation_get_classic_menu_fallback_blocks( $classic_nav_me } /** - * If there's a the classic menu then use it as a fallback. + * Checks for a Classic Menu and attempts to convert into a block-based + * Navigation menu. * - * @return array the normalized parsed blocks. + * @return int|WP_Error The Navigation menu post ID on success. A WP_Error on failure. */ function block_core_navigation_create_classic_menu_fallback() { // See if we have a Classic menu. @@ -360,9 +361,7 @@ function block_core_navigation_create_classic_menu_fallback() { $return_errors // So that we can check whether the result is an error. ); - return $wp_insert_post_result; - } /** @@ -433,10 +432,10 @@ function block_core_navigation_block_contains_core_navigation( $inner_blocks ) { } /** - * Create and returns a navigation menu containing default fallback content. + * Creates a navigation menu containing default fallback content. * (a page-list if registered). * - * @return array the newly created navigation menu. + * @return int|WP_Error The post ID on success. A WP_Error on failure. */ function block_core_navigation_create_default_fallback() { $registry = WP_Block_Type_Registry::get_instance(); @@ -485,19 +484,19 @@ function block_core_navigation_create_fallback() { } // Get the most recently published Navigation menu. - $navigation_menu = block_core_navigation_get_most_recently_published_navigation(); + $existing_navigation_menu = block_core_navigation_get_most_recently_published_navigation(); // If there is already a Navigation menu then exit. - if ( $navigation_menu ) { + if ( $existing_navigation_menu ) { return; } // If there are no Navigation menus then try to find a Classic menu // and attempt to convert it into a Navigation menu. - $navigation_menu = block_core_navigation_create_classic_menu_fallback(); + $converted_classic_menu_id = block_core_navigation_create_classic_menu_fallback(); // If the conversion + creation was successful then exit. - if ( ! is_wp_error( $navigation_menu ) ) { + if ( ! is_wp_error( $converted_classic_menu_id ) ) { return; }