woocommerce/tests/unit-tests/api/orders.php

541 lines
16 KiB
PHP
Raw Normal View History

<?php
/**
2016-08-18 16:24:44 +00:00
* Tests for the orders REST API.
*
* @package WooCommerce\Tests\API
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
2016-08-18 16:24:44 +00:00
class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
/**
2016-08-18 16:24:44 +00:00
* Array of order to track
* @var array
*/
protected $orders = array();
/**
* Setup our test server.
*/
2016-08-18 16:24:44 +00:00
public function setUp() {
parent::setUp();
2016-08-18 16:24:44 +00:00
$this->endpoint = new WC_REST_Orders_Controller();
$this->user = $this->factory->user->create( array(
'role' => 'administrator',
) );
}
/**
* Test route registration.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
public function test_register_routes() {
$routes = $this->server->get_routes();
2017-02-09 20:21:52 +00:00
$this->assertArrayHasKey( '/wc/v2/orders', $routes );
$this->assertArrayHasKey( '/wc/v2/orders/batch', $routes );
$this->assertArrayHasKey( '/wc/v2/orders/(?P<id>[\d]+)', $routes );
2016-08-18 16:24:44 +00:00
}
/**
* Cleanup.
*/
public function stoppit_and_tidyup() {
foreach ( $this->orders as $order ) {
wp_delete_post( $order->get_id(), true );
}
$this->orders = array();
}
/**
* Test getting all orders.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
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 );
}
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) );
2016-08-18 16:24:44 +00:00
$orders = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 10, count( $orders ) );
$this->stoppit_and_tidyup();
}
/**
* Tests to make sure orders cannot be viewed without valid permissions.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
public function test_get_items_without_permission() {
wp_set_current_user( 0 );
$this->orders[] = WC_Helper_Order::create_order();
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) );
2016-08-18 16:24:44 +00:00
$this->assertEquals( 401, $response->get_status() );
$this->stoppit_and_tidyup();
}
/**
* Tests getting a single order.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
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();
2016-08-18 16:24:44 +00:00
$this->orders[] = $order;
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) );
2016-08-18 16:24:44 +00:00
$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 );
2016-08-18 16:24:44 +00:00
$this->stoppit_and_tidyup();
}
/**
* Tests getting a single order without the correct permissions.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
public function test_get_item_without_permission() {
wp_set_current_user( 0 );
$order = WC_Helper_Order::create_order();
$this->orders[] = $order;
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) );
2016-08-18 16:24:44 +00:00
$this->assertEquals( 401, $response->get_status() );
$this->stoppit_and_tidyup();
}
/**
* Tests getting an order with an invalid ID.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
public function test_get_item_invalid_id() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/99999999' ) );
2016-08-18 16:24:44 +00:00
$this->assertEquals( 404, $response->get_status() );
}
/**
* Tests creating an order.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
public function test_create_order() {
wp_set_current_user( $this->user );
$product = WC_Helper_Product::create_simple_product();
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' );
2016-08-18 16:24:44 +00:00
$request->set_body_params( array(
2016-08-19 04:51:25 +00:00
'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',
2016-08-18 16:24:44 +00:00
),
2016-08-19 04:51:25 +00:00
'shipping' => array(
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US',
2016-08-18 16:24:44 +00:00
),
2016-08-19 04:51:25 +00:00
'line_items' => array(
2016-08-18 16:24:44 +00:00
array(
2016-08-19 04:51:25 +00:00
'product_id' => $product->get_id(),
'quantity' => 2,
2016-08-18 16:24:44 +00:00
),
),
2016-08-19 04:51:25 +00:00
'shipping_lines' => array(
2016-08-18 16:24:44 +00:00
array(
2016-08-19 04:51:25 +00:00
'method_id' => 'flat_rate',
'method_title' => 'Flat rate',
2017-11-16 19:47:34 +00:00
'total' => '10',
2016-08-18 16:24:44 +00:00
),
),
) );
$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'] ) );
wp_delete_post( $product->get_id(), true );
wp_delete_post( $data['id'], true );
}
/**
* Tests creating an order without required fields.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
public function test_create_order_invalid_fields() {
wp_set_current_user( $this->user );
$product = WC_Helper_Product::create_simple_product();
// non-existent customer
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' );
2016-08-18 16:24:44 +00:00
$request->set_body_params( array(
2016-08-19 04:51:25 +00:00
'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',
2016-08-18 16:24:44 +00:00
),
2016-08-19 04:51:25 +00:00
'shipping' => array(
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US',
2016-08-18 16:24:44 +00:00
),
2016-08-19 04:51:25 +00:00
'line_items' => array(
2016-08-18 16:24:44 +00:00
array(
2016-08-19 04:51:25 +00:00
'product_id' => $product->get_id(),
'quantity' => 2,
2016-08-18 16:24:44 +00:00
),
),
2016-08-19 04:51:25 +00:00
'shipping_lines' => array(
2016-08-18 16:24:44 +00:00
array(
2016-08-19 04:51:25 +00:00
'method_id' => 'flat_rate',
'method_title' => 'Flat rate',
'total' => 10,
2016-08-18 16:24:44 +00:00
),
),
) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 400, $response->get_status() );
wp_delete_post( $product->get_id(), true );
}
/**
* Tests updating an order.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
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() );
2016-08-18 16:24:44 +00:00
$request->set_body_params( array(
2016-08-19 04:51:25 +00:00
'payment_method' => 'test-update',
'billing' => array(
'first_name' => 'Fish',
'last_name' => 'Face',
2016-08-18 16:24:44 +00:00
),
) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
2016-08-19 04:51:25 +00:00
$this->assertEquals( 'test-update', $data['payment_method'] );
2016-08-18 16:24:44 +00:00
$this->assertEquals( 'Fish', $data['billing']['first_name'] );
$this->assertEquals( 'Face', $data['billing']['last_name'] );
WC_Helper_Order::delete_order( $order->get_id() );
}
/**
* 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'] ) );
WC_Helper_Order::delete_order( $order->get_id() );
2016-08-18 16:24:44 +00:00
}
/**
* Tests updating an order and adding a coupon.
*
* @since 3.0.0
*/
public function test_update_order_add_coupons() {
wp_set_current_user( $this->user );
$order = WC_Helper_Order::create_order();
$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',
),
),
) );
$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'] );
WC_Helper_Order::delete_order( $order->get_id() );
}
/**
* Tests updating an order and removing a coupon.
*
* @since 3.0.0
*/
public function test_update_order_remove_coupons() {
wp_set_current_user( $this->user );
$order = WC_Helper_Order::create_order();
$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,
),
),
) );
$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'] );
WC_Helper_Order::delete_order( $order->get_id() );
}
2016-08-18 16:24:44 +00:00
/**
* Tests updating an order without the correct permissions.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
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() );
2016-08-18 16:24:44 +00:00
$request->set_body_params( array(
2016-08-19 04:51:25 +00:00
'payment_method' => 'test-update',
'billing' => array(
'first_name' => 'Fish',
'last_name' => 'Face',
2016-08-18 16:24:44 +00:00
),
) );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
WC_Helper_Order::delete_order( $order->get_id() );
2016-08-18 16:24:44 +00:00
}
2016-08-18 16:24:44 +00:00
/**
* Tests that updating an order with an invalid id fails.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
public function test_update_order_invalid_id() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/999999' );
2016-08-18 16:24:44 +00:00
$request->set_body_params( array(
2016-08-19 04:51:25 +00:00
'payment_method' => 'test-update',
'billing' => array(
'first_name' => 'Fish',
'last_name' => 'Face',
2016-08-18 16:24:44 +00:00
),
) );
$response = $this->server->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
}
2016-08-18 16:24:44 +00:00
/**
* Test deleting an order.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
public function test_delete_order() {
wp_set_current_user( $this->user );
$order = WC_Helper_Order::create_order();
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() );
2016-08-18 16:24:44 +00:00
$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.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
public function test_delete_order_without_permission() {
wp_set_current_user( 0 );
$order = WC_Helper_Order::create_order();
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() );
2016-08-18 16:24:44 +00:00
$request->set_param( 'force', true );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
wp_delete_post( $order->get_id(), true );
}
/**
* Test deleting an order with an invalid id.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
public function test_delete_order_invalid_id() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/9999999' );
2016-08-18 16:24:44 +00:00
$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();
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/batch' );
2016-08-18 16:24:44 +00:00
$request->set_body_params( array(
'update' => array(
array(
'id' => $order1->get_id(),
'payment_method' => 'updated',
),
),
'delete' => array(
2016-08-19 04:51:25 +00:00
$order2->get_id(),
$order3->get_id(),
2016-08-18 16:24:44 +00:00
),
) );
$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'] );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'GET', '/wc/v2/orders' );
2016-08-18 16:24:44 +00:00
$response = $this->server->dispatch( $request );
$data = $response->get_data();
2016-08-19 04:51:25 +00:00
$this->assertEquals( 1, count( $data ) );
2016-08-18 16:24:44 +00:00
wp_delete_post( $order1->get_id(), true );
wp_delete_post( $order2->get_id(), true );
wp_delete_post( $order3->get_id(), true );
}
/**
* Test the order schema.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-08-18 16:24:44 +00:00
*/
public function test_order_schema() {
wp_set_current_user( $this->user );
$order = WC_Helper_Order::create_order();
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/orders/' . $order->get_id() );
2016-08-18 16:24:44 +00:00
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
2017-03-10 18:59:55 +00:00
$this->assertEquals( 42, count( $properties ) );
2016-08-18 16:24:44 +00:00
$this->assertArrayHasKey( 'id', $properties );
wp_delete_post( $order->get_id(), true );
}
}