Add test for sorting by include param.

This commit is contained in:
Vedanshu Jain 2023-01-02 16:20:25 +05:30
parent e163e1a265
commit be97c1353b
2 changed files with 37 additions and 1 deletions

View File

@ -987,7 +987,10 @@ class OrdersTableQuery {
} }
if ( 'include' === $orderby || 'post__in' === $orderby ) { 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 ); $ids = array_map( 'absint', $ids );
$this->orderby = array( "FIELD( {$this->tables['orders']}.id, " . implode( ',', $ids ) . ' )' ); $this->orderby = array( "FIELD( {$this->tables['orders']}.id, " . implode( ',', $ids ) . ' )' );
return; return;

View File

@ -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. * @testDox Ensure search works as expected on updated orders.
*/ */