Skip to content

Commit

Permalink
fix orderby status for HPOS
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Nov 17, 2023
1 parent 2482efa commit 8b8358c
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 125 deletions.
1 change: 0 additions & 1 deletion includes/API/Customers_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ public function wcpos_search_user_table( $query ): void {
$like_login
);

//$insertion = "({$wpdb->users}.user_email LIKE '%" . $search_keyword . "%') OR ({$wpdb->users}.user_login LIKE '%" . $search_keyword . "%') OR ";
$pattern = "/\(\s*\w+\.meta_key\s*=\s*'[^']+'\s*AND\s*\w+\.meta_value\s*LIKE\s*'[^']+'\s*\)(\s*OR\s*\(\s*\w+\.meta_key\s*=\s*'[^']+'\s*AND\s*\w+\.meta_value\s*LIKE\s*'[^']+'\s*\))*\s*/";

// Add the search keyword to the query
Expand Down
40 changes: 39 additions & 1 deletion includes/API/Orders_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use const WCPOS\WooCommercePOS\PLUGIN_NAME;
use WP_REST_Request;
use WP_REST_Response;

use WP_REST_Server;

/**
Expand Down Expand Up @@ -460,6 +459,35 @@ public function wcpos_get_all_posts(array $fields = array() ): array {
}
}

/**
* Filters all query clauses at once.
* Covers the fields (SELECT), JOIN, WHERE, GROUP BY, ORDER BY, and LIMIT clauses.
*
* @param string[] $clauses {
* Associative array of the clauses for the query.
*
* @var string The SELECT clause of the query.
* @var string The JOIN clause of the query.
* @var string The WHERE clause of the query.
* @var string The GROUP BY clause of the query.
* @var string The ORDER BY clause of the query.
* @var string The LIMIT clause of the query.
* }
*
* @param OrdersTableQuery $query The OrdersTableQuery instance (passed by reference).
* @param array $args Query args.
*
* @return string[] $clauses
*/
public function wcpos_hpos_orderby_status_query( array $clauses, $query, $args ) {
if ( isset( $clauses['orderby'] ) && '' === $clauses['orderby'] ) {
$order = $args['order'] ?? 'ASC';
$clauses['orderby'] = $query->get_table_name('orders') . '.status ' . $order;
}

return $clauses;
}

