From be97c1353b57a03d862390241d05567a7404a7cd Mon Sep 17 00:00:00 2001 From: Vedanshu Jain Date: Mon, 2 Jan 2023 16:20:25 +0530 Subject: [PATCH] Add test for sorting by include param. --- .../DataStores/Orders/OrdersTableQuery.php | 5 ++- .../Orders/OrdersTableDataStoreTests.php | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableQuery.php b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableQuery.php index 76c3c167bd9..53a2606641f 100644 --- a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableQuery.php +++ b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableQuery.php @@ -987,7 +987,10 @@ class OrdersTableQuery { } if ( 'include' === $orderby || 'post__in' === $orderby ) { - $ids = $this->args['id']; + $ids = $this->args['id'] ?? $this->args['includes']; + if ( empty( $ids ) ) { + return; + } $ids = array_map( 'absint', $ids ); $this->orderby = array( "FIELD( {$this->tables['orders']}.id, " . implode( ',', $ids ) . ' )' ); return; diff --git a/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php b/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php index 765095fe095..581a7f52755 100644 --- a/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php +++ b/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php @@ -1349,6 +1349,39 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case { ); } + /** + * @testDox Ensure sorting by `includes` param works as expected. + */ + public function test_cot_query_sort_includes() { + $this->disable_cot_sync(); + $order_1 = new WC_Order(); + $this->switch_data_store( $order_1, $this->sut ); + $order_1->save(); + + $order_2 = new WC_Order(); + $this->switch_data_store( $order_2, $this->sut ); + $this->disable_cot_sync(); + $order_2->save(); + + $query = new OrdersTableQuery( + array( + 'orderby' => 'include', + 'includes' => array( $order_1->get_id(), $order_2->get_id() ), + ) + ); + $orders_array = $query->orders; + $this->assertEquals( array( $order_1->get_id(), $order_2->get_id() ), array( $orders_array[0], $orders_array[1] ) ); + + $query = new OrdersTableQuery( + array( + 'orderby' => 'include', + 'includes' => array( $order_2->get_id(), $order_1->get_id() ), + ) + ); + $orders_array = $query->orders; + $this->assertEquals( array( $order_2->get_id(), $order_1->get_id() ), array( $orders_array[0], $orders_array[1] ) ); + } + /** * @testDox Ensure search works as expected on updated orders. */