Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions tests/phpunit/tests/widgets/isRegisteredSidebar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/**
* Tests for the is_registered_sidebar function.
*
* @group widgets
*
* @covers ::is_registered_sidebar
*/
class Tests_Widgets_IsRegisteredSidebar extends WP_UnitTestCase {

/**
* Registered sidebars, to restore after each test.
*
* @var array
*/
private $original_sidebars;

/**
* Registered widgets, to restore after each test.
*
* @var array
*/
private $original_widgets;

public function set_up() {
parent::set_up();

global $wp_registered_sidebars, $wp_registered_widgets;

$this->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.' );
}
}
158 changes: 158 additions & 0 deletions tests/phpunit/tests/widgets/wpGetSidebar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

/**
* Tests for the wp_get_sidebar function.
*
* @group widgets
*
* @covers ::wp_get_sidebar
*/
class Tests_Widgets_WpGetSidebar extends WP_UnitTestCase {

/**
* Registered sidebars, to restore after each test.
*
* @var array
*/
private $original_sidebars;

/**
* Registered widgets, to restore after each test.
*
* @var array
*/
private $original_widgets;

public function set_up() {
parent::set_up();

global $wp_registered_sidebars, $wp_registered_widgets;

$this->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.' );
}
}
99 changes: 99 additions & 0 deletions tests/phpunit/tests/widgets/wpGetWidgetDefaults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/**
* Tests for the wp_get_widget_defaults function.
*
* @group widgets
*
* @covers ::wp_get_widget_defaults
*/
class Tests_Widgets_WpGetWidgetDefaults extends WP_UnitTestCase {

/**
* Registered sidebars, to restore after each test.
*
* @var array
*/
private $original_sidebars;

/**
* Registered widgets, to restore after each test.
*
* @var array
*/
private $original_widgets;

public function set_up() {
parent::set_up();

global $wp_registered_sidebars, $wp_registered_widgets;

$this->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() );
}
}
Loading
Loading