woocommerce/plugins/woocommerce-admin/tests/api/products.php

99 lines
2.4 KiB
PHP
Raw Normal View History

<?php
/**
* Products REST API Test
*
* @package WooCommerce\Admin\Tests\API
*/
/**
* WC Tests API Products
*/
class WC_Tests_API_Products extends WC_REST_Unit_Test_Case {
/**
* Endpoints.
*
* @var string
*/
protected $endpoint = '/wc-analytics/products';
/**
* 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(
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();
$request = new WP_REST_Request( 'OPTIONS', '/wc-analytics/products/' . $product->get_id() );
$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 ) ) {
$this->assertEquals( array( 'view', 'edit', 'embed' ), $property['context'] );
}
}
Migrate Stock Panel to Homescreen (https://github.com/woocommerce/woocommerce-admin/pull/5729) * Refactor low stock variable to be the count instead of a boolean. * Add initial render of the Stock panel on the homescreen. * Move existing Stock panel to homescreen accordion. * Ensure int value for low stock product count. * Update ProductImage styling. * Update stock activity car styles. * Only show 5 low stock products. * Add "undo" action to the stock updated snackbar. * Fix check for explicit notice dismissal when taking actions. * Hide now-in-stock products after updating. By cllearing "edited" flag on successful update. * Fetch more products after updating stock. * Fix the number of product placeholders shown. * Only show products placeholders on the initial fetch. * Fix placeholder style. * Fetch low stock count dynamically. * Let initialOpen prop toggle Accordion panels if they haven't been toggled by the user. * Refactor item total count state. Allows for auto-updating item totals whenever identical queries (from a totals perspective) are issued. * Add last order date to low stock products API response. * Allow non-date strings in ActivityCard date prop. * Add last order date to stock panel cards. * Remove empty stock panel view. * Add test file for StockPanel. * Only request necessary fields from products endpoint. * Add test for products fetch after stock update. * Fix field name. * Add test for last order date in low stock products API response. * Stock panel should be initially closed. * Skip updating a product if the quantity is unchanged.
2020-11-25 18:51:15 +00:00
$this->assertArrayHasKey( 'last_order_date', $properties );
$product->delete( true );
}
Migrate Stock Panel to Homescreen (https://github.com/woocommerce/woocommerce-admin/pull/5729) * Refactor low stock variable to be the count instead of a boolean. * Add initial render of the Stock panel on the homescreen. * Move existing Stock panel to homescreen accordion. * Ensure int value for low stock product count. * Update ProductImage styling. * Update stock activity car styles. * Only show 5 low stock products. * Add "undo" action to the stock updated snackbar. * Fix check for explicit notice dismissal when taking actions. * Hide now-in-stock products after updating. By cllearing "edited" flag on successful update. * Fetch more products after updating stock. * Fix the number of product placeholders shown. * Only show products placeholders on the initial fetch. * Fix placeholder style. * Fetch low stock count dynamically. * Let initialOpen prop toggle Accordion panels if they haven't been toggled by the user. * Refactor item total count state. Allows for auto-updating item totals whenever identical queries (from a totals perspective) are issued. * Add last order date to low stock products API response. * Allow non-date strings in ActivityCard date prop. * Add last order date to stock panel cards. * Remove empty stock panel view. * Add test file for StockPanel. * Only request necessary fields from products endpoint. * Add test for products fetch after stock update. * Fix field name. * Add test for last order date in low stock products API response. * Stock panel should be initially closed. * Skip updating a product if the quantity is unchanged.
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();
$request = new WP_REST_Request( 'GET', '/wc-analytics/products' );
$request->set_param( 'low_in_stock', true );
$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'] );
}
}