2

I am trying to unit test the function I'm using in a theme to register sidebars.

Here is the function from functions.php.

function fp_register_sidebars() {
register_sidebar( array(
    'name' => __( 'Sidebar 1', 'fp' ),
    'id' => 'sidebar1',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    'before_title' => '<h4 class="widget-title">',
    'after_title' => '</h4>'
) );

register_sidebar( array(
    'name' => __( 'Sidebar 2', 'fp' ),
    'id' => 'sidebar2',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    'before_title' => '<h4 class="widget-title">',
    'after_title' => '</h4>'
) );
}

add_action( 'widgets_init', 'fp_register_sidebars' );

Here is the unit test method.

function test_fp_register_sidebars() {
    do_action( 'widgets_init' ); // Run the widgets_init hook

    /** @var array $sidebars Array containing all active sidebars and their widgets. */
    $sidebars = wp_get_sidebars_widgets();

    $this->assertTrue( array_key_exists( 'sidebar1', $sidebars ), 'Sidebar 1 is registered at the widgets_init hook.' );
    $this->assertTrue( array_key_exists( 'sidebar2', $sidebars ), 'Sidebar 2 is registered at the widgets_init_hook.' );
}

The unit test fails. Looking into this I found that the sidebars are not being updated from the sidebars for twentythirteen to the sidebars for this theme (they stay at sidebar-1 and sidebar-2). I'm switching to my theme in the unit test class setUp() method using switch_theme() and that is successful.

Activating the theme on a WordPress environment does correctly register these sidebars so I'm thinking it is something to do with how switch_theme() works.

1

First, you can (should?) register sidebars generally, there's no need for a specific hook (and widgets_init might not be the best one). Second, you don't need to specify IDs, as they are generated automatically. You can, of course, but there's also no need to.

Just put the following in your functions.php:

register_sidebar( array(
    'name' => __( 'Sidebar 1', 'fp' ),
    // 'id' => 'sidebar1',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    'before_title' => '<h4 class="widget-title">',
    'after_title' => '</h4>',
) );

register_sidebar( array(
    'name' => __( 'Sidebar 2', 'fp' ),
    // 'id' => 'sidebar2',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    'before_title' => '<h4 class="widget-title">',
    'after_title' => '</h4>',
) );

To check the current widgets for all registered sidebars (as well as the inactive etc.), just do the following:

$sidebars = wp_get_sidebars_widgets();
echo '<pre>'.print_r($sidebars, true).'</pre>';

And to check for a given sidebar ID, you can do (as you did):

if (array_key_exists('sidebar1', $sidebars))
    echo 'Sidebar 1 is registered.';

To your question regarding old sidebars. As long as there are widgets in a sidebar, they are stored (as an array) for the according sidebar ID. If you want to share sidebar-widgets settings between themes, you have to register the same sidebar( ID)s.

  • 2
    "there's no need for a specific hook", false. It's being a good citizen. And it makes your theme more extensible. "you don't need to specify IDs", you don't "need" to, but it's a good idea. Making decisions as a developer rather than letting an application make them for you seems like a good idea. – chrisguitarguy Apr 19 '13 at 16:58
  • @chrisguitarguy Well, I didn't say you must not use a hook. And you would say widgets_init is the proper one? As for the sidebar IDs, I just wanted to emphasize that you don't need IDs in general, but in this case you do need them in order to allow for sharing widgets. – tfrommen Apr 19 '13 at 17:07
  • I'm already using wp_get_sidebars_widgets() and checking the array for the sidebar ID in the unit test. Looking at their values in the debugger is what tipped me into my theme sidebars not being registered. – Christopher Lamm Apr 19 '13 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.