From 071d913b0ff041233087675f7a2062af0dc74233 Mon Sep 17 00:00:00 2001 From: kidinov Date: Wed, 21 Jun 2023 15:15:27 +0200 Subject: [PATCH] Added tests --- .../Admin/API/Reports/Customers/DataStore.php | 10 +-- .../api/reports-customers.php | 78 +++++++++++++++++++ 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/plugins/woocommerce/src/Admin/API/Reports/Customers/DataStore.php b/plugins/woocommerce/src/Admin/API/Reports/Customers/DataStore.php index eb4a537c379..2ced88f2021 100644 --- a/plugins/woocommerce/src/Admin/API/Reports/Customers/DataStore.php +++ b/plugins/woocommerce/src/Admin/API/Reports/Customers/DataStore.php @@ -321,11 +321,11 @@ class DataStore extends ReportsDataStore implements DataStoreInterface { ); if ( ! empty( $query_args['filter_empty'] ) ) { - $fields_to_filter_by = array_intersect( $query_args['filter_empty'], $filter_empty_params ); - if ( in_array( 'name', $fields_to_filter_by, true ) ) { - $fields_to_filter_by = array_diff( $fields_to_filter_by, array( 'name' ) ); - $fields_to_filter_by[] = "CONCAT_WS( ' ', first_name, last_name )"; - } + $fields_to_filter_by = array_intersect( $query_args['filter_empty'], $filter_empty_params ); + if ( in_array( 'name', $fields_to_filter_by, true ) ) { + $fields_to_filter_by = array_diff( $fields_to_filter_by, array( 'name' ) ); + $fields_to_filter_by[] = "CONCAT_WS( ' ', first_name, last_name )"; + } $fields_with_not_condition = array_map( function ( $field ) { return $field . ' <> \'\''; diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-customers.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-customers.php index b4debfab642..9c2bea32007 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-customers.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-customers.php @@ -334,6 +334,84 @@ class WC_Admin_Tests_API_Reports_Customers extends WC_REST_Unit_Test_Case { $this->assertEquals( 200, $response->get_status() ); $this->assertCount( 1, $reports ); $this->assertEquals( $test_customers[0]->get_id(), $reports[0]['user_id'] ); + + // Test filter_empty param by name + $request->set_query_params( + array( + 'filter_empty' => array('name'), + ) + ); + + $response = $this->server->dispatch( $request ); + $reports = $response->get_data(); + $this->assertEquals( 200, $response->get_status() ); + $this->assertCount( 10, $reports ); + + // Test filter_empty param by name and email + $request->set_query_params( + array( + 'filter_empty' => array('name', 'email'), + ) + ); + $this->assertEquals( 200, $response->get_status() ); + $this->assertCount( 10, $reports ); + + // Test filter_empty param by name and email + $request->set_query_params( + array( + 'filter_empty' => array('name', 'country'), + ) + ); + $this->assertEquals( 200, $response->get_status() ); + $this->assertCount( 10, $reports ); + + // Test filter_empty param by state + $request->set_query_params( + array( + 'filter_empty' => array('state') + ) + ); + $this->assertEquals( 200, $response->get_status() ); + $this->assertCount( 10, $reports ); + + // Test filter_empty param by state + $request->set_query_params( + array( + 'filter_empty' => array('postcode'), + ) + ); + $this->assertEquals( 200, $response->get_status() ); + $this->assertCount( 10, $reports ); + + $customer = WC_Helper_Customer::create_customer( "customer11", 'password', "" ); + $customer->set_postcode(null); + $customer->save(); + + $request->set_query_params( + array( + 'filter_empty' => array('postcode'), + ) + ); + $this->assertEquals( 200, $response->get_status() ); + $this->assertCount( 10, $reports ); + + // Test filter_empty param by email + $request->set_query_params( + array( + 'filter_empty' => array('email'), + ) + ); + $this->assertEquals( 200, $response->get_status() ); + $this->assertCount( 10, $reports ); + + // Test filter_empty param by name + $request->set_query_params( + array( + 'filter_empty' => array('name'), + ) + ); + $this->assertEquals( 200, $response->get_status() ); + $this->assertCount( 10, $reports ); } /**