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

Commit 20fd3d7

Browse files
authored
Merge pull request #22 from liquidweb/fix/woocommerce-bootstrapping
Adjust plugin bootstrapping
2 parents e437c0b + 277c9a6 commit 20fd3d7

File tree

4 files changed

+101
-6
lines changed

4 files changed

+101
-6
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+
}

tests/test-bootstrap.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Tests for the plugin bootstrapping.
4+
*
5+
* @package Woocommerce_Order_Tables
6+
* @author Liquid Web
7+
*/
8+
9+
class BootstrapTest extends TestCase {
10+
11+
/**
12+
* Tear down the plugin after each test run.
13+
*
14+
* @before
15+
*/
16+
public function tear_down_plugin() {
17+
global $wc_custom_order_table;
18+
19+
// Destroy the global $wc_custom_order_table instance.
20+
unset( $wc_custom_order_table );
21+
}
22+
23+
public function test_plugin_only_loads_after_woocommerce() {
24+
global $wc_custom_order_table;
25+
26+
$this->assertNull(
27+
$wc_custom_order_table,
28+
'Before bootstrapping, $wc_custom_order_table should be empty.'
29+
);
30+
31+
do_action( 'woocommerce_init' );
32+
33+
$this->assertInstanceOf(
34+
'WC_Custom_Order_Table',
35+
$wc_custom_order_table,
36+
'The plugin should not be bootstrapped until woocommerce_init.'
37+
);
38+
}
39+
}

tests/test-sample.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<?php
22
/**
3-
* Class SampleTest
3+
* Sample test case for WooCommerce Custom Order Tables.
44
*
55
* @package Woocommerce_Order_Tables
6+
* @author Liquid Web
67
*/
78

8-
/**
9-
* Sample test case.
10-
*/
11-
class SampleTest extends WP_UnitTestCase {
9+
class SampleTest extends TestCase {
1210

1311
/**
1412
* A single example test.

wc-custom-order-table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ function wc_custom_order_table() {
5050
return $wc_custom_order_table;
5151
}
5252

53-
add_action( 'plugins_loaded', 'wc_custom_order_table' );
53+
add_action( 'woocommerce_init', 'wc_custom_order_table' );

0 commit comments

Comments
 (0)