2019-06-07 15:32:42 +00:00
< ? php
/**
* Tests for the Shipping Methods REST API .
*
* @ package WooCommerce\Tests\API
* @ since 3.5 . 0
*/
2019-06-10 11:36:11 +00:00
namespace WooCommerce\RestApi\UnitTests\Tests\Version4 ;
defined ( 'ABSPATH' ) || exit ;
use \WC_REST_Unit_Test_Case ;
class ShippingMethods extends WC_REST_Unit_Test_Case {
2019-06-07 15:32:42 +00:00
/**
* Setup our test server , endpoints , and user info .
*/
public function setUp () {
parent :: setUp ();
2019-06-10 12:39:46 +00:00
$this -> endpoint = new \WC_REST_Shipping_Methods_Controller ();
2019-06-07 15:32:42 +00:00
$this -> user = $this -> factory -> user -> create (
array (
'role' => 'administrator' ,
)
);
}
/**
* Test route registration .
*
* @ since 3.5 . 0
*/
public function test_register_routes () {
$routes = $this -> server -> get_routes ();
2019-06-10 11:36:11 +00:00
$this -> assertArrayHasKey ( '/wc/v4/shipping_methods' , $routes );
$this -> assertArrayHasKey ( '/wc/v4/shipping_methods/(?P<id>[\w-]+)' , $routes );
2019-06-07 15:32:42 +00:00
}
/**
* Test getting all shipping methods .
*
* @ since 3.5 . 0
*/
public function test_get_shipping_methods () {
wp_set_current_user ( $this -> user );
2019-06-10 11:36:11 +00:00
$response = $this -> server -> dispatch ( new WP_REST_Request ( 'GET' , '/wc/v4/shipping_methods' ) );
2019-06-07 15:32:42 +00:00
$methods = $response -> get_data ();
$this -> assertEquals ( 200 , $response -> get_status () );
$this -> assertContains (
array (
'id' => 'free_shipping' ,
'title' => 'Free shipping' ,
'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.' ,
'_links' => array (
'self' => array (
array (
2019-06-10 11:36:11 +00:00
'href' => rest_url ( '/wc/v4/shipping_methods/free_shipping' ),
2019-06-07 15:32:42 +00:00
),
),
'collection' => array (
array (
2019-06-10 11:36:11 +00:00
'href' => rest_url ( '/wc/v4/shipping_methods' ),
2019-06-07 15:32:42 +00:00
),
),
),
),
$methods
);
}
/**
* Tests to make sure shipping methods cannot viewed without valid permissions .
*
* @ since 3.5 . 0
*/
public function test_get_shipping_methods_without_permission () {
wp_set_current_user ( 0 );
2019-06-10 11:36:11 +00:00
$response = $this -> server -> dispatch ( new WP_REST_Request ( 'GET' , '/wc/v4/shipping_methods' ) );
2019-06-07 15:32:42 +00:00
$this -> assertEquals ( 401 , $response -> get_status () );
}
/**
* Tests getting a single shipping method .
*
* @ since 3.5 . 0
*/
public function test_get_shipping_method () {
wp_set_current_user ( $this -> user );
2019-06-10 11:36:11 +00:00
$response = $this -> server -> dispatch ( new WP_REST_Request ( 'GET' , '/wc/v4/shipping_methods/local_pickup' ) );
2019-06-07 15:32:42 +00:00
$method = $response -> get_data ();
$this -> assertEquals ( 200 , $response -> get_status () );
$this -> assertEquals (
array (
'id' => 'local_pickup' ,
'title' => 'Local pickup' ,
'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 .
*
* @ since 3.5 . 0
*/
public function test_get_shipping_method_without_permission () {
wp_set_current_user ( 0 );
2019-06-10 11:36:11 +00:00
$response = $this -> server -> dispatch ( new WP_REST_Request ( 'GET' , '/wc/v4/shipping_methods/local_pickup' ) );
2019-06-07 15:32:42 +00:00
$this -> assertEquals ( 401 , $response -> get_status () );
}
/**
* Tests getting a shipping method with an invalid ID .
*
* @ since 3.5 . 0
*/
public function test_get_shipping_method_invalid_id () {
wp_set_current_user ( $this -> user );
2019-06-10 11:36:11 +00:00
$response = $this -> server -> dispatch ( new WP_REST_Request ( 'GET' , '/wc/v4/shipping_methods/fake_method' ) );
2019-06-07 15:32:42 +00:00
$this -> assertEquals ( 404 , $response -> get_status () );
}
/**
* Test the shipping method schema .
*
* @ since 3.5 . 0
*/
public function test_shipping_method_schema () {
wp_set_current_user ( $this -> user );
2019-06-10 11:36:11 +00:00
$request = new WP_REST_Request ( 'OPTIONS' , '/wc/v4/shipping_methods' );
2019-06-07 15:32:42 +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 );
}
}