Merge pull request woocommerce/woocommerce-admin#2496 from woocommerce/fix/rest-controller-default-params

Test that REST controllers work with their default parameters.
This commit is contained in:
Jeff Stieler 2019-06-28 08:52:16 -06:00 committed by GitHub
commit 8c40000ec2
4 changed files with 96 additions and 0 deletions

View File

@ -36,6 +36,8 @@ class WC_Admin_REST_Orders_Controller extends WC_REST_Orders_Controller {
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
// Fix the default 'status' value until it can be patched in core.
$params['status']['default'] = array( 'any' );
return $params;
}

View File

@ -501,7 +501,9 @@ class WC_Admin_REST_Reports_Performance_Indicators_Controller extends WC_REST_Re
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'type' => 'string',
'enum' => $this->allowed_stats,
),
'default' => $this->allowed_stats,
);
$params['after'] = array(
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce-admin' ),

View File

@ -0,0 +1,91 @@
<?php
/**
* REST Controller Tests
*
* @package WooCommerce\Tests\API
* @since 3.6.4
*/
/**
* REST Controller Tests Class
*
* @package WooCommerce\Tests\API
* @since 3.6.4
*/
class WC_Tests_API_Report_Controllers extends WC_REST_Unit_Test_Case {
/**
* Setup test admin notes data. Called before every test.
*
* @since 3.5.0
*/
public function setUp() {
parent::setUp();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
wp_set_current_user( $this->user );
}
/**
* Ensure that every REST controller works with their defaults.
*/
public function test_default_params() {
// These controllers intentionally missing:
// - `WC_Admin_REST_Admin_Note_Action_Controller`
// - `WC_Admin_REST_Setting_Options_Controller`
// - `WC_Admin_REST_Data_Download_Ips_Controller`
// - `WC_Admin_REST_Product_Variations_Controller`
// - `WC_Admin_REST_Reports_Import_Controller`
// because they don't have defaults for required params or a get_items() method.
$endpoints = array(
'/wc/v4/admin/notes',
'/wc/v4/coupons',
'/wc/v4/customers',
'/wc/v4/data',
'/wc/v4/data/countries',
'/wc/v4/leaderboards',
'/wc/v4/orders',
'/wc/v4/products',
'/wc/v4/products/categories',
'/wc/v4/reports',
'/wc/v4/reports/products',
'/wc/v4/reports/variations',
'/wc/v4/reports/products/stats',
'/wc/v4/reports/revenue/stats',
'/wc/v4/reports/orders',
'/wc/v4/reports/orders/stats',
'/wc/v4/reports/categories',
'/wc/v4/reports/taxes',
'/wc/v4/reports/taxes/stats',
'/wc/v4/reports/coupons',
'/wc/v4/reports/coupons/stats',
'/wc/v4/reports/stock',
'/wc/v4/reports/stock/stats',
'/wc/v4/reports/downloads',
'/wc/v4/reports/downloads/stats',
'/wc/v4/reports/customers',
'/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 ) {
$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() ) );
}
}
}

View File

@ -89,6 +89,7 @@ function wc_test_includes() {
function _manually_load_plugin() {
define( 'WC_TAX_ROUNDING_MODE', 'auto' );
define( 'WC_USE_TRANSACTIONS', false );
update_option( 'woocommerce_enable_coupons', 'yes' );
require_once wc_dir() . '/woocommerce.php';
wc_admin_install();