From 939b16a0912bfe1172ad91165d1819d8938ad24a Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 07:59:51 +0600 Subject: [PATCH] Tests: Add unit tests for the functions that find and assign the widgets of the sidebars. Covers `is_active_sidebar()`, `is_dynamic_sidebar()`, `wp_find_widgets_sidebar()` and `wp_assign_widget_to_sidebar()`, which had no tests. See #66502. --- .../phpunit/tests/widgets/isActiveSidebar.php | 188 ++++++++++++++ .../tests/widgets/isDynamicSidebar.php | 152 ++++++++++++ .../tests/widgets/wpAssignWidgetToSidebar.php | 234 ++++++++++++++++++ .../tests/widgets/wpFindWidgetsSidebar.php | 158 ++++++++++++ 4 files changed, 732 insertions(+) create mode 100644 tests/phpunit/tests/widgets/isActiveSidebar.php create mode 100644 tests/phpunit/tests/widgets/isDynamicSidebar.php create mode 100644 tests/phpunit/tests/widgets/wpAssignWidgetToSidebar.php create mode 100644 tests/phpunit/tests/widgets/wpFindWidgetsSidebar.php diff --git a/tests/phpunit/tests/widgets/isActiveSidebar.php b/tests/phpunit/tests/widgets/isActiveSidebar.php new file mode 100644 index 0000000000000..19cc966151a6d --- /dev/null +++ b/tests/phpunit/tests/widgets/isActiveSidebar.php @@ -0,0 +1,188 @@ +original_sidebars = $wp_registered_sidebars; + $this->original_widgets = $wp_registered_widgets; + + $wp_registered_sidebars = array(); + $wp_registered_widgets = array(); + $_wp_sidebars_widgets = null; + } + + public function tear_down() { + global $wp_registered_sidebars, $wp_registered_widgets, $_wp_sidebars_widgets; + + $wp_registered_sidebars = $this->original_sidebars; + $wp_registered_widgets = $this->original_widgets; + $_wp_sidebars_widgets = null; + + parent::tear_down(); + } + + /** + * Tests that a sidebar with widgets is active. + * + * @ticket 66502 + */ + public function test_should_return_true_for_a_sidebar_with_widgets() { + wp_set_sidebars_widgets( array( 'main-sidebar' => array( 'search-2' ) ) ); + + $this->assertTrue( is_active_sidebar( 'main-sidebar' ) ); + } + + /** + * Tests that a sidebar without widgets is not active. + * + * @ticket 66502 + */ + public function test_should_return_false_for_a_sidebar_without_widgets() { + wp_set_sidebars_widgets( + array( + 'main-sidebar' => array(), + 'other-sidebar' => array( 'search-2' ), + ) + ); + + $this->assertFalse( is_active_sidebar( 'main-sidebar' ) ); + } + + /** + * Tests that a sidebar that does not exist is not active. + * + * @ticket 66502 + */ + public function test_should_return_false_for_an_unknown_sidebar() { + wp_set_sidebars_widgets( array( 'main-sidebar' => array( 'search-2' ) ) ); + + $this->assertFalse( is_active_sidebar( 'unknown-sidebar' ) ); + } + + /** + * Tests that the state of the sidebar follows the widgets that are assigned to it. + * + * @ticket 66502 + */ + public function test_should_follow_the_widgets_assigned_to_the_sidebar() { + wp_set_sidebars_widgets( array( 'main-sidebar' => array() ) ); + + $this->assertFalse( is_active_sidebar( 'main-sidebar' ), 'The sidebar should not be active without widgets.' ); + + wp_set_sidebars_widgets( array( 'main-sidebar' => array( 'search-2' ) ) ); + + $this->assertTrue( is_active_sidebar( 'main-sidebar' ), 'The sidebar should be active once a widget is assigned.' ); + } + + /** + * Tests that an integer is the number of a sidebar. + * + * @ticket 66502 + */ + public function test_should_use_an_integer_as_the_number_of_the_sidebar() { + wp_set_sidebars_widgets( + array( + 'sidebar-2' => array( 'search-2' ), + '2' => array(), + ) + ); + + $this->assertTrue( is_active_sidebar( 2 ), 'The integer should be the number of the sidebar.' ); + $this->assertFalse( is_active_sidebar( 3 ), 'Another number should not be active.' ); + $this->assertFalse( is_active_sidebar( '2' ), 'A string is the ID of the sidebar, and not its number.' ); + } + + /** + * Tests that the ID of the sidebar is sanitized like a title. + * + * @ticket 66502 + */ + public function test_should_sanitize_the_id_of_the_sidebar() { + wp_set_sidebars_widgets( array( 'main-sidebar' => array( 'search-2' ) ) ); + + $this->assertTrue( is_active_sidebar( 'Main Sidebar' ), 'The ID should be sanitized.' ); + $this->assertTrue( is_active_sidebar( 'MAIN-SIDEBAR' ), 'The case should be ignored.' ); + } + + /** + * Tests that the `is_active_sidebar` filter receives the result and the index of the sidebar. + * + * @ticket 66502 + */ + public function test_should_apply_the_filter_with_the_result_and_the_index() { + wp_set_sidebars_widgets( + array( + 'main-sidebar' => array( 'search-2' ), + 'empty-sidebar' => array(), + ) + ); + + $received = array(); + add_filter( + 'is_active_sidebar', + static function ( $is_active, $index ) use ( &$received ) { + $received[] = array( $is_active, $index ); + + return $is_active; + }, + 10, + 2 + ); + + is_active_sidebar( 'main-sidebar' ); + is_active_sidebar( 'Empty Sidebar' ); + is_active_sidebar( 4 ); + + $this->assertSame( + array( + array( true, 'main-sidebar' ), + array( false, 'empty-sidebar' ), + array( false, 'sidebar-4' ), + ), + $received + ); + } + + /** + * Tests that the result of the `is_active_sidebar` filter is returned. + * + * @ticket 66502 + */ + public function test_should_return_the_result_of_the_filter() { + wp_set_sidebars_widgets( array( 'main-sidebar' => array( 'search-2' ) ) ); + + add_filter( 'is_active_sidebar', '__return_false' ); + + $this->assertFalse( is_active_sidebar( 'main-sidebar' ), 'The filter should be able to make the sidebar inactive.' ); + + remove_filter( 'is_active_sidebar', '__return_false' ); + add_filter( 'is_active_sidebar', '__return_true' ); + + $this->assertTrue( is_active_sidebar( 'unknown-sidebar' ), 'The filter should be able to make the sidebar active.' ); + } +} diff --git a/tests/phpunit/tests/widgets/isDynamicSidebar.php b/tests/phpunit/tests/widgets/isDynamicSidebar.php new file mode 100644 index 0000000000000..1a22fc129d926 --- /dev/null +++ b/tests/phpunit/tests/widgets/isDynamicSidebar.php @@ -0,0 +1,152 @@ +original_sidebars = $wp_registered_sidebars; + $this->original_widgets = $wp_registered_widgets; + + $wp_registered_sidebars = array(); + $wp_registered_widgets = array(); + $_wp_sidebars_widgets = null; + } + + public function tear_down() { + global $wp_registered_sidebars, $wp_registered_widgets, $_wp_sidebars_widgets; + + $wp_registered_sidebars = $this->original_sidebars; + $wp_registered_widgets = $this->original_widgets; + $_wp_sidebars_widgets = null; + + parent::tear_down(); + } + + /** + * Registers a sidebar and a widget, and assigns the widget to the sidebar. + * + * @param string $sidebar_id Sidebar ID. + * @param string[] $widget_ids IDs of the widgets, which are registered and assigned. + */ + private function register_sidebar_with_widgets( $sidebar_id, $widget_ids ) { + register_sidebar( array( 'id' => $sidebar_id ) ); + + foreach ( $widget_ids as $widget_id ) { + wp_register_sidebar_widget( $widget_id, 'Widget ' . $widget_id, '__return_empty_string' ); + } + + $sidebars_widgets = get_option( 'sidebars_widgets', array() ); + $sidebars_widgets[ $sidebar_id ] = $widget_ids; + + wp_set_sidebars_widgets( $sidebars_widgets ); + } + + /** + * Tests that a registered sidebar with a registered widget is dynamic. + * + * @ticket 66502 + */ + public function test_should_return_true_when_a_sidebar_has_a_registered_widget() { + $this->register_sidebar_with_widgets( 'main-sidebar', array( 'dynamic-widget' ) ); + + $this->assertTrue( is_dynamic_sidebar() ); + } + + /** + * Tests that it is false when no sidebar is registered. + * + * @ticket 66502 + */ + public function test_should_return_false_when_no_sidebar_is_registered() { + wp_register_sidebar_widget( 'dynamic-widget', 'Widget', '__return_empty_string' ); + wp_set_sidebars_widgets( array( 'main-sidebar' => array( 'dynamic-widget' ) ) ); + + $this->assertFalse( is_dynamic_sidebar() ); + } + + /** + * Tests that it is false when the registered sidebars have no widgets. + * + * @ticket 66502 + */ + public function test_should_return_false_when_the_registered_sidebars_have_no_widgets() { + register_sidebar( array( 'id' => 'main-sidebar' ) ); + wp_set_sidebars_widgets( array( 'main-sidebar' => array() ) ); + + $this->assertFalse( is_dynamic_sidebar() ); + } + + /** + * Tests that the widgets that are not registered are ignored. + * + * @ticket 66502 + */ + public function test_should_return_false_when_the_widgets_are_not_registered() { + register_sidebar( array( 'id' => 'main-sidebar' ) ); + wp_set_sidebars_widgets( array( 'main-sidebar' => array( 'unregistered-widget' ) ) ); + + $this->assertFalse( is_dynamic_sidebar() ); + } + + /** + * Tests that the widgets of a sidebar that is not registered are ignored. + * + * @ticket 66502 + */ + public function test_should_ignore_the_widgets_of_a_sidebar_that_is_not_registered() { + register_sidebar( array( 'id' => 'main-sidebar' ) ); + wp_register_sidebar_widget( 'dynamic-widget', 'Widget', '__return_empty_string' ); + wp_set_sidebars_widgets( + array( + 'main-sidebar' => array(), + 'unregistered-bar' => array( 'dynamic-widget' ), + 'wp_inactive_widgets' => array( 'dynamic-widget' ), + ) + ); + + $this->assertFalse( is_dynamic_sidebar() ); + } + + /** + * Tests that one registered widget is enough, even after widgets that are not registered. + * + * @ticket 66502 + */ + public function test_should_return_true_when_one_widget_is_registered_among_others() { + register_sidebar( array( 'id' => 'first-sidebar' ) ); + register_sidebar( array( 'id' => 'second-sidebar' ) ); + wp_register_sidebar_widget( 'dynamic-widget', 'Widget', '__return_empty_string' ); + wp_set_sidebars_widgets( + array( + 'first-sidebar' => array( 'unregistered-widget' ), + 'second-sidebar' => array( 'unregistered-widget-2', 'dynamic-widget' ), + ) + ); + + $this->assertTrue( is_dynamic_sidebar() ); + } +} diff --git a/tests/phpunit/tests/widgets/wpAssignWidgetToSidebar.php b/tests/phpunit/tests/widgets/wpAssignWidgetToSidebar.php new file mode 100644 index 0000000000000..089717dd5ccbe --- /dev/null +++ b/tests/phpunit/tests/widgets/wpAssignWidgetToSidebar.php @@ -0,0 +1,234 @@ +original_sidebars = $wp_registered_sidebars; + $this->original_widgets = $wp_registered_widgets; + + $wp_registered_sidebars = array(); + $wp_registered_widgets = array(); + $_wp_sidebars_widgets = null; + } + + public function tear_down() { + global $wp_registered_sidebars, $wp_registered_widgets, $_wp_sidebars_widgets; + + $wp_registered_sidebars = $this->original_sidebars; + $wp_registered_widgets = $this->original_widgets; + $_wp_sidebars_widgets = null; + + parent::tear_down(); + } + + /** + * Gets the widgets of the sidebars, as they are saved. + * + * @return array Widgets of the sidebars, without the version of the array. + */ + private function get_saved_sidebars() { + $sidebars = get_option( 'sidebars_widgets' ); + unset( $sidebars['array_version'] ); + + return $sidebars; + } + + /** + * Tests that a widget that is not assigned is added to the sidebar. + * + * @ticket 66502 + */ + public function test_should_add_a_widget_to_the_sidebar() { + wp_set_sidebars_widgets( array( 'main-sidebar' => array( 'search-2' ) ) ); + + wp_assign_widget_to_sidebar( 'text-3', 'main-sidebar' ); + + $this->assertSame( array( 'main-sidebar' => array( 'search-2', 'text-3' ) ), $this->get_saved_sidebars() ); + } + + /** + * Tests that a widget is added to a sidebar that does not have widgets yet. + * + * @ticket 66502 + */ + public function test_should_add_a_widget_to_a_new_sidebar() { + wp_set_sidebars_widgets( array() ); + + wp_assign_widget_to_sidebar( 'search-2', 'new-sidebar' ); + + $this->assertSame( array( 'new-sidebar' => array( 'search-2' ) ), $this->get_saved_sidebars() ); + } + + /** + * Tests that a widget is moved from the sidebar it was assigned to. + * + * @ticket 66502 + */ + public function test_should_move_the_widget_from_the_sidebar_it_was_assigned_to() { + wp_set_sidebars_widgets( + array( + 'first-sidebar' => array( 'search-2', 'text-3' ), + 'second-sidebar' => array( 'meta-4' ), + ) + ); + + wp_assign_widget_to_sidebar( 'search-2', 'second-sidebar' ); + + $this->assertSame( + array( + 'first-sidebar' => array( 1 => 'text-3' ), + 'second-sidebar' => array( 'meta-4', 'search-2' ), + ), + $this->get_saved_sidebars() + ); + $this->assertSame( 'second-sidebar', wp_find_widgets_sidebar( 'search-2' ), 'The widget should be found in the new sidebar.' ); + } + + /** + * Tests that a widget is removed from its sidebar when the new sidebar is empty. + * + * @ticket 66502 + * + * @dataProvider data_empty_sidebar_ids + * + * @param mixed $sidebar_id Empty ID of the sidebar. + */ + public function test_should_remove_the_widget_when_the_sidebar_is_empty( $sidebar_id ) { + wp_set_sidebars_widgets( + array( + 'first-sidebar' => array( 'search-2', 'text-3' ), + 'second-sidebar' => array( 'meta-4' ), + ) + ); + + wp_assign_widget_to_sidebar( 'search-2', $sidebar_id ); + + $this->assertSame( + array( + 'first-sidebar' => array( 1 => 'text-3' ), + 'second-sidebar' => array( 'meta-4' ), + ), + $this->get_saved_sidebars() + ); + $this->assertNull( wp_find_widgets_sidebar( 'search-2' ), 'The widget should not be assigned to a sidebar.' ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_empty_sidebar_ids() { + return array( + 'empty string' => array( '' ), + 'null' => array( null ), + 'false' => array( false ), + ); + } + + /** + * Tests that the other widgets and sidebars are not changed. + * + * @ticket 66502 + */ + public function test_should_not_change_the_other_widgets_and_sidebars() { + wp_set_sidebars_widgets( + array( + 'first-sidebar' => array( 'search-2' ), + 'second-sidebar' => array( 'meta-4', 'text-5' ), + 'wp_inactive_widgets' => array( 'archives-6' ), + ) + ); + + wp_assign_widget_to_sidebar( 'search-2', 'wp_inactive_widgets' ); + + $this->assertSame( + array( + 'first-sidebar' => array(), + 'second-sidebar' => array( 'meta-4', 'text-5' ), + 'wp_inactive_widgets' => array( 'archives-6', 'search-2' ), + ), + $this->get_saved_sidebars() + ); + } + + /** + * Tests that the widget is removed from every other sidebar it is assigned to. + * + * @ticket 66502 + */ + public function test_should_remove_the_widget_from_all_the_other_sidebars() { + wp_set_sidebars_widgets( + array( + 'first-sidebar' => array( 'search-2' ), + 'second-sidebar' => array( 'search-2', 'meta-4' ), + 'third-sidebar' => array(), + ) + ); + + wp_assign_widget_to_sidebar( 'search-2', 'third-sidebar' ); + + $this->assertSame( + array( + 'first-sidebar' => array(), + 'second-sidebar' => array( 1 => 'meta-4' ), + 'third-sidebar' => array( 'search-2' ), + ), + $this->get_saved_sidebars() + ); + } + + /** + * Tests that the change is used by the functions that read the widgets of the sidebars. + * + * @ticket 66502 + */ + public function test_should_update_the_sidebars_that_are_read_afterwards() { + wp_set_sidebars_widgets( array( 'main-sidebar' => array() ) ); + + $this->assertFalse( is_active_sidebar( 'main-sidebar' ), 'The sidebar should not be active before the widget is assigned.' ); + + wp_assign_widget_to_sidebar( 'search-2', 'main-sidebar' ); + + $this->assertTrue( is_active_sidebar( 'main-sidebar' ), 'The sidebar should be active after the widget is assigned.' ); + $this->assertSame( array( 'main-sidebar' => array( 'search-2' ) ), wp_get_sidebars_widgets(), 'The widgets of the sidebars should include the widget.' ); + } + + /** + * Tests that the version of the array of sidebars is saved with the change. + * + * @ticket 66502 + */ + public function test_should_save_the_version_of_the_array() { + wp_set_sidebars_widgets( array() ); + + wp_assign_widget_to_sidebar( 'search-2', 'main-sidebar' ); + + $this->assertSame( 3, get_option( 'sidebars_widgets' )['array_version'] ); + } +} diff --git a/tests/phpunit/tests/widgets/wpFindWidgetsSidebar.php b/tests/phpunit/tests/widgets/wpFindWidgetsSidebar.php new file mode 100644 index 0000000000000..837794ca93da5 --- /dev/null +++ b/tests/phpunit/tests/widgets/wpFindWidgetsSidebar.php @@ -0,0 +1,158 @@ +original_sidebars = $wp_registered_sidebars; + $this->original_widgets = $wp_registered_widgets; + + $wp_registered_sidebars = array(); + $wp_registered_widgets = array(); + $_wp_sidebars_widgets = null; + } + + public function tear_down() { + global $wp_registered_sidebars, $wp_registered_widgets, $_wp_sidebars_widgets; + + $wp_registered_sidebars = $this->original_sidebars; + $wp_registered_widgets = $this->original_widgets; + $_wp_sidebars_widgets = null; + + parent::tear_down(); + } + + /** + * Tests that the ID of the sidebar of a widget is returned. + * + * @ticket 66502 + */ + public function test_should_return_the_sidebar_of_the_widget() { + wp_set_sidebars_widgets( + array( + 'first-sidebar' => array( 'search-2', 'text-3' ), + 'second-sidebar' => array( 'meta-4' ), + ) + ); + + $this->assertSame( 'first-sidebar', wp_find_widgets_sidebar( 'text-3' ), 'The widget of the first sidebar should be found.' ); + $this->assertSame( 'second-sidebar', wp_find_widgets_sidebar( 'meta-4' ), 'The widget of the second sidebar should be found.' ); + } + + /** + * Tests that the widgets of the inactive sidebar are found too. + * + * @ticket 66502 + */ + public function test_should_find_the_widgets_of_the_inactive_widgets_sidebar() { + wp_set_sidebars_widgets( array( 'wp_inactive_widgets' => array( 'search-2' ) ) ); + + $this->assertSame( 'wp_inactive_widgets', wp_find_widgets_sidebar( 'search-2' ) ); + } + + /** + * Tests that null is returned when the widget is not assigned to a sidebar. + * + * @ticket 66502 + */ + public function test_should_return_null_when_the_widget_is_not_assigned() { + wp_set_sidebars_widgets( array( 'first-sidebar' => array( 'search-2' ) ) ); + + $this->assertNull( wp_find_widgets_sidebar( 'text-3' ) ); + } + + /** + * Tests that null is returned when there are no sidebars. + * + * @ticket 66502 + */ + public function test_should_return_null_when_there_are_no_sidebars() { + wp_set_sidebars_widgets( array() ); + + $this->assertNull( wp_find_widgets_sidebar( 'search-2' ) ); + } + + /** + * Tests that the ID of the widget is matched exactly. + * + * @ticket 66502 + */ + public function test_should_match_the_id_of_the_widget_exactly() { + wp_set_sidebars_widgets( array( 'first-sidebar' => array( 'search-2', '10' ) ) ); + + $this->assertNull( wp_find_widgets_sidebar( 'search-20' ), 'A longer ID should not match.' ); + $this->assertNull( wp_find_widgets_sidebar( 'Search-2' ), 'The ID should be case sensitive.' ); + $this->assertNull( wp_find_widgets_sidebar( 10 ), 'An integer should not match an ID that is a string.' ); + $this->assertSame( 'first-sidebar', wp_find_widgets_sidebar( '10' ), 'The same string should match.' ); + } + + /** + * Tests that the first sidebar is returned when a widget is assigned to more than one. + * + * @ticket 66502 + */ + public function test_should_return_the_first_sidebar_when_the_widget_is_assigned_twice() { + wp_set_sidebars_widgets( + array( + 'first-sidebar' => array( 'search-2' ), + 'second-sidebar' => array( 'search-2' ), + ) + ); + + $this->assertSame( 'first-sidebar', wp_find_widgets_sidebar( 'search-2' ) ); + } + + /** + * Tests that the ID of a sidebar that is a number is returned as a string. + * + * @ticket 66502 + */ + public function test_should_return_the_id_of_the_sidebar_as_a_string() { + wp_set_sidebars_widgets( array( 7 => array( 'search-2' ) ) ); + + $this->assertSame( '7', wp_find_widgets_sidebar( 'search-2' ) ); + } + + /** + * Tests that the sidebars are read through the `sidebars_widgets` filter. + * + * @ticket 66502 + */ + public function test_should_use_the_widgets_that_the_filter_returns() { + wp_set_sidebars_widgets( array( 'first-sidebar' => array( 'search-2' ) ) ); + + add_filter( + 'sidebars_widgets', + static function () { + return array( 'filtered-sidebar' => array( 'text-3' ) ); + } + ); + + $this->assertSame( 'filtered-sidebar', wp_find_widgets_sidebar( 'text-3' ), 'The widget of the filter should be found.' ); + $this->assertNull( wp_find_widgets_sidebar( 'search-2' ), 'The widget that the filter removed should not be found.' ); + } +}