2018-04-17 21:03:14 +00:00
< ? php
/**
2020-06-30 15:02:28 +00:00
* WooCommerce Admin Unit Tests Bootstrap
2018-04-17 21:03:14 +00:00
*
2020-08-11 19:18:47 +00:00
* @ package WooCommerce\Admin\Tests
2018-04-17 21:03:14 +00:00
*/
2021-07-22 22:09:57 +00:00
use Automattic\WooCommerce\Proxies\LegacyProxy ;
use Automattic\WooCommerce\Testing\Tools\DependencyManagement\MockableLegacyProxy ;
2020-06-30 15:02:28 +00:00
/**
* Class WC_Admin_Unit_Tests_Bootstrap
*/
class WC_Admin_Unit_Tests_Bootstrap {
2018-04-17 21:03:14 +00:00
2020-06-30 15:02:28 +00:00
/** @var WC_Admin_Unit_Tests_Bootstrap instance */
protected static $instance = null ;
2018-04-17 21:03:14 +00:00
2020-06-30 15:02:28 +00:00
/** @var string directory where wordpress-tests-lib is installed */
public $wp_tests_dir ;
2018-04-17 21:03:14 +00:00
2020-06-30 15:02:28 +00:00
/** @var string testing directory */
public $tests_dir ;
2018-04-17 21:03:14 +00:00
2020-06-30 15:02:28 +00:00
/** @var string plugin directory */
public $plugin_dir ;
2018-09-20 14:20:04 +00:00
2020-06-30 15:02:28 +00:00
/** @var string WC core directory */
public $wc_core_dir ;
2018-11-02 10:31:41 +00:00
2020-06-30 15:02:28 +00:00
/**
* Setup the unit testing environment .
*/
public function __construct () {
ini_set ( 'display_errors' , 'on' ); // phpcs:ignore WordPress.PHP.IniSet.display_errors_Blacklisted
error_reporting ( E_ALL ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.prevent_path_disclosure_error_reporting, WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_error_reporting
2018-11-02 10:31:41 +00:00
2020-12-17 13:02:10 +00:00
// Ensure theme install tests use direct filesystem method.
define ( 'FS_METHOD' , 'direct' );
2020-06-30 15:02:28 +00:00
// Ensure server variable is set for WP email functions.
// phpcs:disable WordPress.VIP.SuperGlobalInputUsage.AccessDetected
if ( ! isset ( $_SERVER [ 'SERVER_NAME' ] ) ) {
$_SERVER [ 'SERVER_NAME' ] = 'localhost' ;
}
// phpcs:enable WordPress.VIP.SuperGlobalInputUsage.AccessDetected
2018-11-02 10:31:41 +00:00
2020-06-30 15:02:28 +00:00
$this -> tests_dir = dirname ( __FILE__ );
$this -> plugin_dir = dirname ( $this -> tests_dir );
2020-12-17 13:02:10 +00:00
$this -> wc_core_dir = getenv ( 'WC_CORE_DIR' ) ? getenv ( 'WC_CORE_DIR' ) : dirname ( $this -> plugin_dir ) . '/woocommerce' ;
2020-06-30 15:02:28 +00:00
$this -> wp_tests_dir = getenv ( 'WP_TESTS_DIR' ) ? getenv ( 'WP_TESTS_DIR' ) : rtrim ( sys_get_temp_dir (), '/\\' ) . '/wordpress-tests-lib' ;
2018-11-02 10:31:41 +00:00
2020-06-30 15:02:28 +00:00
$wc_tests_framework_base_dir = $this -> wc_core_dir . '/tests' ;
2020-11-11 20:30:27 +00:00
2020-06-30 15:02:28 +00:00
if ( ! is_dir ( $wc_tests_framework_base_dir . '/framework' ) ) {
$wc_tests_framework_base_dir .= '/legacy' ;
}
$this -> wc_core_tests_dir = $wc_tests_framework_base_dir ;
2018-09-20 14:20:04 +00:00
2020-06-30 15:02:28 +00:00
// load test function so tests_add_filter() is available.
require_once $this -> wp_tests_dir . '/includes/functions.php' ;
2018-09-20 14:20:04 +00:00
2020-06-30 15:02:28 +00:00
// load WC.
tests_add_filter ( 'muplugins_loaded' , array ( $this , 'load_wc' ) );
2019-06-27 16:46:54 +00:00
2020-06-30 15:02:28 +00:00
// install WC.
tests_add_filter ( 'setup_theme' , array ( $this , 'install_wc' ) );
2018-09-24 15:49:21 +00:00
2020-06-30 15:02:28 +00:00
// Set up WC-Admin config.
2020-07-06 17:39:58 +00:00
tests_add_filter ( 'woocommerce_admin_get_feature_config' , array ( $this , 'add_development_features' ) );
2018-11-02 10:31:41 +00:00
2021-09-23 10:01:52 +00:00
/*
* Load PHPUnit Polyfills for the WP testing suite .
* @ see https :// github . com / WordPress / wordpress - develop / pull / 1563 /
*/
define ( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' , __DIR__ . '/../vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php' );
2020-06-30 15:02:28 +00:00
// load the WP testing environment.
require_once $this -> wp_tests_dir . '/includes/bootstrap.php' ;
2018-04-17 21:03:14 +00:00
2020-06-30 15:02:28 +00:00
// load WC testing framework.
$this -> includes ();
2021-07-22 22:09:57 +00:00
// replace LegacyProxy class to MockableLegacyProxy from WC container.
$this -> replace_legacy_proxy ();
2020-06-30 15:02:28 +00:00
}
2018-09-20 14:20:04 +00:00
2020-06-30 15:02:28 +00:00
/**
* Load WooCommerce Admin .
*/
public function load_wc () {
define ( 'WC_TAX_ROUNDING_MODE' , 'auto' );
define ( 'WC_USE_TRANSACTIONS' , false );
update_option ( 'woocommerce_enable_coupons' , 'yes' );
update_option ( 'woocommerce_calc_taxes' , 'yes' );
update_option ( 'woocommerce_onboarding_opt_in' , 'yes' );
require_once $this -> wc_core_dir . '/woocommerce.php' ;
require $this -> plugin_dir . '/vendor/autoload.php' ;
require $this -> plugin_dir . '/woocommerce-admin.php' ;
}
2018-09-20 14:20:04 +00:00
2020-06-30 15:02:28 +00:00
/**
* Install WooCommerce after the test environment and WC have been loaded .
*/
public function install_wc () {
// Clean existing install first.
define ( 'WP_UNINSTALL_PLUGIN' , true );
define ( 'WC_REMOVE_ALL_DATA' , true );
include $this -> plugin_dir . '/uninstall.php' ;
WC_Install :: install ();
// Initialize the WC API extensions.
\Automattic\WooCommerce\Admin\Install :: create_tables ();
\Automattic\WooCommerce\Admin\Install :: create_events ();
// Reload capabilities after install, see https://core.trac.wordpress.org/ticket/28374.
if ( version_compare ( $GLOBALS [ 'wp_version' ], '4.7' , '<' ) ) {
$GLOBALS [ 'wp_roles' ] -> reinit ();
} else {
$GLOBALS [ 'wp_roles' ] = null ; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
wp_roles ();
}
echo esc_html ( 'Installing WooCommerce and WooCommerce Admin...' . PHP_EOL );
}
2019-02-12 20:02:02 +00:00
2020-06-30 15:02:28 +00:00
/**
* Load WC - specific test cases and factories .
*/
public function includes () {
// WooCommerce test classes.
$wc_tests_framework_base_dir = $this -> wc_core_tests_dir ;
// Framework.
require_once $wc_tests_framework_base_dir . '/framework/class-wc-unit-test-factory.php' ;
require_once $wc_tests_framework_base_dir . '/framework/class-wc-mock-session-handler.php' ;
require_once $wc_tests_framework_base_dir . '/framework/class-wc-mock-wc-data.php' ;
require_once $wc_tests_framework_base_dir . '/framework/class-wc-mock-wc-object-query.php' ;
require_once $wc_tests_framework_base_dir . '/framework/class-wc-mock-payment-gateway.php' ;
2021-06-14 14:23:04 +00:00
require_once $this -> tests_dir . '/framework/class-wc-mock-enhanced-payment-gateway.php' ;
2020-06-30 15:02:28 +00:00
require_once $wc_tests_framework_base_dir . '/framework/class-wc-payment-token-stub.php' ;
require_once $wc_tests_framework_base_dir . '/framework/vendor/class-wp-test-spy-rest-server.php' ;
// Test cases.
require_once $wc_tests_framework_base_dir . '/includes/wp-http-testcase.php' ;
require_once $wc_tests_framework_base_dir . '/framework/class-wc-unit-test-case.php' ;
require_once $wc_tests_framework_base_dir . '/framework/class-wc-api-unit-test-case.php' ;
require_once $wc_tests_framework_base_dir . '/framework/class-wc-rest-unit-test-case.php' ;
// Helpers.
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-product.php' ;
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-coupon.php' ;
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-fee.php' ;
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-shipping.php' ;
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-customer.php' ;
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-order.php' ;
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-shipping-zones.php' ;
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-payment-token.php' ;
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-settings.php' ;
// Include wc-admin helpers.
require_once $this -> tests_dir . '/framework/helpers/class-wc-helper-reports.php' ;
require_once $this -> tests_dir . '/framework/helpers/class-wc-helper-admin-notes.php' ;
require_once $this -> tests_dir . '/framework/helpers/class-wc-test-action-queue.php' ;
require_once $this -> tests_dir . '/framework/helpers/class-wc-helper-queue.php' ;
}
/**
* Use the `development` features for testing .
2020-07-06 17:39:58 +00:00
*
* @ param array $flags Existing feature flags .
* @ return array Filtered feature flags .
2020-06-30 15:02:28 +00:00
*/
2020-07-06 17:39:58 +00:00
public function add_development_features ( $flags ) {
2020-06-30 15:02:28 +00:00
$config = json_decode ( file_get_contents ( $this -> plugin_dir . '/config/development.json' ) ); // @codingStandardsIgnoreLine.
foreach ( $config -> features as $feature => $bool ) {
$flags [ $feature ] = $bool ;
}
return $flags ;
}
/**
* Get the single class instance .
* @ return WC_Admin_Unit_Tests_Bootstrap
*/
public static function instance () {
if ( is_null ( self :: $instance ) ) {
self :: $instance = new self ();
}
return self :: $instance ;
2019-02-12 20:02:02 +00:00
}
2021-07-22 22:09:57 +00:00
/**
* Replace LegacyProxy to MockableLegacyProxy from the WC container .
*
* @ throws \Exception Thrown when reflection fails .
*/
private function replace_legacy_proxy () {
try {
$inner_container_property = new \ReflectionProperty ( \Automattic\WooCommerce\Container :: class , 'container' );
} catch ( ReflectionException $ex ) {
throw new \Exception ( " Error when trying to get the private 'container' property from the " . \Automattic\WooCommerce\Container :: class . ' class using reflection during unit testing bootstrap, has the property been removed or renamed?' );
}
$inner_container_property -> setAccessible ( true );
$inner_container = $inner_container_property -> getValue ( wc_get_container () );
$inner_container -> replace ( LegacyProxy :: class , MockableLegacyProxy :: class );
$inner_container -> reset_all_resolved ();
$GLOBALS [ 'wc_container' ] = $inner_container ;
}
2019-02-12 20:02:02 +00:00
}
2020-06-30 15:02:28 +00:00
WC_Admin_Unit_Tests_Bootstrap :: instance ();