From edff06612b48b028ccdf29505f7236ae7a05f02f Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 11:16:21 +0600 Subject: [PATCH 1/3] Tests: Add unit tests for the Widget Group block rendering and the sidebar notes. See #66514. --- .../tests/blocks/renderWidgetGroup.php | 351 ++++++++++++++++++ 1 file changed, 351 insertions(+) create mode 100644 tests/phpunit/tests/blocks/renderWidgetGroup.php diff --git a/tests/phpunit/tests/blocks/renderWidgetGroup.php b/tests/phpunit/tests/blocks/renderWidgetGroup.php new file mode 100644 index 0000000000000..c176445137f10 --- /dev/null +++ b/tests/phpunit/tests/blocks/renderWidgetGroup.php @@ -0,0 +1,351 @@ +original_sidebars = $wp_registered_sidebars; + $this->original_sidebar_being_rendered = isset( $_sidebar_being_rendered ) ? $_sidebar_being_rendered : null; + + $wp_registered_sidebars = array(); + unset( $GLOBALS['_sidebar_being_rendered'] ); + } + + public function tear_down() { + global $wp_registered_sidebars; + + $wp_registered_sidebars = $this->original_sidebars; + + if ( null === $this->original_sidebar_being_rendered ) { + unset( $GLOBALS['_sidebar_being_rendered'] ); + } else { + $GLOBALS['_sidebar_being_rendered'] = $this->original_sidebar_being_rendered; + } + + parent::tear_down(); + } + + /** + * Creates a Widget Group block with inner blocks that only contain HTML. + * + * @param array $attributes Attributes of the block. + * @param string[] $inner_html HTML of each inner block. + * @return WP_Block The block. + */ + private function get_block( $attributes = array(), $inner_html = array() ) { + $inner_blocks = array(); + + foreach ( $inner_html as $html ) { + $inner_blocks[] = array( + 'blockName' => null, + 'attrs' => array(), + 'innerBlocks' => array(), + 'innerHTML' => $html, + 'innerContent' => array( $html ), + ); + } + + return new WP_Block( + array( + 'blockName' => 'core/widget-group', + 'attrs' => $attributes, + 'innerBlocks' => $inner_blocks, + 'innerHTML' => '', + 'innerContent' => array_fill( 0, count( $inner_blocks ), null ), + ) + ); + } + + /** + * Registers a sidebar. + * + * @param string $id Sidebar ID. + */ + private function register_test_sidebar( $id ) { + register_sidebar( + array( + 'id' => $id, + 'name' => 'Sidebar ' . $id, + 'before_title' => '

', + 'after_title' => '

', + ) + ); + } + + /** + * Tests that a title uses the default heading when no sidebar is being rendered. + * + * @ticket 66514 + * + * @covers ::render_block_core_widget_group + */ + public function test_should_use_the_default_heading_for_the_title_without_a_sidebar() { + $block = $this->get_block( array( 'title' => 'My group' ) ); + + $this->assertSame( + '

My group

', + render_block_core_widget_group( $block->attributes, '', $block ) + ); + } + + /** + * Tests that the title uses the markup of the sidebar being rendered. + * + * @ticket 66514 + * + * @covers ::render_block_core_widget_group + */ + public function test_should_use_the_title_markup_of_the_sidebar_being_rendered() { + $this->register_test_sidebar( 'group-sidebar' ); + $GLOBALS['_sidebar_being_rendered'] = 'group-sidebar'; + + $block = $this->get_block( array( 'title' => 'My group' ) ); + + $this->assertSame( + '

My group

', + render_block_core_widget_group( $block->attributes, '', $block ) + ); + } + + /** + * Tests that the default heading is used when the sidebar being rendered is not registered. + * + * @ticket 66514 + * + * @covers ::render_block_core_widget_group + */ + public function test_should_use_the_default_heading_when_the_sidebar_being_rendered_is_not_registered() { + $this->register_test_sidebar( 'group-sidebar' ); + $GLOBALS['_sidebar_being_rendered'] = 'unknown-sidebar'; + + $block = $this->get_block( array( 'title' => 'My group' ) ); + + $this->assertSame( + '