/**
* Prepare objects query.
*
Expand All @@ -474,6 +502,7 @@ protected function prepare_objects_query( $request ) {
if ( isset( $request['orderby'] ) ) {
switch ( $request['orderby'] ) {
case 'status':
// NOTE: 'post_status' is not a valid orderby option for WC_Order_Query
$args['orderby'] = 'post_status';

break;
Expand All @@ -493,6 +522,15 @@ protected function prepare_objects_query( $request ) {

break;
}

// If HPOS is enabled and $args['orderby'] = 'post_status', we need to add a custom query clause
if ( class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) &&
method_exists( '\Automattic\WooCommerce\Utilities\OrderUtil', 'custom_orders_table_usage_is_enabled' ) &&
\Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) {
if ('status' === $request['orderby']) {
add_filter( 'woocommerce_orders_table_query_clauses', array( $this, 'wcpos_hpos_orderby_status_query' ), 10, 3 );
}
}
}

return $args;
Expand Down
51 changes: 28 additions & 23 deletions tests/includes/API/Test_Customers_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ public function test_orderby_first_name(): void {
// Order by 'first_name' ascending
$request = $this->wp_rest_get_request('/wcpos/v1/customers');
$request->set_query_params( array( 'orderby' => 'first_name', 'order' => 'asc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$first_names = wp_list_pluck( $data, 'first_name' );

$this->assertEquals( $first_names, array( 'Alice', 'Bob', 'Zara' ) );

// reverse order
$request->set_query_params( array( 'orderby' => 'first_name', 'order' => 'desc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$first_names = wp_list_pluck( $data, 'first_name' );

Expand All @@ -164,15 +164,15 @@ public function test_orderby_last_name(): void {
// Order by 'last_name' ascending
$request = $this->wp_rest_get_request('/wcpos/v1/customers');
$request->set_query_params( array( 'orderby' => 'last_name', 'order' => 'asc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$last_names = wp_list_pluck( $data, 'last_name' );

$this->assertEquals( $last_names, array( 'Anderson', 'Martinez', 'Thompson' ) );

// reverse order
$request->set_query_params( array( 'orderby' => 'last_name', 'order' => 'desc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$last_names = wp_list_pluck( $data, 'last_name' );

Expand All @@ -188,21 +188,26 @@ public function test_orderby_email(): void {
// Order by 'email' ascending
$request = $this->wp_rest_get_request('/wcpos/v1/customers');
$request->set_query_params( array( 'orderby' => 'email', 'order' => 'asc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$emails = wp_list_pluck( $data, 'email' );

$this->assertEquals( $emails, array( '[email protected]', '[email protected]', '[email protected]' ) );

// reverse order
$request->set_query_params( array( 'orderby' => 'email', 'order' => 'desc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$emails = wp_list_pluck( $data, 'email' );

$this->assertEquals( $emails, array( '[email protected]', '[email protected]', '[email protected]' ) );
}

/**
* TODO - wp_capabilities is an array, so orderby doesn't work.
*
* - either find a way or remove the option to order by role
*/
public function test_orderby_role(): void {
// Create some customers
$customer1 = CustomerHelper::create_customer(array('role' => 'administrator'));
Expand All @@ -212,7 +217,7 @@ public function test_orderby_role(): void {
// Order by 'role' ascending
$request = $this->wp_rest_get_request('/wcpos/v1/customers');
$request->set_query_params( array( 'orderby' => 'role', 'order' => 'asc', 'role' => 'all' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$roles = wp_list_pluck( $data, 'role' );

Expand All @@ -224,7 +229,7 @@ public function test_orderby_role(): void {

// reverse order
$request->set_query_params( array( 'orderby' => 'role', 'order' => 'desc', 'role' => 'all' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$roles = wp_list_pluck( $data, 'role' );

Expand All @@ -244,7 +249,7 @@ public function test_orderby_username(): void {
// Order by 'username' ascending
$request = $this->wp_rest_get_request('/wcpos/v1/customers');
$request->set_query_params( array( 'orderby' => 'username', 'order' => 'asc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$usernames = wp_list_pluck( $data, 'username' );

Expand All @@ -255,7 +260,7 @@ public function test_orderby_username(): void {

// reverse order
$request->set_query_params( array( 'orderby' => 'username', 'order' => 'desc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$usernames = wp_list_pluck( $data, 'username' );

Expand Down Expand Up @@ -294,78 +299,78 @@ public function test_customer_search(): void {

// empty search
$request->set_query_params( array( 'search' => '' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals( 9, \count( $data ) );

// search for first_name
$request->set_query_params( array( 'search' => $random_first_name ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals( 1, \count( $data ) );
$this->assertEquals( $customer1->get_id(), $data[0]['id'] );

// search for last_name
$request->set_query_params( array( 'search' => $random_last_name ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals( 1, \count( $data ) );
$this->assertEquals( $customer2->get_id(), $data[0]['id'] );

// search for email
$request->set_query_params( array( 'search' => $random_email ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals( 1, \count( $data ) );
$this->assertEquals( $customer3->get_id(), $data[0]['id'] );

// search for username
$request->set_query_params( array( 'search' => $random_username ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals( 1, \count( $data ) );
$this->assertEquals( $customer4->get_id(), $data[0]['id'] );

// search for billing_first_name
$request->set_query_params( array( 'search' => $random_billing_first_name ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals( 1, \count( $data ) );
$this->assertEquals( $customer5->get_id(), $data[0]['id'] );

// search for billing_last_name
$request->set_query_params( array( 'search' => $random_billing_last_name ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals( 1, \count( $data ) );
$this->assertEquals( $customer6->get_id(), $data[0]['id'] );

// search for billing_last_name
$request->set_query_params( array( 'search' => $random_billing_email ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals( 1, \count( $data ) );
$this->assertEquals( $customer7->get_id(), $data[0]['id'] );

// search for billing_company
$request->set_query_params( array( 'search' => $random_billing_company ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals( 1, \count( $data ) );
$this->assertEquals( $customer8->get_id(), $data[0]['id'] );

// search for billing_last_name
$request->set_query_params( array( 'search' => $random_billing_phone ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals(200, $response->get_status());
$this->assertEquals( 1, \count( $data ) );
Expand All @@ -385,7 +390,7 @@ public function test_create_customer(): void {
'password' => '',
)
);
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
// print_r($data);

Expand All @@ -404,15 +409,15 @@ public function test_update_customer(): void {
);

$request = $this->wp_rest_get_request('/wcpos/v1/customers/' . $customer->get_id() );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'Sarah', $data['first_name'] );

$request = $this->wp_rest_post_request('/wcpos/v1/customers/' . $customer->get_id() );
$data['first_name'] = 'Jane';
$request->set_body_params($data);
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();

$this->assertEquals( 200, $response->get_status() );
Expand Down
25 changes: 15 additions & 10 deletions tests/includes/API/Test_Orders_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,29 @@ public function test_order_response_contains_uuid_meta_data(): void {
$this->assertTrue(Uuid::isValid($uuid_value), 'The UUID value is not valid.');
}

/**
* This works on the app, but not in the test??
*/
public function test_orderby_status(): void {
$order1 = OrderHelper::create_order( array( 'status' => 'pending' ) );
$order2 = OrderHelper::create_order( array( 'status' => 'completed' ) );
$order3 = OrderHelper::create_order( array( 'status' => 'on-hold' ) );
$order4 = OrderHelper::create_order( array( 'status' => 'processing' ) );
$request = $this->wp_rest_get_request( '/wcpos/v1/orders' );
$request->set_query_params( array( 'orderby' => 'status', 'order' => 'asc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$statuses = wp_list_pluck( $data, 'status' );

$this->assertEquals( $statuses, array( 'completed', 'pending' ) );
$this->assertEquals( $statuses, array( 'completed', 'on-hold', 'pending', 'processing' ) );

// reverse order
$request->set_query_params( array( 'orderby' => 'status', 'order' => 'desc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$statuses = wp_list_pluck( $data, 'status' );

$this->assertEquals( $statuses, array( 'pending', 'completed' ) );
$this->assertEquals( $statuses, array( 'processing', 'pending', 'on-hold', 'completed' ) );
}

public function test_orderby_customer(): void {
Expand All @@ -189,15 +194,15 @@ public function test_orderby_customer(): void {
$order2 = OrderHelper::create_order();
$request = $this->wp_rest_get_request( '/wcpos/v1/orders' );
$request->set_query_params( array( 'orderby' => 'customer_id', 'order' => 'asc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$customer_ids = wp_list_pluck( $data, 'customer_id' );

$this->assertEquals( $customer_ids, array( 1, $customer->get_id() ) );

// reverse order
$request->set_query_params( array( 'orderby' => 'customer_id', 'order' => 'desc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$customer_ids = wp_list_pluck( $data, 'customer_id' );

Expand All @@ -209,15 +214,15 @@ public function test_orderby_payment_method(): void {
$order2 = OrderHelper::create_order( array( 'payment_method' => 'pos_card' ) );
$request = $this->wp_rest_get_request( '/wcpos/v1/orders' );
$request->set_query_params( array( 'orderby' => 'payment_method', 'order' => 'asc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$payment_methods = wp_list_pluck( $data, 'payment_method_title' );

$this->assertEquals( $payment_methods, array( 'Card', 'Cash' ) );

// reverse order
$request->set_query_params( array( 'orderby' => 'payment_method', 'order' => 'desc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$payment_methods = wp_list_pluck( $data, 'payment_method_title' );

Expand All @@ -229,15 +234,15 @@ public function test_orderby_total(): void {
$order2 = OrderHelper::create_order( array( 'total' => 200 ) );
$request = $this->wp_rest_get_request( '/wcpos/v1/orders' );
$request->set_query_params( array( 'orderby' => 'total', 'order' => 'asc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$totals = wp_list_pluck( $data, 'total' );

$this->assertEquals( $totals, array( 100, 200 ) );

// reverse order
$request->set_query_params( array( 'orderby' => 'total', 'order' => 'desc' ) );
$response = rest_get_server()->dispatch( $request );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$totals = wp_list_pluck( $data, 'total' );

Expand Down
Loading

0 comments on commit 8b8358c

Please sign in to comment.