Skip to content
This repository was archived by the owner on Jun 15, 2022. It is now read-only.

Commit b74d8d1

Browse files
committed
Automatically install WooCommerce into the test environment if it isn't already present.
1 parent 7ede02e commit b74d8d1

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

tests/bootstrap.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,61 @@ function _manually_load_plugin() {
3030
// Start up the WP testing environment.
3131
require $_tests_dir . '/includes/bootstrap.php';
3232
require __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

Comments
 (0)