My group

', + render_block_core_widget_group( $block->attributes, '', $block ) + ); + } + + /** + * Tests that the title is escaped. + * + * @ticket 66514 + * + * @covers ::render_block_core_widget_group + */ + public function test_should_escape_the_title() { + $block = $this->get_block( array( 'title' => ' & more' ) ); + + $this->assertSame( + '

<script>alert( "x" );</script> & more

', + render_block_core_widget_group( $block->attributes, '', $block ) + ); + } + + /** + * Tests that no heading is rendered without a title. + * + * @ticket 66514 + * + * @covers ::render_block_core_widget_group + * + * @dataProvider data_attributes_without_a_title + * + * @param array $attributes Attributes of the block. + */ + public function test_should_not_render_a_heading_without_a_title( $attributes ) { + $block = $this->get_block( $attributes ); + + $this->assertSame( + '
', + render_block_core_widget_group( $block->attributes, '', $block ) + ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_attributes_without_a_title() { + return array( + 'no attributes' => array( array() ), + 'empty title' => array( array( 'title' => '' ) ), + 'null title' => array( array( 'title' => null ) ), + ); + } + + /** + * Tests that the inner blocks are rendered in order inside the wrapper. + * + * @ticket 66514 + * + * @covers ::render_block_core_widget_group + */ + public function test_should_render_the_inner_blocks_in_order_inside_the_wrapper() { + $block = $this->get_block( array( 'title' => 'Group' ), array( '

One

', '

Two

', '

Three

' ) ); + + $this->assertSame( + '

Group

One

Two

Three

', + render_block_core_widget_group( $block->attributes, '', $block ) + ); + } + + /** + * Tests that the content that is passed is not used. + * + * @ticket 66514 + * + * @covers ::render_block_core_widget_group + */ + public function test_should_ignore_the_content_that_is_passed() { + $block = $this->get_block( array(), array( '

Inner

' ) ); + + $this->assertSame( + '

Inner

', + render_block_core_widget_group( $block->attributes, '

Ignored

', $block ) + ); + } + + /** + * Tests that the block is rendered by the dynamic sidebar with the markup of that sidebar. + * + * @ticket 66514 + * + * @covers ::note_sidebar_being_rendered + * @covers ::render_block_core_widget_group + */ + public function test_should_use_the_markup_of_the_sidebar_when_rendered_from_a_dynamic_sidebar() { + $this->register_test_sidebar( 'group-sidebar' ); + + $block = $this->get_block( array( 'title' => 'My group' ), array( '

Inner

' ) ); + + do_action( 'dynamic_sidebar_before', 'group-sidebar', true ); + + $this->assertSame( + '

My group

Inner

