From 74a3f1d6d863d89335aac7d17e45d7e7ce3e5e96 Mon Sep 17 00:00:00 2001 From: Steve Grunwell Date: Fri, 11 May 2018 16:18:42 +0000 Subject: [PATCH 1/2] 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. --- includes/class-wc-install.php | 3 +++ tests/unit-tests/util/install.php | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index d96ac02521e..45cab833214 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -809,6 +809,9 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta ( */ $tables = apply_filters( 'woocommerce_install_get_tables', $tables ); + // Sort the tables alphabetically. + sort( $tables ); + return $tables; } diff --git a/tests/unit-tests/util/install.php b/tests/unit-tests/util/install.php index 4ecb176fd40..488ff347878 100644 --- a/tests/unit-tests/util/install.php +++ b/tests/unit-tests/util/install.php @@ -146,4 +146,31 @@ class WC_Tests_Install extends WC_Unit_Test_Case { 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; + } } From 317e7ba29fe2bb75be54090407f7f9ebc107f81f Mon Sep 17 00:00:00 2001 From: Steve Grunwell Date: Fri, 11 May 2018 22:46:39 +0000 Subject: [PATCH 2/2] Revert most of 74a3f1d6 As @claudiosanches pointed out, there isn't much real-world need for `WC_Install::get_tables()` to sort results. Instead, sort the returned value within the `WC_Tests_Install::test_get_tables()` method. --- includes/class-wc-install.php | 3 --- tests/unit-tests/util/install.php | 31 +++---------------------------- 2 files changed, 3 insertions(+), 31 deletions(-) diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index 45cab833214..d96ac02521e 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -809,9 +809,6 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta ( */ $tables = apply_filters( 'woocommerce_install_get_tables', $tables ); - // Sort the tables alphabetically. - sort( $tables ); - return $tables; } diff --git a/tests/unit-tests/util/install.php b/tests/unit-tests/util/install.php index 488ff347878..2e6d7922900 100644 --- a/tests/unit-tests/util/install.php +++ b/tests/unit-tests/util/install.php @@ -122,8 +122,10 @@ class WC_Tests_Install extends WC_Unit_Test_Case { $tables = $wpdb->get_col( "SHOW TABLES WHERE `Tables_in_{$wpdb->dbname}` LIKE '{$wpdb->prefix}woocommerce\_%' OR `Tables_in_{$wpdb->dbname}` LIKE '{$wpdb->prefix}wc\_%'" ); + $result = WC_Install::get_tables(); + sort( $result ); - $this->assertEquals( $tables, WC_Install::get_tables() ); + $this->assertEquals( $tables, $result ); } /** @@ -146,31 +148,4 @@ class WC_Tests_Install extends WC_Unit_Test_Case { 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; - } }