2018-12-12 13:35:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Products REST API Test
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\API
|
2018-12-12 13:35:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC Tests API Products
|
|
|
|
*/
|
|
|
|
class WC_Tests_API_Products extends WC_REST_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoints.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-11-12 18:15:55 +00:00
|
|
|
protected $endpoint = '/wc-analytics/products';
|
2018-12-12 13:35:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup test data. Called before every test.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2019-02-14 23:18:34 +00:00
|
|
|
$this->user = $this->factory->user->create(
|
2018-12-12 13:35:56 +00:00
|
|
|
array(
|
|
|
|
'role' => 'administrator',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test product schema contains embed fields.
|
|
|
|
*/
|
|
|
|
public function test_product_schema() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
$product = WC_Helper_Product::create_simple_product();
|
2019-11-12 18:15:55 +00:00
|
|
|
$request = new WP_REST_Request( 'OPTIONS', '/wc-analytics/products/' . $product->get_id() );
|
2018-12-12 13:35:56 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
$properties = $data['schema']['properties'];
|
|
|
|
|
|
|
|
$properties_to_embed = array(
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'slug',
|
|
|
|
'permalink',
|
|
|
|
'images',
|
|
|
|
'description',
|
|
|
|
'short_description',
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $properties as $property_key => $property ) {
|
2019-02-14 23:18:34 +00:00
|
|
|
if ( in_array( $property_key, $properties_to_embed, true ) ) {
|
2018-12-12 13:35:56 +00:00
|
|
|
$this->assertEquals( array( 'view', 'edit', 'embed' ), $property['context'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:51:15 +00:00
|
|
|
$this->assertArrayHasKey( 'last_order_date', $properties );
|
|
|
|
|
2018-12-12 13:35:56 +00:00
|
|
|
$product->delete( true );
|
|
|
|
}
|
2020-11-25 18:51:15 +00:00
|
|
|
|
2021-07-20 23:47:22 +00:00
|
|
|
|
2020-11-25 18:51:15 +00:00
|
|
|
/**
|
|
|
|
* Test low stock query.
|
|
|
|
*/
|
|
|
|
public function test_low_stock() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// Create a product with stock management.
|
|
|
|
$product = WC_Helper_Product::create_simple_product();
|
|
|
|
$product->set_manage_stock( true );
|
|
|
|
$product->set_low_stock_amount( 2 );
|
|
|
|
$product->set_stock_quantity( 5 );
|
|
|
|
$product->save();
|
|
|
|
|
|
|
|
// Order enough of the product to trigger low stock status.
|
|
|
|
$order_time = '2020-11-24T10:00:00';
|
|
|
|
$order = WC_Helper_Order::create_order( 1, $product );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_date_created( $order_time );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
// Sync analytics data (used for last order date).
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
2021-07-20 23:47:22 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', '/wc-analytics/products/low-in-stock' );
|
2020-11-25 18:51:15 +00:00
|
|
|
$request->set_param( 'low_in_stock', true );
|
2021-07-20 23:47:22 +00:00
|
|
|
$request->set_param( 'status', 'publish' );
|
2020-11-25 18:51:15 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 1, $data );
|
|
|
|
$this->assertEquals( $product->get_id(), $data[0]['id'] );
|
|
|
|
$this->assertEquals( $order_time, $data[0]['last_order_date'] );
|
|
|
|
}
|
2018-12-12 13:35:56 +00:00
|
|
|
}
|