Skip to content
This repository was archived by the owner on Jun 15, 2022. It is now read-only.
57 changes: 17 additions & 40 deletions includes/class-wc-custom-order-table-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,43 @@
class WC_Custom_Order_Table_Install {

/**
* The database table schema version.
*
* @var int
*/
protected $table_version = 1;


/**
* Actions to perform on plugin activation.
*/
public function activate() {
$this->maybe_install_tables();
}

/**
* Retrieve the latest table schema version.
*
* @return int The latest schema version.
* The option key that contains the current schema version.
*/
public function get_latest_table_version() {
return absint( $this->table_version );
}
const SCHEMA_VERSION_KEY = 'wc_orders_table_version';

/**
* Retrieve the current table version from the options table.
* The database table schema version.
*
* @return int The current schema version.
* @var int
*/
public function get_installed_table_version() {
return absint( get_option( 'wc_orders_table_version' ) );
}
protected static $table_version = 1;

/**
* Install or update the tables if the site is not using the current schema.
* Actions to perform on plugin activation.
*/
protected function maybe_install_tables() {
if ( $this->get_installed_table_version() < $this->get_latest_table_version() ) {
$this->install_tables();
public static function activate() {
// We're already on the latest schema version.
if ( (int) self::$table_version === (int) get_option( self::SCHEMA_VERSION_KEY ) ) {
return false;
}

self::install_tables();
}

/**
* Perform the database delta to create the table.
*
* @global $wpdb
*/
protected function install_tables() {
protected static function install_tables() {
global $wpdb;

// Load wp-admin/includes/upgrade.php, which defines dbDelta().
require_once ABSPATH . 'wp-admin/includes/upgrade.php';

$collate = '';

if ( $wpdb->has_cap( 'collation' ) ) {
$collate = $wpdb->get_charset_collate();
}

$table = wc_custom_order_table()->get_table_name();
$tables = "
$table = wc_custom_order_table()->get_table_name();
$collate = $wpdb->get_charset_collate();
$tables = "
CREATE TABLE {$table} (
order_id BIGINT UNSIGNED NOT NULL,
order_key varchar(100) NOT NULL,
Expand Down Expand Up @@ -119,6 +96,6 @@ protected function install_tables() {
dbDelta( $tables );

// Store the table version in the options table.
update_option( 'wc_orders_table_version', $this->get_latest_table_version() );
update_option( self::SCHEMA_VERSION_KEY, (int) self::$table_version, false );
}
}
78 changes: 78 additions & 0 deletions tests/test-installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,82 @@ public function test_table_is_created_on_plugin_activation() {
'Upon activation, the table should be created.'
);
}

public function test_table_is_only_installed_if_it_does_not_already_exist() {
self::reactivate_plugin();

$this->assertTrue(
self::orders_table_exists(),
'Upon activation, the table should be created.'
);

// Deactivate, then re-activate the plugin.
self::reactivate_plugin();

$this->assertTrue(
self::orders_table_exists(),
'The table should still exist, just as it was.'
);
}

public function test_can_install_table() {
$this->assertFalse(
self::orders_table_exists(),
'The wp_woocommerce_orders table should not exist at the beginning of this test.'
);

WC_Custom_Order_Table_Install::activate();

$this->assertTrue(
self::orders_table_exists(),
'Upon activation, the table should be created.'
);
$this->assertNotEmpty(
get_option( WC_Custom_Order_Table_Install::SCHEMA_VERSION_KEY ),
'The schema version should be stored in the options table.'
);
}

public function test_returns_early_if_already_on_latest_schema_version() {
WC_Custom_Order_Table_Install::activate();

$this->assertFalse(
WC_Custom_Order_Table_Install::activate(),
'The activate() method should return false if the schema versions match.'
);
}

public function test_can_upgrade_table() {
WC_Custom_Order_Table_Install::activate();

// Get the current schema version, then increment it.
$property = new ReflectionProperty( 'WC_Custom_Order_Table_Install', 'table_version' );
$property->setAccessible( true );
$version = $property->getValue();
$property->setValue( $version + 1 );

// Run the activation script again.
WC_Custom_Order_Table_Install::activate();

$this->assertEquals(
$version + 1,
get_option( WC_Custom_Order_Table_Install::SCHEMA_VERSION_KEY ),
'The schema version should have been incremented.'
);
}

public function test_current_schema_version_is_not_autoloaded() {
global $wpdb;

WC_Custom_Order_Table_Install::activate();

$this->assertEquals(
'no',
$wpdb->get_var( $wpdb->prepare(
"SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1",
WC_Custom_Order_Table_Install::SCHEMA_VERSION_KEY
) ),
'The schema version should not be autoloaded.'
);
}
}
2 changes: 2 additions & 0 deletions tests/testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ protected static function drop_orders_table() {
global $wpdb;

$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_orders" );

delete_option( WC_Custom_Order_Table_Install::SCHEMA_VERSION_KEY );
}

/**
Expand Down
11 changes: 2 additions & 9 deletions wc-custom-order-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,9 @@
}

/**
* Installation procedure for the plugin.
*
* This function is responsible for creating the new plugin database tables.
* Install the database tables upon plugin activation.
*/
function wc_custom_order_table_install() {
$installer = new WC_Custom_Order_Table_Install();
$installer->activate();
}

register_activation_hook( __FILE__, 'wc_custom_order_table_install' );
register_activation_hook( __FILE__, array( 'WC_Custom_Order_Table_Install', 'activate' ) );

/**
* Retrieve an instance of the WC_Custom_Order_Table class.
Expand Down