From 174e6388eda0026a98781b1d401573f7b6322302 Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Sat, 7 Nov 2026 11:22:17 +0600 Subject: [PATCH] Tests: Add unit tests for the server-side rendering of the Navigation Submenu block. See #66589. --- .../tests/blocks/renderNavigationSubmenu.php | 778 ++++++++++++++++++ 1 file changed, 778 insertions(+) create mode 100644 tests/phpunit/tests/blocks/renderNavigationSubmenu.php diff --git a/tests/phpunit/tests/blocks/renderNavigationSubmenu.php b/tests/phpunit/tests/blocks/renderNavigationSubmenu.php new file mode 100644 index 0000000000000..a545a05588271 --- /dev/null +++ b/tests/phpunit/tests/blocks/renderNavigationSubmenu.php @@ -0,0 +1,778 @@ +original_block_to_render = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = array( + 'blockName' => 'core/navigation-submenu', + 'attrs' => array(), + ); + + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-submenu' ); + $this->original_supports = $block_type->supports; + } + + public function tear_down() { + WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-submenu' )->supports = $this->original_supports; + WP_Block_Supports::$block_to_render = $this->original_block_to_render; + + parent::tear_down(); + } + + /** + * Renders the block. + * + * @param array $attributes The attributes of the block. The label is set by default. + * @param array $context Optional. The context of the block. Default none. + * @param string[] $children Optional. The markup of the inner blocks. Default none. + * @return string The rendered block. + */ + private function render( $attributes = array(), $context = array(), $children = array() ) { + $attributes = array_merge( array( 'label' => 'Label' ), $attributes ); + $inner_blocks = array(); + + foreach ( $children as $markup ) { + $inner_blocks[] = array( + 'blockName' => 'core/html', + 'attrs' => array(), + 'innerBlocks' => array(), + 'innerHTML' => $markup, + 'innerContent' => array( $markup ), + ); + } + + $block = new WP_Block( + array( + 'blockName' => 'core/navigation-submenu', + 'attrs' => $attributes, + 'innerBlocks' => $inner_blocks, + 'innerHTML' => '', + 'innerContent' => array_fill( 0, count( $inner_blocks ), null ), + ), + $context + ); + + return render_block_core_navigation_submenu( $attributes, '', $block ); + } + + /** + * Returns the first element with the tag, with its attributes. + * + * @param string $markup The markup. + * @param string $tag The tag name. + * @return array|null The attributes, or null when there is no element. + */ + private function get_element( $markup, $tag ) { + $processor = new WP_HTML_Tag_Processor( $markup ); + + if ( ! $processor->next_tag( $tag ) ) { + return null; + } + + $attributes = array(); + + foreach ( $processor->get_attribute_names_with_prefix( '' ) as $name ) { + $attributes[ $name ] = $processor->get_attribute( $name ); + } + + return $attributes; + } + + /** + * Returns the classes of an element as a sorted list. + * + * @param array $element The attributes of the element. + * @return string[] The classes. + */ + private function get_classes( $element ) { + $classes = preg_split( '/\s+/', $element['class'] ?? '', -1, PREG_SPLIT_NO_EMPTY ); + sort( $classes ); + + return $classes; + } + + /** + * Tests that nothing is rendered without a label. + * + * @ticket 66589 + * + * @dataProvider data_no_label + * + * @param mixed $label The label. + */ + public function test_should_render_nothing_without_a_label( $label ) { + $this->assertSame( '', $this->render( array( 'label' => $label ), array(), array( '
  • Child
  • ' ) ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_no_label() { + return array( + 'empty string' => array( '' ), + 'zero string' => array( '0' ), + 'null' => array( null ), + ); + } + + /** + * Tests the item with a link. + * + * @ticket 66589 + */ + public function test_should_render_an_item_with_a_link() { + $markup = $this->render( array( 'url' => 'https://example.org/page/' ) ); + $item = $this->get_element( $markup, 'li' ); + $link = $this->get_element( $markup, 'a' ); + + $this->assertSame( array( 'wp-block-navigation-item', 'wp-block-navigation-submenu' ), $this->get_classes( $item ) ); + $this->assertSame( 'wp-block-navigation-item__content', $link['class'] ); + $this->assertSame( 'https://example.org/page/', $link['href'] ); + $this->assertArrayNotHasKey( 'aria-current', $link ); + $this->assertArrayNotHasKey( 'target', $link ); + $this->assertArrayNotHasKey( 'rel', $link ); + $this->assertArrayNotHasKey( 'title', $link ); + $this->assertStringStartsWith( '
  • assertStringEndsWith( 'Label
  • ', $markup ); + $this->assertStringNotContainsString( 'assertStringNotContainsString( 'assertArrayNotHasKey( 'href', $this->get_element( $this->render(), 'a' ) ); + } + + /** + * Tests that the URL is escaped. + * + * @ticket 66589 + */ + public function test_should_escape_the_url() { + $this->assertStringContainsString( 'href="https://example.org/?a=1&b=2"', $this->render( array( 'url' => 'https://example.org/?a=1&b=2' ) ) ); + $this->assertSame( '', $this->get_element( $this->render( array( 'url' => 'javascript:alert(1)' ) ), 'a' )['href'] ); + } + + /** + * Tests the label, which is filtered as post content. + * + * @ticket 66589 + */ + public function test_should_filter_the_label() { + $markup = $this->render( array( 'label' => 'Bold' ) ); + + $this->assertStringContainsString( 'Bold', $markup ); + $this->assertStringNotContainsString( 'render( array( 'description' => 'About' ) ); + + $this->assertStringContainsString( 'About', $markup ); + $this->assertStringNotContainsString( 'assertStringNotContainsString( 'wp-block-navigation-item__description', $this->render( array( 'description' => '' ) ) ); + } + + /** + * Tests the attributes of the link. + * + * @ticket 66589 + */ + public function test_should_render_the_attributes_of_the_link() { + $link = $this->get_element( + $this->render( + array( + 'url' => 'https://example.org/', + 'opensInNewTab' => true, + 'rel' => 'noopener "quoted"', + 'title' => 'A "title"', + ) + ), + 'a' + ); + + $this->assertSame( '_blank', $link['target'] ); + $this->assertSame( 'noopener "quoted"', $link['rel'] ); + $this->assertSame( 'A "title"', $link['title'] ); + } + + /** + * Tests that the link does not open in a new tab unless it is set to. + * + * @ticket 66589 + * + * @dataProvider data_not_new_tab + * + * @param mixed $value The value of the opens in a new tab attribute. + */ + public function test_should_not_open_in_a_new_tab_unless_it_is_set_to( $value ) { + $this->assertArrayNotHasKey( 'target', $this->get_element( $this->render( array( 'opensInNewTab' => $value ) ), 'a' ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_not_new_tab() { + return array( + 'false' => array( false ), + 'string' => array( 'true' ), + 'one' => array( 1 ), + ); + } + + /** + * Tests the nofollow attribute, and that the rel attribute wins over it. + * + * @ticket 66589 + */ + public function test_should_add_nofollow_to_the_rel_of_the_link() { + $this->assertSame( 'nofollow', $this->get_element( $this->render( array( 'nofollow' => true ) ), 'a' )['rel'] ); + $this->assertArrayNotHasKey( 'rel', $this->get_element( $this->render( array( 'nofollow' => false ) ), 'a' ) ); + $this->assertSame( + 'external', + $this->get_element( + $this->render( + array( + 'nofollow' => true, + 'rel' => 'external', + ) + ), + 'a' + )['rel'] + ); + } + + /** + * Tests that a linked post that is not published is not rendered. + * + * @ticket 66589 + */ + public function test_should_render_nothing_for_a_linked_post_that_is_not_published() { + $draft_id = self::factory()->post->create( array( 'post_status' => 'draft' ) ); + + $this->assertSame( + '', + $this->render( + array( + 'id' => $draft_id, + 'kind' => 'post-type', + ) + ) + ); + $this->assertSame( + '', + $this->render( + array( + 'id' => 999999, + 'kind' => 'post-type', + ) + ), + 'A post that does not exist.' + ); + $this->assertSame( + '', + $this->render( + array( + 'id' => $draft_id, + 'type' => 'page', + ) + ) + ); + } + + /** + * Tests a linked post that is published, and links that are not posts. + * + * @ticket 66589 + */ + public function test_should_render_a_linked_post_that_is_published() { + $post_id = self::factory()->post->create(); + $draft_id = self::factory()->post->create( array( 'post_status' => 'draft' ) ); + + $this->assertNotSame( + '', + $this->render( + array( + 'id' => $post_id, + 'kind' => 'post-type', + ) + ) + ); + $this->assertNotSame( + '', + $this->render( + array( + 'id' => $draft_id, + 'kind' => 'taxonomy', + ) + ), + 'A link to a term is not checked.' + ); + } + + /** + * Tests the filter of the post statuses. + * + * @ticket 66589 + */ + public function test_should_use_the_filter_of_the_allowed_post_statuses() { + $draft_id = self::factory()->post->create( array( 'post_status' => 'draft' ) ); + + add_filter( + 'render_block_core_navigation_link_allowed_post_status', + static function () { + return array( 'draft' ); + } + ); + + $this->assertNotSame( + '', + $this->render( + array( + 'id' => $draft_id, + 'kind' => 'post-type', + ) + ) + ); + } + + /** + * Tests the submenu. + * + * @ticket 66589 + */ + public function test_should_render_the_inner_blocks_in_a_submenu() { + $markup = $this->render( array(), array(), array( '
  • One
  • ', '
  • Two
  • ' ) ); + + $this->assertContains( 'has-child', $this->get_classes( $this->get_element( $markup, 'li' ) ) ); + $this->assertMatchesRegularExpression( '/
    • One<\/li>
    • Two<\/li><\/ul><\/li>$/', $markup ); + } + + /** + * Tests that there is no submenu when the inner blocks have no output. + * + * @ticket 66589 + */ + public function test_should_not_render_a_submenu_when_the_inner_blocks_have_no_output() { + $markup = $this->render( array(), array(), array( '', " \n" ) ); + + $this->assertNotContains( 'has-child', $this->get_classes( $this->get_element( $markup, 'li' ) ) ); + $this->assertStringNotContainsString( 'get_classes( $this->get_element( $this->render( array(), $context, array( '
    • Child
    • ' ) ), 'li' ) ); + $common = array( 'has-child', 'wp-block-navigation-item', 'wp-block-navigation-submenu' ); + + sort( $expected ); + + $this->assertSame( array_values( array_diff( $classes, $common ) ), $expected ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_visibility_classes() { + return array( + 'default' => array( array(), array() ), + 'hover' => array( array( 'submenuVisibility' => 'hover' ), array() ), + 'hover with icons' => array( + array( + 'submenuVisibility' => 'hover', + 'showSubmenuIcon' => true, + ), + array( 'open-on-hover-click' ), + ), + 'default with icons' => array( array( 'showSubmenuIcon' => true ), array( 'open-on-hover-click' ) ), + 'icons off' => array( array( 'showSubmenuIcon' => false ), array() ), + 'click' => array( array( 'submenuVisibility' => 'click' ), array( 'open-on-click' ) ), + 'click with icons' => array( + array( + 'submenuVisibility' => 'click', + 'showSubmenuIcon' => true, + ), + array( 'open-on-click' ), + ), + 'always' => array( array( 'submenuVisibility' => 'always' ), array( 'open-always' ) ), + 'always with icons' => array( + array( + 'submenuVisibility' => 'always', + 'showSubmenuIcon' => true, + ), + array( 'open-always' ), + ), + 'deprecated click' => array( array( 'openSubmenusOnClick' => true ), array( 'open-on-click' ) ), + 'deprecated hover' => array( + array( + 'openSubmenusOnClick' => false, + 'showSubmenuIcon' => true, + ), + array( 'open-on-hover-click' ), + ), + 'deprecated click wins' => array( + array( + 'openSubmenusOnClick' => true, + 'submenuVisibility' => 'always', + ), + array( 'open-on-click' ), + ), + ); + } + + /** + * Tests the button next to the link that opens the submenu. + * + * @ticket 66589 + */ + public function test_should_render_the_submenu_button_next_to_the_link() { + $markup = $this->render( array( 'label' => 'Parent' ), array( 'showSubmenuIcon' => true ), array( '
    • Child
    • ' ) ); + $button = $this->get_element( $markup, 'button' ); + + $this->assertSame( 'Parent submenu', $button['aria-label'] ); + $this->assertSame( 'wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle', $button['class'] ); + $this->assertSame( 'false', $button['aria-expanded'] ); + $this->assertStringContainsString( '
        assertStringNotContainsString( 'render( array(), array(), array( '
      • Child
      • ' ) ) ); + $this->assertStringNotContainsString( 'render( array(), array( 'showSubmenuIcon' => true ) ) ); + $this->assertStringNotContainsString( 'render( array(), array( 'showSubmenuIcon' => false ), array( '
      • Child
      • ' ) ) ); + } + + /** + * Tests the button in place of the link when the submenu opens on click. + * + * @ticket 66589 + */ + public function test_should_render_a_button_in_place_of_the_link_when_opening_on_click() { + $markup = $this->render( + array( + 'url' => 'https://example.org/', + 'label' => 'Parent', + 'description' => 'About', + ), + array( 'submenuVisibility' => 'click' ), + array( '
      • Child
      • ' ) + ); + + $button = $this->get_element( $markup, 'button' ); + + $this->assertNull( $this->get_element( $markup, 'a' ), 'There should be no link.' ); + $this->assertSame( 'Parent submenu', $button['aria-label'] ); + $this->assertSame( 'wp-block-navigation-item__content wp-block-navigation-submenu__toggle', $button['class'] ); + $this->assertSame( 'false', $button['aria-expanded'] ); + $this->assertStringContainsString( '', $markup ); + $this->assertStringContainsString( '' . block_core_shared_navigation_render_submenu_icon() . '
          render( array(), array( 'submenuVisibility' => 'click' ) ); + + $this->assertStringContainsString( '