widgets = array();
parent::clean_up_global_scope();
}
function tearDown() {
global $wp_customize;
$wp_customize = null;
parent::tearDown();
}
/**
* @see register_widget()
* @see unregister_widget()
*/
function test_register_and_unregister_widget_core_widget() {
global $wp_widget_factory;
$widget_class = 'WP_Widget_Search';
register_widget( $widget_class );
$this->assertArrayHasKey( $widget_class, $wp_widget_factory->widgets );
unregister_widget( $widget_class );
$this->assertArrayNotHasKey( $widget_class, $wp_widget_factory->widgets );
}
/**
* Test that registering a widget class and registering a widget instance work together.
*
* @see register_widget()
* @see unregister_widget()
* @ticket 28216
*/
function test_register_and_unregister_widget_instance() {
global $wp_widget_factory, $wp_registered_widgets;
$this->assertEmpty( $wp_widget_factory->widgets );
$this->assertEmpty( $wp_registered_widgets );
update_option( 'widget_search', array(
2 => array( 'title' => '' ),
'_multiwidget' => 1,
) );
update_option( 'widget_better_search', array(
3 => array( 'title' => '' ),
'_multiwidget' => 1,
) );
update_option( 'widget_best_search', array(
4 => array( 'title' => '' ),
'_multiwidget' => 1,
) );
register_widget( 'WP_Widget_Search' );
$this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
$widget_better_search = new WP_Widget_Search();
$widget_better_search->id_base = 'better_search';
$widget_better_search->name = 'Better Search';
$widget_better_search->option_name = 'widget_' . $widget_better_search->id_base;
$widget_better_search->widget_options['classname'] = 'widget_' . $widget_better_search->id_base;
$widget_better_search->control_options['id_base'] = $widget_better_search->id_base;
register_widget( $widget_better_search );
$this->assertContains( $widget_better_search, $wp_widget_factory->widgets );
$widget_best_search = new WP_Widget_Search();
$widget_best_search->id_base = 'best_search';
$widget_best_search->name = 'Best Search';
$widget_best_search->option_name = 'widget_' . $widget_best_search->id_base;
$widget_best_search->widget_options['classname'] = 'widget_' . $widget_best_search->id_base;
$widget_best_search->control_options['id_base'] = $widget_best_search->id_base;
register_widget( $widget_best_search );
$this->assertContains( $widget_best_search, $wp_widget_factory->widgets );
$this->assertCount( 3, $wp_widget_factory->widgets );
$this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
$this->assertContains( $widget_better_search, $wp_widget_factory->widgets );
$this->assertContains( $widget_best_search, $wp_widget_factory->widgets );
$wp_widget_factory->_register_widgets();
$this->assertArrayHasKey( 'search-2', $wp_registered_widgets );
$this->assertArrayHasKey( 'better_search-3', $wp_registered_widgets );
$this->assertArrayHasKey( 'best_search-4', $wp_registered_widgets );
$this->assertInstanceOf( 'WP_Widget_Search', $wp_registered_widgets['search-2']['callback'][0] );
$this->assertSame( $widget_better_search, $wp_registered_widgets['better_search-3']['callback'][0] );
$this->assertSame( $widget_best_search, $wp_registered_widgets['best_search-4']['callback'][0] );
$this->assertContains( $widget_better_search, $wp_widget_factory->widgets );
$this->assertContains( $widget_best_search, $wp_widget_factory->widgets );
$this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
unregister_widget( 'WP_Widget_Search' );
unregister_widget( $widget_better_search );
unregister_widget( $widget_best_search );
$this->assertNotContains( $widget_better_search, $wp_widget_factory->widgets );
$this->assertNotContains( $widget_best_search, $wp_widget_factory->widgets );
$this->assertArrayNotHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
}
/**
* @group sidebar
*/
function test_register_sidebars_single() {
global $wp_registered_sidebars;
register_sidebars( 1, array( 'id' => 'wp-unit-test' ) );
$this->assertTrue( isset( $wp_registered_sidebars['wp-unit-test'] ) );
}
/**
* @group sidebar
*/
function test_register_sidebars_multiple() {
global $wp_registered_sidebars;
$result = array();
$num = 3;
$id_base = 'WP Unit Test';
register_sidebars( $num, array( 'name' => $id_base . ' %d' ) );
$names = wp_list_pluck( $wp_registered_sidebars, 'name' );
for ( $i = 1; $i <= $num; $i++ ) {
if ( in_array( "$id_base $i", $names ) ) {
$result[] = true;
}
}
$this->assertEquals( $num, count( $result ) );
}
/**
* @group sidebar
*/
function test_register_sidebar_with_no_id() {
global $wp_registered_sidebars;
$this->setExpectedIncorrectUsage( 'register_sidebar' );
// Incorrectly register a couple of sidebars for fun.
register_sidebar();
register_sidebar();
$derived_sidebar_id = "sidebar-2"; // Number of sidebars in the global + 1.
$this->assertArrayHasKey( $derived_sidebar_id, $wp_registered_sidebars );
}
/**
* @group sidebar
*/
function test_unregister_sidebar_registered_with_no_id() {
global $wp_registered_sidebars;
$this->setExpectedIncorrectUsage( 'register_sidebar' );
// Incorrectly register a couple of sidebars for fun.
register_sidebar();
register_sidebar();
$derived_sidebar_id = "sidebar-2"; // Number of sidebars in the global + 1.
unregister_sidebar( $derived_sidebar_id );
$this->assertArrayNotHasKey( $derived_sidebar_id, $wp_registered_sidebars );
}
/**
* @group sidebar
*/
function test_register_sidebar_with_string_id() {
global $wp_registered_sidebars;
$sidebar_id = 'wp-unit-test';
register_sidebar( array( 'id' => $sidebar_id ) );
$this->assertArrayHasKey( $sidebar_id, $wp_registered_sidebars );
}
/**
* @group sidebar
*/
function test_unregister_sidebar_with_string_id() {
global $wp_registered_sidebars;
$sidebar_id = 'wp-unit-tests';
register_sidebar( array( 'id' => $sidebar_id ) );
unregister_sidebar( $sidebar_id );
$this->assertArrayNotHasKey( $sidebar_id, $wp_registered_sidebars );
}
/**
* @group sidebar
*/
function test_register_sidebar_with_numeric_id() {
global $wp_registered_sidebars;
$sidebar_id = 2;
register_sidebar( array( 'id' => $sidebar_id ) );
$this->assertArrayHasKey( $sidebar_id, $wp_registered_sidebars );
}
/**
* @group sidebar
*/
function test_unregister_sidebar_with_numeric_id() {
global $wp_registered_sidebars;
$sidebar_id = 2;
register_sidebar( array( 'id' => $sidebar_id ) );
unregister_sidebar( $sidebar_id );
$this->assertArrayNotHasKey( $sidebar_id, $wp_registered_sidebars );
}
/**
* Utility hook callback used to store a sidebar ID mid-function.
*/
function retrieve_sidebar_id( $index, $valid_sidebar ) {
$this->sidebar_index = $index;
$this->valid_sidebar = $valid_sidebar;
}
/**
* @group sidebar
*/
function test_dynamic_sidebar_using_sidebar_registered_with_no_id() {
$this->setExpectedIncorrectUsage( 'register_sidebar' );
// Incorrectly register a couple of sidebars for fun.
register_sidebar();
register_sidebar();
$derived_sidebar_id = "sidebar-2"; // Number of sidebars in the global + 1.
add_action( 'dynamic_sidebar_before', array( $this, 'retrieve_sidebar_id' ), 10, 2 );
dynamic_sidebar( 2 );
$this->assertSame( $derived_sidebar_id, $this->sidebar_index );
}
/**
* @group sidebar
*/
function test_dynamic_sidebar_using_invalid_sidebar_id() {
register_sidebar( array( 'id' => 'wp-unit-text' ) );
add_action( 'dynamic_sidebar_before', array( $this, 'retrieve_sidebar_id' ), 10, 2 );
// 5 is a fake sidebar ID.
dynamic_sidebar( 5 );
/*
* If the sidebar ID is invalid, the second argument passed to
* the 'dynamic_sidebar_before' hook will be false.
*/
$this->assertSame( false, $this->valid_sidebar );
}
/**
* @group sidebar
*/
function test_dynamic_sidebar_numeric_id() {
$sidebar_id = 2;
register_sidebar( array( 'id' => $sidebar_id ) );
add_action( 'dynamic_sidebar_before', array( $this, 'retrieve_sidebar_id' ), 10, 2 );
dynamic_sidebar( $sidebar_id );
$this->assertSame( "sidebar-{$sidebar_id}", $this->sidebar_index );
}
/**
* @group sidebar
*/
function test_dynamic_sidebar_string_id() {
$sidebar_id = 'wp-unit-tests';
register_sidebar( array( 'id' => $sidebar_id ) );
add_action( 'dynamic_sidebar_before', array( $this, 'retrieve_sidebar_id' ), 10, 2 );
dynamic_sidebar( $sidebar_id );
$this->assertSame( $sidebar_id, $this->sidebar_index );
}
/**
* @see WP_Widget_Search::form()
*/
function test_wp_widget_search_form() {
$widget = new WP_Widget_Search( 'foo', 'Foo' );
ob_start();
$args = array(
'before_widget' => '