diff --git a/tests/phpunit/tests/widgets/isRegisteredSidebar.php b/tests/phpunit/tests/widgets/isRegisteredSidebar.php new file mode 100644 index 0000000000000..788b976d26448 --- /dev/null +++ b/tests/phpunit/tests/widgets/isRegisteredSidebar.php @@ -0,0 +1,107 @@ +original_sidebars = $wp_registered_sidebars; + $this->original_widgets = $wp_registered_widgets; + + $wp_registered_sidebars = array(); + $wp_registered_widgets = array(); + } + + public function tear_down() { + global $wp_registered_sidebars, $wp_registered_widgets; + + $wp_registered_sidebars = $this->original_sidebars; + $wp_registered_widgets = $this->original_widgets; + + parent::tear_down(); + } + + /** + * Tests that a registered sidebar is found. + * + * @ticket 66502 + */ + public function test_should_return_true_for_a_registered_sidebar() { + register_sidebar( array( 'id' => 'registered-sidebar' ) ); + + $this->assertTrue( is_registered_sidebar( 'registered-sidebar' ) ); + } + + /** + * Tests that a sidebar that is not registered is not found. + * + * @ticket 66502 + */ + public function test_should_return_false_for_a_sidebar_that_is_not_registered() { + register_sidebar( array( 'id' => 'registered-sidebar' ) ); + + $this->assertFalse( is_registered_sidebar( 'another-sidebar' ) ); + } + + /** + * Tests that no sidebar is found when none is registered. + * + * @ticket 66502 + */ + public function test_should_return_false_when_no_sidebar_is_registered() { + $this->assertFalse( is_registered_sidebar( 'registered-sidebar' ) ); + } + + /** + * Tests that a sidebar is not found any more after it is unregistered. + * + * @ticket 66502 + */ + public function test_should_return_false_after_the_sidebar_is_unregistered() { + register_sidebar( array( 'id' => 'temporary-sidebar' ) ); + + $this->assertTrue( is_registered_sidebar( 'temporary-sidebar' ), 'The sidebar should be registered.' ); + + unregister_sidebar( 'temporary-sidebar' ); + + $this->assertFalse( is_registered_sidebar( 'temporary-sidebar' ), 'The sidebar should not be registered after it is unregistered.' ); + } + + /** + * Tests that the ID of a sidebar is matched exactly, and that a numeric ID is accepted. + * + * @ticket 66502 + */ + public function test_should_match_the_id_exactly() { + register_sidebar( array( 'id' => 'Sidebar-One' ) ); + register_sidebar( array( 'id' => 5 ) ); + + $this->assertTrue( is_registered_sidebar( 'Sidebar-One' ), 'The exact ID should be found.' ); + $this->assertFalse( is_registered_sidebar( 'sidebar-one' ), 'The ID should be case sensitive.' ); + $this->assertTrue( is_registered_sidebar( 5 ), 'A numeric ID should be found.' ); + $this->assertTrue( is_registered_sidebar( '5' ), 'A numeric ID given as a string should be found.' ); + } +} diff --git a/tests/phpunit/tests/widgets/wpGetSidebar.php b/tests/phpunit/tests/widgets/wpGetSidebar.php new file mode 100644 index 0000000000000..c69c66fc6504e --- /dev/null +++ b/tests/phpunit/tests/widgets/wpGetSidebar.php @@ -0,0 +1,158 @@ +original_sidebars = $wp_registered_sidebars; + $this->original_widgets = $wp_registered_widgets; + + $wp_registered_sidebars = array(); + $wp_registered_widgets = array(); + } + + public function tear_down() { + global $wp_registered_sidebars, $wp_registered_widgets; + + $wp_registered_sidebars = $this->original_sidebars; + $wp_registered_widgets = $this->original_widgets; + + parent::tear_down(); + } + + /** + * Tests that the data of a registered sidebar is returned. + * + * @ticket 66502 + */ + public function test_should_return_the_registered_sidebar() { + global $wp_registered_sidebars; + + register_sidebar( + array( + 'id' => 'main-sidebar', + 'name' => 'Main sidebar', + ) + ); + + $sidebar = wp_get_sidebar( 'main-sidebar' ); + + $this->assertSame( $wp_registered_sidebars['main-sidebar'], $sidebar, 'The registered data should be returned.' ); + $this->assertSame( 'main-sidebar', $sidebar['id'], 'The ID should be the requested one.' ); + $this->assertSame( 'Main sidebar', $sidebar['name'], 'The name should be the registered one.' ); + } + + /** + * Tests that the requested sidebar is returned when there are several. + * + * @ticket 66502 + */ + public function test_should_return_the_requested_sidebar_among_several() { + register_sidebar( + array( + 'id' => 'first-sidebar', + 'name' => 'First', + ) + ); + register_sidebar( + array( + 'id' => 'second-sidebar', + 'name' => 'Second', + ) + ); + + $this->assertSame( 'First', wp_get_sidebar( 'first-sidebar' )['name'], 'The first sidebar should be returned.' ); + $this->assertSame( 'Second', wp_get_sidebar( 'second-sidebar' )['name'], 'The second sidebar should be returned.' ); + } + + /** + * Tests that the inactive widgets sidebar is returned even when it is not registered. + * + * @ticket 66502 + */ + public function test_should_return_the_inactive_widgets_sidebar_when_it_is_not_registered() { + $sidebar = wp_get_sidebar( 'wp_inactive_widgets' ); + + $this->assertSame( + array( + 'id' => 'wp_inactive_widgets', + 'name' => 'Inactive widgets', + ), + $sidebar + ); + } + + /** + * Tests that the inactive widgets sidebar that is registered is returned as it is registered. + * + * @ticket 66502 + */ + public function test_should_return_the_inactive_widgets_sidebar_that_is_registered() { + register_sidebar( + array( + 'id' => 'wp_inactive_widgets', + 'name' => 'Custom inactive name', + ) + ); + + $this->assertSame( 'Custom inactive name', wp_get_sidebar( 'wp_inactive_widgets' )['name'] ); + } + + /** + * Tests that null is returned when the sidebar is not registered. + * + * @ticket 66502 + */ + public function test_should_return_null_for_a_sidebar_that_is_not_registered() { + register_sidebar( array( 'id' => 'main-sidebar' ) ); + + $this->assertNull( wp_get_sidebar( 'unknown-sidebar' ) ); + } + + /** + * Tests that null is returned when no sidebar is registered. + * + * @ticket 66502 + */ + public function test_should_return_null_when_no_sidebar_is_registered() { + $this->assertNull( wp_get_sidebar( 'main-sidebar' ) ); + } + + /** + * Tests that the ID is compared strictly, so a sidebar is not found by an ID of another type. + * + * @ticket 66502 + */ + public function test_should_compare_the_id_strictly() { + register_sidebar( array( 'id' => 'main-sidebar' ) ); + register_sidebar( array( 'id' => '5' ) ); + + $this->assertNull( wp_get_sidebar( 'MAIN-SIDEBAR' ), 'The ID should be case sensitive.' ); + $this->assertNull( wp_get_sidebar( 5 ), 'An integer should not match the sidebar with the same ID as a string.' ); + $this->assertSame( '5', wp_get_sidebar( '5' )['id'], 'The ID as a string should match.' ); + } +} diff --git a/tests/phpunit/tests/widgets/wpGetWidgetDefaults.php b/tests/phpunit/tests/widgets/wpGetWidgetDefaults.php new file mode 100644 index 0000000000000..56737bbc44af6 --- /dev/null +++ b/tests/phpunit/tests/widgets/wpGetWidgetDefaults.php @@ -0,0 +1,99 @@ +original_sidebars = $wp_registered_sidebars; + $this->original_widgets = $wp_registered_widgets; + + $wp_registered_sidebars = array(); + $wp_registered_widgets = array(); + } + + public function tear_down() { + global $wp_registered_sidebars, $wp_registered_widgets; + + $wp_registered_sidebars = $this->original_sidebars; + $wp_registered_widgets = $this->original_widgets; + + parent::tear_down(); + } + + /** + * Tests that an empty list of widgets is returned for each registered sidebar. + * + * @ticket 66502 + */ + public function test_should_return_an_empty_list_for_each_registered_sidebar() { + register_sidebar( array( 'id' => 'first-sidebar' ) ); + register_sidebar( array( 'id' => 'second-sidebar' ) ); + + $this->assertSame( + array( + 'first-sidebar' => array(), + 'second-sidebar' => array(), + ), + wp_get_widget_defaults() + ); + } + + /** + * Tests that the sidebars are returned in the order they are registered. + * + * @ticket 66502 + */ + public function test_should_return_the_sidebars_in_the_order_they_are_registered() { + register_sidebar( array( 'id' => 'sidebar-b' ) ); + register_sidebar( array( 'id' => 'sidebar-a' ) ); + register_sidebar( array( 'id' => 'sidebar-c' ) ); + + $this->assertSame( array( 'sidebar-b', 'sidebar-a', 'sidebar-c' ), array_keys( wp_get_widget_defaults() ) ); + } + + /** + * Tests that no defaults are returned when no sidebar is registered. + * + * @ticket 66502 + */ + public function test_should_return_an_empty_array_when_no_sidebar_is_registered() { + $this->assertSame( array(), wp_get_widget_defaults() ); + } + + /** + * Tests that the widgets assigned to the sidebars do not change the defaults. + * + * @ticket 66502 + */ + public function test_should_not_include_the_widgets_that_are_assigned_to_the_sidebars() { + register_sidebar( array( 'id' => 'main-sidebar' ) ); + + wp_set_sidebars_widgets( array( 'main-sidebar' => array( 'search-2', 'text-3' ) ) ); + + $this->assertSame( array( 'main-sidebar' => array() ), wp_get_widget_defaults() ); + } +} diff --git a/tests/phpunit/tests/widgets/wpSidebarDescription.php b/tests/phpunit/tests/widgets/wpSidebarDescription.php new file mode 100644 index 0000000000000..7e82b63aebb1f --- /dev/null +++ b/tests/phpunit/tests/widgets/wpSidebarDescription.php @@ -0,0 +1,168 @@ +original_sidebars = $wp_registered_sidebars; + $this->original_widgets = $wp_registered_widgets; + + $wp_registered_sidebars = array(); + $wp_registered_widgets = array(); + } + + public function tear_down() { + global $wp_registered_sidebars, $wp_registered_widgets; + + $wp_registered_sidebars = $this->original_sidebars; + $wp_registered_widgets = $this->original_widgets; + + parent::tear_down(); + } + + /** + * Registers a sidebar with a description. + * + * @param string $id ID of the sidebar. + * @param string $description Description of the sidebar. + */ + private function register_sidebar_with_description( $id, $description ) { + register_sidebar( + array( + 'id' => $id, + 'description' => $description, + ) + ); + } + + /** + * Tests that the description of the sidebar is returned. + * + * @ticket 66502 + */ + public function test_should_return_the_description_of_the_sidebar() { + $this->register_sidebar_with_description( 'described-sidebar', 'Shown on the right.' ); + + $this->assertSame( 'Shown on the right.', wp_sidebar_description( 'described-sidebar' ) ); + } + + /** + * Tests that the markup and the attributes that are not allowed are removed from the description. + * + * @ticket 66502 + */ + public function test_should_remove_the_markup_and_attributes_that_are_not_allowed() { + $this->register_sidebar_with_description( 'markup-sidebar', 'Link ' ); + + $this->assertSame( 'Link ', wp_sidebar_description( 'markup-sidebar' ) ); + } + + /** + * Tests that the markup that the `wp_kses_allowed_html` filter allows for the context of the description is kept. + * + * @ticket 66502 + */ + public function test_should_keep_the_markup_that_the_filter_allows_for_the_description() { + add_filter( + 'wp_kses_allowed_html', + static function ( $tags, $context ) { + if ( 'sidebar_description' === $context ) { + return array( 'b' => array() ); + } + + return $tags; + }, + 10, + 2 + ); + + $this->register_sidebar_with_description( 'markup-sidebar', 'Bold italic' ); + + $this->assertSame( 'Bold italic', wp_sidebar_description( 'markup-sidebar' ) ); + } + + /** + * Tests that only the description of the requested sidebar is returned. + * + * @ticket 66502 + */ + public function test_should_return_the_description_of_the_requested_sidebar() { + $this->register_sidebar_with_description( 'first-sidebar', 'First description' ); + $this->register_sidebar_with_description( 'second-sidebar', 'Second description' ); + + $this->assertSame( 'First description', wp_sidebar_description( 'first-sidebar' ), 'The description of the first sidebar should be returned.' ); + $this->assertSame( 'Second description', wp_sidebar_description( 'second-sidebar' ), 'The description of the second sidebar should be returned.' ); + } + + /** + * Tests that the description of a sidebar registered without one is an empty string. + * + * @ticket 66502 + */ + public function test_should_return_an_empty_string_for_a_sidebar_without_a_description() { + register_sidebar( array( 'id' => 'plain-sidebar' ) ); + + $this->assertSame( '', wp_sidebar_description( 'plain-sidebar' ) ); + } + + /** + * Tests that nothing is returned when the sidebar is not registered. + * + * @ticket 66502 + */ + public function test_should_return_null_for_a_sidebar_that_is_not_registered() { + $this->assertNull( wp_sidebar_description( 'unknown-sidebar' ) ); + } + + /** + * Tests that nothing is returned when the ID is not a scalar. + * + * @ticket 66502 + * + * @dataProvider data_ids_that_are_not_scalar + * + * @param mixed $id ID that is not a scalar. + */ + public function test_should_return_null_when_the_id_is_not_a_scalar( $id ) { + $this->register_sidebar_with_description( 'described-sidebar', 'Shown on the right.' ); + + $this->assertNull( wp_sidebar_description( $id ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_ids_that_are_not_scalar() { + return array( + 'array' => array( array( 'described-sidebar' ) ), + 'null' => array( null ), + 'object' => array( new stdClass() ), + ); + } +} diff --git a/tests/phpunit/tests/widgets/wpWidgetDescription.php b/tests/phpunit/tests/widgets/wpWidgetDescription.php new file mode 100644 index 0000000000000..93cc7841da7dd --- /dev/null +++ b/tests/phpunit/tests/widgets/wpWidgetDescription.php @@ -0,0 +1,147 @@ +original_sidebars = $wp_registered_sidebars; + $this->original_widgets = $wp_registered_widgets; + + $wp_registered_sidebars = array(); + $wp_registered_widgets = array(); + } + + public function tear_down() { + global $wp_registered_sidebars, $wp_registered_widgets; + + $wp_registered_sidebars = $this->original_sidebars; + $wp_registered_widgets = $this->original_widgets; + + parent::tear_down(); + } + + /** + * Registers a widget with a description. + * + * @param string $id ID of the widget. + * @param mixed $description Description of the widget. + */ + private function register_widget_with_description( $id, $description ) { + wp_register_sidebar_widget( + $id, + 'Widget ' . $id, + '__return_empty_string', + array( 'description' => $description ) + ); + } + + /** + * Tests that the description of the widget is returned. + * + * @ticket 66502 + */ + public function test_should_return_the_description_of_the_widget() { + $this->register_widget_with_description( 'described-widget', 'Shows the latest posts.' ); + + $this->assertSame( 'Shows the latest posts.', wp_widget_description( 'described-widget' ) ); + } + + /** + * Tests that the description of the widget is escaped for HTML. + * + * @ticket 66502 + */ + public function test_should_escape_the_description_of_the_widget() { + $this->register_widget_with_description( 'markup-widget', 'Bold & "quoted"' ); + + $this->assertSame( '<b>Bold</b> & "quoted"', wp_widget_description( 'markup-widget' ) ); + } + + /** + * Tests that only the description of the requested widget is returned. + * + * @ticket 66502 + */ + public function test_should_return_the_description_of_the_requested_widget() { + $this->register_widget_with_description( 'first-widget', 'First description' ); + $this->register_widget_with_description( 'second-widget', 'Second description' ); + + $this->assertSame( 'First description', wp_widget_description( 'first-widget' ), 'The description of the first widget should be returned.' ); + $this->assertSame( 'Second description', wp_widget_description( 'second-widget' ), 'The description of the second widget should be returned.' ); + } + + /** + * Tests that an empty description is returned as an empty string. + * + * @ticket 66502 + */ + public function test_should_return_an_empty_string_for_an_empty_description() { + $this->register_widget_with_description( 'empty-widget', '' ); + + $this->assertSame( '', wp_widget_description( 'empty-widget' ) ); + } + + /** + * Tests that nothing is returned when the widget is not registered, or has no description. + * + * @ticket 66502 + */ + public function test_should_return_null_when_there_is_no_description() { + wp_register_sidebar_widget( 'plain-widget', 'Plain widget', '__return_empty_string' ); + + $this->assertNull( wp_widget_description( 'plain-widget' ), 'A widget without a description should return null.' ); + $this->assertNull( wp_widget_description( 'unknown-widget' ), 'A widget that is not registered should return null.' ); + } + + /** + * Tests that nothing is returned when the ID is not a scalar. + * + * @ticket 66502 + * + * @dataProvider data_ids_that_are_not_scalar + * + * @param mixed $id ID that is not a scalar. + */ + public function test_should_return_null_when_the_id_is_not_a_scalar( $id ) { + $this->register_widget_with_description( 'described-widget', 'Shows the latest posts.' ); + + $this->assertNull( wp_widget_description( $id ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_ids_that_are_not_scalar() { + return array( + 'array' => array( array( 'described-widget' ) ), + 'null' => array( null ), + 'object' => array( new stdClass() ), + ); + } +}