diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php index 52f80ee2196..2039437b32b 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-customers-controller.php @@ -116,9 +116,14 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control * @return WP_REST_Response */ public function prepare_item_for_response( $report, $request ) { - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $report, $request ); - $data = $this->filter_response_by_context( $data, $context ); + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; + $data = $this->add_additional_fields_to_object( $report, $request ); + $data['date_registered_gmt'] = wc_rest_prepare_date_response( $data['date_registered'] ); + $data['date_registered'] = wc_rest_prepare_date_response( $data['date_registered'], false ); + $data['date_last_active_gmt'] = wc_rest_prepare_date_response( $data['date_last_active'] ); + $data['date_last_active'] = wc_rest_prepare_date_response( $data['date_last_active'], false ); + $data = $this->filter_response_by_context( $data, $context ); + // Wrap the data in a response object. $response = rest_ensure_response( $data ); $response->add_links( $this->prepare_links( $report ) ); diff --git a/plugins/woocommerce-admin/tests/api/reports-customers.php b/plugins/woocommerce-admin/tests/api/reports-customers.php index 8f765a987da..84064a9bdbf 100644 --- a/plugins/woocommerce-admin/tests/api/reports-customers.php +++ b/plugins/woocommerce-admin/tests/api/reports-customers.php @@ -46,6 +46,28 @@ class WC_Tests_API_Reports_Customers extends WC_REST_Unit_Test_Case { $this->assertArrayHasKey( $this->endpoint, $routes ); } + /** + * Asserts the report item schema is correct. + * + * @param array $schema Item to check schema. + */ + public function assert_report_item_schema( $schema ) { + $this->assertArrayHasKey( 'customer_id', $schema ); + $this->assertArrayHasKey( 'user_id', $schema ); + $this->assertArrayHasKey( 'name', $schema ); + $this->assertArrayHasKey( 'username', $schema ); + $this->assertArrayHasKey( 'country', $schema ); + $this->assertArrayHasKey( 'city', $schema ); + $this->assertArrayHasKey( 'postcode', $schema ); + $this->assertArrayHasKey( 'date_registered', $schema ); + $this->assertArrayHasKey( 'date_registered_gmt', $schema ); + $this->assertArrayHasKey( 'date_last_active', $schema ); + $this->assertArrayHasKey( 'date_last_active_gmt', $schema ); + $this->assertArrayHasKey( 'orders_count', $schema ); + $this->assertArrayHasKey( 'total_spend', $schema ); + $this->assertArrayHasKey( 'avg_order_value', $schema ); + } + /** * Test reports schema. * @@ -59,21 +81,8 @@ class WC_Tests_API_Reports_Customers extends WC_REST_Unit_Test_Case { $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertEquals( 14, count( $properties ) ); - $this->assertArrayHasKey( 'customer_id', $properties ); - $this->assertArrayHasKey( 'user_id', $properties ); - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'username', $properties ); - $this->assertArrayHasKey( 'country', $properties ); - $this->assertArrayHasKey( 'city', $properties ); - $this->assertArrayHasKey( 'postcode', $properties ); - $this->assertArrayHasKey( 'date_registered', $properties ); - $this->assertArrayHasKey( 'date_registered_gmt', $properties ); - $this->assertArrayHasKey( 'date_last_active', $properties ); - $this->assertArrayHasKey( 'date_last_active_gmt', $properties ); - $this->assertArrayHasKey( 'orders_count', $properties ); - $this->assertArrayHasKey( 'total_spend', $properties ); - $this->assertArrayHasKey( 'avg_order_value', $properties ); + $this->assertCount( 14, $properties ); + $this->assert_report_item_schema( $properties ); } /** @@ -141,5 +150,6 @@ class WC_Tests_API_Reports_Customers extends WC_REST_Unit_Test_Case { $this->assertEquals( $test_customers[0]->get_id(), $reports[0]['user_id'] ); $this->assertEquals( 1, $reports[0]['orders_count'] ); $this->assertEquals( 100, $reports[0]['total_spend'] ); + $this->assert_report_item_schema( $reports[0] ); } }