Any ideas how to make unit test read the theme functions.php?

I have performed a unit testing for a plugin that works perfectly until the section where I need to activate the theme and check if the theme is supplying the right hooks data to the plugin.

Inside the plugin that I’m testing, I have apply_filters that will implement the add_filter hooks defined inside the theme functions.php.

This is my unit testing code:

function test_theme_hooks() {       

//Define required plugins for testing
$requiredplugins=array( '0' => 'myplugin1/plugin1.php','1' => 'myplugin2/plugin2.php');

//Activate plugins
update_option('active_plugins',$requiredplugins);       

//Activate theme    
switch_theme('my_theme_to_test', 'my_theme_to_test');

//Get groups defined by the theme
$Class_My_Plugin = new Class_My_Plugin;

    $groups_definedfrom_theme = $Class_My_Plugin->get_groups();

    $correct_groups=array('0'=>'Some array');


    $this->assertEqual($correct_groups,$groups_definedfrom_theme);

}

The problem is that, it seems that the theme functions.php is not read or executed during the test. I have manually checked that the theme has been successfully switched/enabled as well as the required plugins. But the result of $groups_definedfrom_theme is NULL which is not correct.

Any tips how to do this test correctly or how to make the unit test read the active theme functions.php as well as well as it’s filters?

Thanks for any help.

1 Answer
1

The only thing switch_theme() does is to set an option in the database that says which theme is active. So the theme files will not be included by using that.

You need to include the files yourself, either inside the test function, in thesetUp()function or thesetUpBeforeClass()function of your test class. Or at the top of the file outside the class.

If you want wordpress to load it you could also set the active theme in $GLOBALS['wp_tests_options'] in your bootstrap.php file, but that would force activate that theme for the whole unit test suite and that may or may not be what you desire.

Leave a Comment