From e637e42043ac44c8e9330d52710e9ac42e1e6662 Mon Sep 17 00:00:00 2001 From: Abdalsalaam Halawa Date: Wed, 26 Apr 2023 13:53:30 +0200 Subject: [PATCH] update_total_sales_counts test --- .../php/includes/wc-order-functions-test.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/plugins/woocommerce/tests/php/includes/wc-order-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-order-functions-test.php index 9a024eb4636..11c6c3d4280 100644 --- a/plugins/woocommerce/tests/php/includes/wc-order-functions-test.php +++ b/plugins/woocommerce/tests/php/includes/wc-order-functions-test.php @@ -56,4 +56,44 @@ class WC_Order_Functions_Test extends \WC_Unit_Test_Case { $this->assertEquals( 0, (int) $line_item->get_meta( '_reduced_stock', true ) ); $this->assertEquals( 2, (int) $line_item->get_meta( '_restock_refunded_items', true ) ); } + + /** + * Test update_total_sales_counts and check total_sales after order reflection. + * + * Tests the fix for issue #23796 + */ + public function test_wc_update_total_sales_counts() { + + $product = WC_Helper_Product::create_simple_product(); + + WC()->cart->add_to_cart( $product->get_id() ); + + $order_id = WC_Checkout::instance()->create_order( + array( + 'billing_email' => 'a@b.com', + 'payment_method' => 'dummy', + ) + ); + + $this->assertEquals( 0, $product->get_total_sales() ); + + $order = new WC_Order( $order_id ); + + $order->update_status( 'processing' ); + $this->assertEquals( 1, $product->get_total_sales() ); + + $order->update_status( 'cancelled' ); + $this->assertEquals( 0, $product->get_total_sales() ); + + $order->update_status( 'processing' ); + $this->assertEquals( 1, $product->get_total_sales() ); + + $order->update_status( 'completed' ); + $this->assertEquals( 1, $product->get_total_sales() ); + + if ( $order->delete( true ) ) { + $this->assertEquals( 0, $product->get_total_sales() ); + } + } + }