2016-08-25 18:48:17 +00:00
< ? php
/**
* Tests for the Shipping Methods REST API .
*
* @ package WooCommerce\Tests\API
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-08-25 18:48:17 +00:00
*/
class Shipping_Methods extends WC_REST_Unit_Test_Case {
/**
* Setup our test server , endpoints , and user info .
*/
public function setUp () {
parent :: setUp ();
$this -> endpoint = new WC_REST_Shipping_Methods_Controller ();
$this -> user = $this -> factory -> user -> create ( array (
'role' => 'administrator' ,
) );
}
/**
* Test route registration .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-08-25 18:48:17 +00:00
*/
public function test_register_routes () {
$routes = $this -> server -> get_routes ();
2017-02-09 20:21:52 +00:00
$this -> assertArrayHasKey ( '/wc/v2/shipping_methods' , $routes );
$this -> assertArrayHasKey ( '/wc/v2/shipping_methods/(?P<id>[\w-]+)' , $routes );
2016-08-25 18:48:17 +00:00
}
/**
* Test getting all shipping methods .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-08-25 18:48:17 +00:00
*/
public function test_get_shipping_methods () {
wp_set_current_user ( $this -> user );
2017-02-09 20:21:52 +00:00
$response = $this -> server -> dispatch ( new WP_REST_Request ( 'GET' , '/wc/v2/shipping_methods' ) );
2016-08-25 18:48:17 +00:00
$methods = $response -> get_data ();
$this -> assertEquals ( 200 , $response -> get_status () );
$this -> assertContains ( array (
'id' => 'free_shipping' ,
2016-10-12 10:16:30 +00:00
'title' => 'Free shipping' ,
'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.' ,
2016-08-25 18:48:17 +00:00
'_links' => array (
'self' => array (
array (
2017-02-09 20:21:52 +00:00
'href' => rest_url ( '/wc/v2/shipping_methods/free_shipping' ),
2016-08-25 18:48:17 +00:00
),
),
'collection' => array (
array (
2017-02-09 20:21:52 +00:00
'href' => rest_url ( '/wc/v2/shipping_methods' ),
2016-08-25 18:48:17 +00:00
),
),
),
), $methods );
}
/**
* Tests to make sure shipping methods cannot viewed without valid permissions .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-08-25 18:48:17 +00:00
*/
public function test_get_shipping_methods_without_permission () {
wp_set_current_user ( 0 );
2017-02-09 20:21:52 +00:00
$response = $this -> server -> dispatch ( new WP_REST_Request ( 'GET' , '/wc/v2/shipping_methods' ) );
2016-08-25 18:48:17 +00:00
$this -> assertEquals ( 401 , $response -> get_status () );
}
/**
* Tests getting a single shipping method .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-08-25 18:48:17 +00:00
*/
public function test_get_shipping_method () {
wp_set_current_user ( $this -> user );
2017-02-09 20:21:52 +00:00
$response = $this -> server -> dispatch ( new WP_REST_Request ( 'GET' , '/wc/v2/shipping_methods/local_pickup' ) );
2016-08-25 18:48:17 +00:00
$method = $response -> get_data ();
$this -> assertEquals ( 200 , $response -> get_status () );
$this -> assertEquals ( array (
'id' => 'local_pickup' ,
2016-10-12 10:16:30 +00:00
'title' => 'Local pickup' ,
2016-08-25 18:48:17 +00:00
'description' => 'Allow customers to pick up orders themselves. By default, when using local pickup store base taxes will apply regardless of customer address.' ,
), $method );
}
/**
* Tests getting a single shipping method without the correct permissions .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-08-25 18:48:17 +00:00
*/
public function test_get_shipping_method_without_permission () {
wp_set_current_user ( 0 );
2017-02-09 20:21:52 +00:00
$response = $this -> server -> dispatch ( new WP_REST_Request ( 'GET' , '/wc/v2/shipping_methods/local_pickup' ) );
2016-08-25 18:48:17 +00:00
$this -> assertEquals ( 401 , $response -> get_status () );
}
/**
* Tests getting a shipping method with an invalid ID .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-08-25 18:48:17 +00:00
*/
public function test_get_shipping_method_invalid_id () {
wp_set_current_user ( $this -> user );
2017-02-09 20:21:52 +00:00
$response = $this -> server -> dispatch ( new WP_REST_Request ( 'GET' , '/wc/v2/shipping_methods/fake_method' ) );
2016-08-25 18:48:17 +00:00
$this -> assertEquals ( 404 , $response -> get_status () );
}
/**
2016-08-31 21:16:52 +00:00
* Test the shipping method schema .
2016-08-25 18:48:17 +00:00
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-08-25 18:48:17 +00:00
*/
public function test_shipping_method_schema () {
wp_set_current_user ( $this -> user );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request ( 'OPTIONS' , '/wc/v2/shipping_methods' );
2016-08-25 18:48:17 +00:00
$response = $this -> server -> dispatch ( $request );
$data = $response -> get_data ();
$properties = $data [ 'schema' ][ 'properties' ];
$this -> assertEquals ( 3 , count ( $properties ) );
$this -> assertArrayHasKey ( 'id' , $properties );
$this -> assertArrayHasKey ( 'title' , $properties );
$this -> assertArrayHasKey ( 'description' , $properties );
}
}