PHPUnit测试插件激活

时间:2016-02-24 作者:GeekDaddy

我正在尝试测试我的插件是否使用PHPUnit正确激活。我使用了在此基础上生成的样板结构site 并添加了此测试:

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 );
  }
}
但我有一个错误:

1) PluginTest::test_plugin_activation
Plugin file does not exist.
Failed asserting that WP_Error Object (...) is not an instance of class "WP_Error".
我已经手动检查,我可以通过Wordpress管理面板激活和停用插件。

2 个回复
SO网友:ocReaper

实际上,您不能使用WP_UnitTestCase, 因为在bootstrap.php 您已经将插件加载为mu-plugin.

我建议您测试插件激活:调用do_action(\'activate_\' . FULL_ABSPATH_TO_YOUR_PLUGIN_PHP), 哪里FULL_ABSPATH_TO_YOUR_PLUGIN_PHP 将类似于:var/www/html/wordpress/wp-content/plugins/hello-world/hello-world.php

在本例中hello-world 插件吊销指定用户激活时的功能:

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 );
    }
}

SO网友:J.D.

使用核心WordPress测试套件测试插件激活/安装和卸载可以更容易、更正确地完成these additional utilities. 他们主要关注的是测试卸载,但也测试激活/安装。这是其中的示例测试用例,并显示了如何测试安装和卸载:

<?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\' );
    }
}
我想这可能就是你想要实现的目标。

至于你的测试为什么会失败,我不确定。要调试它,您需要查看该错误的来源activate_plugin(); 插件文件检查的哪一部分失败了?像xdebug这样的工具对于这类事情来说是非常宝贵的。如果您没有为正在使用的PHP构建安装xdebug(您可能应该得到它),那么您必须通过查看activate_plugin() 并查看错误的来源。

相关推荐