Add various Util methods to OrderUtil class (#34141)

Add method in utils to check whether posts and order tables are in sync.

* Add changelog.
* Fix Yoda condition

Co-authored-by: Nestor Soriano <konamiman@konamiman.com>
This commit is contained in:
Vedanshu Jain 2022-08-08 23:58:35 +05:30 committed by GitHub
parent fbb6bd1c53
commit fe27a9a87c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 94 additions and 2 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: add
Add util methods to check whether tables are in sync and orders are migrated.

View File

@ -5,6 +5,8 @@
namespace Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders; namespace Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer;
use Automattic\WooCommerce\Internal\DependencyManagement\AbstractServiceProvider; use Automattic\WooCommerce\Internal\DependencyManagement\AbstractServiceProvider;
use Automattic\WooCommerce\Internal\Utilities\COTMigrationUtil; use Automattic\WooCommerce\Internal\Utilities\COTMigrationUtil;
use Automattic\WooCommerce\Internal\Utilities\DatabaseUtil; use Automattic\WooCommerce\Internal\Utilities\DatabaseUtil;
@ -35,6 +37,7 @@ class UtilsClassesServiceProvider extends AbstractServiceProvider {
$this->share( DatabaseUtil::class ); $this->share( DatabaseUtil::class );
$this->share( HtmlSanitizer::class ); $this->share( HtmlSanitizer::class );
$this->share( OrderUtil::class ); $this->share( OrderUtil::class );
$this->share( COTMigrationUtil::class ); $this->share( COTMigrationUtil::class )
->addArguments( array( CustomOrdersTableController::class, DataSynchronizer::class ) );
} }
} }

View File

@ -6,6 +6,7 @@
namespace Automattic\WooCommerce\Internal\Utilities; namespace Automattic\WooCommerce\Internal\Utilities;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer;
use WC_Order; use WC_Order;
use WP_Post; use WP_Post;
@ -13,6 +14,35 @@ use WP_Post;
* Utility functions meant for helping in migration from posts tables to custom order tables. * Utility functions meant for helping in migration from posts tables to custom order tables.
*/ */
class COTMigrationUtil { class COTMigrationUtil {
/**
* Custom order table controller.
*
* @var CustomOrdersTableController
*/
private $table_controller;
/**
* Data synchronizer.
*
* @var DataSynchronizer
*/
private $data_synchronizer;
/**
* Initialize method, invoked by the DI container.
*
* @internal Automatically called by the container.
* @param CustomOrdersTableController $table_controller Custom order table controller.
* @param DataSynchronizer $data_synchronizer Data synchronizer.
*
* @return void
*/
final public function init( CustomOrdersTableController $table_controller, DataSynchronizer $data_synchronizer ) {
$this->table_controller = $table_controller;
$this->data_synchronizer = $data_synchronizer;
}
/** /**
* Helper function to get screen name of orders page in wp-admin. * Helper function to get screen name of orders page in wp-admin.
* *
@ -35,7 +65,17 @@ class COTMigrationUtil {
* @return bool * @return bool
*/ */
private function custom_orders_table_usage_is_enabled() : bool { private function custom_orders_table_usage_is_enabled() : bool {
return wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled(); return $this->table_controller->custom_orders_table_usage_is_enabled();
}
/**
* Checks if posts and order custom table sync is enabled and there are no pending orders.
*
* @return bool
*/
public function is_custom_order_tables_in_sync() : bool {
$sync_status = $this->data_synchronizer->get_sync_status();
return $sync_status['current_pending_count'] === 0 && $this->data_synchronizer->data_sync_is_enabled();
} }
/** /**

View File

@ -34,6 +34,15 @@ final class OrderUtil {
return wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled(); return wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled();
} }
/**
* Checks if posts and order custom table sync is enabled and there are no pending orders.
*
* @return bool
*/
public static function is_custom_order_tables_in_sync() : bool {
return wc_get_container()->get( COTMigrationUtil::class )->is_custom_order_tables_in_sync();
}
/** /**
* Gets value of a meta key from WC_Data object if passed, otherwise from the post object. * Gets value of a meta key from WC_Data object if passed, otherwise from the post object.
* This helper function support backward compatibility for meta box functions, when moving from posts based store to custom tables. * This helper function support backward compatibility for meta box functions, when moving from posts based store to custom tables.

View File

@ -3,6 +3,8 @@
* Tests for COTMigration utility. * Tests for COTMigration utility.
*/ */
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer;
use Automattic\WooCommerce\Internal\Utilities\COTMigrationUtil; use Automattic\WooCommerce\Internal\Utilities\COTMigrationUtil;
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper; use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
@ -65,4 +67,38 @@ class COTMigrationUtilTest extends WC_Unit_Test_Case {
$this->assertEquals( $order2->get_id(), $this->sut->get_post_or_order_id( $post_from_order2 ) ); $this->assertEquals( $order2->get_id(), $this->sut->get_post_or_order_id( $post_from_order2 ) );
} }
/**
* @testDox `is_custom_order_tables_in_sync` should return true when Custom Order Tables are in sync.
*/
public function test_is_custom_order_tables_in_sync_is_true() {
$data_sync_mock = $this->getMockBuilder( DataSynchronizer::class )
->setMethods( array( 'get_sync_status', 'data_sync_is_enabled' ) )
->getMock();
$data_sync_mock->method( 'get_sync_status' )->willReturn( array( 'current_pending_count' => 0 ) );
$data_sync_mock->method( 'data_sync_is_enabled' )->willReturn( true );
$cot_controller = wc_get_container()->get( CustomOrdersTableController::class );
$this->sut = new COTMigrationUtil();
$this->sut->init( $cot_controller, $data_sync_mock );
$this->assertTrue( $this->sut->is_custom_order_tables_in_sync() );
}
/**
* @testDox `is_custom_order_tables_in_sync` should return false when Custom Order Tables are not in sync.
*/
public function test_is_custom_order_tables_in_sync_is_false() {
$data_sync_mock = $this->getMockBuilder( DataSynchronizer::class )
->setMethods( array( 'get_sync_status', 'data_sync_is_enabled' ) )
->getMock();
$data_sync_mock->method( 'get_sync_status' )->willReturn( array( 'current_pending_count' => 0 ) );
$data_sync_mock->method( 'data_sync_is_enabled' )->willReturn( false );
$cot_controller = wc_get_container()->get( CustomOrdersTableController::class );
$this->sut = new COTMigrationUtil();
$this->sut->init( $cot_controller, $data_sync_mock );
$this->assertFalse( $this->sut->is_custom_order_tables_in_sync() );
}
} }