2018-12-22 00:40:41 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Orders REST API Test
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\API
|
2018-12-22 00:40:41 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-05 18:14:25 +00:00
|
|
|
use \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore;
|
2020-11-30 15:11:49 +00:00
|
|
|
use Automattic\WooCommerce\Admin\API\Reports\Controller as ReportsController;
|
2019-08-05 18:14:25 +00:00
|
|
|
|
2018-12-22 00:40:41 +00:00
|
|
|
/**
|
|
|
|
* WC Tests API Orders
|
|
|
|
*/
|
|
|
|
class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoints.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-11-12 18:15:55 +00:00
|
|
|
protected $endpoint = '/wc-analytics/orders';
|
2018-12-22 00:40:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup test data. Called before every test.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->user = $this->factory->user->create(
|
|
|
|
array(
|
|
|
|
'role' => 'administrator',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-30 15:11:49 +00:00
|
|
|
/**
|
|
|
|
* Test the order status (it should allow all actionable statuses).
|
|
|
|
*/
|
|
|
|
public function test_order_status() {
|
|
|
|
// Add a status to the actionable list.
|
|
|
|
$actionable_statuses = get_option( 'woocommerce_actionable_order_statuses', array() );
|
|
|
|
update_option( 'woocommerce_actionable_order_statuses', array( 'test-status' ) );
|
|
|
|
|
|
|
|
// Ideally this would be a test using an endpoint, but the option value is used
|
|
|
|
// at `rest_api_init` time to create the collection param schema. It's too late
|
|
|
|
// here to test with the actual endpoint code. Instantiating a new WP_REST_Server
|
|
|
|
// didn't seem to work either.
|
|
|
|
$this->assertContains( 'test-status', ReportsController::get_order_statuses() );
|
|
|
|
|
|
|
|
// Restore the actionable statuses.
|
|
|
|
update_option( 'woocommerce_actionable_order_statuses', $actionable_statuses );
|
|
|
|
}
|
|
|
|
|
2018-12-22 00:40:41 +00:00
|
|
|
/**
|
|
|
|
* Test order number param.
|
|
|
|
*/
|
|
|
|
public function test_get_items_number_param() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
$orders = array();
|
|
|
|
for ( $i = 0; $i < 10; $i++ ) {
|
|
|
|
$orders[] = WC_Helper_Order::create_order( $this->user );
|
|
|
|
}
|
|
|
|
|
|
|
|
$order = $orders[9];
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
2019-03-22 20:29:17 +00:00
|
|
|
'number' => (string) $order->get_id(),
|
2018-12-22 00:40:41 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$orders = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( $order->get_id(), $orders[0]['id'] );
|
|
|
|
}
|
2019-06-20 02:39:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify that deleted refund parent orders don't cause
|
|
|
|
*/
|
|
|
|
public function test_refund_parent_order_deleted() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// Create an order.
|
|
|
|
$order = WC_Helper_Order::create_order( $this->user );
|
|
|
|
|
|
|
|
// Create a refund order.
|
|
|
|
$refund = wc_create_refund(
|
|
|
|
array(
|
|
|
|
'order_id' => $order->get_id(),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Forcibly delete the original order.
|
|
|
|
$wpdb->delete( $wpdb->prefix . 'posts', array( 'ID' => $order->get_id() ), array( '%d' ) );
|
|
|
|
clean_post_cache( $order->get_id() );
|
|
|
|
|
|
|
|
// Trigger an order sync on the refund which should handle the missing parent order.
|
2019-08-05 18:14:25 +00:00
|
|
|
$this->assertTrue( OrdersStatsDataStore::sync_order( $refund->get_id() ) );
|
2019-06-20 02:39:48 +00:00
|
|
|
}
|
2018-12-22 00:40:41 +00:00
|
|
|
}
|