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( '