diff --git a/includes/API/Customers_Controller.php b/includes/API/Customers_Controller.php index 1ff7206..d3e62df 100644 --- a/includes/API/Customers_Controller.php +++ b/includes/API/Customers_Controller.php @@ -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 diff --git a/includes/API/Orders_Controller.php b/includes/API/Orders_Controller.php index 55287ca..69ce32c 100644 --- a/includes/API/Orders_Controller.php +++ b/includes/API/Orders_Controller.php @@ -17,7 +17,6 @@ use const WCPOS\WooCommercePOS\PLUGIN_NAME; use WP_REST_Request; use WP_REST_Response; - use WP_REST_Server; /** @@ -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. * @@ -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; @@ -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; diff --git a/tests/includes/API/Test_Customers_Controller.php b/tests/includes/API/Test_Customers_Controller.php index 445e4ac..18c4346 100644 --- a/tests/includes/API/Test_Customers_Controller.php +++ b/tests/includes/API/Test_Customers_Controller.php @@ -140,7 +140,7 @@ 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' ); @@ -148,7 +148,7 @@ public function test_orderby_first_name(): void { // 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' ); @@ -164,7 +164,7 @@ 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' ); @@ -172,7 +172,7 @@ public function test_orderby_last_name(): void { // 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' ); @@ -188,7 +188,7 @@ 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' ); @@ -196,13 +196,18 @@ public function test_orderby_email(): void { // 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( 'sarah.smith@sample.com', 'john.doe@example.com', 'alex.miller@demo.net' ) ); } + /** + * 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')); @@ -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' ); @@ -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' ); @@ -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' ); @@ -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' ); @@ -294,14 +299,14 @@ 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 ) ); @@ -309,7 +314,7 @@ public function test_customer_search(): void { // 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 ) ); @@ -317,7 +322,7 @@ public function test_customer_search(): void { // 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 ) ); @@ -325,7 +330,7 @@ public function test_customer_search(): void { // 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 ) ); @@ -333,7 +338,7 @@ public function test_customer_search(): void { // 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 ) ); @@ -341,7 +346,7 @@ public function test_customer_search(): void { // 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 ) ); @@ -349,7 +354,7 @@ public function test_customer_search(): void { // 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 ) ); @@ -357,7 +362,7 @@ public function test_customer_search(): void { // 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 ) ); @@ -365,7 +370,7 @@ public function test_customer_search(): void { // 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 ) ); @@ -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); @@ -404,7 +409,7 @@ 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'] ); @@ -412,7 +417,7 @@ public function test_update_customer(): void { $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() ); diff --git a/tests/includes/API/Test_Orders_Controller.php b/tests/includes/API/Test_Orders_Controller.php index aa454df..78beed9 100644 --- a/tests/includes/API/Test_Orders_Controller.php +++ b/tests/includes/API/Test_Orders_Controller.php @@ -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 { @@ -189,7 +194,7 @@ 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' ); @@ -197,7 +202,7 @@ public function test_orderby_customer(): void { // 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' ); @@ -209,7 +214,7 @@ 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' ); @@ -217,7 +222,7 @@ public function test_orderby_payment_method(): void { // 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' ); @@ -229,7 +234,7 @@ 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' ); @@ -237,7 +242,7 @@ public function test_orderby_total(): void { // 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' ); diff --git a/tests/includes/API/Test_Product_Variations_Controller.php b/tests/includes/API/Test_Product_Variations_Controller.php index ad189d9..9c6283e 100644 --- a/tests/includes/API/Test_Product_Variations_Controller.php +++ b/tests/includes/API/Test_Product_Variations_Controller.php @@ -92,6 +92,9 @@ public function get_expected_response_fields() { '_links', // Added by WCPOS. 'barcode', + // Added in WooCommerce 8.3.0 :/ + 'name', + 'parent_id', ); } @@ -249,7 +252,7 @@ public function test_variation_orderby_sku(): void { $product = ProductHelper::create_variation_product(); $request = $this->wp_rest_get_request( '/wcpos/v1/products/' . $product->get_id() . '/variations'); $request->set_query_params( array( 'orderby' => 'sku', 'order' => 'asc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'sku' ); @@ -257,7 +260,7 @@ public function test_variation_orderby_sku(): void { // reverse order $request->set_query_params( array( 'orderby' => 'sku', 'order' => 'desc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'sku' ); @@ -278,7 +281,7 @@ public function test_variation_orderby_barcode(): void { $request = $this->wp_rest_get_request('/wcpos/v1/products/' . $product->get_id() . '/variations'); $request->set_query_params( array( 'orderby' => 'barcode', 'order' => 'asc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $barcodes = wp_list_pluck( $data, 'barcode' ); @@ -286,7 +289,7 @@ public function test_variation_orderby_barcode(): void { // reverse order $request->set_query_params( array( 'orderby' => 'barcode', 'order' => 'desc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $barcodes = wp_list_pluck( $data, 'barcode' ); @@ -301,7 +304,7 @@ public function test_variation_orderby_stock_status(): void { $request = $this->wp_rest_get_request('/wcpos/v1/products/' . $product->get_id() . '/variations'); $request->set_query_params( array( 'orderby' => 'stock_status', 'order' => 'asc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'stock_status' ); @@ -309,7 +312,7 @@ public function test_variation_orderby_stock_status(): void { // reverse order $request->set_query_params( array( 'orderby' => 'stock_status', 'order' => 'desc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'stock_status' ); @@ -326,7 +329,7 @@ public function test_variation_orderby_stock_quantity(): void { $request = $this->wp_rest_get_request('/wcpos/v1/products/' . $product->get_id() . '/variations'); $request->set_query_params( array( 'orderby' => 'stock_quantity', 'order' => 'asc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'stock_quantity' ); @@ -334,7 +337,7 @@ public function test_variation_orderby_stock_quantity(): void { // reverse order $request->set_query_params( array( 'orderby' => 'stock_quantity', 'order' => 'desc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'stock_quantity' ); @@ -348,23 +351,16 @@ public function test_variation_decimal_stock_quantity_schema(): void { $schema = $this->endpoint->get_item_schema(); $this->assertEquals( 'integer', $schema['properties']['stock_quantity']['type'] ); - add_filter( 'woocommerce_pos_general_settings', function() { - return array( - 'decimal_qty' => true, - ); - }); + $this->setup_decimal_quantity_tests(); + $this->assertTrue( woocommerce_pos_get_settings( 'general', 'decimal_qty' ) ); + $schema = $this->endpoint->get_item_schema(); $this->assertEquals( 'string', $schema['properties']['stock_quantity']['type'] ); } public function test_variation_response_with_decimal_quantities(): void { - add_filter( 'woocommerce_pos_general_settings', function() { - return array( - 'decimal_qty' => true, - ); - }); - remove_filter('woocommerce_stock_amount', 'intval'); - add_filter( 'woocommerce_stock_amount', 'floatval' ); + $this->setup_decimal_quantity_tests(); + $this->assertTrue( woocommerce_pos_get_settings( 'general', 'decimal_qty' ) ); $product = ProductHelper::create_variation_product(); $variation_ids = $product->get_children(); @@ -385,13 +381,8 @@ public function test_variation_response_with_decimal_quantities(): void { // @TODO - this works in the POS, but not in the tests, I have no idea why public function test_variation_update_decimal_quantities(): void { - add_filter( 'woocommerce_pos_general_settings', function() { - return array( - 'decimal_qty' => true, - ); - }); - remove_filter('woocommerce_stock_amount', 'intval'); - add_filter( 'woocommerce_stock_amount', 'floatval' ); + $this->setup_decimal_quantity_tests(); + $this->assertTrue( woocommerce_pos_get_settings( 'general', 'decimal_qty' ) ); $product = ProductHelper::create_variation_product(); $variation_ids = $product->get_children(); @@ -413,13 +404,8 @@ public function test_variation_update_decimal_quantities(): void { } public function test_variation_orderby_decimal_stock_quantity(): void { - add_filter( 'woocommerce_pos_general_settings', function() { - return array( - 'decimal_qty' => true, - ); - }); - remove_filter('woocommerce_stock_amount', 'intval'); - add_filter( 'woocommerce_stock_amount', 'floatval' ); + $this->setup_decimal_quantity_tests(); + $this->assertTrue( woocommerce_pos_get_settings( 'general', 'decimal_qty' ) ); $product = ProductHelper::create_variation_product(); $variation_ids = $product->get_children(); @@ -429,7 +415,7 @@ public function test_variation_orderby_decimal_stock_quantity(): void { update_post_meta( $variation_ids[1], '_manage_stock', 'yes' ); $request = $this->wp_rest_get_request('/wcpos/v1/products/' . $product->get_id() . '/variations'); $request->set_query_params( array( 'orderby' => 'stock_quantity', 'order' => 'asc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'stock_quantity' ); @@ -437,10 +423,16 @@ public function test_variation_orderby_decimal_stock_quantity(): void { // reverse order $request->set_query_params( array( 'orderby' => 'stock_quantity', 'order' => 'desc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'stock_quantity' ); $this->assertEquals( $skus, array( 11.2, 3.5 ) ); } + + // /** + // * Variations should order by menu_order by default. + // */ + // public function test_variation_orderby_menu_order(): void { + // } } diff --git a/tests/includes/API/Test_Products_Controller.php b/tests/includes/API/Test_Products_Controller.php index 0ef5f34..3d2d195 100644 --- a/tests/includes/API/Test_Products_Controller.php +++ b/tests/includes/API/Test_Products_Controller.php @@ -5,7 +5,6 @@ use Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper; use Ramsey\Uuid\Uuid; use WCPOS\WooCommercePOS\API\Products_Controller; -use WP_Test_Spy_REST_Server; /** * @internal @@ -252,7 +251,7 @@ public function test_product_orderby_sku(): void { $product4 = ProductHelper::create_simple_product( array( 'sku' => 'alpha' ) ); $request = $this->wp_rest_get_request( '/wcpos/v1/products' ); $request->set_query_params( array( 'orderby' => 'sku', 'order' => 'asc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'sku' ); @@ -260,7 +259,7 @@ public function test_product_orderby_sku(): void { // reverse order $request->set_query_params( array( 'orderby' => 'sku', 'order' => 'desc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'sku' ); @@ -284,7 +283,7 @@ public function test_product_orderby_barcode(): void { $request = $this->wp_rest_get_request( '/wcpos/v1/products' ); $request->set_query_params( array( 'orderby' => 'barcode', 'order' => 'asc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $barcodes = wp_list_pluck( $data, 'barcode' ); @@ -292,7 +291,7 @@ public function test_product_orderby_barcode(): void { // reverse order $request->set_query_params( array( 'orderby' => 'barcode', 'order' => 'desc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $barcodes = wp_list_pluck( $data, 'barcode' ); @@ -304,7 +303,7 @@ public function test_product_orderby_stock_status(): void { $product2 = ProductHelper::create_simple_product( array( 'stock_status' => 'outofstock' ) ); $request = $this->wp_rest_get_request( '/wcpos/v1/products' ); $request->set_query_params( array( 'orderby' => 'stock_status', 'order' => 'asc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'stock_status' ); @@ -312,7 +311,7 @@ public function test_product_orderby_stock_status(): void { // reverse order $request->set_query_params( array( 'orderby' => 'stock_status', 'order' => 'desc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'stock_status' ); @@ -328,7 +327,7 @@ public function test_product_orderby_stock_quantity(): void { $product6 = ProductHelper::create_simple_product(); $request = $this->wp_rest_get_request( '/wcpos/v1/products' ); $request->set_query_params( array( 'orderby' => 'stock_quantity', 'order' => 'asc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'stock_quantity' ); @@ -336,7 +335,7 @@ public function test_product_orderby_stock_quantity(): void { // reverse order $request->set_query_params( array( 'orderby' => 'stock_quantity', 'order' => 'desc' ) ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch( $request ); $data = $response->get_data(); $skus = wp_list_pluck( $data, 'stock_quantity' ); @@ -350,23 +349,16 @@ public function test_product_decimal_stock_quantity_schema(): void { $schema = $this->endpoint->get_item_schema(); $this->assertEquals( 'integer', $schema['properties']['stock_quantity']['type'] ); - add_filter( 'woocommerce_pos_general_settings', function() { - return array( - 'decimal_qty' => true, - ); - }); + $this->setup_decimal_quantity_tests(); + $this->assertTrue( woocommerce_pos_get_settings( 'general', 'decimal_qty' ) ); + $schema = $this->endpoint->get_item_schema(); $this->assertEquals( 'string', $schema['properties']['stock_quantity']['type'] ); } public function test_product_response_with_decimal_quantities(): void { - add_filter( 'woocommerce_pos_general_settings', function() { - return array( - 'decimal_qty' => true, - ); - }); - remove_filter('woocommerce_stock_amount', 'intval'); - add_filter( 'woocommerce_stock_amount', 'floatval' ); + $this->setup_decimal_quantity_tests(); + $this->assertTrue( woocommerce_pos_get_settings( 'general', 'decimal_qty' ) ); $product = ProductHelper::create_simple_product(); $product->set_manage_stock( true ); @@ -385,13 +377,8 @@ public function test_product_response_with_decimal_quantities(): void { // @TODO - this works in the POS, but not in the tests, I have no idea why public function test_product_update_decimal_quantities(): void { - add_filter( 'woocommerce_pos_general_settings', function() { - return array( - 'decimal_qty' => true, - ); - }); - remove_filter('woocommerce_stock_amount', 'intval'); - add_filter( 'woocommerce_stock_amount', 'floatval' ); + $this->setup_decimal_quantity_tests(); + $this->assertTrue( woocommerce_pos_get_settings( 'general', 'decimal_qty' ) ); $product = ProductHelper::create_simple_product(); $product->set_manage_stock( true ); @@ -401,11 +388,8 @@ public function test_product_update_decimal_quantities(): void { $request->set_body_params( array( 'stock_quantity' => '3.85', ) ); - // $server = rest_get_server(); // re-init the server, so that params are re-read?? - $wp_rest_server = new WP_Test_Spy_REST_Server(); - $response = $wp_rest_server->dispatch($request); - - $data = $response->get_data(); + $response = $this->server->dispatch($request); + $data = $response->get_data(); $this->assertEquals(200, $response->get_status()); @@ -413,31 +397,27 @@ public function test_product_update_decimal_quantities(): void { } public function test_product_orderby_decimal_stock_quantity(): void { - add_filter( 'woocommerce_pos_general_settings', function() { - return array( - 'decimal_qty' => true, - ); - }); - remove_filter('woocommerce_stock_amount', 'intval'); - add_filter( 'woocommerce_stock_amount', 'floatval' ); + $this->setup_decimal_quantity_tests(); + $this->assertTrue( woocommerce_pos_get_settings( 'general', 'decimal_qty' ) ); $product1 = ProductHelper::create_simple_product( array( 'stock_quantity' => '11.2', 'manage_stock' => true ) ); $product2 = ProductHelper::create_simple_product( array( 'stock_quantity' => '3.5', 'manage_stock' => true ) ); + $product2 = ProductHelper::create_simple_product( array( 'stock_quantity' => '20.7', 'manage_stock' => true ) ); $request = $this->wp_rest_get_request( '/wcpos/v1/products' ); $request->set_query_params( array( 'orderby' => 'stock_quantity', 'order' => 'asc' ) ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $skus = wp_list_pluck( $data, 'stock_quantity' ); + $response = $this->server->dispatch($request); + $data = $response->get_data(); + $skus = wp_list_pluck( $data, 'stock_quantity' ); - $this->assertEquals( $skus, array( 3.5, 11.2 ) ); + $this->assertEquals( $skus, array( 3.5, 11.2, 20.7 ) ); // reverse order $request->set_query_params( array( 'orderby' => 'stock_quantity', 'order' => 'desc' ) ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $skus = wp_list_pluck( $data, 'stock_quantity' ); + $response = $this->server->dispatch($request); + $data = $response->get_data(); + $skus = wp_list_pluck( $data, 'stock_quantity' ); - $this->assertEquals( $skus, array( 11.2, 3.5 ) ); + $this->assertEquals( $skus, array( 20.7, 11.2, 3.5 ) ); } public function test_product_search(): void { @@ -463,14 +443,14 @@ public function test_product_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( 4, \count( $data ) ); // search for title $request->set_query_params( array( 'search' => $random_title ) ); - $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 ) ); @@ -478,7 +458,7 @@ public function test_product_search(): void { // search for sku $request->set_query_params( array( 'search' => $random_sku ) ); - $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 ) ); @@ -486,7 +466,7 @@ public function test_product_search(): void { // search for barcode $request->set_query_params( array( 'search' => $random_barcode ) ); - $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 ) ); @@ -525,7 +505,7 @@ public function test_pos_only_products(): void { $request = $this->wp_rest_get_request( '/wcpos/v1/products' ); $request->set_param( 'posts_per_page', -1 ); $request->set_param( 'fields', array('id') ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertEquals(200, $response->get_status()); $this->assertEquals( 3, \count( $data ) ); @@ -535,7 +515,7 @@ public function test_pos_only_products(): void { // test products response $request = $this->wp_rest_get_request( '/wcpos/v1/products' ); - $response = rest_get_server()->dispatch( $request ); + $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertEquals(200, $response->get_status()); $this->assertEquals( 3, \count( $data ) ); diff --git a/tests/includes/API/WCPOS_REST_Unit_Test_Case.php b/tests/includes/API/WCPOS_REST_Unit_Test_Case.php index a4196b7..be829e1 100644 --- a/tests/includes/API/WCPOS_REST_Unit_Test_Case.php +++ b/tests/includes/API/WCPOS_REST_Unit_Test_Case.php @@ -79,4 +79,12 @@ public function get_reflected_property_value($propertyName) { return $property->getValue($this->endpoint); } + + protected function setup_decimal_quantity_tests(): void { + add_filter( 'woocommerce_pos_general_settings', function() { + return array('decimal_qty' => true); + }); + remove_filter('woocommerce_stock_amount', 'intval'); + add_filter('woocommerce_stock_amount', 'floatval'); + } }