PHPCS fixes

This commit is contained in:
Rodrigo Primo 2018-05-04 11:42:45 -03:00
parent eeb97510eb
commit 7ce69dfca2
5 changed files with 869 additions and 690 deletions

View File

@ -19,9 +19,11 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_Orders_Controller();
$this->user = $this->factory->user->create( array(
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
) );
)
);
}
/**
@ -135,7 +137,8 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
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(
$request->set_body_params(
array(
'payment_method' => 'bacs',
'payment_method_title' => 'Direct Bank Transfer',
'set_paid' => true,
@ -174,7 +177,8 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
'total' => '10',
),
),
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$order = wc_get_order( $data['id'] );
@ -218,7 +222,8 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
// non-existent customer
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' );
$request->set_body_params( array(
$request->set_body_params(
array(
'payment_method' => 'bacs',
'payment_method_title' => 'Direct Bank Transfer',
'set_paid' => true,
@ -258,7 +263,8 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
'total' => 10,
),
),
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 400, $response->get_status() );
@ -274,13 +280,15 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
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(
$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();
@ -301,26 +309,30 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
wp_set_current_user( $this->user );
$order = WC_Helper_Order::create_order();
$fee = new WC_Order_Item_Fee();
$fee->set_props( array(
$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(
$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();
@ -343,7 +355,8 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
$coupon->set_amount( 5 );
$coupon->save();
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
$request->set_body_params( array(
$request->set_body_params(
array(
'coupon_lines' => array(
array(
'code' => 'fake-coupon',
@ -358,7 +371,8 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
'total' => '35.00',
),
),
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -391,7 +405,8 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
$coupon_data = current( $order->get_items( 'coupon' ) );
$request->set_body_params( array(
$request->set_body_params(
array(
'coupon_lines' => array(
array(
'id' => $coupon_data->get_id(),
@ -405,7 +420,8 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
'total' => '40.00',
),
),
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -425,13 +441,15 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
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(
$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() );
WC_Helper_Order::delete_order( $order->get_id() );
@ -444,13 +462,15 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
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(
$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() );
}
@ -507,7 +527,8 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
$order3 = WC_Helper_Order::create_order();
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/batch' );
$request->set_body_params( array(
$request->set_body_params(
array(
'update' => array(
array(
'id' => $order1->get_id(),
@ -518,7 +539,8 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
$order2->get_id(),
$order3->get_id(),
),
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();

View File

@ -14,9 +14,11 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_Product_Reviews_Controller();
$this->user = $this->factory->user->create( array(
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
) );
)
);
}
/**
@ -48,7 +50,8 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 10, count( $product_reviews ) );
$this->assertContains( array(
$this->assertContains(
array(
'id' => $review_id,
'date_created' => $product_reviews[0]['date_created'],
'date_created_gmt' => $product_reviews[0]['date_created_gmt'],
@ -74,7 +77,8 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
),
),
),
), $product_reviews );
), $product_reviews
);
$product->delete( true );
}
@ -117,7 +121,8 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( array(
$this->assertEquals(
array(
'id' => $product_review_id,
'date_created' => $data['date_created'],
'date_created_gmt' => $data['date_created_gmt'],
@ -126,7 +131,8 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
'name' => 'admin',
'email' => 'woo@woo.local',
'verified' => false,
), $data );
), $data
);
$product->delete( true );
}
@ -166,17 +172,20 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
wp_set_current_user( $this->user );
$product = WC_Helper_Product::create_simple_product();
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/reviews' );
$request->set_body_params( array(
$request->set_body_params(
array(
'review' => 'Hello world.',
'name' => 'Admin',
'email' => 'woo@woo.local',
'rating' => '5',
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 201, $response->get_status() );
$this->assertEquals( array(
$this->assertEquals(
array(
'id' => $data['id'],
'date_created' => $data['date_created'],
'date_created_gmt' => $data['date_created_gmt'],
@ -185,7 +194,8 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
'name' => 'Admin',
'email' => 'woo@woo.local',
'verified' => false,
), $data );
), $data
);
$product->delete( true );
}
@ -200,10 +210,12 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
// missing review
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/reviews' );
$request->set_body_params( array(
$request->set_body_params(
array(
'name' => 'Admin',
'email' => 'woo@woo.local',
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -211,10 +223,12 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
// missing name
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/reviews' );
$request->set_body_params( array(
$request->set_body_params(
array(
'review' => 'Hello world.',
'email' => 'woo@woo.local',
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -222,10 +236,12 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
// missing email
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/reviews' );
$request->set_body_params( array(
$request->set_body_params(
array(
'review' => 'Hello world.',
'name' => 'Admin',
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -251,12 +267,14 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$this->assertEquals( 0, $data['rating'] );
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/reviews/' . $product_review_id );
$request->set_body_params( array(
$request->set_body_params(
array(
'review' => 'Hello world - updated.',
'name' => 'Justin',
'email' => 'woo2@woo.local',
'rating' => 3,
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'Hello world - updated.', $data['review'] );
@ -277,11 +295,13 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/reviews/' . $product_review_id );
$request->set_body_params( array(
$request->set_body_params(
array(
'review' => 'Hello world.',
'name' => 'Admin',
'email' => 'woo@woo.dev',
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -299,11 +319,13 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$product = WC_Helper_Product::create_simple_product();
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/reviews/0' );
$request->set_body_params( array(
$request->set_body_params(
array(
'review' => 'Hello world.',
'name' => 'Admin',
'email' => 'woo@woo.dev',
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -376,7 +398,8 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$review_4_id = WC_Helper_Product::create_product_review( $product->get_id() );
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/reviews/batch' );
$request->set_body_params( array(
$request->set_body_params(
array(
'update' => array(
array(
'id' => $review_1_id,
@ -394,7 +417,8 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
'email' => 'woo3@woo.local',
),
),
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();

View File

@ -14,9 +14,11 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_Product_Variations_Controller();
$this->user = $this->factory->user->create( array(
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
) );
)
);
}
/**
@ -171,13 +173,24 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
$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(
$request->set_body_params(
array(
'sku' => 'FIXED-SKU',
'sale_price' => '8',
'description' => 'O_O',
'image' => array( 'position' => 0, 'src' => 'https://cldup.com/Dr1Bczxq4q.png', 'alt' => 'test upload image' ),
'attributes' => array( array( 'name' => 'pa_size', 'option' => 'medium' ) ),
) );
'image' => array(
'position' => 0,
'src' => 'https://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();
@ -204,9 +217,11 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
$variation_id = $children[0];
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
$request->set_body_params( array(
$request->set_body_params(
array(
'sku' => 'FIXED-SKU-NO-PERMISSION',
) );
)
);
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
$product->delete( true );
@ -221,9 +236,11 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
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(
$request->set_body_params(
array(
'sku' => 'FIXED-SKU-NO-PERMISSION',
) );
)
);
$response = $this->server->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
$product->delete( true );
@ -243,12 +260,19 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
$this->assertEquals( 2, count( $variations ) );
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' );
$request->set_body_params( array(
$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' ) ),
) );
'attributes' => array(
array(
'name' => 'pa_size',
'option' => 'medium',
),
),
)
);
$response = $this->server->dispatch( $request );
$variation = $response->get_data();
@ -275,12 +299,19 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
$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(
$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' ) ),
) );
'attributes' => array(
array(
'name' => 'pa_size',
'option' => 'medium',
),
),
)
);
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
$product->delete( true );
@ -294,12 +325,17 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
$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(
$request->set_body_params(
array(
'update' => array(
array(
'id' => $children[0],
'description' => 'Updated description.',
'image' => array( 'position' => 0, 'src' => 'https://cldup.com/Dr1Bczxq4q.png', 'alt' => 'test upload image' ),
'image' => array(
'position' => 0,
'src' => 'https://cldup.com/Dr1Bczxq4q.png',
'alt' => 'test upload image',
),
),
),
'delete' => array(
@ -310,10 +346,16 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
'regular_price' => '12',
'description' => 'A medium size.',
'attributes' => array( array( 'name' => 'pa_size', 'option' => 'medium' ) ),
'attributes' => array(
array(
'name' => 'pa_size',
'option' => 'medium',
),
),
) );
),
),
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -399,9 +441,11 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
// Set stock to true.
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
$request->set_body_params( array(
$request->set_body_params(
array(
'manage_stock' => true,
) );
)
);
$response = $this->server->dispatch( $request );
$variation = $response->get_data();
@ -411,9 +455,11 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
// Set stock to false.
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
$request->set_body_params( array(
$request->set_body_params(
array(
'manage_stock' => false,
) );
)
);
$response = $this->server->dispatch( $request );
$variation = $response->get_data();
@ -425,9 +471,11 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
$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(
$request->set_body_params(
array(
'manage_stock' => false,
) );
)
);
$response = $this->server->dispatch( $request );
$variation = $response->get_data();

View File

@ -14,9 +14,11 @@ class Products_API extends WC_REST_Unit_Test_Case {
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_Products_Controller();
$this->user = $this->factory->user->create( array(
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
) );
)
);
}
/**
@ -89,14 +91,16 @@ class Products_API extends WC_REST_Unit_Test_Case {
$product = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertContains( array(
$this->assertContains(
array(
'id' => $simple->get_id(),
'name' => 'Dummy External Product',
'type' => 'simple',
'status' => 'publish',
'sku' => 'DUMMY EXTERNAL SKU',
'regular_price' => 10,
), $product );
), $product
);
$simple->delete( true );
}
@ -179,12 +183,20 @@ class Products_API extends WC_REST_Unit_Test_Case {
$this->assertEmpty( $data['sale_price'] );
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
$request->set_body_params( array(
$request->set_body_params(
array(
'sku' => 'FIXED-SKU',
'sale_price' => '8',
'description' => 'Testing',
'images' => array( array( 'position' => 0, 'src' => 'https://cldup.com/Dr1Bczxq4q.png', 'alt' => 'test upload image' ) ),
) );
'images' => array(
array(
'position' => 0,
'src' => 'https://cldup.com/Dr1Bczxq4q.png',
'alt' => 'test upload image',
),
),
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -205,12 +217,25 @@ class Products_API extends WC_REST_Unit_Test_Case {
$this->assertEquals( array( 'small' ), $data['attributes'][0]['options'] );
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
$request->set_body_params( array(
$request->set_body_params(
array(
'attributes' => array(
array( 'id' => 0, 'name' => 'pa_color', 'options' => array( 'red', 'yellow' ), 'visible' => false, 'variation' => 1 ),
array( 'name' => 'pa_size', 'options' => array( 'small' ), 'visible' => false, 'variation' => 1 ),
array(
'id' => 0,
'name' => 'pa_color',
'options' => array( 'red', 'yellow' ),
'visible' => false,
'variation' => 1,
),
) );
array(
'name' => 'pa_size',
'options' => array( 'small' ),
'visible' => false,
'variation' => 1,
),
),
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -227,10 +252,12 @@ class Products_API extends WC_REST_Unit_Test_Case {
$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(
$request->set_body_params(
array(
'button_text' => 'Test API Update',
'external_url' => 'http://automattic.com',
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -248,9 +275,11 @@ class Products_API extends WC_REST_Unit_Test_Case {
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(
$request->set_body_params(
array(
'sku' => 'FIXED-SKU-NO-PERMISSION',
) );
)
);
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
$product->delete( true );
@ -265,9 +294,11 @@ class Products_API extends WC_REST_Unit_Test_Case {
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(
$request->set_body_params(
array(
'sku' => 'FIXED-SKU-INVALID-ID',
) );
)
);
$response = $this->server->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
$product->delete( true );
@ -282,22 +313,26 @@ class Products_API extends WC_REST_Unit_Test_Case {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'POST', '/wc/v2/products/shipping_classes' );
$request->set_body_params( array(
$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(
$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();
@ -311,14 +346,16 @@ class Products_API extends WC_REST_Unit_Test_Case {
// Create external
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
$request->set_body_params( array(
$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();
@ -333,7 +370,8 @@ class Products_API extends WC_REST_Unit_Test_Case {
// Create variable
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
$request->set_body_params( array(
$request->set_body_params(
array(
'type' => 'variable',
'name' => 'Test Variable Product',
'sku' => 'DUMMY SKU VARIABLE API',
@ -349,7 +387,8 @@ class Products_API extends WC_REST_Unit_Test_Case {
'variation' => 1,
),
),
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -372,10 +411,12 @@ class Products_API extends WC_REST_Unit_Test_Case {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
$request->set_body_params( array(
$request->set_body_params(
array(
'name' => 'Test Product',
'regular_price' => '12',
) );
)
);
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
}
@ -388,7 +429,8 @@ class Products_API extends WC_REST_Unit_Test_Case {
$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(
$request->set_body_params(
array(
'update' => array(
array(
'id' => $product->get_id(),
@ -413,7 +455,8 @@ class Products_API extends WC_REST_Unit_Test_Case {
'type' => 'simple',
),
),
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -445,10 +488,12 @@ class Products_API extends WC_REST_Unit_Test_Case {
for ( $i = 0; $i < 8; $i++ ) {
$product = WC_Helper_Product::create_simple_product();
if ( 0 === $i % 2 ) {
wp_update_post( array(
wp_update_post(
array(
'ID' => $product->get_id(),
'post_status' => 'draft',
) );
)
);
}
}

View File

@ -21,9 +21,11 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_Shipping_Zones_Controller();
$this->user = $this->factory->user->create( array(
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
) );
)
);
$this->zones = array();
}
@ -82,7 +84,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 1 );
$this->assertContains( array(
$this->assertContains(
array(
'id' => 0,
'name' => 'Locations not covered by your other zones',
'order' => 0,
@ -103,7 +106,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
),
),
),
), $data );
), $data
);
// Create a zone and make sure it's in the response
$this->create_shipping_zone( 'Zone 1' );
@ -113,7 +117,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 2 );
$this->assertContains( array(
$this->assertContains(
array(
'id' => 1,
'name' => 'Zone 1',
'order' => 0,
@ -134,7 +139,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
),
),
),
), $data );
), $data
);
}
/**
@ -187,15 +193,18 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' );
$request->set_body_params( array(
$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(
$this->assertEquals(
array(
'id' => $data['id'],
'name' => 'Test Zone',
'order' => 1,
@ -216,7 +225,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
),
),
),
), $data );
), $data
);
}
/**
@ -227,10 +237,12 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
wp_set_current_user( 0 );
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' );
$request->set_body_params( array(
$request->set_body_params(
array(
'name' => 'Test Zone',
'order' => 1,
) );
)
);
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
}
@ -245,15 +257,18 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$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(
$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(
$this->assertEquals(
array(
'id' => $zone->get_id(),
'name' => 'Zone Test',
'order' => 2,
@ -274,7 +289,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
),
),
),
), $data );
), $data
);
}
/**
@ -285,10 +301,12 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/555555' );
$request->set_body_params( array(
$request->set_body_params(
array(
'name' => 'Zone Test',
'order' => 2,
) );
)
);
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
@ -347,7 +365,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( array(
$this->assertEquals(
array(
'id' => $zone->get_id(),
'name' => 'Test Zone',
'order' => 0,
@ -368,7 +387,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
),
),
),
), $data );
), $data
);
}
/**
@ -391,19 +411,22 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
wp_set_current_user( $this->user );
// Create a zone
$zone = $this->create_shipping_zone( 'Zone 1', 0, array(
$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(
$this->assertEquals(
array(
array(
'code' => 'US',
'type' => 'country',
@ -420,7 +443,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
),
),
),
), $data );
), $data
);
}
/**
@ -446,7 +470,9 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$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(
$request->set_body(
json_encode(
array(
array(
'code' => 'UK',
'type' => 'country',
@ -461,12 +487,15 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
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(
$this->assertEquals(
array(
array(
'code' => 'UK',
'type' => 'country',
@ -515,7 +544,8 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
),
),
),
), $data );
), $data
);
}
/**
@ -659,11 +689,13 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
// 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(
$request->set_body_params(
array(
'settings' => array(
'cost' => 5,
),
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -676,12 +708,14 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
// Test multiple settings
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
$request->set_body_params( array(
$request->set_body_params(
array(
'settings' => array(
'cost' => 10,
'tax_status' => 'none',
),
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -694,12 +728,14 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
// Test bogus
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
$request->set_body_params( array(
$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() );
@ -708,10 +744,12 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
$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(
$request->set_body_params(
array(
'enabled' => false,
'order' => 2,
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@ -729,11 +767,13 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
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(
$request->set_body_params(
array(
'method_id' => 'flat_rate',
'enabled' => false,
'order' => 2,
) );
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data();