Add test to ensure that verify_base_table also creates table if needed.

This commit is contained in:
vedanshujain 2020-05-26 23:46:58 +05:30
parent e89ee25f36
commit af942a131a
1 changed files with 44 additions and 1 deletions

View File

@ -28,8 +28,13 @@ class WCInstallTest extends \WC_Unit_Test_Case {
$clear_query = 'DROP TABLE IF EXISTS %s;';
$rename_table_query = 'RENAME TABLE %s to %s;';
// Workaround to call a private function.
$schema = function () {
return static::get_schema();
};
// Rename a base table to simulate it as non-existing.
dbDelta( \WC_Install::get_schema() ); // Restore correct state.
dbDelta( $schema->call( new \WC_Install() ) ); // Restore correct state.
$wpdb->query( sprintf( $clear_query, $changed_table_name ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query( sprintf( $rename_table_query, $original_table_name, $changed_table_name ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
@ -48,4 +53,42 @@ class WCInstallTest extends \WC_Unit_Test_Case {
$this->assertNotContains( 'base_tables_missing', \WC_Admin_Notices::get_notices() );
}
/**
* Test if verify base table can fix the table as well.
*/
public function test_verify_base_tables_fix_tables() {
global $wpdb;
// Remove drop filter because we do want to drop temp table if it exists.
// This filter was added to only allow dropping temporary tables which will then be rollbacked after the test.
remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
$original_table_name = "{$wpdb->prefix}wc_tax_rate_classes";
$changed_table_name = "{$wpdb->prefix}wc_tax_rate_classes_2";
$clear_query = 'DROP TABLE IF EXISTS %s;';
$rename_table_query = 'RENAME TABLE %s to %s;';
// Workaround to call a private function.
$schema = function () {
return static::get_schema();
};
// Rename a base table to simulate it as non-existing.
dbDelta( $schema->call( new \WC_Install() ) ); // Restore correct state.
$wpdb->query( sprintf( $clear_query, $changed_table_name ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query( sprintf( $rename_table_query, $original_table_name, $changed_table_name ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$missing_tables = \WC_Install::verify_base_tables( true, true );
$wpdb->query( sprintf( $clear_query, $original_table_name ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query( sprintf( $rename_table_query, $changed_table_name, $original_table_name ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
add_filter( 'query', array( $this, '_drop_temporary_tables' ) );
// Ideally, no missing table because verify base tables created the table as well.
$this->assertNotContains( $original_table_name, $missing_tables );
$this->assertNotContains( 'base_tables_missing', \WC_Admin_Notices::get_notices() );
}
}