Add table name methods to OrderUtil (#37624)

This commit is contained in:
Vedanshu Jain 2023-04-17 20:05:51 +05:30 committed by GitHub
commit ffc8990baa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 120 additions and 1 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add methods to OrderUtil to get the names of order database tables

View File

@ -6,7 +6,7 @@
namespace Automattic\WooCommerce\Internal\Utilities;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer;
use Automattic\WooCommerce\Internal\DataStores\Orders\{ DataSynchronizer, OrdersTableDataStore };
use WC_Order;
use WP_Post;
@ -165,4 +165,36 @@ class COTMigrationUtil {
$order_data_store = \WC_Data_Store::load( 'order' );
return $order_data_store->get_order_type( $order_id );
}
/**
* Get the name of the database table that's currently in use for orders.
*
* @return string
*/
public function get_table_for_orders() {
if ( $this->custom_orders_table_usage_is_enabled() ) {
$table_name = OrdersTableDataStore::get_orders_table_name();
} else {
global $wpdb;
$table_name = $wpdb->posts;
}
return $table_name;
}
/**
* Get the name of the database table that's currently in use for orders.
*
* @return string
*/
public function get_table_for_order_meta() {
if ( $this->custom_orders_table_usage_is_enabled() ) {
$table_name = OrdersTableDataStore::get_meta_table_name();
} else {
global $wpdb;
$table_name = $wpdb->postmeta;
}
return $table_name;
}
}

View File

@ -134,4 +134,22 @@ final class OrderUtil {
public static function get_order_admin_new_url() : string {
return wc_get_container()->get( PageController::class )->get_new_page_url();
}
/**
* Get the name of the database table that's currently in use for orders.
*
* @return string
*/
public static function get_table_for_orders() {
return wc_get_container()->get( COTMigrationUtil::class )->get_table_for_orders();
}
/**
* Get the name of the database table that's currently in use for orders.
*
* @return string
*/
public static function get_table_for_order_meta() {
return wc_get_container()->get( COTMigrationUtil::class )->get_table_for_order_meta();
}
}

View File

@ -18,12 +18,30 @@ class COTMigrationUtilTest extends WC_Unit_Test_Case {
*/
private $sut;
/**
* @var bool
*/
private $prev_cot_state;
/**
* Set-up subject under test.
*/
public function setUp(): void {
parent::setUp();
$this->sut = wc_get_container()->get( COTMigrationUtil::class );
$cot_controller = wc_get_container()->get( CustomOrdersTableController::class );
$this->prev_cot_state = $cot_controller->custom_orders_table_usage_is_enabled();
}
/**
* Restore the COT state after the test.
*
* @return void
*/
public function tearDown(): void {
OrderHelper::toggle_cot( $this->prev_cot_state );
parent::tearDown();
}
/**
@ -101,4 +119,51 @@ class COTMigrationUtilTest extends WC_Unit_Test_Case {
$this->assertFalse( $this->sut->is_custom_order_tables_in_sync() );
}
/**
* @testdox `get_table_for_orders` should return the name of the posts table when HPOS is not in use.
*/
public function test_get_table_for_orders_posts() {
global $wpdb;
OrderHelper::toggle_cot( false );
$table_name = $this->sut->get_table_for_orders();
$this->assertEquals( $wpdb->posts, $table_name );
}
/**
* @testdox `get_table_for_orders` should return the name of the orders table when HPOS is in use.
*/
public function test_get_table_for_orders_hpos() {
global $wpdb;
OrderHelper::toggle_cot( true );
$table_name = $this->sut->get_table_for_orders();
$this->assertEquals( "{$wpdb->prefix}wc_orders", $table_name );
}
/**
* @testdox `get_table_for_order_meta` should return the name of the postmeta table when HPOS is not in use.
*/
public function test_get_table_for_order_meta_posts() {
global $wpdb;
OrderHelper::toggle_cot( false );
$table_name = $this->sut->get_table_for_order_meta();
$this->assertEquals( $wpdb->postmeta, $table_name );
}
/**
* @testdox `get_table_for_order_meta` should return the name of the orders meta table when HPOS is in use.
*/
public function test_get_table_for_order_meta_hpos() {
global $wpdb;
OrderHelper::toggle_cot( true );
$table_name = $this->sut->get_table_for_order_meta();
$this->assertEquals( "{$wpdb->prefix}wc_orders_meta", $table_name );
}
}