Move methods and tests to COTMigrationUtil

This commit is contained in:
Corey McKrill 2023-04-12 14:25:09 -07:00
parent 7fe96ac988
commit b2e96aaf5a
No known key found for this signature in database
GPG Key ID: 84BBFE669C4D97B8
4 changed files with 101 additions and 102 deletions

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

@ -7,7 +7,7 @@ namespace Automattic\WooCommerce\Utilities;
use Automattic\WooCommerce\Caches\OrderCacheController;
use Automattic\WooCommerce\Internal\Admin\Orders\PageController;
use Automattic\WooCommerce\Internal\DataStores\Orders\{ CustomOrdersTableController, OrdersTableDataStore };
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
use Automattic\WooCommerce\Internal\Features\FeaturesController;
use Automattic\WooCommerce\Internal\Utilities\COTMigrationUtil;
use WC_Order;
@ -141,14 +141,7 @@ final class OrderUtil {
* @return string
*/
public static function get_table_for_orders() {
if ( self::custom_orders_table_usage_is_enabled() ) {
$table_name = OrdersTableDataStore::get_orders_table_name();
} else {
global $wpdb;
$table_name = $wpdb->posts;
}
return $table_name;
return wc_get_container()->get( COTMigrationUtil::class )->get_table_for_orders();
}
/**
@ -157,13 +150,6 @@ final class OrderUtil {
* @return string
*/
public static function get_table_for_order_meta() {
if ( self::custom_orders_table_usage_is_enabled() ) {
$table_name = OrdersTableDataStore::get_meta_table_name();
} else {
global $wpdb;
$table_name = $wpdb->postmeta;
}
return $table_name;
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 );
}
}

View File

@ -1,84 +0,0 @@
<?php
namespace Automattic\WooCommerce\Tests\Utilities;
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
use Automattic\WooCommerce\Utilities\OrderUtil;
/**
* A collection of tests for the string utility class.
*/
class OrderUtilTest extends \WC_Unit_Test_Case {
/**
* @var bool
*/
protected $prev_cot_state;
/**
* Store the COT state before the test.
*
* @return void
*/
public function setUp(): void {
parent::setUp();
$this->prev_cot_state = OrderUtil::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();
}
/**
* @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 = OrderUtil::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 = OrderUtil::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 = OrderUtil::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 = OrderUtil::get_table_for_order_meta();
$this->assertEquals( "{$wpdb->prefix}wc_orders_meta", $table_name );
}
}