@@ -30,3 +30,61 @@ function _manually_load_plugin() {
3030// Start up the WP testing environment.
3131require $ _tests_dir . '/includes/bootstrap.php ' ;
3232require __DIR__ . '/testcase.php ' ;
33+
34+ /*
35+ * Automatically activate WooCommerce in the test environment.
36+ *
37+ * If WooCommerce cannot be activated, an error message will be thrown and the test execution
38+ * halted, with a non-zero exit code.
39+ */
40+ $ activated = activate_plugin ( 'woocommerce/woocommerce.php ' );
41+
42+ // If the issue is that the plugin isn't installed, attempt to install it.
43+ if ( is_wp_error ( $ activated ) && 'plugin_not_found ' === $ activated ->get_error_code () ) {
44+ include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php ' ;
45+
46+ echo PHP_EOL . "WooCommerce is not currently installed in the test environment, attempting to install... " ;
47+
48+ // Retrieve information about WooCommerce.
49+ $ plugin_data = wp_remote_get ( 'https://api.wordpress.org/plugins/info/1.0/woocommerce.json ' );
50+
51+ if ( ! is_wp_error ( $ plugin_data ) ) {
52+ $ plugin_data = json_decode ( wp_remote_retrieve_body ( $ plugin_data ) );
53+ $ plugin_url = $ plugin_data ->download_link ;
54+ } else {
55+ $ plugin_url = false ;
56+ }
57+
58+ // Download the plugin from the WordPress.org repository.
59+ $ upgrader = new Plugin_Upgrader ( new Automatic_Upgrader_Skin () );
60+ $ installed = $ upgrader ->install ( $ plugin_url );
61+
62+ if ( true === $ installed ) {
63+ echo "\033[0;32mOK \033[0;m " . PHP_EOL . PHP_EOL ;
64+ } else {
65+ echo "\033[0;31mFAIL \033[0;m " . PHP_EOL ;
66+
67+ if ( is_wp_error ( $ installed ) ) {
68+ printf ( 'Unable to install WooCommerce: %s. ' , $ installed ->get_error_message () );
69+ }
70+
71+ printf (
72+ 'Please download and install WooCommerce into %s ' . PHP_EOL ,
73+ trailingslashit (dirname ( dirname ( $ _tests_dir ) ) )
74+ );
75+
76+ exit ( 1 );
77+ }
78+
79+ // Try once again to activate.
80+ $ activated = activate_plugin ( 'woocommerce/woocommerce.php ' );
81+ }
82+
83+ // Nothing more we can do, unfortunately.
84+ if ( is_wp_error ( $ activated ) ) {
85+ echo PHP_EOL . 'WooCommerce could not automatically be activated in the test environment: ' ;
86+ echo PHP_EOL . $ activated ->get_error_message ();
87+ echo PHP_EOL . PHP_EOL . "\033[0;31mUnable to proceed with tests, aborting. \033[0m " ;
88+ echo PHP_EOL ;
89+ exit ( 1 );
90+ }
0 commit comments