HPOS: Remove buggy check in the data cleanup tool. (#43727)

* Add refund order to unit test.

* Remove buggy defensive check since existance of order is already verified.

* Changelog.

* PHPCS fixes.

* Added more assertions after cleanup to verify cleanup worked.
This commit is contained in:
Vedanshu Jain 2024-01-17 14:58:11 +05:30 committed by GitHub
parent 8878f00707
commit 90b3e11dba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 7 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
HPOS: Remove buggy check in the data cleanup tool.

View File

@ -173,13 +173,11 @@ class LegacyDataHandler {
/** /**
* Checks whether an HPOS-backed order is newer than the corresponding post. * Checks whether an HPOS-backed order is newer than the corresponding post.
* *
* @param int|\WC_Order $order An HPOS order. * @param \WC_Abstract_Order $order An HPOS order.
* @return bool TRUE if the order is up to date with the corresponding post. * @return bool TRUE if the order is up to date with the corresponding post.
* @throws \Exception When the order is not an HPOS order. * @throws \Exception When the order is not an HPOS order.
*/ */
private function is_order_newer_than_post( $order ): bool { private function is_order_newer_than_post( \WC_Abstract_Order $order ): bool {
$order = is_a( $order, 'WC_Order' ) ? $order : wc_get_order( absint( $order ) );
if ( ! is_a( $order->get_data_store()->get_current_class_name(), OrdersTableDataStore::class, true ) ) { if ( ! is_a( $order->get_data_store()->get_current_class_name(), OrdersTableDataStore::class, true ) ) {
throw new \Exception( __( 'Order is not an HPOS order.', 'woocommerce' ) ); throw new \Exception( __( 'Order is not an HPOS order.', 'woocommerce' ) );
} }

View File

@ -42,10 +42,16 @@ class LegacyDataHandlerTests extends WC_Unit_Test_Case {
*/ */
public function test_post_data_cleanup() { public function test_post_data_cleanup() {
$this->enable_cot_sync(); $this->enable_cot_sync();
$orders = array( $orders = array(
OrderHelper::create_order(), OrderHelper::create_order(),
OrderHelper::create_order(), OrderHelper::create_order(),
); );
$refund_order = wc_create_refund(
array(
'order_id' => $orders[0]->get_id(),
'amount' => 10,
)
);
$this->disable_cot_sync(); $this->disable_cot_sync();
// Confirm orders have been synced up (i.e. are not placeholders) and contain metadata. // Confirm orders have been synced up (i.e. are not placeholders) and contain metadata.
@ -53,10 +59,11 @@ class LegacyDataHandlerTests extends WC_Unit_Test_Case {
$this->assertEquals( 'shop_order', get_post_type( $order->get_id() ) ); $this->assertEquals( 'shop_order', get_post_type( $order->get_id() ) );
$this->assertNotEmpty( get_post_meta( $order->get_id() ) ); $this->assertNotEmpty( get_post_meta( $order->get_id() ) );
} }
$this->assertEquals( 'shop_order_refund', get_post_type( $refund_order->get_id() ) );
// Check that counts are working ok. // Check that counts are working ok.
$this->assertEquals( 1, $this->sut->count_orders_for_cleanup( array( $orders[0]->get_id() ) ) ); $this->assertEquals( 1, $this->sut->count_orders_for_cleanup( array( $orders[0]->get_id() ) ) );
$this->assertEquals( 2, $this->sut->count_orders_for_cleanup() ); $this->assertEquals( 3, $this->sut->count_orders_for_cleanup() );
// Cleanup one of the orders. // Cleanup one of the orders.
$this->sut->cleanup_post_data( $orders[0]->get_id() ); $this->sut->cleanup_post_data( $orders[0]->get_id() );
@ -67,10 +74,15 @@ class LegacyDataHandlerTests extends WC_Unit_Test_Case {
// Check counts. // Check counts.
$this->assertEquals( 0, $this->sut->count_orders_for_cleanup( array( $orders[0]->get_id() ) ) ); $this->assertEquals( 0, $this->sut->count_orders_for_cleanup( array( $orders[0]->get_id() ) ) );
$this->assertEquals( 1, $this->sut->count_orders_for_cleanup() ); $this->assertEquals( 2, $this->sut->count_orders_for_cleanup() );
// Confirm that we now have 1 unsynced order (due to the removal of the backup data). // Confirm that we now have 1 unsynced order (due to the removal of the backup data).
$this->assertEquals( 1, wc_get_container()->get( DataSynchronizer::class )->get_current_orders_pending_sync_count() ); $this->assertEquals( 1, wc_get_container()->get( DataSynchronizer::class )->get_current_orders_pending_sync_count() );
// Cleanup the refund order.
$this->sut->cleanup_post_data( $refund_order->get_id() );
$this->assertEquals( 1, $this->sut->count_orders_for_cleanup() );
$this->assertEquals( 2, wc_get_container()->get( DataSynchronizer::class )->get_current_orders_pending_sync_count() );
} }
/** /**