Sort the results of WC_Install::get_tables()

Since custom tables can be registered within WooCommerce via the 'woocommerce_install_get_tables' filter, it's helpful to ensure that `WC_Install::get_tables()` automatically sorts the table names alphabetically.

This helps avoid false failures when testing a plugin that uses a custom table against the WooCommerce core test suite, as queries like `WC_Tests_Install::test_get_tables()` assumes that tables will be returned in alphabetical order.
This commit is contained in:
Steve Grunwell 2018-05-11 16:18:42 +00:00
parent 8a60d0aee9
commit 74a3f1d6d8
2 changed files with 30 additions and 0 deletions

View File

@ -809,6 +809,9 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
*/ */
$tables = apply_filters( 'woocommerce_install_get_tables', $tables ); $tables = apply_filters( 'woocommerce_install_get_tables', $tables );
// Sort the tables alphabetically.
sort( $tables );
return $tables; return $tables;
} }

View File

@ -146,4 +146,31 @@ class WC_Tests_Install extends WC_Unit_Test_Case {
return $tables; return $tables;
} }
/**
* Test - WC_Install::get_tables() should sort tables alphabetically.
*/
public function test_get_tables_sorts_results() {
global $wpdb;
add_filter( 'woocommerce_install_get_tables', array( $this, 'append_tables_for_sorting' ) );
$sorted = WC_Install::get_tables();
sort( $sorted );
$this->assertEquals( $sorted, WC_Install::get_tables() );
}
/**
* Filter callback for test_get_tables_sorts_results().
*/
public function append_tables_for_sorting( $tables ) {
global $wpdb;
$tables[] = "{$wpdb->prefix}wc_another_table";
$tables[] = "{$wpdb->prefix}woocommerce_another_one";
$tables[] = "{$wpdb->prefix}woocommerce_yet_another_table";
return $tables;
}
} }