Why Can’t PHPUnit UnitTest My WordPress Website

Unit tests for WordPress are a tricky thing. If you need to test the code with a live database, etc. I would suggest using the official WordPress test suite. I’ve gotten unit tests running for a plugin using that method before, but it wasn’t pretty, and was fairly unreliable. If you need to do it … Read more

Using wp_mail during an integration test

I was working on some tests for code that involved wp_mail() just the other day. I couldn’t find any docs on it either, but I took a look at the source of mock-mailer.php, and everything was rather simple. First, you should understand that WordPress always uses the $phpmailer global to hold an instance of the … Read more

How to create a test that calls is_front_page in phpunit?

The is_front_page() function calls wp_query::is_front_page(). If you scroll down to look at the source, you will see the code you’re looking to trigger: elseif ( ‘page’ == get_option( ‘show_on_front’) && get_option( ‘page_on_front’ ) && $this->is_page( get_option( ‘page_on_front’ ) ) ) return true; To meet that condition, you’ll just need to do this in the code … Read more

Writing a unit test for add_menu_page

Rather than worrying about the internal implementation of the menu code and the globals that it uses (there are several beside $menu), I’d use a function like menu_page_url() to check if the page exists. That function will return the URL if the menu is registered, or an empty string if it is not. So you … Read more

PHPUnit testing WordPress Plugin

The reason the WordPress test bootstrap is loaded at the end is precisely because it loads WordPress: // Load WordPress require_once ABSPATH . ‘/wp-settings.php’; If you don’t hook your function to load your plugin to ‘muplugins_loaded’ before including the bootstrap, your plugin won’t be loaded with WordPress. In most cases that will mean that your … Read more