I'm trying to test if my plugin activates properly with PHPUnit. I have used boilerplate structure generated on this site and added this test:
class PluginTest extends WP_UnitTestCase {
function test_plugin_activation() {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$result = activate_plugin( WP_CONTENT_DIR . '/plugins/example-plugin/example-plugin.php', '', TRUE, FALSE );
$this->assertNotWPError( $result );
}
}
But I get this error:
1) PluginTest::test_plugin_activation
Plugin file does not exist.
Failed asserting that WP_Error Object (...) is not an instance of class "WP_Error".
I have checked manually and I can activate and deactivate plugin via Wordpress admin panel.
I'm trying to test if my plugin activates properly with PHPUnit. I have used boilerplate structure generated on this site and added this test:
class PluginTest extends WP_UnitTestCase {
function test_plugin_activation() {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$result = activate_plugin( WP_CONTENT_DIR . '/plugins/example-plugin/example-plugin.php', '', TRUE, FALSE );
$this->assertNotWPError( $result );
}
}
But I get this error:
1) PluginTest::test_plugin_activation
Plugin file does not exist.
Failed asserting that WP_Error Object (...) is not an instance of class "WP_Error".
I have checked manually and I can activate and deactivate plugin via Wordpress admin panel.
Share Improve this question asked Feb 24, 2016 at 12:39 GeekDaddyGeekDaddy 1254 bronze badges 1 |2 Answers
Reset to default 3Actually 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 );
}
}
Testing plugin activation/installation and uninstall with the core WordPress test suite can be done more easily and properly with these additional utilities. Their main focus is testing uninstallation, but they test activation/installation as well. This is the example test case from there, and shows how you would test install and uninstall:
<?php
/**
* Test uninstallation.
*/
/**
* Plugin uninstall test case.
*
* Be sure to add "@group uninstall", so that the test will run only as part of the
* uninstall group.
*
* @group uninstall
*/
class My_Plugin_Uninstall_Test extends WP_Plugin_Uninstall_UnitTestCase {
//
// Protected properties.
//
/**
* The full path to the main plugin file.
*
* @type string $plugin_file
*/
protected $plugin_file;
//
// Public methods.
//
/**
* Set up for the tests.
*/
public function setUp() {
// You must set the path to your plugin here.
$this->plugin_file = dirname( dirname( __FILE__ ) ) . '/myplugin.php';
// Don't forget to call the parent's setUp(), or the plugin won't get installed.
parent::setUp();
}
/**
* Test installation and uninstallation.
*/
public function test_uninstall() {
/*
* First test that the plugin installed itself properly.
*/
// Check that a database table was added.
$this->assertTableExists( $wpdb->prefix . 'myplugin_table' );
// Check that an option was added to the database.
$this->assertEquals( 'default', get_option( 'myplugin_option' ) );
/*
* Now, test that it uninstalls itself properly.
*/
// You must call this to perform uninstallation.
$this->uninstall();
// Check that the table was deleted.
$this->assertTableNotExists( $wpdb->prefix . 'myplugin_table' );
// Check that all options with a prefix was deleted.
$this->assertNoOptionsWithPrefix( 'myplugin' );
// Same for usermeta and comment meta.
$this->assertNoUserMetaWithPrefix( 'myplugin' );
$this->assertNoCommentMetaWithPrefix( 'myplugin' );
}
}
I think this is probably what you are trying to achieve.
As far why your tests are failing as they are, I'm not sure. To debug it you need to see where that error is coming from in activate_plugin()
; what part of the check for the plugin file is failing? Tools like xdebug are invaluable for this sort of thing. If you don't have xdebug installed for the PHP build that you are using (you probably should get it), you'll have to debug it manually by looking at the source code of activate_plugin()
et al. and seeing where that error is originating.
assetNotWPError()
method seems to check if the result is an\WP_Error
instance (what you can check withis_wp_error()
). I would assume that you need to check if there's an\Exception
of any kind. Aside from that, I am not sure if you set up everything properly. Including some file normally should happen in some tear up step. You might want to exchange WP core unit test stuff with BrainMonkey from @gmazzap. – kaiser Commented Feb 24, 2016 at 13:07