diff --git a/tests/bootstrap.php b/tests/bootstrap.php index fa4bf9ab5de..aebb20063c6 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -10,14 +10,13 @@ class WC_Unit_Tests_Bootstrap { protected static $instance = null; /** @var string directory where wordpress-tests-lib is installed */ - protected $wp_tests_dir; + public $wp_tests_dir; /** @var string testing directory */ - protected $tests_dir; + public $tests_dir; /** @var string plugin directory */ - protected $plugin_dir; - + public $plugin_dir; /** * Setup the unit testing environment @@ -33,36 +32,50 @@ class WC_Unit_Tests_Bootstrap { $this->plugin_dir = dirname( $this->tests_dir ); $this->wp_tests_dir = getenv( 'WP_TESTS_DIR' ) ? getenv( 'WP_TESTS_DIR' ) : $this->plugin_dir . '/tmp/wordpress-tests-lib'; + // load test function so tests_add_filter() is available + require_once( $this->wp_tests_dir . '/includes/functions.php' ); + + // load WC + tests_add_filter( 'muplugins_loaded', array( $this, 'load_wc' ) ); + + // install WC + tests_add_filter( 'setup_theme', array( $this, 'install_wc' ) ); + // load the WP testing environment require_once( $this->wp_tests_dir . '/includes/bootstrap.php' ); - $this->install_wc(); - - activate_plugin( 'woocommerce/woocommerce.php' ); - + // load WC testing framework $this->includes(); } + /** + * Load WooCommerce + * + * @since 2.2 + */ + public function load_wc() { + require_once( $this->plugin_dir . '/woocommerce.php' ); + } /** - * Install WooCommerce after the test environment has loaded, but prior - * to activating the plugin + * Install WooCommerce after the test environment and WC have been loaded * * @since 2.2 */ public function install_wc() { - echo 'Installing WooCommerce...' . PHP_EOL; - - require_once( $this->plugin_dir . '/woocommerce.php' ); + // clean existing install first + define( 'WP_UNINSTALL_PLUGIN', true ); + include( $this->plugin_dir . '/uninstall.php' ); $installer = include( $this->plugin_dir . '/includes/class-wc-install.php' ); $installer->install(); - $GLOBALS['current_user'] = new WP_User(1); - $GLOBALS['current_user']->set_role('administrator'); - } + // reload capabilities after install, see https://core.trac.wordpress.org/ticket/28374 + $GLOBALS['wp_roles']->reinit(); + echo "Installing WooCommerce..." . PHP_EOL; + } /** * Load WC-specific test cases and factories @@ -76,7 +89,6 @@ class WC_Unit_Tests_Bootstrap { require_once( $this->tests_dir . '/framework/class-wc-api-unit-test-case.php' ); } - /** * Get the single class instance *