From b2e96aaf5a6a7eba7fb80bfbd261c1d6571f0105 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Wed, 12 Apr 2023 14:25:09 -0700 Subject: [PATCH] Move methods and tests to COTMigrationUtil --- .../Internal/Utilities/COTMigrationUtil.php | 34 +++++++- .../woocommerce/src/Utilities/OrderUtil.php | 20 +---- .../Utilities/COTMigrationUtilTest.php | 65 ++++++++++++++ .../tests/php/src/Utilities/OrderUtilTest.php | 84 ------------------- 4 files changed, 101 insertions(+), 102 deletions(-) delete mode 100644 plugins/woocommerce/tests/php/src/Utilities/OrderUtilTest.php diff --git a/plugins/woocommerce/src/Internal/Utilities/COTMigrationUtil.php b/plugins/woocommerce/src/Internal/Utilities/COTMigrationUtil.php index c1be7077e7b..eb12309b83b 100644 --- a/plugins/woocommerce/src/Internal/Utilities/COTMigrationUtil.php +++ b/plugins/woocommerce/src/Internal/Utilities/COTMigrationUtil.php @@ -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; + } } diff --git a/plugins/woocommerce/src/Utilities/OrderUtil.php b/plugins/woocommerce/src/Utilities/OrderUtil.php index e341ef8ef50..21775789852 100644 --- a/plugins/woocommerce/src/Utilities/OrderUtil.php +++ b/plugins/woocommerce/src/Utilities/OrderUtil.php @@ -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(); } } diff --git a/plugins/woocommerce/tests/php/src/Internal/Utilities/COTMigrationUtilTest.php b/plugins/woocommerce/tests/php/src/Internal/Utilities/COTMigrationUtilTest.php index 235202ba559..5ff7faf68ea 100644 --- a/plugins/woocommerce/tests/php/src/Internal/Utilities/COTMigrationUtilTest.php +++ b/plugins/woocommerce/tests/php/src/Internal/Utilities/COTMigrationUtilTest.php @@ -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 ); + } } diff --git a/plugins/woocommerce/tests/php/src/Utilities/OrderUtilTest.php b/plugins/woocommerce/tests/php/src/Utilities/OrderUtilTest.php deleted file mode 100644 index 02a004be351..00000000000 --- a/plugins/woocommerce/tests/php/src/Utilities/OrderUtilTest.php +++ /dev/null @@ -1,84 +0,0 @@ -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 ); - } -}