Merge pull request #21189 from woocommerce/save-old-tests
Set up unit tests for v3 api and v2 api
This commit is contained in:
commit
4dbef1703a
|
@ -20,6 +20,11 @@ class WC_REST_Unit_Test_Case extends WC_Unit_Test_Case {
|
|||
$wp_rest_server = new WP_Test_Spy_REST_Server();
|
||||
$this->server = $wp_rest_server;
|
||||
do_action( 'rest_api_init' );
|
||||
|
||||
// Reset payment gateways.
|
||||
$gateways = WC_Payment_Gateways::instance();
|
||||
$gateways->payment_gateways = array();
|
||||
$gateways->init();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
/**
|
||||
* Coupon API Tests
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
||||
|
||||
|
@ -10,9 +10,9 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Setup test coupon data.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Coupons_Controller();
|
||||
$this->user = $this->factory->user->create( array(
|
||||
|
@ -22,18 +22,18 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test route registration.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/coupons', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/coupons/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/coupons/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/coupons', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/coupons/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/coupons/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting coupons.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_coupons() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -42,7 +42,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
$post_1 = get_post( $coupon_1->get_id() );
|
||||
$coupon_2 = WC_Helper_Coupon::create_coupon( 'dummycoupon-2' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons' ) );
|
||||
$coupons = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -78,12 +78,12 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/coupons/' . $coupon_1->get_id() ),
|
||||
'href' => rest_url( '/wc/v3/coupons/' . $coupon_1->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/coupons' ),
|
||||
'href' => rest_url( '/wc/v3/coupons' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -92,23 +92,23 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test getting coupons without valid permissions.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_coupons_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single coupon.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$post = get_post( $coupon->get_id() );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons/' . $coupon->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -145,32 +145,32 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test getting a single coupon with an invalid ID.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/0' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single coupon without valid permissions.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_coupon_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons/' . $coupon->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single coupon.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/coupons' );
|
||||
$request->set_body_params( array(
|
||||
'code' => 'test',
|
||||
'amount' => '5.00',
|
||||
|
@ -215,13 +215,13 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test creating a single coupon with invalid fields.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_coupon_invalid_fields() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test no code...
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/coupons' );
|
||||
$request->set_body_params( array(
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
|
@ -234,13 +234,13 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test creating a single coupon without valid permissions.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_coupon_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
// test no code...
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/coupons' );
|
||||
$request->set_body_params( array(
|
||||
'code' => 'fail',
|
||||
'amount' => '5.00',
|
||||
|
@ -254,20 +254,20 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test updating a single coupon.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$post = get_post( $coupon->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons/' . $coupon->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'This is a dummy coupon', $data['description'] );
|
||||
$this->assertEquals( 'fixed_cart', $data['discount_type'] );
|
||||
$this->assertEquals( '1.00', $data['amount'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/coupons/' . $coupon->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
|
@ -282,12 +282,12 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test updating a single coupon with an invalid ID.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/0' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/coupons/0' );
|
||||
$request->set_body_params( array(
|
||||
'code' => 'tester',
|
||||
'amount' => '10.00',
|
||||
|
@ -301,14 +301,14 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test updating a single coupon without valid permissions.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_coupon_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$post = get_post( $coupon->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/coupons/' . $coupon->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
|
@ -320,12 +320,12 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test deleting a single coupon.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/coupons/' . $coupon->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -333,11 +333,11 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test deleting a single coupon with an invalid ID.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/0' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/coupons/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
|
@ -346,12 +346,12 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test deleting a single coupon without valid permissions.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_coupon_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/coupons/' . $coupon->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
|
@ -359,7 +359,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test batch operations on coupons.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_batch_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -369,7 +369,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
$coupon_3 = WC_Helper_Coupon::create_coupon( 'dummycoupon-3' );
|
||||
$coupon_4 = WC_Helper_Coupon::create_coupon( 'dummycoupon-4' );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/coupons/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -397,7 +397,7 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( $coupon_2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $coupon_3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/coupons' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/coupons' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
|
@ -406,11 +406,11 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test coupon schema.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_coupon_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/coupons' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/coupons' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
* Tests for the Customers REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class Customers extends WC_REST_Unit_Test_Case {
|
||||
|
@ -19,20 +19,20 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( '/wc/v2/customers', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/customers/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/customers/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/customers', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/customers/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/customers/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting customers.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_customers() {
|
||||
wp_set_current_user( 1 );
|
||||
|
@ -40,7 +40,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
$customer_1 = WC_Helper_Customer::create_customer();
|
||||
WC_Helper_Customer::create_customer( 'test2', 'test2', 'test2@woo.local' );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/customers' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/customers' );
|
||||
$request->set_query_params( array(
|
||||
'orderby' => 'id',
|
||||
) );
|
||||
|
@ -86,19 +86,17 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $customer_1->get_avatar_url(),
|
||||
'meta_data' => array(),
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/customers/' . $customer_1->get_id() . '' ),
|
||||
'href' => rest_url( '/wc/v3/customers/' . $customer_1->get_id() . '' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/customers' ),
|
||||
'href' => rest_url( '/wc/v3/customers' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -108,24 +106,24 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting customers without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_customers_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a new customer.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
|
||||
// Test just the basics first..
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/customers' );
|
||||
$request->set_body_params( array(
|
||||
'username' => 'create_customer_test',
|
||||
'password' => 'test123',
|
||||
|
@ -172,13 +170,11 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
), $data );
|
||||
|
||||
// Test extra data
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/customers' );
|
||||
$request->set_body_params( array(
|
||||
'username' => 'create_customer_test2',
|
||||
'password' => 'test123',
|
||||
|
@ -235,13 +231,11 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
), $data );
|
||||
|
||||
// Test without required field
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/customers' );
|
||||
$request->set_body_params( array(
|
||||
'username' => 'create_customer_test3',
|
||||
'first_name' => 'Test',
|
||||
|
@ -256,11 +250,11 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test creating customers without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/customers' );
|
||||
$request->set_body_params( array(
|
||||
'username' => 'create_customer_test_without_permission',
|
||||
'password' => 'test123',
|
||||
|
@ -273,12 +267,12 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting a single customer.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'get_customer_test', 'test123', 'get_customer_test@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( array(
|
||||
|
@ -318,8 +312,6 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'get_customer_test',
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
), $data );
|
||||
}
|
||||
|
@ -327,41 +319,41 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting a single customer without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'get_customer_test_without_permission', 'test123', 'get_customer_test_without_permission@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single customer with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/0' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a customer.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'update_customer_test', 'test123', 'update_customer_test@woo.local' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'update_customer_test', $data['username'] );
|
||||
$this->assertEquals( 'update_customer_test@woo.local', $data['email'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/customers/' . $customer->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/customers/' . $customer->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'email' => 'updated_email@woo.local',
|
||||
'first_name' => 'UpdatedTest',
|
||||
|
@ -376,23 +368,23 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test updating a customer without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'update_customer_test_without_permission', 'test123', 'update_customer_test_without_permission@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a customer with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/0' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -400,12 +392,12 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test deleting a customer.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'delete_customer_test', 'test123', 'delete_customer_test@woo.local' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/' . $customer->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/customers/' . $customer->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -414,11 +406,11 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test deleting a customer with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/0' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/customers/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
@ -427,12 +419,12 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test deleting a customer without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'delete_customer_test_without_permission', 'test123', 'delete_customer_test_without_permission@woo.local' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/' . $customer->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/customers/' . $customer->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
|
@ -441,7 +433,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test customer batch endpoint.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_batch_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
|
@ -451,7 +443,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
$customer_3 = WC_Helper_Customer::create_customer( 'test_batch_customer3', 'test123', 'test_batch_customer3@woo.local' );
|
||||
$customer_4 = WC_Helper_Customer::create_customer( 'test_batch_customer4', 'test123', 'test_batch_customer4@woo.local' );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/customers/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -480,7 +472,7 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( $customer_2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $customer_3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/customers' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
|
@ -490,16 +482,16 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test customer schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_customer_schema() {
|
||||
wp_set_current_user( 1 );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/customers' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 18, count( $properties ) );
|
||||
$this->assertEquals( 16, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||
|
@ -511,8 +503,6 @@ class Customers extends WC_REST_Unit_Test_Case {
|
|||
$this->assertArrayHasKey( 'role', $properties );
|
||||
$this->assertArrayHasKey( 'username', $properties );
|
||||
$this->assertArrayHasKey( 'password', $properties );
|
||||
$this->assertArrayHasKey( 'orders_count', $properties );
|
||||
$this->assertArrayHasKey( 'total_spent', $properties );
|
||||
$this->assertArrayHasKey( 'avatar_url', $properties );
|
||||
$this->assertArrayHasKey( 'billing', $properties );
|
||||
$this->assertArrayHasKey( 'first_name', $properties['billing']['properties'] );
|
||||
|
|
|
@ -41,7 +41,7 @@ class WC_Tests_API_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.6.0
|
||||
*/
|
||||
public function test_wc_rest_validate_reports_request_arg() {
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/foo', array(
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/foo', array(
|
||||
'args' => array(
|
||||
'date' => array(
|
||||
'type' => 'string',
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
* Tests for the orders REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
||||
|
||||
|
@ -28,18 +28,18 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test route registration.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/orders', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/orders/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/orders/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/orders', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/orders/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/orders/(?P<id>[\d]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all orders.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_items() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -49,7 +49,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
$this->orders[] = WC_Helper_Order::create_order( $this->user );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders' ) );
|
||||
$orders = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -59,18 +59,18 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests to make sure orders cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_items_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$this->orders[] = WC_Helper_Order::create_order();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single order.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_item() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -79,7 +79,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
$order->add_meta_data( 'key2', 'value2' );
|
||||
$order->save();
|
||||
$this->orders[] = $order;
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -94,23 +94,23 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Tests getting a single order without the correct permissions.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_item_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$this->orders[] = $order;
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting an order with an invalid ID.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_item_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/99999999' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/99999999' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -124,18 +124,18 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
$refund = wc_create_refund( array(
|
||||
'order_id' => $order->get_id(),
|
||||
) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $refund->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $refund->get_id() ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating an order.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/orders' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'bacs',
|
||||
|
@ -210,14 +210,14 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Tests creating an order without required fields.
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_order_invalid_fields() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
// non-existent customer
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/orders' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'bacs',
|
||||
|
@ -269,12 +269,12 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests updating an order.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'test-update',
|
||||
|
@ -296,7 +296,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests updating an order and removing items.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_remove_items() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -313,7 +313,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
$order->add_item( $fee );
|
||||
$order->save();
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
|
||||
$fee_data = current( $order->get_items( 'fee' ) );
|
||||
|
||||
$request->set_body_params(
|
||||
|
@ -336,7 +336,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests updating an order and adding a coupon.
|
||||
*
|
||||
* @since 3.3.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_add_coupons() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -345,7 +345,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
$coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' );
|
||||
$coupon->set_amount( 5 );
|
||||
$coupon->save();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'coupon_lines' => array(
|
||||
|
@ -375,7 +375,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests updating an order and removing a coupon.
|
||||
*
|
||||
* @since 3.3.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_remove_coupons() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -391,7 +391,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
// Check that the coupon is applied.
|
||||
$this->assertEquals( '45.00', $order->get_total() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
|
||||
$coupon_data = current( $order->get_items( 'coupon' ) );
|
||||
|
||||
$request->set_body_params(
|
||||
|
@ -422,12 +422,12 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests updating an order without the correct permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'test-update',
|
||||
|
@ -443,11 +443,12 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Tests that updating an order with an invalid id fails.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/999999' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/orders/999999' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'test-update',
|
||||
|
@ -463,12 +464,13 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test deleting an order.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/orders/' . $order->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -477,12 +479,13 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test deleting an order without permission/creds.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_order_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/orders/' . $order->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
|
@ -491,11 +494,11 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test deleting an order with an invalid id.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_order_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/9999999' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/orders/9999999' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
@ -503,6 +506,8 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test batch managing product reviews.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_orders_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -511,7 +516,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
$order2 = WC_Helper_Order::create_order();
|
||||
$order3 = WC_Helper_Order::create_order();
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/orders/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
|
@ -533,7 +538,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( $order2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $order3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/orders' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/orders' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 1, count( $data ) );
|
||||
|
@ -541,12 +546,13 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test the order schema.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_order_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/orders/' . $order->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
* Tests for the Payment Gateways REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
||||
|
@ -22,23 +22,23 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/payment_gateways', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/payment_gateways/(?P<id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/payment_gateways', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/payment_gateways/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all payment gateways.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_payment_gateways() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways' ) );
|
||||
$gateways = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -50,6 +50,9 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
'enabled' => false,
|
||||
'method_title' => 'Check payments',
|
||||
'method_description' => 'Take payments in person via checks. This offline gateway can also be useful to test purchases.',
|
||||
'method_supports' => array(
|
||||
'products',
|
||||
),
|
||||
'settings' => array_diff_key( $this->get_settings( 'WC_Gateway_Cheque' ), array(
|
||||
'enabled' => false,
|
||||
'description' => false,
|
||||
|
@ -57,12 +60,12 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/payment_gateways/cheque' ),
|
||||
'href' => rest_url( '/wc/v3/payment_gateways/cheque' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/payment_gateways' ),
|
||||
'href' => rest_url( '/wc/v3/payment_gateways' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -72,23 +75,23 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests to make sure payment gateways cannot viewed without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_payment_gateways_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single payment gateway.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_payment_gateway() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/paypal' ) );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -100,6 +103,10 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
'enabled' => false,
|
||||
'method_title' => 'PayPal',
|
||||
'method_description' => 'PayPal Standard redirects customers to PayPal to enter their payment information.',
|
||||
'method_supports' => array(
|
||||
'products',
|
||||
'refunds',
|
||||
),
|
||||
'settings' => array_diff_key( $this->get_settings( 'WC_Gateway_Paypal' ), array(
|
||||
'enabled' => false,
|
||||
'description' => false,
|
||||
|
@ -110,35 +117,35 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting a payment gateway without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_payment_gateway_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/paypal' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a payment gateway with an invalid id.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_payment_gateway_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/totally_fake_method' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/totally_fake_method' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single payment gateway.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_payment_gateway() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Test defaults
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/paypal' ) );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] );
|
||||
|
@ -146,7 +153,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test updating single setting
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'email' => 'woo@woo.local',
|
||||
|
@ -161,7 +168,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test updating multiple settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'testmode' => 'yes',
|
||||
|
@ -177,7 +184,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// Test other parameters, and recheck settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'enabled' => false,
|
||||
'order' => 2,
|
||||
|
@ -192,7 +199,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test bogus
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'paymentaction' => 'afasfasf',
|
||||
|
@ -201,7 +208,7 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'paymentaction' => 'authorization',
|
||||
|
@ -215,11 +222,11 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test updating a payment gateway without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_payment_gateway_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'testmode' => 'yes',
|
||||
|
@ -233,11 +240,11 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test updating a payment gateway with an invalid id.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_payment_gateway_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/totally_fake_method' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/totally_fake_method' );
|
||||
$request->set_body_params( array(
|
||||
'enabled' => true,
|
||||
) );
|
||||
|
@ -248,17 +255,17 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test the payment gateway schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_payment_gateway_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/payment_gateways' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/payment_gateways' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 8, count( $properties ) );
|
||||
$this->assertEquals( 9, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'title', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
|
@ -266,13 +273,14 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
$this->assertArrayHasKey( 'enabled', $properties );
|
||||
$this->assertArrayHasKey( 'method_title', $properties );
|
||||
$this->assertArrayHasKey( 'method_description', $properties );
|
||||
$this->assertArrayHasKey( 'method_supports', $properties );
|
||||
$this->assertArrayHasKey( 'settings', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a particular gateway's settings so we can correctly test API output.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
* @param string $gateway_class Name of WC_Payment_Gateway class.
|
||||
*/
|
||||
private function get_settings( $gateway_class ) {
|
||||
|
@ -284,8 +292,8 @@ class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
|||
if ( empty( $field['title'] ) || empty( $field['type'] ) ) {
|
||||
continue;
|
||||
}
|
||||
// Ignore 'title' settings/fields -- they are UI only
|
||||
if ( 'title' === $field['type'] ) {
|
||||
// Ignore 'enabled' and 'description', to be in line with \WC_REST_Payment_Gateways_Controller::get_settings.
|
||||
if ( in_array( $id, array( 'enabled', 'description' ), true ) ) {
|
||||
continue;
|
||||
}
|
||||
$data = array(
|
||||
|
|
|
@ -23,7 +23,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
@ -35,7 +35,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting all product reviews.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_reviews() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -87,7 +87,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests to make sure product reviews cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_reviews_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
@ -99,7 +99,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests to make sure an error is returned when an invalid product is loaded.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_reviews_invalid_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -110,7 +110,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests getting a single product review.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -141,7 +141,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests getting a single product review without the correct permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_review_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
@ -154,7 +154,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests getting a product review with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -166,7 +166,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests creating a product review.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -205,7 +205,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests creating a product review without required fields.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_product_review_invalid_fields() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -254,7 +254,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests updating a product review.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -288,7 +288,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests updating a product review without the correct permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_product_review_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
@ -312,7 +312,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests that updating a product review with an invalid id fails.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -335,7 +335,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test deleting a product review.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -351,7 +351,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test deleting a product review without permission/creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
@ -367,7 +367,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test deleting a product review with an invalid id.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -383,6 +383,8 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test batch managing product reviews.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_reviews_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -436,7 +438,7 @@ class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test the product review schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_review_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
* Tests for Variations API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
||||
|
@ -24,24 +24,24 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/(?P<product_id>[\d]+)/variations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/(?P<product_id>[\d]+)/variations/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/(?P<product_id>[\d]+)/variations/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting variations.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_variations() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $variations ) );
|
||||
|
@ -52,19 +52,19 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting variations without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_variations_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single variation.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -72,7 +72,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -83,21 +83,21 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting single variation without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single variation.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -105,12 +105,12 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 1, count( $variations ) );
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test deleting a single variation without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
@ -126,7 +126,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
|
@ -135,12 +135,12 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test deleting a single variation with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_variation_with_invalid_id() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/0' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() . '/variations/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
@ -149,7 +149,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test editing a single variation.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -157,7 +157,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $variation['sku'] );
|
||||
|
@ -165,7 +165,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEmpty( $variation['sale_price'] );
|
||||
$this->assertEquals( 'small', $variation['attributes'][0]['option'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU',
|
||||
|
@ -201,7 +201,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test updating a single variation without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
@ -209,7 +209,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
|
@ -222,12 +222,12 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test updating a single variation with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_variation_with_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/0' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/0' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
|
@ -240,17 +240,17 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test creating a single variation.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 2, count( $variations ) );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/' . $product->get_id() . '/variations' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
|
@ -274,7 +274,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $variation['sku'] );
|
||||
$this->assertEquals( 'medium', $variation['attributes'][0]['option'] );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 3, count( $variations ) );
|
||||
}
|
||||
|
@ -282,13 +282,13 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test creating a single variation without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/' . $product->get_id() . '/variations' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
|
@ -308,12 +308,14 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test batch managing product variations.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_variations_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/' . $product->get_id() . '/variations/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
|
@ -353,7 +355,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'medium', $data['create'][0]['attributes'][0]['option'] );
|
||||
$this->assertEquals( $children[1], $data['delete'][0]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
|
@ -363,12 +365,12 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test variation schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_variation_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/products/' . $product->get_id() . '/variations' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
@ -386,7 +388,6 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertArrayHasKey( 'date_on_sale_from', $properties );
|
||||
$this->assertArrayHasKey( 'date_on_sale_to', $properties );
|
||||
$this->assertArrayHasKey( 'on_sale', $properties );
|
||||
$this->assertArrayHasKey( 'visible', $properties );
|
||||
$this->assertArrayHasKey( 'purchasable', $properties );
|
||||
$this->assertArrayHasKey( 'virtual', $properties );
|
||||
$this->assertArrayHasKey( 'downloadable', $properties );
|
||||
|
@ -414,7 +415,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test updating a variation stock.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_variation_manage_stock() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -427,7 +428,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$variation_id = $children[0];
|
||||
|
||||
// Set stock to true.
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'manage_stock' => true,
|
||||
|
@ -441,7 +442,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( true, $variation['manage_stock'] );
|
||||
|
||||
// Set stock to false.
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'manage_stock' => false,
|
||||
|
@ -457,7 +458,7 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
|||
// Set stock to false but parent is managing stock.
|
||||
$product->set_manage_stock( true );
|
||||
$product->save();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'manage_stock' => false,
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
* Tests for Products API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
||||
|
@ -24,7 +24,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
@ -36,7 +36,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting products.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_products() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -58,7 +58,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting products without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_products_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
@ -70,7 +70,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting a single product.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -94,7 +94,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting single product without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
@ -106,7 +106,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test deleting a single product.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -125,7 +125,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test deleting a single product without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
@ -139,7 +139,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test deleting a single product with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_product_with_invalid_id() {
|
||||
wp_set_current_user( 0 );
|
||||
|
@ -152,7 +152,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test editing a single product. Tests multiple product types.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -259,7 +259,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test updating a single product without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
@ -277,7 +277,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test updating a single product with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_product_with_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -295,7 +295,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test creating a single product.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -393,7 +393,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test creating a single product without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
@ -411,6 +411,8 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test batch managing products.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_products_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -467,7 +469,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
* Tests to make sure you can filter products post statuses by both
|
||||
* the status query arg and WP_Query.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_products_filter_post_status() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -516,7 +518,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test product schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
* Settings API Tests.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class Settings extends WC_REST_Unit_Test_Case {
|
||||
|
@ -23,24 +23,24 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/settings', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/settings/(?P<group_id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/settings/(?P<group_id>[\w-]+)/(?P<id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/settings', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/settings/(?P<group_id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/settings/(?P<group_id>[\w-]+)/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all groups.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_groups() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -54,7 +54,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'options' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/test' ),
|
||||
'href' => rest_url( '/wc/v3/settings/test' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -69,7 +69,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'options' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/sub-test' ),
|
||||
'href' => rest_url( '/wc/v3/settings/sub-test' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -79,19 +79,19 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test /settings without valid permissions/creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_groups_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /settings without valid permissions/creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
* @covers WC_Rest_Settings_Controller::get_items
|
||||
*/
|
||||
public function test_get_groups_none_registered() {
|
||||
|
@ -99,7 +99,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
|
||||
remove_all_filters( 'woocommerce_settings_groups' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings' ) );
|
||||
$this->assertEquals( 500, $response->get_status() );
|
||||
|
||||
WC_Helper_Settings::register();
|
||||
|
@ -108,10 +108,10 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test groups schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_group_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/settings' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/settings' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
@ -126,14 +126,14 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test settings schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_setting_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/settings/test/woocommerce_shop_page_display' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/settings/test/woocommerce_shop_page_display' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 9, count( $properties ) );
|
||||
$this->assertEquals( 10, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'label', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
|
@ -143,12 +143,13 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertArrayHasKey( 'placeholder', $properties );
|
||||
$this->assertArrayHasKey( 'type', $properties );
|
||||
$this->assertArrayHasKey( 'options', $properties );
|
||||
$this->assertArrayHasKey( 'group_id', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single group.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_group() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -158,15 +159,15 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertIsWPError( $result );
|
||||
|
||||
// test getting a group that does not exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/not-real' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/not-real' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting the 'invalid' group
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/invalid' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/invalid' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting a valid group with settings attached to it
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 1, count( $data ) );
|
||||
$this->assertEquals( 'woocommerce_shop_page_display', $data[0]['id'] );
|
||||
|
@ -176,30 +177,30 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting a single group without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_group_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/coupon-data' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/coupon-data' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single setting.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_setting() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test defaults first
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( '', $data['value'] );
|
||||
|
||||
// test updating shop display setting
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'both',
|
||||
) );
|
||||
|
@ -209,7 +210,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'both', $data['value'] );
|
||||
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'subcategories',
|
||||
) );
|
||||
|
@ -219,7 +220,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'subcategories', $data['value'] );
|
||||
$this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => '',
|
||||
) );
|
||||
|
@ -233,18 +234,18 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test updating multiple settings at once.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test defaults first
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( '', $data[0]['value'] );
|
||||
|
||||
// test setting both at once
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -260,7 +261,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) );
|
||||
|
||||
// test updating one, but making sure the other value stays the same
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -278,23 +279,23 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting a single setting.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_setting() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test getting an invalid setting from a group that does not exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/not-real/woocommerce_shop_page_display' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/not-real/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting an invalid setting from a group that does exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/invalid/invalid' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/invalid/invalid' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting a valid setting
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -309,19 +310,19 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test getting a single setting without valid user permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_setting_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test/woocommerce_shop_page_display' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the GET single setting route handler receiving an empty setting ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_setting_empty_setting_id() {
|
||||
$result = $this->endpoint->get_setting( 'test', '' );
|
||||
|
@ -332,7 +333,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests the GET single setting route handler receiving an invalid setting ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_setting_invalid_setting_id() {
|
||||
$result = $this->endpoint->get_setting( 'test', 'invalid' );
|
||||
|
@ -343,7 +344,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests the GET single setting route handler encountering an invalid setting type.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_setting_invalid_setting_type() {
|
||||
// $controller = $this->getMock( 'WC_Rest_Setting_Options_Controller', array( 'get_group_settings', 'is_setting_type_valid' ) );
|
||||
|
@ -367,12 +368,12 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test updating a single setting without valid user permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_setting_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'subcategories',
|
||||
) );
|
||||
|
@ -384,12 +385,12 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test updating multiple settings without valid user permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_settings_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
|
@ -405,13 +406,13 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test updating a bad setting ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
* @covers WC_Rest_Setting_Options_Controller::update_item
|
||||
*/
|
||||
public function test_update_setting_bad_setting_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/test/invalid' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/test/invalid' );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'test',
|
||||
) );
|
||||
|
@ -422,13 +423,13 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests our classic setting registration to make sure settings added for WP-Admin are available over the API.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_classic_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Make sure the group is properly registered
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/products' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertTrue( is_array( $data ) );
|
||||
$this->assertContains( array(
|
||||
|
@ -442,25 +443,25 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/products/woocommerce_downloads_require_login' ),
|
||||
'href' => rest_url( '/wc/v3/settings/products/woocommerce_downloads_require_login' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/products' ),
|
||||
'href' => rest_url( '/wc/v3/settings/products' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data );
|
||||
), $data );
|
||||
|
||||
// test get single
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products/woocommerce_dimension_unit' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/products/woocommerce_dimension_unit' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'cm', $data['default'] );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_dimension_unit' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_dimension_unit' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'yd',
|
||||
) );
|
||||
|
@ -474,12 +475,12 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests our email etting registration to make sure settings added for WP-Admin are available over the API.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_email_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/email_new_order' ) );
|
||||
$settings = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -495,19 +496,19 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/email_new_order/recipient' ),
|
||||
'href' => rest_url( '/wc/v3/settings/email_new_order/recipient' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/email_new_order' ),
|
||||
'href' => rest_url( '/wc/v3/settings/email_new_order' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $settings );
|
||||
|
||||
// test get single
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order/subject' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/email_new_order/subject' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( array(
|
||||
|
@ -518,10 +519,11 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
'default' => '',
|
||||
'tip' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'value' => '',
|
||||
'group_id' => 'email_new_order',
|
||||
), $setting );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_new_order', 'subject' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_new_order', 'subject' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'This is my subject',
|
||||
) );
|
||||
|
@ -536,17 +538,18 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
'default' => '',
|
||||
'tip' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'value' => 'This is my subject',
|
||||
'group_id' => 'email_new_order',
|
||||
), $setting );
|
||||
|
||||
// test updating another subject and making sure it works with a "similar" id
|
||||
$request = new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$request = new WP_REST_Request( 'GET', sprintf( '/wc/v3/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEmpty( $setting['value'] );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'This is my new subject',
|
||||
) );
|
||||
|
@ -556,7 +559,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 'This is my new subject', $setting['value'] );
|
||||
|
||||
// make sure the other is what we left it
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order/subject' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/email_new_order/subject' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'This is my subject', $setting['value'] );
|
||||
|
@ -565,13 +568,13 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test validation of checkbox settings.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_validation_checkbox() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test bogus value
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'not_yes_or_no',
|
||||
) );
|
||||
|
@ -579,7 +582,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// test yes
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'yes',
|
||||
) );
|
||||
|
@ -587,7 +590,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
// test no
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'no',
|
||||
) );
|
||||
|
@ -598,13 +601,13 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test validation of radio settings.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_validation_radio() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// not a valid option
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'billing2',
|
||||
) );
|
||||
|
@ -612,7 +615,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// valid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'billing',
|
||||
) );
|
||||
|
@ -623,16 +626,16 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test validation of multiselect.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_validation_multiselect() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v3/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEmpty( $setting['value'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => array( 'AX', 'DZ', 'MMM' ),
|
||||
) );
|
||||
|
@ -644,17 +647,17 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test validation of select.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_validation_select() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'kg', $setting['value'] );
|
||||
|
||||
// invalid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'pounds', // invalid, should be lbs
|
||||
) );
|
||||
|
@ -662,7 +665,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// valid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'lbs', // invalid, should be lbs
|
||||
) );
|
||||
|
@ -676,11 +679,11 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
* That it is returned as 'select' and not 'single_select_country',
|
||||
* and that both state and country options are returned.
|
||||
*
|
||||
* @since 3.0.7
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_woocommerce_default_country() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_default_country' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_default_country' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'select', $setting['type'] );
|
||||
|
@ -691,18 +694,18 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure the store address setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.1.1
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_woocommerce_store_address() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_address' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_address' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $new_value,
|
||||
) );
|
||||
|
@ -711,7 +714,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $old_value,
|
||||
) );
|
||||
|
@ -723,18 +726,18 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure the store address 2 (line 2) setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.1.1
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_woocommerce_store_address_2() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_address_2' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_address_2' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address_2' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address_2' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $new_value,
|
||||
) );
|
||||
|
@ -743,7 +746,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address_2' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address_2' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $old_value,
|
||||
) );
|
||||
|
@ -755,18 +758,18 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure the store city setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.1.1
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_woocommerce_store_city() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_city' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_city' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_city' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_city' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $new_value,
|
||||
) );
|
||||
|
@ -775,7 +778,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_city' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_city' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $old_value,
|
||||
) );
|
||||
|
@ -787,18 +790,18 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure the store postcode setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.1.1
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_woocommerce_store_postcode() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_postcode' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_postcode' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_postcode' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_postcode' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $new_value,
|
||||
) );
|
||||
|
@ -807,7 +810,7 @@ class Settings extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_postcode' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_postcode' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $old_value,
|
||||
) );
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
* Tests for the Shipping Methods REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class Shipping_Methods extends WC_REST_Unit_Test_Case {
|
||||
|
@ -22,39 +22,39 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping_methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping_methods/(?P<id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping_methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping_methods/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all shipping methods.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods' ) );
|
||||
$methods = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertContains( array(
|
||||
$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(
|
||||
'href' => rest_url( '/wc/v2/shipping_methods/free_shipping' ),
|
||||
'href' => rest_url( '/wc/v3/shipping_methods/free_shipping' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping_methods' ),
|
||||
'href' => rest_url( '/wc/v3/shipping_methods' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -64,65 +64,65 @@ class Shipping_Methods extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Tests to make sure shipping methods cannot viewed without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_methods_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single shipping method.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/local_pickup' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods/local_pickup' ) );
|
||||
$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 );
|
||||
$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.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_method_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/local_pickup' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods/local_pickup' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a shipping method with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_method_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/fake_method' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods/fake_method' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the shipping method schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_shipping_method_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/shipping_methods' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/shipping_methods' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
/**
|
||||
* Shipping Zones API Tests
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
||||
|
||||
|
@ -50,49 +51,51 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test route registration.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<id>[\d-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<id>[\d]+)/locations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<zone_id>[\d]+)/methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<zone_id>[\d]+)/methods/(?P<instance_id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping/zones', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P<id>[\d-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P<id>[\d]+)/locations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P<zone_id>[\d]+)/methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P<zone_id>[\d]+)/methods/(?P<instance_id>[\d]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all Shipping Zones.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_zones() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// "Rest of the World" zone exists by default
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 1 );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 0,
|
||||
'id' => $data[0]['id'],
|
||||
'name' => 'Locations not covered by your other zones',
|
||||
'order' => 0,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/0' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[0]['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/0/locations' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[0]['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -102,30 +105,30 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
// Create a zone and make sure it's in the response
|
||||
$this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 2 );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 1,
|
||||
'id' => $data[1]['id'],
|
||||
'name' => 'Zone 1',
|
||||
'order' => 0,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/1' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[1]['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/1/locations' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[1]['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -135,25 +138,27 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test /shipping/zones without valid permissions/creds.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_zones_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /shipping/zones while Shipping is disabled in WooCommerce.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_zones_disabled_shipping() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
add_filter( 'wc_shipping_enabled', '__return_false' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
remove_filter( 'wc_shipping_enabled', '__return_false' );
|
||||
|
@ -161,10 +166,11 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test Shipping Zone schema.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_zone_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/shipping/zones' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/shipping/zones' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
@ -177,12 +183,13 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test Shipping Zone create endpoint.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test Zone',
|
||||
|
@ -201,17 +208,17 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $data['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] . '/locations' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $data['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -221,12 +228,13 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test Shipping Zone create endpoint.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_shipping_zone_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test Zone',
|
||||
|
@ -239,14 +247,15 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test Shipping Zone update endpoint.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Zone Test',
|
||||
|
@ -265,17 +274,17 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -285,12 +294,13 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test Shipping Zone update endpoint with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/555555' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/shipping/zones/555555' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Zone Test',
|
||||
|
@ -304,13 +314,14 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test Shipping Zone delete endpoint.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
@ -320,13 +331,14 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test Shipping Zone delete endpoint without permissions.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_shipping_zone_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
|
@ -334,24 +346,26 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test Shipping Zone delete endpoint with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/555555' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/shipping/zones/555555' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single Shipping Zone.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_single_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -363,17 +377,17 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -383,19 +397,21 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test getting a single Shipping Zone with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_single_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting Shipping Zone Locations.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_locations() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -410,7 +426,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -423,12 +439,12 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -439,26 +455,28 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test getting Shipping Zone Locations with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_locations_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/locations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/1/locations' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone Locations update endpoint.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_locations() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' );
|
||||
$request->add_header( 'Content-Type', 'application/json' );
|
||||
$request->set_body(
|
||||
json_encode(
|
||||
|
@ -492,12 +510,12 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -508,12 +526,12 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -524,12 +542,12 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -540,19 +558,21 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test updating Shipping Zone Locations with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_locations_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/1/locations' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'PUT', '/wc/v3/shipping/zones/1/locations' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all Shipping Zone Methods and getting a single Shipping Zone Method.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -582,7 +602,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$settings[ $id ] = $data;
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods' ) );
|
||||
$data = $response->get_data();
|
||||
$expected = array(
|
||||
'id' => $instance_id,
|
||||
|
@ -597,17 +617,17 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -617,7 +637,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( count( $data ), 1 );
|
||||
$this->assertContains( $expected, $data );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -626,36 +646,39 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test getting all Shipping Zone Methods with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_methods_invalid_zone_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/1/methods' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods/1' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/1/methods/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single Shipping Zone Method with a bad ID.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_methods_invalid_method_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/1' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a Shipping Zone Method.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -666,7 +689,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$method = $methods[ $instance_id ];
|
||||
|
||||
// Test defaults
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
|
@ -678,7 +701,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( '0', $data['settings']['cost']['value'] );
|
||||
|
||||
// Update a single value
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
|
@ -697,7 +720,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( '5', $data['settings']['cost']['value'] );
|
||||
|
||||
// Test multiple settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
|
@ -717,7 +740,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$this->assertEquals( '10', $data['settings']['cost']['value'] );
|
||||
|
||||
// Test bogus
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
|
@ -733,7 +756,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$this->assertTrue( $data['enabled'] );
|
||||
$this->assertEquals( 1, $data['order'] );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'enabled' => false,
|
||||
|
@ -751,12 +774,13 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test creating a Shipping Zone Method.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
|
@ -775,7 +799,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test deleting a Shipping Zone Method.
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -783,7 +808,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
|||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
$methods = $zone->get_shipping_methods();
|
||||
$method = $methods[ $instance_id ];
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
* System Status REST Tests.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
||||
|
||||
|
@ -31,19 +31,19 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/system_status', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/system_status/tools', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/system_status/tools/(?P<id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/system_status', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/system_status/tools', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/system_status/tools/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure system status cannot be accessed without valid creds
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
@ -51,11 +51,11 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
* Test to make sure root properties are present.
|
||||
* (environment, theme, database, etc).
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_returns_root_properties() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'environment', $data );
|
||||
|
@ -70,11 +70,11 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure environment response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_environment() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$environment = (array) $data['environment'];
|
||||
|
||||
|
@ -90,12 +90,12 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure database response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_database() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$database = (array) $data['database'];
|
||||
|
||||
|
@ -109,14 +109,14 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure active plugins response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_active_plugins() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$actual_plugins = array( 'hello.php' );
|
||||
update_option( 'active_plugins', $actual_plugins );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
update_option( 'active_plugins', array() );
|
||||
|
||||
$data = $response->get_data();
|
||||
|
@ -129,13 +129,13 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure theme response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_theme() {
|
||||
wp_set_current_user( $this->user );
|
||||
$active_theme = wp_get_theme();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$theme = (array) $data['theme'];
|
||||
|
||||
|
@ -146,7 +146,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure settings response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -157,7 +157,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
$term_response[ $term->slug ] = strtolower( $term->name );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$settings = (array) $data['settings'];
|
||||
|
||||
|
@ -170,12 +170,12 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure security response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_security() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$settings = (array) $data['security'];
|
||||
|
||||
|
@ -187,11 +187,11 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure pages response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_pages() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$pages = $data['pages'];
|
||||
$this->assertEquals( 5, count( $pages ) );
|
||||
|
@ -200,10 +200,10 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test system status schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_system_status_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/system_status' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
@ -220,7 +220,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure get_items (all tools) response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_tools() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -228,7 +228,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
$tools_controller = new WC_REST_System_Status_Tools_Controller();
|
||||
$raw_tools = $tools_controller->get_tools();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -242,7 +242,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
'_links' => array(
|
||||
'item' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/system_status/tools/reset_tracking' ),
|
||||
'href' => rest_url( '/wc/v3/system_status/tools/reset_tracking' ),
|
||||
'embeddable' => true,
|
||||
),
|
||||
),
|
||||
|
@ -286,18 +286,18 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure system status tools cannot be accessed without valid creds
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_tools_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure we can load a single tool correctly.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_tool() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -306,7 +306,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
$raw_tools = $tools_controller->get_tools();
|
||||
$raw_tool = $raw_tools['recount_terms'];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools/recount_terms' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
@ -342,18 +342,18 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure a single system status toolscannot be accessed without valid creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_tool_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools/recount_terms' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure we can RUN a tool correctly.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_execute_system_tool() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -362,7 +362,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
$raw_tools = $tools_controller->get_tools();
|
||||
$raw_tool = $raw_tools['recount_terms'];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/recount_terms' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'recount_terms', $data['id'] );
|
||||
|
@ -372,7 +372,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
$this->assertTrue( $data['success'] );
|
||||
$this->assertEquals( 1, did_action( 'woocommerce_rest_insert_system_status_tool' ) );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/not_a_real_tool' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/not_a_real_tool' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// Test _fields for execute system tool request.
|
||||
|
@ -405,21 +405,21 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* Test to make sure a tool cannot be run without valid creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_execute_system_status_tool_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/recount_terms' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test system status schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_system_status_tool_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status/tools' );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/system_status/tools' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
@ -435,6 +435,8 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test execute_tool() with the "add_order_indexes" tool.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_execute_system_tool_add_order_indexes() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
@ -446,7 +448,7 @@ class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
|||
delete_post_meta( $order2->get_id(), '_shipping_address_index' );
|
||||
|
||||
$controller = new WC_REST_System_Status_Tools_Controller();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/add_order_indexes' ) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/add_order_indexes' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertTrue( $data['success'] );
|
||||
|
|
|
@ -0,0 +1,446 @@
|
|||
<?php
|
||||
/**
|
||||
* Coupon API Tests
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
class WC_Tests_API_Coupons_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
protected $endpoint;
|
||||
|
||||
/**
|
||||
* Setup test coupon data.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Coupons_Controller();
|
||||
$this->user = $this->factory->user->create( array(
|
||||
'role' => 'administrator',
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/coupons', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/coupons/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/coupons/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting coupons.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_coupons() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$coupon_1 = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$post_1 = get_post( $coupon_1->get_id() );
|
||||
$coupon_2 = WC_Helper_Coupon::create_coupon( 'dummycoupon-2' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons' ) );
|
||||
$coupons = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $coupons ) );
|
||||
$this->assertContains( array(
|
||||
'id' => $coupon_1->get_id(),
|
||||
'code' => 'dummycoupon-1',
|
||||
'amount' => '1.00',
|
||||
'date_created' => wc_rest_prepare_date_response( $post_1->post_date_gmt, false ),
|
||||
'date_created_gmt' => wc_rest_prepare_date_response( $post_1->post_date_gmt ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $post_1->post_modified_gmt, false ),
|
||||
'date_modified_gmt' => wc_rest_prepare_date_response( $post_1->post_modified_gmt ),
|
||||
'discount_type' => 'fixed_cart',
|
||||
'description' => 'This is a dummy coupon',
|
||||
'date_expires' => '',
|
||||
'date_expires_gmt' => '',
|
||||
'usage_count' => 0,
|
||||
'individual_use' => false,
|
||||
'product_ids' => array(),
|
||||
'excluded_product_ids' => array(),
|
||||
'usage_limit' => '',
|
||||
'usage_limit_per_user' => '',
|
||||
'limit_usage_to_x_items' => null,
|
||||
'free_shipping' => false,
|
||||
'product_categories' => array(),
|
||||
'excluded_product_categories' => array(),
|
||||
'exclude_sale_items' => false,
|
||||
'minimum_amount' => '0.00',
|
||||
'maximum_amount' => '0.00',
|
||||
'email_restrictions' => array(),
|
||||
'used_by' => array(),
|
||||
'meta_data' => array(),
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/coupons/' . $coupon_1->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/coupons' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $coupons );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting coupons without valid permissions.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_coupons_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single coupon.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$post = get_post( $coupon->get_id() );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( array(
|
||||
'id' => $coupon->get_id(),
|
||||
'code' => 'dummycoupon-1',
|
||||
'amount' => '1.00',
|
||||
'date_created' => wc_rest_prepare_date_response( $post->post_date_gmt, false ),
|
||||
'date_created_gmt' => wc_rest_prepare_date_response( $post->post_date_gmt ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt, false ),
|
||||
'date_modified_gmt' => wc_rest_prepare_date_response( $post->post_modified_gmt ),
|
||||
'discount_type' => 'fixed_cart',
|
||||
'description' => 'This is a dummy coupon',
|
||||
'date_expires' => null,
|
||||
'date_expires_gmt' => null,
|
||||
'usage_count' => 0,
|
||||
'individual_use' => false,
|
||||
'product_ids' => array(),
|
||||
'excluded_product_ids' => array(),
|
||||
'usage_limit' => null,
|
||||
'usage_limit_per_user' => null,
|
||||
'limit_usage_to_x_items' => null,
|
||||
'free_shipping' => false,
|
||||
'product_categories' => array(),
|
||||
'excluded_product_categories' => array(),
|
||||
'exclude_sale_items' => false,
|
||||
'minimum_amount' => '0.00',
|
||||
'maximum_amount' => '0.00',
|
||||
'email_restrictions' => array(),
|
||||
'used_by' => array(),
|
||||
'meta_data' => array(),
|
||||
), $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single coupon with an invalid ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single coupon without valid permissions.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_coupon_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single coupon.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons' );
|
||||
$request->set_body_params( array(
|
||||
'code' => 'test',
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
'description' => 'Test',
|
||||
'usage_limit' => 10,
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals( array(
|
||||
'id' => $data['id'],
|
||||
'code' => 'test',
|
||||
'amount' => '5.00',
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'discount_type' => 'fixed_product',
|
||||
'description' => 'Test',
|
||||
'date_expires' => null,
|
||||
'date_expires_gmt' => null,
|
||||
'usage_count' => 0,
|
||||
'individual_use' => false,
|
||||
'product_ids' => array(),
|
||||
'excluded_product_ids' => array(),
|
||||
'usage_limit' => 10,
|
||||
'usage_limit_per_user' => null,
|
||||
'limit_usage_to_x_items' => null,
|
||||
'free_shipping' => false,
|
||||
'product_categories' => array(),
|
||||
'excluded_product_categories' => array(),
|
||||
'exclude_sale_items' => false,
|
||||
'minimum_amount' => '0.00',
|
||||
'maximum_amount' => '0.00',
|
||||
'email_restrictions' => array(),
|
||||
'used_by' => array(),
|
||||
'meta_data' => array(),
|
||||
), $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single coupon with invalid fields.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_coupon_invalid_fields() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test no code...
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons' );
|
||||
$request->set_body_params( array(
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single coupon without valid permissions.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_coupon_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
// test no code...
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons' );
|
||||
$request->set_body_params( array(
|
||||
'code' => 'fail',
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single coupon.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$post = get_post( $coupon->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'This is a dummy coupon', $data['description'] );
|
||||
$this->assertEquals( 'fixed_cart', $data['discount_type'] );
|
||||
$this->assertEquals( '1.00', $data['amount'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( '10.00', $data['amount'] );
|
||||
$this->assertEquals( 'New description', $data['description'] );
|
||||
$this->assertEquals( 'fixed_cart', $data['discount_type'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single coupon with an invalid ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/0' );
|
||||
$request->set_body_params( array(
|
||||
'code' => 'tester',
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single coupon without valid permissions.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_coupon_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$post = get_post( $coupon->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single coupon.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single coupon with an invalid ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single coupon without valid permissions.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_coupon_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch operations on coupons.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_batch_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$coupon_1 = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
|
||||
$coupon_2 = WC_Helper_Coupon::create_coupon( 'dummycoupon-2' );
|
||||
$coupon_3 = WC_Helper_Coupon::create_coupon( 'dummycoupon-3' );
|
||||
$coupon_4 = WC_Helper_Coupon::create_coupon( 'dummycoupon-4' );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $coupon_1->get_id(),
|
||||
'amount' => '5.15',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$coupon_2->get_id(),
|
||||
$coupon_3->get_id(),
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'code' => 'new-coupon',
|
||||
'amount' => '11.00',
|
||||
),
|
||||
),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( '5.15', $data['update'][0]['amount'] );
|
||||
$this->assertEquals( '11.00', $data['create'][0]['amount'] );
|
||||
$this->assertEquals( 'new-coupon', $data['create'][0]['code'] );
|
||||
$this->assertEquals( $coupon_2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $coupon_3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/coupons' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test coupon schema.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_coupon_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/coupons' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 27, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'code', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'discount_type', $properties );
|
||||
$this->assertArrayHasKey( 'amount', $properties );
|
||||
$this->assertArrayHasKey( 'date_expires', $properties );
|
||||
$this->assertArrayHasKey( 'date_expires_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'usage_count', $properties );
|
||||
$this->assertArrayHasKey( 'individual_use', $properties );
|
||||
$this->assertArrayHasKey( 'product_ids', $properties );
|
||||
$this->assertArrayHasKey( 'excluded_product_ids', $properties );
|
||||
$this->assertArrayHasKey( 'usage_limit', $properties );
|
||||
$this->assertArrayHasKey( 'usage_limit_per_user', $properties );
|
||||
$this->assertArrayHasKey( 'limit_usage_to_x_items', $properties );
|
||||
$this->assertArrayHasKey( 'free_shipping', $properties );
|
||||
$this->assertArrayHasKey( 'product_categories', $properties );
|
||||
$this->assertArrayHasKey( 'excluded_product_categories', $properties );
|
||||
$this->assertArrayHasKey( 'exclude_sale_items', $properties );
|
||||
$this->assertArrayHasKey( 'minimum_amount', $properties );
|
||||
$this->assertArrayHasKey( 'maximum_amount', $properties );
|
||||
$this->assertArrayHasKey( 'email_restrictions', $properties );
|
||||
$this->assertArrayHasKey( 'used_by', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,540 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the Customers REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
||||
class Customers_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Customers_Controller();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( '/wc/v2/customers', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/customers/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/customers/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting customers.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_customers() {
|
||||
wp_set_current_user( 1 );
|
||||
|
||||
$customer_1 = WC_Helper_Customer::create_customer();
|
||||
WC_Helper_Customer::create_customer( 'test2', 'test2', 'test2@woo.local' );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/customers' );
|
||||
$request->set_query_params( array(
|
||||
'orderby' => 'id',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$customers = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $customers ) );
|
||||
|
||||
$this->assertContains( array(
|
||||
'id' => $customer_1->get_id(),
|
||||
'date_created' => wc_rest_prepare_date_response( $customer_1->get_date_created(), false ),
|
||||
'date_created_gmt' => wc_rest_prepare_date_response( $customer_1->get_date_created() ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $customer_1->get_date_modified(), false ),
|
||||
'date_modified_gmt' => wc_rest_prepare_date_response( $customer_1->get_date_modified() ),
|
||||
'email' => 'test@woo.local',
|
||||
'first_name' => 'Justin',
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'testcustomer',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $customer_1->get_avatar_url(),
|
||||
'meta_data' => array(),
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/customers/' . $customer_1->get_id() . '' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/customers' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $customers );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting customers without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_customers_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a new customer.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
|
||||
// Test just the basics first..
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request->set_body_params( array(
|
||||
'username' => 'create_customer_test',
|
||||
'password' => 'test123',
|
||||
'email' => 'create_customer_test@woo.local',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals( array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => 'create_customer_test@woo.local',
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'create_customer_test',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '',
|
||||
'address_2' => '',
|
||||
'city' => '',
|
||||
'state' => '',
|
||||
'postcode' => '',
|
||||
'country' => '',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '',
|
||||
'address_2' => '',
|
||||
'city' => '',
|
||||
'state' => '',
|
||||
'postcode' => '',
|
||||
'country' => '',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
), $data );
|
||||
|
||||
// Test extra data
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request->set_body_params( array(
|
||||
'username' => 'create_customer_test2',
|
||||
'password' => 'test123',
|
||||
'email' => 'create_customer_test2@woo.local',
|
||||
'first_name' => 'Test',
|
||||
'last_name' => 'McTestFace',
|
||||
'billing' => array(
|
||||
'country' => 'US',
|
||||
'state' => 'WA',
|
||||
),
|
||||
'shipping' => array(
|
||||
'state' => 'CA',
|
||||
'country' => 'US',
|
||||
),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals( array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => 'create_customer_test2@woo.local',
|
||||
'first_name' => 'Test',
|
||||
'last_name' => 'McTestFace',
|
||||
'role' => 'customer',
|
||||
'username' => 'create_customer_test2',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '',
|
||||
'address_2' => '',
|
||||
'city' => '',
|
||||
'state' => 'WA',
|
||||
'postcode' => '',
|
||||
'country' => 'US',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '',
|
||||
'address_2' => '',
|
||||
'city' => '',
|
||||
'state' => 'CA',
|
||||
'postcode' => '',
|
||||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
), $data );
|
||||
|
||||
// Test without required field
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request->set_body_params( array(
|
||||
'username' => 'create_customer_test3',
|
||||
'first_name' => 'Test',
|
||||
'last_name' => 'McTestFace',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating customers without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request->set_body_params( array(
|
||||
'username' => 'create_customer_test_without_permission',
|
||||
'password' => 'test123',
|
||||
'email' => 'create_customer_test_without_permission@woo.local',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single customer.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'get_customer_test', 'test123', 'get_customer_test@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => 'get_customer_test@woo.local',
|
||||
'first_name' => 'Justin',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'get_customer_test',
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
), $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single customer without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'get_customer_test_without_permission', 'test123', 'get_customer_test_without_permission@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single customer with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a customer.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'update_customer_test', 'test123', 'update_customer_test@woo.local' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'update_customer_test', $data['username'] );
|
||||
$this->assertEquals( 'update_customer_test@woo.local', $data['email'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/customers/' . $customer->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'email' => 'updated_email@woo.local',
|
||||
'first_name' => 'UpdatedTest',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'updated_email@woo.local', $data['email'] );
|
||||
$this->assertEquals( 'UpdatedTest', $data['first_name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a customer without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'update_customer_test_without_permission', 'test123', 'update_customer_test_without_permission@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a customer with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test deleting a customer.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'delete_customer_test', 'test123', 'delete_customer_test@woo.local' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/' . $customer->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a customer with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a customer without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'delete_customer_test_without_permission', 'test123', 'delete_customer_test_without_permission@woo.local' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/' . $customer->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test customer batch endpoint.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_batch_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
|
||||
$customer_1 = WC_Helper_Customer::create_customer( 'test_batch_customer', 'test123', 'test_batch_customer@woo.local' );
|
||||
$customer_2 = WC_Helper_Customer::create_customer( 'test_batch_customer2', 'test123', 'test_batch_customer2@woo.local' );
|
||||
$customer_3 = WC_Helper_Customer::create_customer( 'test_batch_customer3', 'test123', 'test_batch_customer3@woo.local' );
|
||||
$customer_4 = WC_Helper_Customer::create_customer( 'test_batch_customer4', 'test123', 'test_batch_customer4@woo.local' );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $customer_1->get_id(),
|
||||
'last_name' => 'McTest',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$customer_2->get_id(),
|
||||
$customer_3->get_id(),
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'username' => 'newuser',
|
||||
'password' => 'test123',
|
||||
'email' => 'newuser@woo.local',
|
||||
),
|
||||
),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'McTest', $data['update'][0]['last_name'] );
|
||||
$this->assertEquals( 'newuser', $data['create'][0]['username'] );
|
||||
$this->assertEmpty( $data['create'][0]['last_name'] );
|
||||
$this->assertEquals( $customer_2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $customer_3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test customer schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_customer_schema() {
|
||||
wp_set_current_user( 1 );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 18, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'email', $properties );
|
||||
$this->assertArrayHasKey( 'first_name', $properties );
|
||||
$this->assertArrayHasKey( 'last_name', $properties );
|
||||
$this->assertArrayHasKey( 'role', $properties );
|
||||
$this->assertArrayHasKey( 'username', $properties );
|
||||
$this->assertArrayHasKey( 'password', $properties );
|
||||
$this->assertArrayHasKey( 'orders_count', $properties );
|
||||
$this->assertArrayHasKey( 'total_spent', $properties );
|
||||
$this->assertArrayHasKey( 'avatar_url', $properties );
|
||||
$this->assertArrayHasKey( 'billing', $properties );
|
||||
$this->assertArrayHasKey( 'first_name', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'last_name', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'company', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'address_1', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'address_2', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'city', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'state', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'postcode', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'country', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'email', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'phone', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'shipping', $properties );
|
||||
$this->assertArrayHasKey( 'first_name', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'last_name', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'company', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'address_1', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'address_2', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'city', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'state', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'postcode', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'country', $properties['shipping']['properties'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,557 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the orders REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
class WC_Tests_API_Orders_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Array of order to track
|
||||
* @var array
|
||||
*/
|
||||
protected $orders = array();
|
||||
|
||||
/**
|
||||
* Setup our test server.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Orders_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/orders', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/orders/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/orders/(?P<id>[\d]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all orders.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_items() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Create 10 orders.
|
||||
for ( $i = 0; $i < 10; $i++ ) {
|
||||
$this->orders[] = WC_Helper_Order::create_order( $this->user );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) );
|
||||
$orders = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 10, count( $orders ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure orders cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_items_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$this->orders[] = WC_Helper_Order::create_order();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single order.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_item() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$order->add_meta_data( 'key', 'value' );
|
||||
$order->add_meta_data( 'key2', 'value2' );
|
||||
$order->save();
|
||||
$this->orders[] = $order;
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $order->get_id(), $data['id'] );
|
||||
|
||||
// Test meta data is set.
|
||||
$this->assertEquals( 'key', $data['meta_data'][0]->key );
|
||||
$this->assertEquals( 'value', $data['meta_data'][0]->value );
|
||||
$this->assertEquals( 'key2', $data['meta_data'][1]->key );
|
||||
$this->assertEquals( 'value2', $data['meta_data'][1]->value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single order without the correct permissions.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_item_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$this->orders[] = $order;
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting an order with an invalid ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_item_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/99999999' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting an order with an invalid ID.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_item_refund_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$refund = wc_create_refund( array(
|
||||
'order_id' => $order->get_id(),
|
||||
) );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $refund->get_id() ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating an order.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => 'Direct Bank Transfer',
|
||||
'set_paid' => true,
|
||||
'billing' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
'email' => 'john.doe@example.com',
|
||||
'phone' => '(555) 555-5555',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity' => 2,
|
||||
),
|
||||
),
|
||||
'shipping_lines' => array(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'method_title' => 'Flat rate',
|
||||
'total' => '10',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$order = wc_get_order( $data['id'] );
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals( $order->get_payment_method(), $data['payment_method'] );
|
||||
$this->assertEquals( $order->get_payment_method_title(), $data['payment_method_title'] );
|
||||
$this->assertEquals( $order->get_billing_first_name(), $data['billing']['first_name'] );
|
||||
$this->assertEquals( $order->get_billing_last_name(), $data['billing']['last_name'] );
|
||||
$this->assertEquals( '', $data['billing']['company'] );
|
||||
$this->assertEquals( $order->get_billing_address_1(), $data['billing']['address_1'] );
|
||||
$this->assertEquals( $order->get_billing_address_2(), $data['billing']['address_2'] );
|
||||
$this->assertEquals( $order->get_billing_city(), $data['billing']['city'] );
|
||||
$this->assertEquals( $order->get_billing_state(), $data['billing']['state'] );
|
||||
$this->assertEquals( $order->get_billing_postcode(), $data['billing']['postcode'] );
|
||||
$this->assertEquals( $order->get_billing_country(), $data['billing']['country'] );
|
||||
$this->assertEquals( $order->get_billing_email(), $data['billing']['email'] );
|
||||
$this->assertEquals( $order->get_billing_phone(), $data['billing']['phone'] );
|
||||
$this->assertEquals( $order->get_shipping_first_name(), $data['shipping']['first_name'] );
|
||||
$this->assertEquals( $order->get_shipping_last_name(), $data['shipping']['last_name'] );
|
||||
$this->assertEquals( '', $data['shipping']['company'] );
|
||||
$this->assertEquals( $order->get_shipping_address_1(), $data['shipping']['address_1'] );
|
||||
$this->assertEquals( $order->get_shipping_address_2(), $data['shipping']['address_2'] );
|
||||
$this->assertEquals( $order->get_shipping_city(), $data['shipping']['city'] );
|
||||
$this->assertEquals( $order->get_shipping_state(), $data['shipping']['state'] );
|
||||
$this->assertEquals( $order->get_shipping_postcode(), $data['shipping']['postcode'] );
|
||||
$this->assertEquals( $order->get_shipping_country(), $data['shipping']['country'] );
|
||||
$this->assertEquals( 1, count( $data['line_items'] ) );
|
||||
$this->assertEquals( 1, count( $data['shipping_lines'] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating an order without required fields.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_order_invalid_fields() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
// non-existent customer
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => 'Direct Bank Transfer',
|
||||
'set_paid' => true,
|
||||
'customer_id' => 99999,
|
||||
'billing' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
'email' => 'john.doe@example.com',
|
||||
'phone' => '(555) 555-5555',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity' => 2,
|
||||
),
|
||||
),
|
||||
'shipping_lines' => array(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'method_title' => 'Flat rate',
|
||||
'total' => 10,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
'first_name' => 'Fish',
|
||||
'last_name' => 'Face',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'test-update', $data['payment_method'] );
|
||||
$this->assertEquals( 'Fish', $data['billing']['first_name'] );
|
||||
$this->assertEquals( 'Face', $data['billing']['last_name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order and removing items.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_order_remove_items() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$fee = new WC_Order_Item_Fee();
|
||||
$fee->set_props(
|
||||
array(
|
||||
'name' => 'Some Fee',
|
||||
'tax_status' => 'taxable',
|
||||
'total' => '100',
|
||||
'tax_class' => '',
|
||||
)
|
||||
);
|
||||
$order->add_item( $fee );
|
||||
$order->save();
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$fee_data = current( $order->get_items( 'fee' ) );
|
||||
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'fee_lines' => array(
|
||||
array(
|
||||
'id' => $fee_data->get_id(),
|
||||
'name' => null,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertTrue( empty( $data['fee_lines'] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order and adding a coupon.
|
||||
*
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public function test_update_order_add_coupons() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$order_item = current( $order->get_items() );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' );
|
||||
$coupon->set_amount( 5 );
|
||||
$coupon->save();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'coupon_lines' => array(
|
||||
array(
|
||||
'code' => 'fake-coupon',
|
||||
'discount_total' => '5',
|
||||
'discount_tax' => '0',
|
||||
),
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'id' => $order_item->get_id(),
|
||||
'product_id' => $order_item->get_product_id(),
|
||||
'total' => '35.00',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $data['coupon_lines'] );
|
||||
$this->assertEquals( '45.00', $data['total'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order and removing a coupon.
|
||||
*
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public function test_update_order_remove_coupons() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$order_item = current( $order->get_items() );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' );
|
||||
$coupon->set_amount( 5 );
|
||||
$coupon->save();
|
||||
|
||||
$order->apply_coupon( $coupon );
|
||||
$order->save();
|
||||
|
||||
// Check that the coupon is applied.
|
||||
$this->assertEquals( '45.00', $order->get_total() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$coupon_data = current( $order->get_items( 'coupon' ) );
|
||||
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'coupon_lines' => array(
|
||||
array(
|
||||
'id' => $coupon_data->get_id(),
|
||||
'code' => null,
|
||||
),
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'id' => $order_item->get_id(),
|
||||
'product_id' => $order_item->get_product_id(),
|
||||
'total' => '40.00',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertTrue( empty( $data['coupon_lines'] ) );
|
||||
$this->assertEquals( '50.00', $data['total'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order without the correct permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_order_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
'first_name' => 'Fish',
|
||||
'last_name' => 'Face',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that updating an order with an invalid id fails.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_order_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/999999' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
'first_name' => 'Fish',
|
||||
'last_name' => 'Face',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting an order.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( null, get_post( $order->get_id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting an order without permission/creds.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_order_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting an order with an invalid id.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_order_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/9999999' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch managing product reviews.
|
||||
*/
|
||||
public function test_orders_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$order1 = WC_Helper_Order::create_order();
|
||||
$order2 = WC_Helper_Order::create_order();
|
||||
$order3 = WC_Helper_Order::create_order();
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $order1->get_id(),
|
||||
'payment_method' => 'updated',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$order2->get_id(),
|
||||
$order3->get_id(),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'updated', $data['update'][0]['payment_method'] );
|
||||
$this->assertEquals( $order2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $order3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/orders' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 1, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the order schema.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_order_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/orders/' . $order->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 42, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,309 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the Payment Gateways REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
||||
class Payment_Gateways_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Payment_Gateways_Controller();
|
||||
$this->user = $this->factory->user->create( array(
|
||||
'role' => 'administrator',
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/payment_gateways', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/payment_gateways/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all payment gateways.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_payment_gateways() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) );
|
||||
$gateways = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertContains( array(
|
||||
'id' => 'cheque',
|
||||
'title' => 'Check payments',
|
||||
'description' => 'Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.',
|
||||
'order' => '',
|
||||
'enabled' => false,
|
||||
'method_title' => 'Check payments',
|
||||
'method_description' => 'Take payments in person via checks. This offline gateway can also be useful to test purchases.',
|
||||
'settings' => array_diff_key( $this->get_settings( 'WC_Gateway_Cheque' ), array(
|
||||
'enabled' => false,
|
||||
'description' => false,
|
||||
) ),
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/payment_gateways/cheque' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/payment_gateways' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $gateways );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure payment gateways cannot viewed without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_payment_gateways_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single payment gateway.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_payment_gateway() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( array(
|
||||
'id' => 'paypal',
|
||||
'title' => 'PayPal',
|
||||
'description' => "Pay via PayPal; you can pay with your credit card if you don't have a PayPal account.",
|
||||
'order' => '',
|
||||
'enabled' => false,
|
||||
'method_title' => 'PayPal',
|
||||
'method_description' => 'PayPal Standard redirects customers to PayPal to enter their payment information.',
|
||||
'settings' => array_diff_key( $this->get_settings( 'WC_Gateway_Paypal' ), array(
|
||||
'enabled' => false,
|
||||
'description' => false,
|
||||
) ),
|
||||
), $paypal );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a payment gateway without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_payment_gateway_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a payment gateway with an invalid id.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_payment_gateway_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/totally_fake_method' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single payment gateway.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_payment_gateway() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Test defaults
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] );
|
||||
$this->assertEquals( 'admin@example.org', $paypal['settings']['email']['value'] );
|
||||
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test updating single setting
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'email' => 'woo@woo.local',
|
||||
),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] );
|
||||
$this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] );
|
||||
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test updating multiple settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'testmode' => 'yes',
|
||||
'title' => 'PayPal - New Title',
|
||||
),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] );
|
||||
$this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] );
|
||||
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// Test other parameters, and recheck settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'enabled' => false,
|
||||
'order' => 2,
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertFalse( $paypal['enabled'] );
|
||||
$this->assertEquals( 2, $paypal['order'] );
|
||||
$this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] );
|
||||
$this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] );
|
||||
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test bogus
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'paymentaction' => 'afasfasf',
|
||||
),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'paymentaction' => 'authorization',
|
||||
),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$paypal = $response->get_data();
|
||||
$this->assertEquals( 'authorization', $paypal['settings']['paymentaction']['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a payment gateway without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_payment_gateway_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params( array(
|
||||
'settings' => array(
|
||||
'testmode' => 'yes',
|
||||
'title' => 'PayPal - New Title',
|
||||
),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a payment gateway with an invalid id.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_payment_gateway_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/totally_fake_method' );
|
||||
$request->set_body_params( array(
|
||||
'enabled' => true,
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the payment gateway schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_payment_gateway_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/payment_gateways' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 8, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'title', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'order', $properties );
|
||||
$this->assertArrayHasKey( 'enabled', $properties );
|
||||
$this->assertArrayHasKey( 'method_title', $properties );
|
||||
$this->assertArrayHasKey( 'method_description', $properties );
|
||||
$this->assertArrayHasKey( 'settings', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a particular gateway's settings so we can correctly test API output.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param string $gateway_class Name of WC_Payment_Gateway class.
|
||||
*/
|
||||
private function get_settings( $gateway_class ) {
|
||||
$gateway = new $gateway_class();
|
||||
$settings = array();
|
||||
$gateway->init_form_fields();
|
||||
foreach ( $gateway->form_fields as $id => $field ) {
|
||||
// Make sure we at least have a title and type
|
||||
if ( empty( $field['title'] ) || empty( $field['type'] ) ) {
|
||||
continue;
|
||||
}
|
||||
// Ignore 'title' settings/fields -- they are UI only
|
||||
if ( 'title' === $field['type'] ) {
|
||||
continue;
|
||||
}
|
||||
$data = array(
|
||||
'id' => $id,
|
||||
'label' => empty( $field['label'] ) ? $field['title'] : $field['label'],
|
||||
'description' => empty( $field['description'] ) ? '' : $field['description'],
|
||||
'type' => $field['type'],
|
||||
'value' => $gateway->settings[ $id ],
|
||||
'default' => empty( $field['default'] ) ? '' : $field['default'],
|
||||
'tip' => empty( $field['description'] ) ? '' : $field['description'],
|
||||
'placeholder' => empty( $field['placeholder'] ) ? '' : $field['placeholder'],
|
||||
);
|
||||
if ( ! empty( $field['options'] ) ) {
|
||||
$data['options'] = $field['options'];
|
||||
}
|
||||
$settings[ $id ] = $data;
|
||||
}
|
||||
return $settings;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,465 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the product reviews REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class WC_Tests_API_Product_Reviews_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/products/reviews', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/reviews/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/reviews/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all product reviews.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_reviews() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
// Create 10 products reviews for the product
|
||||
for ( $i = 0; $i < 10; $i++ ) {
|
||||
$review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ) );
|
||||
$product_reviews = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 10, count( $product_reviews ) );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $review_id,
|
||||
'date_created' => $product_reviews[0]['date_created'],
|
||||
'date_created_gmt' => $product_reviews[0]['date_created_gmt'],
|
||||
'product_id' => $product->get_id(),
|
||||
'status' => 'approved',
|
||||
'reviewer' => 'admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
'review' => "<p>Review content here</p>\n",
|
||||
'rating' => 0,
|
||||
'verified' => false,
|
||||
'reviewer_avatar_urls' => $product_reviews[0]['reviewer_avatar_urls'],
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/products/reviews/' . $review_id ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/products/reviews' ),
|
||||
),
|
||||
),
|
||||
'up' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/products/' . $product->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $product_reviews
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure product reviews cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_reviews_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure an error is returned when an invalid product is loaded.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_reviews_invalid_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/0/reviews' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single product review.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $product_review_id,
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'product_id' => $product->get_id(),
|
||||
'status' => 'approved',
|
||||
'reviewer' => 'admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
'review' => "<p>Review content here</p>\n",
|
||||
'rating' => 0,
|
||||
'verified' => false,
|
||||
'reviewer_avatar_urls' => $data['reviewer_avatar_urls'],
|
||||
), $data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single product review without the correct permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_review_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a product review with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating a product review.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
'rating' => '5',
|
||||
'product_id' => $product->get_id(),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'product_id' => $product->get_id(),
|
||||
'status' => 'approved',
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
'review' => 'Hello world.',
|
||||
'rating' => 5,
|
||||
'verified' => false,
|
||||
'reviewer_avatar_urls' => $data['reviewer_avatar_urls'],
|
||||
), $data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating a product review without required fields.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_product_review_invalid_fields() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
// missing review
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// Missing reviewer.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// missing reviewer_email
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer' => 'Admin',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating a product review.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( "<p>Review content here</p>\n", $data['review'] );
|
||||
$this->assertEquals( 'admin', $data['reviewer'] );
|
||||
$this->assertEquals( 'woo@woo.local', $data['reviewer_email'] );
|
||||
$this->assertEquals( 0, $data['rating'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $product_review_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world - updated.',
|
||||
'reviewer' => 'Justin',
|
||||
'reviewer_email' => 'woo2@woo.local',
|
||||
'rating' => 3,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'Hello world - updated.', $data['review'] );
|
||||
$this->assertEquals( 'Justin', $data['reviewer'] );
|
||||
$this->assertEquals( 'woo2@woo.local', $data['reviewer_email'] );
|
||||
$this->assertEquals( 3, $data['rating'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating a product review without the correct permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_product_review_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $product_review_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.dev',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that updating a product review with an invalid id fails.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/0' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.dev',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a product review.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/' . $product_review_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a product review without permission/creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/' . $product_review_id );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a product review with an invalid id.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch managing product reviews.
|
||||
*/
|
||||
public function test_product_reviews_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
$review_1_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$review_2_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$review_3_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$review_4_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $review_1_id,
|
||||
'review' => 'Updated review.',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$review_2_id,
|
||||
$review_3_id,
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'review' => 'New review.',
|
||||
'reviewer' => 'Justin',
|
||||
'reviewer_email' => 'woo3@woo.local',
|
||||
'product_id' => $product->get_id(),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'Updated review.', $data['update'][0]['review'] );
|
||||
$this->assertEquals( 'New review.', $data['create'][0]['review'] );
|
||||
$this->assertEquals( $review_2_id, $data['delete'][0]['previous']['id'] );
|
||||
$this->assertEquals( $review_3_id, $data['delete'][1]['previous']['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/products/reviews' );
|
||||
$request->set_param( 'product', $product->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the product review schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_product_review_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/products/reviews' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 11, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'product_id', $properties );
|
||||
$this->assertArrayHasKey( 'status', $properties );
|
||||
$this->assertArrayHasKey( 'reviewer', $properties );
|
||||
$this->assertArrayHasKey( 'reviewer_email', $properties );
|
||||
$this->assertArrayHasKey( 'review', $properties );
|
||||
$this->assertArrayHasKey( 'rating', $properties );
|
||||
$this->assertArrayHasKey( 'verified', $properties );
|
||||
|
||||
if ( get_option( 'show_avatars' ) ) {
|
||||
$this->assertArrayHasKey( 'reviewer_avatar_urls', $properties );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,473 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for Variations API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
||||
class Product_Variations_API_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Product_Variations_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting variations.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_variations() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $variations ) );
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE LARGE', $variations[0]['sku'] );
|
||||
$this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting variations without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_variations_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single variation.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $variation_id, $variation['id'] );
|
||||
$this->assertEquals( 'size', $variation['attributes'][0]['name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting single variation without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single variation.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 1, count( $variations ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single variation without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single variation with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_variation_with_invalid_id() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test editing a single variation.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $variation['sku'] );
|
||||
$this->assertEquals( 10, $variation['regular_price'] );
|
||||
$this->assertEmpty( $variation['sale_price'] );
|
||||
$this->assertEquals( 'small', $variation['attributes'][0]['option'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU',
|
||||
'sale_price' => '8',
|
||||
'description' => 'O_O',
|
||||
'image' => array(
|
||||
'position' => 0,
|
||||
'src' => 'http://cldup.com/Dr1Bczxq4q.png',
|
||||
'alt' => 'test upload image',
|
||||
),
|
||||
'attributes' => array(
|
||||
array(
|
||||
'name' => 'pa_size',
|
||||
'option' => 'medium',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertTrue( isset( $variation['description'] ), print_r( $variation, true ) );
|
||||
$this->assertContains( 'O_O', $variation['description'], print_r( $variation, true ) );
|
||||
$this->assertEquals( '8', $variation['price'], print_r( $variation, true ) );
|
||||
$this->assertEquals( '8', $variation['sale_price'], print_r( $variation, true ) );
|
||||
$this->assertEquals( '10', $variation['regular_price'], print_r( $variation, true ) );
|
||||
$this->assertEquals( 'FIXED-SKU', $variation['sku'], print_r( $variation, true ) );
|
||||
$this->assertEquals( 'medium', $variation['attributes'][0]['option'], print_r( $variation, true ) );
|
||||
$this->assertContains( 'Dr1Bczxq4q', $variation['image']['src'], print_r( $variation, true ) );
|
||||
$this->assertContains( 'test upload image', $variation['image']['alt'], print_r( $variation, true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single variation without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single variation with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_variation_with_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/0' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single variation.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 2, count( $variations ) );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
'regular_price' => '12',
|
||||
'description' => 'A medium size.',
|
||||
'attributes' => array(
|
||||
array(
|
||||
'name' => 'pa_size',
|
||||
'option' => 'medium',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertContains( 'A medium size.', $variation['description'] );
|
||||
$this->assertEquals( '12', $variation['price'] );
|
||||
$this->assertEquals( '12', $variation['regular_price'] );
|
||||
$this->assertTrue( $variation['purchasable'] );
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $variation['sku'] );
|
||||
$this->assertEquals( 'medium', $variation['attributes'][0]['option'] );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 3, count( $variations ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single variation without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
'regular_price' => '12',
|
||||
'description' => 'A medium size.',
|
||||
'attributes' => array(
|
||||
array(
|
||||
'name' => 'pa_size',
|
||||
'option' => 'medium',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch managing product variations.
|
||||
*/
|
||||
public function test_product_variations_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $children[0],
|
||||
'description' => 'Updated description.',
|
||||
'image' => array(
|
||||
'position' => 0,
|
||||
'src' => 'http://cldup.com/Dr1Bczxq4q.png',
|
||||
'alt' => 'test upload image',
|
||||
),
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$children[1],
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
'regular_price' => '12',
|
||||
'description' => 'A medium size.',
|
||||
'attributes' => array(
|
||||
array(
|
||||
'name' => 'pa_size',
|
||||
'option' => 'medium',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertContains( 'Updated description.', $data['update'][0]['description'] );
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $data['create'][0]['sku'] );
|
||||
$this->assertEquals( 'medium', $data['create'][0]['attributes'][0]['option'] );
|
||||
$this->assertEquals( $children[1], $data['delete'][0]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 2, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test variation schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_variation_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 37, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'permalink', $properties );
|
||||
$this->assertArrayHasKey( 'sku', $properties );
|
||||
$this->assertArrayHasKey( 'price', $properties );
|
||||
$this->assertArrayHasKey( 'regular_price', $properties );
|
||||
$this->assertArrayHasKey( 'sale_price', $properties );
|
||||
$this->assertArrayHasKey( 'date_on_sale_from', $properties );
|
||||
$this->assertArrayHasKey( 'date_on_sale_to', $properties );
|
||||
$this->assertArrayHasKey( 'on_sale', $properties );
|
||||
$this->assertArrayHasKey( 'visible', $properties );
|
||||
$this->assertArrayHasKey( 'purchasable', $properties );
|
||||
$this->assertArrayHasKey( 'virtual', $properties );
|
||||
$this->assertArrayHasKey( 'downloadable', $properties );
|
||||
$this->assertArrayHasKey( 'downloads', $properties );
|
||||
$this->assertArrayHasKey( 'download_limit', $properties );
|
||||
$this->assertArrayHasKey( 'download_expiry', $properties );
|
||||
$this->assertArrayHasKey( 'tax_status', $properties );
|
||||
$this->assertArrayHasKey( 'tax_class', $properties );
|
||||
$this->assertArrayHasKey( 'manage_stock', $properties );
|
||||
$this->assertArrayHasKey( 'stock_quantity', $properties );
|
||||
$this->assertArrayHasKey( 'in_stock', $properties );
|
||||
$this->assertArrayHasKey( 'backorders', $properties );
|
||||
$this->assertArrayHasKey( 'backorders_allowed', $properties );
|
||||
$this->assertArrayHasKey( 'backordered', $properties );
|
||||
$this->assertArrayHasKey( 'weight', $properties );
|
||||
$this->assertArrayHasKey( 'dimensions', $properties );
|
||||
$this->assertArrayHasKey( 'shipping_class', $properties );
|
||||
$this->assertArrayHasKey( 'shipping_class_id', $properties );
|
||||
$this->assertArrayHasKey( 'image', $properties );
|
||||
$this->assertArrayHasKey( 'attributes', $properties );
|
||||
$this->assertArrayHasKey( 'menu_order', $properties );
|
||||
$this->assertArrayHasKey( 'meta_data', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a variation stock.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_variation_manage_stock() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$product->set_manage_stock( false );
|
||||
$product->save();
|
||||
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
// Set stock to true.
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'manage_stock' => true,
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( true, $variation['manage_stock'] );
|
||||
|
||||
// Set stock to false.
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'manage_stock' => false,
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( false, $variation['manage_stock'] );
|
||||
|
||||
// Set stock to false but parent is managing stock.
|
||||
$product->set_manage_stock( true );
|
||||
$product->save();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'manage_stock' => false,
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'parent', $variation['manage_stock'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,527 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for Products API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
||||
class Products_API_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Products_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/products', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_products() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Product::create_external_product();
|
||||
sleep( 1 ); // So both products have different timestamps.
|
||||
WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 2, count( $products ) );
|
||||
$this->assertEquals( 'Dummy Product', $products[0]['name'] );
|
||||
$this->assertEquals( 'DUMMY SKU', $products[0]['sku'] );
|
||||
$this->assertEquals( 'Dummy External Product', $products[1]['name'] );
|
||||
$this->assertEquals( 'DUMMY EXTERNAL SKU', $products[1]['sku'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_products_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single product.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
$simple = WC_Helper_Product::create_external_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $simple->get_id() ) );
|
||||
$product = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $simple->get_id(),
|
||||
'name' => 'Dummy External Product',
|
||||
'type' => 'simple',
|
||||
'status' => 'publish',
|
||||
'sku' => 'DUMMY EXTERNAL SKU',
|
||||
'regular_price' => 10,
|
||||
), $product
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting single product without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single product.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 0, count( $variations ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single product without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single product with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_product_with_invalid_id() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test editing a single product. Tests multiple product types.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test simple products
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'DUMMY SKU', $data['sku'] );
|
||||
$this->assertEquals( 10, $data['regular_price'] );
|
||||
$this->assertEmpty( $data['sale_price'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU',
|
||||
'sale_price' => '8',
|
||||
'description' => 'Testing',
|
||||
'images' => array(
|
||||
array(
|
||||
'position' => 0,
|
||||
'src' => 'http://cldup.com/Dr1Bczxq4q.png',
|
||||
'alt' => 'test upload image',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertContains( 'Testing', $data['description'] );
|
||||
$this->assertEquals( '8', $data['price'] );
|
||||
$this->assertEquals( '8', $data['sale_price'] );
|
||||
$this->assertEquals( '10', $data['regular_price'] );
|
||||
$this->assertEquals( 'FIXED-SKU', $data['sku'] );
|
||||
$this->assertContains( 'Dr1Bczxq4q', $data['images'][0]['src'] );
|
||||
$this->assertContains( 'test upload image', $data['images'][0]['alt'] );
|
||||
$product->delete( true );
|
||||
|
||||
// test variable product (variations are tested in product-variations.php)
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( array( 'large', 'small' ), $data['attributes'][0]['options'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_body_params( array(
|
||||
'attributes' => array(
|
||||
array(
|
||||
'id' => 0,
|
||||
'name' => 'pa_color',
|
||||
'options' => array(
|
||||
'red',
|
||||
'yellow',
|
||||
),
|
||||
'visible' => false,
|
||||
'variation' => 1,
|
||||
),
|
||||
array(
|
||||
'id' => 0,
|
||||
'name' => 'pa_size',
|
||||
'options' => array(
|
||||
'small',
|
||||
),
|
||||
'visible' => false,
|
||||
'variation' => 1,
|
||||
),
|
||||
),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( array( 'small' ), $data['attributes'][0]['options'] );
|
||||
$this->assertEquals( array( 'red', 'yellow' ), $data['attributes'][1]['options'] );
|
||||
$product->delete( true );
|
||||
|
||||
// test external product
|
||||
$product = WC_Helper_Product::create_external_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'Buy external product', $data['button_text'] );
|
||||
$this->assertEquals( 'http://woocommerce.com', $data['external_url'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'button_text' => 'Test API Update',
|
||||
'external_url' => 'http://automattic.com',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'Test API Update', $data['button_text'] );
|
||||
$this->assertEquals( 'http://automattic.com', $data['external_url'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single product without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single product with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_product_with_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/0' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-INVALID-ID',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single product.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/shipping_classes' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$shipping_class_id = $data['id'];
|
||||
|
||||
// Create simple
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'type' => 'simple',
|
||||
'name' => 'Test Simple Product',
|
||||
'sku' => 'DUMMY SKU SIMPLE API',
|
||||
'regular_price' => '10',
|
||||
'shipping_class' => 'test',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( '10', $data['price'] );
|
||||
$this->assertEquals( '10', $data['regular_price'] );
|
||||
$this->assertTrue( $data['purchasable'] );
|
||||
$this->assertEquals( 'DUMMY SKU SIMPLE API', $data['sku'] );
|
||||
$this->assertEquals( 'Test Simple Product', $data['name'] );
|
||||
$this->assertEquals( 'simple', $data['type'] );
|
||||
$this->assertEquals( $shipping_class_id, $data['shipping_class_id'] );
|
||||
|
||||
// Create external
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'type' => 'external',
|
||||
'name' => 'Test External Product',
|
||||
'sku' => 'DUMMY SKU EXTERNAL API',
|
||||
'regular_price' => '10',
|
||||
'button_text' => 'Test Button',
|
||||
'external_url' => 'https://wordpress.org',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( '10', $data['price'] );
|
||||
$this->assertEquals( '10', $data['regular_price'] );
|
||||
$this->assertFalse( $data['purchasable'] );
|
||||
$this->assertEquals( 'DUMMY SKU EXTERNAL API', $data['sku'] );
|
||||
$this->assertEquals( 'Test External Product', $data['name'] );
|
||||
$this->assertEquals( 'external', $data['type'] );
|
||||
$this->assertEquals( 'Test Button', $data['button_text'] );
|
||||
$this->assertEquals( 'https://wordpress.org', $data['external_url'] );
|
||||
|
||||
// Create variable
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'type' => 'variable',
|
||||
'name' => 'Test Variable Product',
|
||||
'sku' => 'DUMMY SKU VARIABLE API',
|
||||
'attributes' => array(
|
||||
array(
|
||||
'id' => 0,
|
||||
'name' => 'pa_size',
|
||||
'options' => array(
|
||||
'small',
|
||||
'medium',
|
||||
),
|
||||
'visible' => false,
|
||||
'variation' => 1,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE API', $data['sku'] );
|
||||
$this->assertEquals( 'Test Variable Product', $data['name'] );
|
||||
$this->assertEquals( 'variable', $data['type'] );
|
||||
$this->assertEquals( array( 'small', 'medium' ), $data['attributes'][0]['options'] );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
|
||||
$products = $response->get_data();
|
||||
$this->assertEquals( 3, count( $products ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single product without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test Product',
|
||||
'regular_price' => '12',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch managing products.
|
||||
*/
|
||||
public function test_products_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_2 = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $product->get_id(),
|
||||
'description' => 'Updated description.',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$product_2->get_id(),
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU BATCH TEST 1',
|
||||
'regular_price' => '10',
|
||||
'name' => 'Test Batch Create 1',
|
||||
'type' => 'external',
|
||||
'button_text' => 'Test Button',
|
||||
),
|
||||
array(
|
||||
'sku' => 'DUMMY SKU BATCH TEST 2',
|
||||
'regular_price' => '20',
|
||||
'name' => 'Test Batch Create 2',
|
||||
'type' => 'simple',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertContains( 'Updated description.', $data['update'][0]['description'] );
|
||||
$this->assertEquals( 'DUMMY SKU BATCH TEST 1', $data['create'][0]['sku'] );
|
||||
$this->assertEquals( 'DUMMY SKU BATCH TEST 2', $data['create'][1]['sku'] );
|
||||
$this->assertEquals( 'Test Button', $data['create'][0]['button_text'] );
|
||||
$this->assertEquals( 'external', $data['create'][0]['type'] );
|
||||
$this->assertEquals( 'simple', $data['create'][1]['type'] );
|
||||
$this->assertEquals( $product_2->get_id(), $data['delete'][0]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests to make sure you can filter products post statuses by both
|
||||
* the status query arg and WP_Query.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_products_filter_post_status() {
|
||||
wp_set_current_user( $this->user );
|
||||
for ( $i = 0; $i < 8; $i++ ) {
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
if ( 0 === $i % 2 ) {
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $product->get_id(),
|
||||
'post_status' => 'draft',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Test filtering with status=publish
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_param( 'status', 'publish' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 4, count( $products ) );
|
||||
foreach ( $products as $product ) {
|
||||
$this->assertEquals( 'publish', $product['status'] );
|
||||
}
|
||||
|
||||
// Test filtering with status=draft
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_param( 'status', 'draft' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 4, count( $products ) );
|
||||
foreach ( $products as $product ) {
|
||||
$this->assertEquals( 'draft', $product['status'] );
|
||||
}
|
||||
|
||||
// Test filtering with no filters - which should return 'any' (all 8)
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 8, count( $products ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test product schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_product_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/products/' . $product->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 65, count( $properties ) );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,818 @@
|
|||
<?php
|
||||
/**
|
||||
* Settings API Tests.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
||||
class Settings_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Setting_Options_Controller();
|
||||
WC_Helper_Settings::register();
|
||||
$this->user = $this->factory->user->create( array(
|
||||
'role' => 'administrator',
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/settings', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/settings/(?P<group_id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/settings/(?P<group_id>[\w-]+)/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all groups.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_groups() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertContains( array(
|
||||
'id' => 'test',
|
||||
'label' => 'Test extension',
|
||||
'parent_id' => '',
|
||||
'description' => 'My awesome test settings.',
|
||||
'sub_groups' => array( 'sub-test' ),
|
||||
'_links' => array(
|
||||
'options' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/test' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data );
|
||||
|
||||
$this->assertContains( array(
|
||||
'id' => 'sub-test',
|
||||
'label' => 'Sub test',
|
||||
'parent_id' => 'test',
|
||||
'description' => '',
|
||||
'sub_groups' => array(),
|
||||
'_links' => array(
|
||||
'options' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/sub-test' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /settings without valid permissions/creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_groups_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /settings without valid permissions/creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @covers WC_Rest_Settings_Controller::get_items
|
||||
*/
|
||||
public function test_get_groups_none_registered() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
remove_all_filters( 'woocommerce_settings_groups' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) );
|
||||
$this->assertEquals( 500, $response->get_status() );
|
||||
|
||||
WC_Helper_Settings::register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test groups schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_group_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/settings' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 5, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'parent_id', $properties );
|
||||
$this->assertArrayHasKey( 'label', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'sub_groups', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test settings schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_setting_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/settings/test/woocommerce_shop_page_display' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 9, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'label', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'value', $properties );
|
||||
$this->assertArrayHasKey( 'default', $properties );
|
||||
$this->assertArrayHasKey( 'tip', $properties );
|
||||
$this->assertArrayHasKey( 'placeholder', $properties );
|
||||
$this->assertArrayHasKey( 'type', $properties );
|
||||
$this->assertArrayHasKey( 'options', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single group.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_group() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test route callback receiving an empty group id
|
||||
$result = $this->endpoint->get_group_settings( '' );
|
||||
$this->assertIsWPError( $result );
|
||||
|
||||
// test getting a group that does not exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/not-real' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting the 'invalid' group
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/invalid' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting a valid group with settings attached to it
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 1, count( $data ) );
|
||||
$this->assertEquals( 'woocommerce_shop_page_display', $data[0]['id'] );
|
||||
$this->assertEmpty( $data[0]['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single group without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_group_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/coupon-data' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single setting.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_setting() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test defaults first
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( '', $data['value'] );
|
||||
|
||||
// test updating shop display setting
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'both',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'both', $data['value'] );
|
||||
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'subcategories',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'subcategories', $data['value'] );
|
||||
$this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => '',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( '', $data['value'] );
|
||||
$this->assertEquals( '', get_option( 'woocommerce_shop_page_display' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating multiple settings at once.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test defaults first
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( '', $data[0]['value'] );
|
||||
|
||||
// test setting both at once
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => 'woocommerce_shop_page_display',
|
||||
'value' => 'both',
|
||||
),
|
||||
),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'both', $data['update'][0]['value'] );
|
||||
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) );
|
||||
|
||||
// test updating one, but making sure the other value stays the same
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => 'woocommerce_shop_page_display',
|
||||
'value' => 'subcategories',
|
||||
),
|
||||
),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'subcategories', $data['update'][0]['value'] );
|
||||
$this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single setting.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_setting() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test getting an invalid setting from a group that does not exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/not-real/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting an invalid setting from a group that does exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/invalid/invalid' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting a valid setting
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 'woocommerce_shop_page_display', $data['id'] );
|
||||
$this->assertEquals( 'Shop page display', $data['label'] );
|
||||
$this->assertEquals( '', $data['default'] );
|
||||
$this->assertEquals( 'select', $data['type'] );
|
||||
$this->assertEquals( '', $data['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single setting without valid user permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_setting_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the GET single setting route handler receiving an empty setting ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_setting_empty_setting_id() {
|
||||
$result = $this->endpoint->get_setting( 'test', '' );
|
||||
|
||||
$this->assertIsWPError( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the GET single setting route handler receiving an invalid setting ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_setting_invalid_setting_id() {
|
||||
$result = $this->endpoint->get_setting( 'test', 'invalid' );
|
||||
|
||||
$this->assertIsWPError( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the GET single setting route handler encountering an invalid setting type.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_setting_invalid_setting_type() {
|
||||
// $controller = $this->getMock( 'WC_Rest_Setting_Options_Controller', array( 'get_group_settings', 'is_setting_type_valid' ) );
|
||||
$controller = $this->getMockBuilder( 'WC_Rest_Setting_Options_Controller' )->setMethods( array( 'get_group_settings', 'is_setting_type_valid' ) )->getMock();
|
||||
|
||||
$controller
|
||||
->expects( $this->any() )
|
||||
->method( 'get_group_settings' )
|
||||
->will( $this->returnValue( WC_Helper_Settings::register_test_settings( array() ) ) );
|
||||
|
||||
$controller
|
||||
->expects( $this->any() )
|
||||
->method( 'is_setting_type_valid' )
|
||||
->will( $this->returnValue( false ) );
|
||||
|
||||
$result = $controller->get_setting( 'test', 'woocommerce_shop_page_display' );
|
||||
|
||||
$this->assertIsWPError( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single setting without valid user permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_setting_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'subcategories',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test updating multiple settings without valid user permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_settings_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' );
|
||||
$request->set_body_params( array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => 'woocommerce_shop_page_display',
|
||||
'value' => 'subcategories',
|
||||
),
|
||||
),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a bad setting ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @covers WC_Rest_Setting_Options_Controller::update_item
|
||||
*/
|
||||
public function test_update_setting_bad_setting_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/test/invalid' );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'test',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests our classic setting registration to make sure settings added for WP-Admin are available over the API.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_classic_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Make sure the group is properly registered
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertTrue( is_array( $data ) );
|
||||
$this->assertContains( array(
|
||||
'id' => 'woocommerce_downloads_require_login',
|
||||
'label' => 'Access restriction',
|
||||
'description' => 'Downloads require login',
|
||||
'type' => 'checkbox',
|
||||
'default' => 'no',
|
||||
'tip' => 'This setting does not apply to guest purchases.',
|
||||
'value' => 'no',
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/products/woocommerce_downloads_require_login' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/products' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data );
|
||||
|
||||
// test get single
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products/woocommerce_dimension_unit' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'cm', $data['default'] );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_dimension_unit' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'yd',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'yd', $data['value'] );
|
||||
$this->assertEquals( 'yd', get_option( 'woocommerce_dimension_unit' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests our email etting registration to make sure settings added for WP-Admin are available over the API.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_email_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order' ) );
|
||||
$settings = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertContains( array(
|
||||
'id' => 'recipient',
|
||||
'label' => 'Recipient(s)',
|
||||
'description' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.',
|
||||
'type' => 'text',
|
||||
'default' => '',
|
||||
'tip' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.',
|
||||
'value' => '',
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/email_new_order/recipient' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/email_new_order' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $settings );
|
||||
|
||||
// test get single
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order/subject' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( array(
|
||||
'id' => 'subject',
|
||||
'label' => 'Subject',
|
||||
'description' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'type' => 'text',
|
||||
'default' => '',
|
||||
'tip' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'value' => '',
|
||||
), $setting );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_new_order', 'subject' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'This is my subject',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( array(
|
||||
'id' => 'subject',
|
||||
'label' => 'Subject',
|
||||
'description' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'type' => 'text',
|
||||
'default' => '',
|
||||
'tip' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'value' => 'This is my subject',
|
||||
), $setting );
|
||||
|
||||
// test updating another subject and making sure it works with a "similar" id
|
||||
$request = new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEmpty( $setting['value'] );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'This is my new subject',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'This is my new subject', $setting['value'] );
|
||||
|
||||
// make sure the other is what we left it
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order/subject' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'This is my subject', $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of checkbox settings.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_validation_checkbox() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test bogus value
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'not_yes_or_no',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// test yes
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'yes',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
// test no
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'no',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of radio settings.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_validation_radio() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// not a valid option
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'billing2',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// valid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'billing',
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of multiselect.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_validation_multiselect() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEmpty( $setting['value'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => array( 'AX', 'DZ', 'MMM' ),
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( array( 'AX', 'DZ' ), $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of select.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_validation_select() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'kg', $setting['value'] );
|
||||
|
||||
// invalid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'pounds', // invalid, should be lbs
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// valid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request->set_body_params( array(
|
||||
'value' => 'lbs', // invalid, should be lbs
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'lbs', $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the 'base location' setting is present in the response.
|
||||
* That it is returned as 'select' and not 'single_select_country',
|
||||
* and that both state and country options are returned.
|
||||
*
|
||||
* @since 3.0.7
|
||||
*/
|
||||
public function test_woocommerce_default_country() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_default_country' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'select', $setting['type'] );
|
||||
$this->assertArrayHasKey( 'GB', $setting['options'] );
|
||||
$this->assertArrayHasKey( 'US:OR', $setting['options'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the store address setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public function test_woocommerce_store_address() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_address' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $new_value,
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $old_value,
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $old_value, $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the store address 2 (line 2) setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public function test_woocommerce_store_address_2() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_address_2' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address_2' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $new_value,
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address_2' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $old_value,
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $old_value, $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the store city setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public function test_woocommerce_store_city() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_city' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_city' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $new_value,
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_city' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $old_value,
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $old_value, $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the store postcode setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public function test_woocommerce_store_postcode() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_postcode' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_postcode' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $new_value,
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_postcode' );
|
||||
$request->set_body_params( array(
|
||||
'value' => $old_value,
|
||||
) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $old_value, $setting['value'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the Shipping Methods REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
||||
class Shipping_Methods_V2 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.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping_methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping_methods/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all shipping methods.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods' ) );
|
||||
$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(
|
||||
'href' => rest_url( '/wc/v2/shipping_methods/free_shipping' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping_methods' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $methods );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure shipping methods cannot viewed without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_methods_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single shipping method.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/local_pickup' ) );
|
||||
$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.0.0
|
||||
*/
|
||||
public function test_get_shipping_method_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/local_pickup' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a shipping method with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_method_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/fake_method' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the shipping method schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_shipping_method_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/shipping_methods' );
|
||||
$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 );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,791 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Shipping Zones API Tests
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
protected $server;
|
||||
|
||||
protected $endpoint;
|
||||
|
||||
protected $user;
|
||||
|
||||
protected $zones;
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Shipping_Zones_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
$this->zones = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a Shipping Zone.
|
||||
*
|
||||
* @param string $name Zone name.
|
||||
* @param int $order Optional. Zone sort order.
|
||||
* @return WC_Shipping_Zone
|
||||
*/
|
||||
protected function create_shipping_zone( $name, $order = 0, $locations = array() ) {
|
||||
$zone = new WC_Shipping_Zone( null );
|
||||
$zone->set_zone_name( $name );
|
||||
$zone->set_zone_order( $order );
|
||||
$zone->set_locations( $locations );
|
||||
$zone->save();
|
||||
|
||||
$this->zones[] = $zone;
|
||||
|
||||
return $zone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<id>[\d-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<id>[\d]+)/locations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<zone_id>[\d]+)/methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<zone_id>[\d]+)/methods/(?P<instance_id>[\d]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all Shipping Zones.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_zones() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// "Rest of the World" zone exists by default
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 1 );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $data[0]['id'],
|
||||
'name' => 'Locations not covered by your other zones',
|
||||
'order' => 0,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[0]['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[0]['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data
|
||||
);
|
||||
|
||||
// Create a zone and make sure it's in the response
|
||||
$this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 2 );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $data[1]['id'],
|
||||
'name' => 'Zone 1',
|
||||
'order' => 0,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[1]['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[1]['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /shipping/zones without valid permissions/creds.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_zones_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /shipping/zones while Shipping is disabled in WooCommerce.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_zones_disabled_shipping() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
add_filter( 'wc_shipping_enabled', '__return_false' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
remove_filter( 'wc_shipping_enabled', '__return_false' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone schema.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_zone_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/shipping/zones' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 3, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertTrue( $properties['id']['readonly'] );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'order', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone create endpoint.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test Zone',
|
||||
'order' => 1,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'name' => 'Test Zone',
|
||||
'order' => 1,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone create endpoint.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_shipping_zone_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test Zone',
|
||||
'order' => 1,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone update endpoint.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Zone Test',
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $zone->get_id(),
|
||||
'name' => 'Zone Test',
|
||||
'order' => 2,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone update endpoint with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/555555' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Zone Test',
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone delete endpoint.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone delete endpoint without permissions.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_shipping_zone_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone delete endpoint with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/555555' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single Shipping Zone.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_single_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $zone->get_id(),
|
||||
'name' => 'Test Zone',
|
||||
'order' => 0,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single Shipping Zone with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_single_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting Shipping Zone Locations.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_locations() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Create a zone
|
||||
$zone = $this->create_shipping_zone(
|
||||
'Zone 1', 0, array(
|
||||
array(
|
||||
'code' => 'US',
|
||||
'type' => 'country',
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 1 );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
array(
|
||||
'code' => 'US',
|
||||
'type' => 'country',
|
||||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting Shipping Zone Locations with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_locations_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/locations' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone Locations update endpoint.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_locations() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' );
|
||||
$request->add_header( 'Content-Type', 'application/json' );
|
||||
$request->set_body(
|
||||
json_encode(
|
||||
array(
|
||||
array(
|
||||
'code' => 'UK',
|
||||
'type' => 'country',
|
||||
),
|
||||
array(
|
||||
'code' => 'US', // test that locations missing "type" treated as country.
|
||||
),
|
||||
array(
|
||||
'code' => 'SW1A0AA',
|
||||
'type' => 'postcode',
|
||||
),
|
||||
array(
|
||||
'type' => 'continent', // test that locations missing "code" aren't saved
|
||||
),
|
||||
)
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
array(
|
||||
'code' => 'UK',
|
||||
'type' => 'country',
|
||||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'code' => 'US',
|
||||
'type' => 'country',
|
||||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'code' => 'SW1A0AA',
|
||||
'type' => 'postcode',
|
||||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
), $data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating Shipping Zone Locations with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_locations_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/1/locations' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all Shipping Zone Methods and getting a single Shipping Zone Method.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Create a shipping method and make sure it's in the response
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
$methods = $zone->get_shipping_methods();
|
||||
$method = $methods[ $instance_id ];
|
||||
|
||||
$settings = array();
|
||||
$method->init_instance_settings();
|
||||
foreach ( $method->get_instance_form_fields() as $id => $field ) {
|
||||
$data = array(
|
||||
'id' => $id,
|
||||
'label' => $field['title'],
|
||||
'description' => ( empty( $field['description'] ) ? '' : $field['description'] ),
|
||||
'type' => $field['type'],
|
||||
'value' => $method->instance_settings[ $id ],
|
||||
'default' => ( empty( $field['default'] ) ? '' : $field['default'] ),
|
||||
'tip' => ( empty( $field['description'] ) ? '' : $field['description'] ),
|
||||
'placeholder' => ( empty( $field['placeholder'] ) ? '' : $field['placeholder'] ),
|
||||
);
|
||||
if ( ! empty( $field['options'] ) ) {
|
||||
$data['options'] = $field['options'];
|
||||
}
|
||||
$settings[ $id ] = $data;
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ) );
|
||||
$data = $response->get_data();
|
||||
$expected = array(
|
||||
'id' => $instance_id,
|
||||
'instance_id' => $instance_id,
|
||||
'title' => $method->instance_settings['title'],
|
||||
'order' => $method->method_order,
|
||||
'enabled' => ( 'yes' === $method->enabled ),
|
||||
'method_id' => $method->id,
|
||||
'method_title' => $method->method_title,
|
||||
'method_description' => $method->method_description,
|
||||
'settings' => $settings,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 1 );
|
||||
$this->assertContains( $expected, $data );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $expected, $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all Shipping Zone Methods with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_methods_invalid_zone_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single Shipping Zone Method with a bad ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_methods_invalid_method_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a Shipping Zone Method.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
$methods = $zone->get_shipping_methods();
|
||||
$method = $methods[ $instance_id ];
|
||||
|
||||
// Test defaults
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'title', $data['settings'] );
|
||||
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] );
|
||||
$this->assertArrayHasKey( 'tax_status', $data['settings'] );
|
||||
$this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '0', $data['settings']['cost']['value'] );
|
||||
|
||||
// Update a single value
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'cost' => 5,
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'title', $data['settings'] );
|
||||
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] );
|
||||
$this->assertArrayHasKey( 'tax_status', $data['settings'] );
|
||||
$this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '5', $data['settings']['cost']['value'] );
|
||||
|
||||
// Test multiple settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'cost' => 10,
|
||||
'tax_status' => 'none',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'title', $data['settings'] );
|
||||
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] );
|
||||
$this->assertArrayHasKey( 'tax_status', $data['settings'] );
|
||||
$this->assertEquals( 'none', $data['settings']['tax_status']['value'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '10', $data['settings']['cost']['value'] );
|
||||
|
||||
// Test bogus
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'cost' => 10,
|
||||
'tax_status' => 'this_is_not_a_valid_option',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// Test other parameters
|
||||
$this->assertTrue( $data['enabled'] );
|
||||
$this->assertEquals( 1, $data['order'] );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'enabled' => false,
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertFalse( $data['enabled'] );
|
||||
$this->assertEquals( 2, $data['order'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '10', $data['settings']['cost']['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a Shipping Zone Method.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'enabled' => false,
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertFalse( $data['enabled'] );
|
||||
$this->assertEquals( 2, $data['order'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '0', $data['settings']['cost']['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a Shipping Zone Method.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
$methods = $zone->get_shipping_methods();
|
||||
$method = $methods[ $instance_id ];
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,379 @@
|
|||
<?php
|
||||
/**
|
||||
* Class WC_Tests_REST_System_Status file.
|
||||
*
|
||||
* @package WooCommerce/Tests
|
||||
*/
|
||||
|
||||
/**
|
||||
* System Status REST Tests.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0
|
||||
*/
|
||||
class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_System_Status_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/system_status', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/system_status/tools', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/system_status/tools/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure system status cannot be accessed without valid creds
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure root properties are present.
|
||||
* (environment, theme, database, etc).
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_returns_root_properties() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'environment', $data );
|
||||
$this->assertArrayHasKey( 'database', $data );
|
||||
$this->assertArrayHasKey( 'active_plugins', $data );
|
||||
$this->assertArrayHasKey( 'theme', $data );
|
||||
$this->assertArrayHasKey( 'settings', $data );
|
||||
$this->assertArrayHasKey( 'security', $data );
|
||||
$this->assertArrayHasKey( 'pages', $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure environment response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_environment() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$environment = (array) $data['environment'];
|
||||
|
||||
// Make sure all expected data is present
|
||||
$this->assertEquals( 32, count( $environment ) );
|
||||
|
||||
// Test some responses to make sure they match up.
|
||||
$this->assertEquals( get_option( 'home' ), $environment['home_url'] );
|
||||
$this->assertEquals( get_option( 'siteurl' ), $environment['site_url'] );
|
||||
$this->assertEquals( WC()->version, $environment['version'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure database response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_database() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$database = (array) $data['database'];
|
||||
|
||||
$this->assertEquals( get_option( 'woocommerce_db_version' ), $database['wc_database_version'] );
|
||||
$this->assertEquals( $wpdb->prefix, $database['database_prefix'] );
|
||||
$this->assertEquals( WC_Geolocation::get_local_database_path(), $database['maxmind_geoip_database'] );
|
||||
$this->assertArrayHasKey( 'woocommerce', $database['database_tables'], print_r( $database, true ) );
|
||||
$this->assertArrayHasKey( $wpdb->prefix . 'woocommerce_payment_tokens', $database['database_tables']['woocommerce'], print_r( $database, true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure active plugins response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_active_plugins() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$actual_plugins = array( 'hello.php' );
|
||||
update_option( 'active_plugins', $actual_plugins );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
update_option( 'active_plugins', array() );
|
||||
|
||||
$data = $response->get_data();
|
||||
$plugins = (array) $data['active_plugins'];
|
||||
|
||||
$this->assertEquals( 1, count( $plugins ) );
|
||||
$this->assertEquals( 'Hello Dolly', $plugins[0]['name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure theme response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_theme() {
|
||||
wp_set_current_user( $this->user );
|
||||
$active_theme = wp_get_theme();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$theme = (array) $data['theme'];
|
||||
|
||||
$this->assertEquals( 13, count( $theme ) );
|
||||
$this->assertEquals( $active_theme->Name, $theme['name'] ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure settings response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$term_response = array();
|
||||
$terms = get_terms( 'product_type', array( 'hide_empty' => 0 ) );
|
||||
foreach ( $terms as $term ) {
|
||||
$term_response[ $term->slug ] = strtolower( $term->name );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$settings = (array) $data['settings'];
|
||||
|
||||
$this->assertEquals( 11, count( $settings ) );
|
||||
$this->assertEquals( ( 'yes' === get_option( 'woocommerce_api_enabled' ) ), $settings['api_enabled'] );
|
||||
$this->assertEquals( get_woocommerce_currency(), $settings['currency'] );
|
||||
$this->assertEquals( $term_response, $settings['taxonomies'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure security response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_security() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$settings = (array) $data['security'];
|
||||
|
||||
$this->assertEquals( 2, count( $settings ) );
|
||||
$this->assertEquals( 'https' === substr( get_permalink( wc_get_page_id( 'shop' ) ), 0, 5 ), $settings['secure_connection'] );
|
||||
$this->assertEquals( ! ( defined( 'WP_DEBUG' ) && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG && WP_DEBUG_DISPLAY ) || 0 === intval( ini_get( 'display_errors' ) ), $settings['hide_errors'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure pages response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_pages() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$pages = $data['pages'];
|
||||
$this->assertEquals( 5, count( $pages ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test system status schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_system_status_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 7, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'environment', $properties );
|
||||
$this->assertArrayHasKey( 'database', $properties );
|
||||
$this->assertArrayHasKey( 'active_plugins', $properties );
|
||||
$this->assertArrayHasKey( 'theme', $properties );
|
||||
$this->assertArrayHasKey( 'settings', $properties );
|
||||
$this->assertArrayHasKey( 'security', $properties );
|
||||
$this->assertArrayHasKey( 'pages', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure get_items (all tools) response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_tools() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$tools_controller = new WC_REST_System_Status_Tools_Controller();
|
||||
$raw_tools = $tools_controller->get_tools();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $raw_tools ), count( $data ) );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'reset_tracking',
|
||||
'name' => 'Reset usage tracking',
|
||||
'action' => 'Reset',
|
||||
'description' => 'This will reset your usage tracking settings, causing it to show the opt-in banner again and not sending any data.',
|
||||
'_links' => array(
|
||||
'item' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/system_status/tools/reset_tracking' ),
|
||||
'embeddable' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure system status tools cannot be accessed without valid creds
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_tools_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure we can load a single tool correctly.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_tool() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$tools_controller = new WC_REST_System_Status_Tools_Controller();
|
||||
$raw_tools = $tools_controller->get_tools();
|
||||
$raw_tool = $raw_tools['recount_terms'];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 'recount_terms', $data['id'] );
|
||||
$this->assertEquals( 'Term counts', $data['name'] );
|
||||
$this->assertEquals( 'Recount terms', $data['action'] );
|
||||
$this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure a single system status toolscannot be accessed without valid creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_tool_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure we can RUN a tool correctly.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_execute_system_tool() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$tools_controller = new WC_REST_System_Status_Tools_Controller();
|
||||
$raw_tools = $tools_controller->get_tools();
|
||||
$raw_tool = $raw_tools['recount_terms'];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'recount_terms', $data['id'] );
|
||||
$this->assertEquals( 'Term counts', $data['name'] );
|
||||
$this->assertEquals( 'Recount terms', $data['action'] );
|
||||
$this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] );
|
||||
$this->assertTrue( $data['success'] );
|
||||
$this->assertEquals( 1, did_action( 'woocommerce_rest_insert_system_status_tool' ) );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/not_a_real_tool' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure a tool cannot be run without valid creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_execute_system_status_tool_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test system status schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_system_status_tool_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status/tools' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 6, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'action', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'success', $properties );
|
||||
$this->assertArrayHasKey( 'message', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test execute_tool() with the "add_order_indexes" tool.
|
||||
*/
|
||||
public function test_execute_system_tool_add_order_indexes() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$order1 = WC_Helper_Order::create_order();
|
||||
$order2 = WC_Helper_Order::create_order();
|
||||
delete_post_meta( $order1->get_id(), '_billing_address_index' );
|
||||
delete_post_meta( $order2->get_id(), '_billing_address_index' );
|
||||
delete_post_meta( $order2->get_id(), '_shipping_address_index' );
|
||||
|
||||
$controller = new WC_REST_System_Status_Tools_Controller();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/add_order_indexes' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertTrue( $data['success'] );
|
||||
$this->assertNotEmpty( get_post_meta( $order1->get_id(), '_billing_address_index', true ) );
|
||||
$this->assertNotEmpty( get_post_meta( $order2->get_id(), '_billing_address_index', true ) );
|
||||
$this->assertNotEmpty( get_post_meta( $order2->get_id(), '_shipping_address_index', true ) );
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue