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

Commit 77022c5

Browse files
committed
Remove the double plugin activation hook and write a regression test for it.
Previously, wc_custom_order_table_install() — which is called via register_activation_hook() — was then calling register_activation_hook() a second time, preventing the WC_Custom_Order_Table_Install::activate() method from ever firing on plugin activation. Fixes #5, closes #7.
1 parent f18a29c commit 77022c5

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

tests/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ function _manually_load_plugin() {
2929

3030
// Start up the WP testing environment.
3131
require $_tests_dir . '/includes/bootstrap.php';
32+
require __DIR__ . '/testcase.php';

tests/test-installation.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Tests for the plugin installation.
4+
*
5+
* @package Woocommerce_Order_Tables
6+
* @author Liquid Web
7+
*/
8+
9+
class InstallationTest extends TestCase {
10+
11+
public function setUp() {
12+
self::drop_orders_table();
13+
}
14+
15+
public function test_table_is_created_on_plugin_activation() {
16+
$this->assertFalse(
17+
self::orders_table_exists(),
18+
'The wp_woocommerce_orders table should not exist at the beginning of this test.'
19+
);
20+
21+
self::reactivate_plugin();
22+
23+
$this->assertTrue(
24+
self::orders_table_exists(),
25+
'Upon activation, the table should be created.'
26+
);
27+
}
28+
}

tests/testcase.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Base test case for WooCommerce Custom Order Tables.
4+
*
5+
* @package Woocommerce_Order_Tables
6+
* @author Liquid Web
7+
*/
8+
9+
class TestCase extends WP_UnitTestCase {
10+
11+
/**
12+
* Determine if the custom orders table exists.
13+
*
14+
* @global $wpdb
15+
*/
16+
protected static function orders_table_exists() {
17+
global $wpdb;
18+
19+
return (bool) $wpdb->get_var( $wpdb->prepare(
20+
'SELECT COUNT(*) FROM information_schema.tables WHERE table_name = %s LIMIT 1',
21+
$wpdb->prefix . 'woocommerce_orders'
22+
) );
23+
}
24+
25+
/**
26+
* Drop the wp_woocommerce_orders table.
27+
*
28+
* @global $wpdb
29+
*/
30+
protected static function drop_orders_table() {
31+
global $wpdb;
32+
33+
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_orders" );
34+
}
35+
36+
/**
37+
* Emulate deactivating, then subsequently reactivating the plugin.
38+
*/
39+
protected static function reactivate_plugin() {
40+
$plugin = plugin_basename( dirname( __DIR__ ) . '/wc-custom-order-table.php' );
41+
42+
do_action( 'deactivate_' . $plugin, false );
43+
do_action( 'activate_' . $plugin, false );
44+
}
45+
}

wc-custom-order-table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
function wc_custom_order_table_install() {
3434
$installer = new WC_Custom_Order_Table_Install();
35-
register_activation_hook( __FILE__, array( $installer, 'activate' ) );
35+
$installer->activate();
3636
}
3737

3838
register_activation_hook( __FILE__, 'wc_custom_order_table_install' );

0 commit comments

Comments
 (0)