', + render_block_core_widget_group( $block->attributes, '', $block ) + ); + } + + /** + * Tests that the sidebar index is noted. + * + * @ticket 66514 + * + * @covers ::note_sidebar_being_rendered + * + * @dataProvider data_sidebar_indexes + * + * @param int|string $index Index, name or ID of the sidebar. + */ + public function test_should_note_the_sidebar_being_rendered( $index ) { + note_sidebar_being_rendered( $index ); + + $this->assertSame( $index, $GLOBALS['_sidebar_being_rendered'] ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_sidebar_indexes() { + return array( + 'an ID' => array( 'sidebar-1' ), + 'a name' => array( 'Main Sidebar' ), + 'an int' => array( 2 ), + ); + } + + /** + * Tests that the latest sidebar being rendered is the one that is noted. + * + * @ticket 66514 + * + * @covers ::note_sidebar_being_rendered + */ + public function test_should_replace_the_noted_sidebar() { + note_sidebar_being_rendered( 'first' ); + note_sidebar_being_rendered( 'second' ); + + $this->assertSame( 'second', $GLOBALS['_sidebar_being_rendered'] ); + } + + /** + * Tests that the functions are hooked to the start and the end of a dynamic sidebar. + * + * @ticket 66514 + * + * @covers ::note_sidebar_being_rendered + * @covers ::discard_sidebar_being_rendered + */ + public function test_should_hook_the_functions_to_the_dynamic_sidebar_actions() { + $this->assertSame( 10, has_action( 'dynamic_sidebar_before', 'note_sidebar_being_rendered' ), 'The sidebar should be noted before it is rendered.' ); + $this->assertSame( 10, has_action( 'dynamic_sidebar_after', 'discard_sidebar_being_rendered' ), 'The sidebar should be discarded after it was rendered.' ); + } + + /** + * Tests that the noted sidebar is set by the action before a dynamic sidebar. + * + * @ticket 66514 + * + * @covers ::note_sidebar_being_rendered + */ + public function test_should_note_the_sidebar_on_the_dynamic_sidebar_before_action() { + do_action( 'dynamic_sidebar_before', 'sidebar-9', true ); + + $this->assertSame( 'sidebar-9', $GLOBALS['_sidebar_being_rendered'] ); + } + + /** + * Tests that the block is registered on init. + * + * @ticket 66514 + * + * @covers ::register_block_core_widget_group + */ + public function test_should_register_the_block_on_init() { + $registry = WP_Block_Type_Registry::get_instance(); + $was_registered = $registry->is_registered( 'core/widget-group' ); + + if ( $was_registered ) { + unregister_block_type( 'core/widget-group' ); + } + + register_block_core_widget_group(); + $block_type = $registry->get_registered( 'core/widget-group' ); + + $this->assertInstanceOf( WP_Block_Type::class, $block_type ); + $this->assertSame( 'render_block_core_widget_group', $block_type->render_callback ); + + if ( ! $was_registered ) { + unregister_block_type( 'core/widget-group' ); + } + } +} From 5e476f99eddf867d2bd47c46da0e1893514a8640 Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 17:02:09 +0600 Subject: [PATCH 2/3] Tests: Pass the attributes of the Widget Group block as they are and name the registration test. See #66514. --- tests/phpunit/tests/blocks/renderWidgetGroup.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/blocks/renderWidgetGroup.php b/tests/phpunit/tests/blocks/renderWidgetGroup.php index c176445137f10..2cec694388285 100644 --- a/tests/phpunit/tests/blocks/renderWidgetGroup.php +++ b/tests/phpunit/tests/blocks/renderWidgetGroup.php @@ -181,9 +181,10 @@ public function test_should_escape_the_title() { public function test_should_not_render_a_heading_without_a_title( $attributes ) { $block = $this->get_block( $attributes ); + // The attributes are passed as they are, because the block removes the ones that are null when it prepares them. $this->assertSame( '
', - render_block_core_widget_group( $block->attributes, '', $block ) + render_block_core_widget_group( $attributes, '', $block ) ); } @@ -324,13 +325,13 @@ public function test_should_note_the_sidebar_on_the_dynamic_sidebar_before_actio } /** - * Tests that the block is registered on init. + * Tests that the block is registered with the render callback. * * @ticket 66514 * * @covers ::register_block_core_widget_group */ - public function test_should_register_the_block_on_init() { + public function test_should_register_the_block_with_the_render_callback() { $registry = WP_Block_Type_Registry::get_instance(); $was_registered = $registry->is_registered( 'core/widget-group' ); From 7f979cf30913cd0985b02577c1590a703249b624 Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 21:28:18 +0600 Subject: [PATCH 3/3] Tests: Do not leave the sidebar being rendered null in the Widget Group tests. See #66514. --- tests/phpunit/tests/blocks/renderWidgetGroup.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/renderWidgetGroup.php b/tests/phpunit/tests/blocks/renderWidgetGroup.php index 2cec694388285..6dcb1c513e9bd 100644 --- a/tests/phpunit/tests/blocks/renderWidgetGroup.php +++ b/tests/phpunit/tests/blocks/renderWidgetGroup.php @@ -33,7 +33,9 @@ public function set_up() { $this->original_sidebar_being_rendered = isset( $_sidebar_being_rendered ) ? $_sidebar_being_rendered : null; $wp_registered_sidebars = array(); - unset( $GLOBALS['_sidebar_being_rendered'] ); + + // No sidebar is being rendered. The block reads the value as an array key, so it should not be null. + $GLOBALS['_sidebar_being_rendered'] = ''; } public function tear_down() {