From 5ee318064aa758f7a0a75049591cf50ca4060464 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Tue, 15 Oct 2019 10:46:12 -0400 Subject: [PATCH] Remove the Onboarding Levels API (https://github.com/woocommerce/woocommerce-admin/pull/3041) --- plugins/woocommerce-admin/src/API/Init.php | 1 - .../src/API/OnboardingLevels.php | 266 ------------------ .../tests/api/onboarding-levels.php | 107 ------- .../tests/api/onboarding-profile.php | 22 ++ .../tests/api/report-controllers.php | 3 - 5 files changed, 22 insertions(+), 377 deletions(-) delete mode 100644 plugins/woocommerce-admin/src/API/OnboardingLevels.php delete mode 100644 plugins/woocommerce-admin/tests/api/onboarding-levels.php diff --git a/plugins/woocommerce-admin/src/API/Init.php b/plugins/woocommerce-admin/src/API/Init.php index 217f5e50678..f8081c28bee 100644 --- a/plugins/woocommerce-admin/src/API/Init.php +++ b/plugins/woocommerce-admin/src/API/Init.php @@ -79,7 +79,6 @@ class Init { $controllers = array_merge( $controllers, array( - 'Automattic\WooCommerce\Admin\API\OnboardingLevels', 'Automattic\WooCommerce\Admin\API\OnboardingProfile', 'Automattic\WooCommerce\Admin\API\OnboardingPlugins', 'Automattic\WooCommerce\Admin\API\OnboardingTasks', diff --git a/plugins/woocommerce-admin/src/API/OnboardingLevels.php b/plugins/woocommerce-admin/src/API/OnboardingLevels.php deleted file mode 100644 index 8a16c9bafdf..00000000000 --- a/plugins/woocommerce-admin/src/API/OnboardingLevels.php +++ /dev/null @@ -1,266 +0,0 @@ -namespace, - '/' . $this->rest_base, - array( - array( - 'methods' => \WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_items' ), - 'permission_callback' => array( $this, 'get_items_permissions_check' ), - ), - 'schema' => array( $this, 'get_public_item_schema' ), - ) - ); - } - - /** - * Get an array of all levels and child tasks. - * - * @todo Status values below should pull from the database task status once implemented. - */ - public function get_levels() { - $levels = array( - 'account' => array( - 'tasks' => array( - 'create_account' => array( - 'label' => __( 'Create an account', 'woocommerce-admin' ), - 'description' => __( 'Speed up & secure your store', 'woocommerce-admin' ), - 'illustration' => '', - 'status' => 'visible', - 'is_required' => false, - ), - ), - ), - 'storefront' => array( - 'tasks' => array( - 'add_products' => array( - 'label' => __( 'Add your products', 'woocommerce-admin' ), - 'description' => __( 'Bring your store to life', 'woocommerce-admin' ), - 'illustration' => '', - 'status' => 'visible', - 'is_required' => true, - ), - 'customize_appearance' => array( - 'label' => __( 'Customize Appearance', 'woocommerce-admin' ), - 'description' => __( 'Ensure your store is on-brand', 'woocommerce-admin' ), - 'illustration' => '', - 'status' => 'visible', - 'is_required' => false, - ), - ), - ), - 'checkout' => array( - 'id' => 'checkout', - 'tasks' => array( - 'configure_shipping' => array( - 'label' => __( 'Configure shipping', 'woocommerce-admin' ), - 'description' => __( 'Set up prices and destinations', 'woocommerce-admin' ), - 'illustration' => '', - 'status' => 'visible', - 'is_required' => true, - ), - 'configure_taxes' => array( - 'label' => __( 'Configure taxes', 'woocommerce-admin' ), - 'description' => __( 'Set up sales tax rates', 'woocommerce-admin' ), - 'illustration' => '', - 'status' => 'visible', - 'is_required' => false, - ), - 'configure_payments' => array( - 'label' => __( 'Configure payments', 'woocommerce-admin' ), - 'description' => __( 'Choose payment providers', 'woocommerce-admin' ), - 'illustration' => '', - 'status' => 'visible', - 'is_required' => true, - ), - ), - ), - ); - - return apply_filters( 'woocommerce_onboarding_levels', $levels ); - } - - /** - * Return all level items and child tasks. - * - * @param WP_REST_Request $request Request data. - * @return WP_Error|WP_REST_Response - */ - public function get_items( $request ) { - global $wpdb; - - $levels = $this->get_levels(); - $data = array(); - - if ( ! empty( $levels ) ) { - foreach ( $levels as $id => $level ) { - $level = $this->convert_to_non_associative( $level, $id ); - $response = $this->prepare_item_for_response( $level, $request ); - $data[] = $this->prepare_response_for_collection( $response ); - } - } - - return rest_ensure_response( $data ); - } - - /** - * Prepare the data object for response. - * - * @param object $item Data object. - * @param WP_REST_Request $request Request object. - * @return WP_REST_Response $response Response data. - */ - public function prepare_item_for_response( $item, $request ) { - $data = $this->add_additional_fields_to_object( $item, $request ); - $data = $this->filter_response_by_context( $data, 'view' ); - $response = rest_ensure_response( $data ); - - $response->add_links( $this->prepare_links( $item ) ); - - /** - * Filter the list returned from the API. - * - * @param WP_REST_Response $response The response object. - * @param array $item The original item. - * @param WP_REST_Request $request Request used to generate the response. - */ - return apply_filters( 'woocommerce_rest_prepare_onboarding_level', $response, $item, $request ); - } - - /** - * Convert the associative levels and tasks to non-associative for JSON use. - * - * @param array $item Level. - * @param string $id Level ID. - * @return array - */ - public function convert_to_non_associative( $item, $id ) { - $item = array( 'id' => $id ) + $item; - - $tasks = array(); - foreach ( $item['tasks'] as $key => $task ) { - $tasks[] = array( 'id' => $key ) + $task; - } - $item['tasks'] = $tasks; - - return $item; - } - - /** - * Prepare links for the request. - * - * @param object $item Data object. - * @return array Links for the given object. - * @todo Check to make sure this generates a valid URL after #1897. - */ - protected function prepare_links( $item ) { - $links = array( - 'collection' => array( - 'href' => rest_url( sprintf( '/%s/onboarding/tasks?level=%s', $this->namespace, $item['id'] ) ), - ), - ); - return $links; - } - - /** - * Get the schema, conforming to JSON Schema. - * - * @return array - */ - public function get_item_schema() { - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'onboarding_level', - 'type' => 'object', - 'properties' => array( - 'id' => array( - 'type' => 'string', - 'description' => __( 'Level ID.', 'woocommerce-admin' ), - 'context' => array( 'view' ), - 'readonly' => true, - ), - 'tasks' => array( - 'type' => 'array', - 'description' => __( 'Array of tasks under the level.', 'woocommerce-admin' ), - 'context' => array( 'view' ), - 'readonly' => true, - 'items' => array( - 'type' => 'object', - 'properties' => array( - 'id' => array( - 'description' => __( 'Task ID.', 'woocommerce-admin' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'label' => array( - 'description' => __( 'Task label.', 'woocommerce-admin' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'description' => array( - 'description' => __( 'Task description.', 'woocommerce-admin' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'illustration' => array( - 'description' => __( 'URL for illustration used.', 'woocommerce-admin' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'status' => array( - 'description' => __( 'Task status.', 'woocommerce-admin' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - 'enum' => array( 'visible', 'hidden', 'in-progress', 'skipped', 'completed' ), - ), - ), - ), - ), - ), - ); - - return $this->add_additional_fields_schema( $schema ); - } -} diff --git a/plugins/woocommerce-admin/tests/api/onboarding-levels.php b/plugins/woocommerce-admin/tests/api/onboarding-levels.php deleted file mode 100644 index e7c52b05e22..00000000000 --- a/plugins/woocommerce-admin/tests/api/onboarding-levels.php +++ /dev/null @@ -1,107 +0,0 @@ -user = $this->factory->user->create( - array( - 'role' => 'administrator', - ) - ); - } - - /** - * Test that levels are returned by the endpoint. - */ - public function test_get_level_items() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'GET', $this->endpoint ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 'account', $data[0]['id'] ); - } - - /** - * Test reports schema. - * - * @since 3.5.0 - */ - public function test_schema() { - wp_set_current_user( $this->user ); - - $request = new WP_REST_Request( 'OPTIONS', $this->endpoint ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - $properties = $data['schema']['properties']; - - $this->assertCount( 2, $properties ); - $this->assert_item_schema( $properties ); - } - - /** - * Asserts the item schema is correct. - * - * @param array $schema Item to check schema. - */ - public function assert_item_schema( $schema ) { - $this->assertArrayHasKey( 'id', $schema ); - $this->assertArrayHasKey( 'tasks', $schema ); - - $task_properties = $schema['tasks']['items']['properties']; - $this->assertCount( 5, $task_properties ); - $this->assertArrayHasKey( 'id', $task_properties ); - $this->assertArrayHasKey( 'label', $task_properties ); - $this->assertArrayHasKey( 'description', $task_properties ); - $this->assertArrayHasKey( 'illustration', $task_properties ); - $this->assertArrayHasKey( 'status', $task_properties ); - } - - /** - * Test that levels response changes based on applied filters. - */ - public function test_filter_levels() { - wp_set_current_user( $this->user ); - - add_filter( - 'woocommerce_onboarding_levels', - function( $levels ) { - $levels['test_level'] = array( - 'id' => 'test_level', - 'tasks' => array(), - ); - return $levels; - } - ); - - $request = new WP_REST_Request( 'GET', $this->endpoint ); - $response = $this->server->dispatch( $request ); - $data = $response->get_data(); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 'test_level', end( $data )['id'] ); - - } -} diff --git a/plugins/woocommerce-admin/tests/api/onboarding-profile.php b/plugins/woocommerce-admin/tests/api/onboarding-profile.php index 2621589afb0..9f50be94991 100644 --- a/plugins/woocommerce-admin/tests/api/onboarding-profile.php +++ b/plugins/woocommerce-admin/tests/api/onboarding-profile.php @@ -154,4 +154,26 @@ class WC_Tests_API_Onboarding_Profiles extends WC_REST_Unit_Test_Case { $this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 'woo', $data['test_profile_datum'] ); } + + /** + * Ensure that every REST controller works with their defaults. + */ + public function test_default_params() { + $endpoints = array( + '/wc-admin/v1/onboarding/profile', + '/wc-admin/v1/onboarding/plugins', + ); + + foreach ( $endpoints as $endpoint ) { + $request = new WP_REST_Request( 'GET', $endpoint ); + $response = $this->server->dispatch( $request ); + + // Surface any errors for easier debugging. + if ( is_wp_error( $response ) ) { + $this->fail( $response->get_error_message() ); + } + + $this->assertTrue( is_array( $response->get_data() ) ); + } + } } diff --git a/plugins/woocommerce-admin/tests/api/report-controllers.php b/plugins/woocommerce-admin/tests/api/report-controllers.php index 19ba332f530..a638838dc95 100644 --- a/plugins/woocommerce-admin/tests/api/report-controllers.php +++ b/plugins/woocommerce-admin/tests/api/report-controllers.php @@ -71,9 +71,6 @@ class WC_Tests_API_Report_Controllers extends WC_REST_Unit_Test_Case { '/wc/v4/reports/customers/stats', '/wc/v4/taxes', '/wc/v4/reports/performance-indicators', - '/wc-admin/v1/onboarding/levels', - '/wc-admin/v1/onboarding/profile', - '/wc-admin/v1/onboarding/plugins', ); foreach ( $endpoints as $endpoint ) {