woocommerce/tests/php/includes/class-wc-ajax-test.php

89 lines
2.4 KiB
PHP
Raw Normal View History

2020-05-29 20:50:20 +00:00
<?php
/**
* Class WC_AJAX_Test file.
*
2020-08-07 16:36:41 +00:00
* @package WooCommerce\Tests\WC_AJAX.
2020-05-29 20:50:20 +00:00
*/
/**
* Class WC_AJAX_Test file.
*/
class WC_AJAX_Test extends \WC_Unit_Test_Case {
/**
* Stock should not be reduced from AJAX when an item is added to an order.
*/
public function test_add_item_to_pending_payment_order() {
$product = WC_Helper_Product::create_simple_product();
$product->set_manage_stock( true );
$product->set_stock_quantity( 1000 );
$product->save();
$order = WC_Helper_Order::create_order();
$data = array(
array(
'id' => $product->get_id(),
'qty' => 10,
),
);
// Call private method `maybe_add_order_item`.
$maybe_add_order_item_func = function () use ( $order, $data ) {
return static::maybe_add_order_item( $order->get_id(), '', $data );
};
$maybe_add_order_item_func->call( new WC_AJAX() );
// Refresh from DB.
$product = wc_get_product( $product->get_id() );
// Stock should not have been reduced because order status is 'pending'.
$this->assertEquals( 1000, $product->get_stock_quantity() );
$line_items = $order->get_items();
foreach ( $line_items as $line_item ) {
if ( $line_item->get_product_id() === $product->get_id() ) {
$this->assertEquals( false, $line_item->get_meta( '_reduced_stock', true ) );
}
}
}
/**
* Stock should be reduced from AJAX when an item is added to an order, when status is being changed
*/
public function test_add_item_to_processing_order() {
$product = WC_Helper_Product::create_simple_product();
$product->set_manage_stock( true );
$product->set_stock_quantity( 1000 );
$product->save();
$order = WC_Helper_Order::create_order();
$order->set_status( 'pending' );
$order->save();
$data = array(
array(
'id' => $product->get_id(),
'qty' => 10,
),
);
// Call private method `maybe_add_order_item`.
$maybe_add_order_item_func = function () use ( $order, $data ) {
return static::maybe_add_order_item( $order->get_id(), '', $data );
};
$maybe_add_order_item_func->call( new WC_AJAX() );
$order->set_status( 'processing' );
$order->save();
// Refresh from DB.
$product = wc_get_product( $product->get_id() );
$this->assertEquals( 990, $product->get_stock_quantity() );
$line_items = $order->get_items();
foreach ( $line_items as $line_item ) {
if ( $line_item->get_product_id() === $product->get_id() ) {
$this->assertEquals( 10, $line_item->get_meta( '_reduced_stock', true ) );
}
}
}
}