PHPUnit test plugin activation

Actually you cannot activate your plugin using WP_UnitTestCase, because in the bootstrap.php you’ve already loaded your plugin as a mu-plugin.

What I can suggest you to test your plugin activation is: call the do_action('activate_' . FULL_ABSPATH_TO_YOUR_PLUGIN_PHP), where FULL_ABSPATH_TO_YOUR_PLUGIN_PHP will be something like: var/www/html/wordpress/wp-content/plugins/hello-world/hello-world.php

In this example the hello-world plugin revoke’s a specified user’s capabilities on activation:

class ActivationEventTest extends WP_UnitTestCase {

    const PLUGIN_BASENAME = 'var/www/html/wordpress/wp-content/plugins/hello-world/hello-world.php';

    public function testActivateWithSupport() {
        $this->factory()->user->create( [
            'user_email' => '[email protected]',
            'user_pass'  => 'reallyheavypasword',
            'user_login' => 'hello',
            'user_role'  => 4,
            'role'       => 4
        ] );

        do_action( 'activate_' . static::PLUGIN_BASENAME );

        $user = get_user_by( 'login', 'hello' );
        $this->assertEmpty( $user->caps );
        $this->assertEmpty( $user->roles );
    }
}

Leave a Comment