diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 1513f3034514ee..265cb9a127f72a 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -105,7 +105,10 @@ function block_core_navigation_link_render_submenu_icon() { */ function render_block_core_navigation_link( $attributes, $content, $block ) { // Don't render the block's subtree if it is a draft. - if ( isset( $attributes['id'] ) && is_numeric( $attributes['id'] ) ) { + if ( + isset( $attributes['id'] ) && is_numeric( $attributes['id'] ) && + ( 'post' === $attributes['type'] || 'page' === $attributes['type'] ) + ) { $post = get_post( $attributes['id'] ); if ( 'publish' !== $post->post_status ) { return ''; diff --git a/phpunit/class-block-library-navigation-link-test.php b/phpunit/class-block-library-navigation-link-test.php new file mode 100644 index 00000000000000..65f152fafb7b5a --- /dev/null +++ b/phpunit/class-block-library-navigation-link-test.php @@ -0,0 +1,149 @@ +post->create_and_get( + array( + 'post_type' => 'page', + 'post_status' => 'draft', + 'post_name' => 'ceilingcat', + 'post_title' => 'Ceiling Cat', + 'post_content' => 'Ceiling Cat content', + 'post_excerpt' => 'Ceiling Cat', + ) + ); + self::$pages[] = self::$draft; + + self::$page = self::factory()->post->create_and_get( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_name' => 'tabby', + 'post_title' => 'Tabby cats', + 'post_content' => 'Tabby cat content', + 'post_excerpt' => 'Tabby cat', + ) + ); + self::$pages[] = self::$page; + + self::$category = self::factory()->category->create_and_get( + array( + 'taxonomy' => 'category', + 'name' => 'cats', + 'slug' => 'cats', + 'description' => 'Cats Category', + ) + ); + + self::$terms[] = self::$category; + } + + public static function wpTearDownAfterClass() { + foreach ( self::$pages as $page_to_delete ) { + wp_delete_post( $page_to_delete ); + } + foreach ( self::$terms as $term_to_delete ) { + wp_delete_term( $term_to_delete->term_id, $term_to_delete->taxonomy ); + } + } + + function test_returns_link_when_post_is_published() { + $page_id = self::$page->ID; + + $parsed_blocks = parse_blocks( + "" + ); + $this->assertEquals( 1, count( $parsed_blocks ) ); + + $navigation_link_block = new WP_Block( $parsed_blocks[0], array() ); + $this->assertEquals( + true, + strpos( + gutenberg_render_block_core_navigation_link( + $navigation_link_block->attributes, + array(), + $navigation_link_block + ), + 'Sample Page' + ) !== false + ); + } + + function test_returns_empty_when_label_is_missing() { + $page_id = self::$page->ID; + + $parsed_blocks = parse_blocks( + "" + ); + $this->assertEquals( 1, count( $parsed_blocks ) ); + + $navigation_link_block = new WP_Block( $parsed_blocks[0], array() ); + $this->assertEquals( + '', + gutenberg_render_block_core_navigation_link( + $navigation_link_block->attributes, + array(), + $navigation_link_block + ) + ); + } + + function test_returns_empty_when_draft() { + $page_id = self::$draft->ID; + + $parsed_blocks = parse_blocks( + "" + ); + $this->assertEquals( 1, count( $parsed_blocks ) ); + + $navigation_link_block = new WP_Block( $parsed_blocks[0], array() ); + + $this->assertEquals( + '', + gutenberg_render_block_core_navigation_link( + $navigation_link_block->attributes, + array(), + $navigation_link_block + ) + ); + } + + function test_returns_link_for_category() { + $category_id = self::$category->term_id; + + $parsed_blocks = parse_blocks( + "" + ); + $this->assertEquals( 1, count( $parsed_blocks ) ); + + $navigation_link_block = new WP_Block( $parsed_blocks[0], array() ); + $this->assertEquals( + true, + strpos( + gutenberg_render_block_core_navigation_link( + $navigation_link_block->attributes, + array(), + $navigation_link_block + ), + 'Cats' + ) !== false + ); + } +}