2019-07-09 21:07:44 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Checkout tests.
|
|
|
|
*
|
|
|
|
* @package WooCommerce|Tests|Checkout
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WC_Checkout
|
|
|
|
*/
|
|
|
|
class WC_Tests_Checkout extends WC_Unit_Test_Case {
|
2020-04-21 10:07:21 +00:00
|
|
|
|
2019-12-13 10:36:04 +00:00
|
|
|
/**
|
2020-04-21 10:07:21 +00:00
|
|
|
* TearDown for tests.
|
2019-12-13 10:36:04 +00:00
|
|
|
*/
|
2019-07-09 21:07:44 +00:00
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
|
|
|
WC()->cart->empty_cart();
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:36:04 +00:00
|
|
|
/**
|
2020-04-21 10:07:21 +00:00
|
|
|
* Setup for tests.
|
2019-12-13 10:36:04 +00:00
|
|
|
*/
|
2019-07-09 21:07:44 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
WC()->cart->empty_cart();
|
|
|
|
}
|
|
|
|
|
2019-12-13 10:36:04 +00:00
|
|
|
/**
|
2019-07-09 21:07:44 +00:00
|
|
|
* Test if order can be created when a coupon with usage limit is applied.
|
|
|
|
*
|
|
|
|
* @throws Exception When unable to create order.
|
|
|
|
*/
|
|
|
|
public function test_create_order_with_limited_coupon() {
|
2020-04-21 10:07:21 +00:00
|
|
|
$coupon_code = 'coupon4one';
|
2019-07-09 21:07:44 +00:00
|
|
|
$coupon_data_store = WC_Data_Store::load( 'coupon' );
|
2020-04-21 10:07:21 +00:00
|
|
|
$coupon = WC_Helper_Coupon::create_coupon(
|
2019-07-09 21:07:44 +00:00
|
|
|
$coupon_code,
|
|
|
|
array( 'usage_limit' => 1 )
|
|
|
|
);
|
2020-04-21 10:07:21 +00:00
|
|
|
$product = WC_Helper_Product::create_simple_product( true );
|
2019-07-09 21:07:44 +00:00
|
|
|
WC()->cart->add_to_cart( $product->get_id(), 1 );
|
|
|
|
WC()->cart->add_discount( $coupon->get_code() );
|
|
|
|
$checkout = WC_Checkout::instance();
|
|
|
|
$order_id = $checkout->create_order(
|
|
|
|
array(
|
2020-04-21 10:07:21 +00:00
|
|
|
'billing_email' => 'a@b.com',
|
2019-07-09 21:07:44 +00:00
|
|
|
'payment_method' => 'dummy_payment_gateway',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertNotWPError( $order_id );
|
2020-04-21 10:07:21 +00:00
|
|
|
$order = new WC_Order( $order_id );
|
2019-07-09 21:07:44 +00:00
|
|
|
$coupon_held_key = $order->get_data_store()->get_coupon_held_keys( $order );
|
|
|
|
$this->assertEquals( count( $coupon_held_key ), 1 );
|
|
|
|
$this->assertEquals( array_keys( $coupon_held_key )[0], $coupon->get_id() );
|
|
|
|
$this->assertEquals( strpos( $coupon_held_key[ $coupon->get_id() ], '_coupon_held_' ), 0 );
|
|
|
|
$this->assertEquals( $coupon_data_store->get_tentative_usage_count( $coupon->get_id() ), 1 );
|
|
|
|
|
|
|
|
WC()->cart->empty_cart();
|
|
|
|
WC()->cart->add_to_cart( $product->get_id(), 1 );
|
|
|
|
WC()->cart->add_discount( $coupon->get_code() );
|
|
|
|
$order2_id = $checkout->create_order(
|
|
|
|
array(
|
2020-04-21 10:07:21 +00:00
|
|
|
'billing_email' => 'a@c.com',
|
2019-07-09 21:07:44 +00:00
|
|
|
'payment_method' => 'dummy_payment_gateway',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertWPError( $order2_id );
|
|
|
|
$this->assertEquals( $coupon_data_store->get_tentative_usage_count( $coupon->get_id() ), 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test when order is created with multiple coupon when usage limit for one is exhausted.
|
|
|
|
*
|
|
|
|
* @throws Exception When unable to create an order.
|
|
|
|
*/
|
|
|
|
public function test_create_order_with_multiple_limited_coupons() {
|
2020-04-21 10:07:21 +00:00
|
|
|
$coupon_code1 = 'coupon1';
|
|
|
|
$coupon_code2 = 'coupon2';
|
2019-07-09 21:07:44 +00:00
|
|
|
$coupon_data_store = WC_Data_Store::load( 'coupon' );
|
|
|
|
|
|
|
|
$coupon1 = WC_Helper_Coupon::create_coupon(
|
|
|
|
$coupon_code1,
|
|
|
|
array( 'usage_limit' => 2 )
|
|
|
|
);
|
|
|
|
$coupon2 = WC_Helper_Coupon::create_coupon(
|
|
|
|
$coupon_code2,
|
|
|
|
array( 'usage_limit' => 1 )
|
|
|
|
);
|
|
|
|
$product = WC_Helper_Product::create_simple_product( true );
|
|
|
|
WC()->cart->add_to_cart( $product->get_id(), 1 );
|
|
|
|
WC()->cart->add_discount( $coupon_code1 );
|
|
|
|
WC()->cart->add_discount( $coupon_code2 );
|
2020-04-21 10:07:21 +00:00
|
|
|
$checkout = WC_Checkout::instance();
|
2019-07-09 21:07:44 +00:00
|
|
|
$order_id1 = $checkout->create_order(
|
|
|
|
array(
|
2020-04-21 10:07:21 +00:00
|
|
|
'billing_email' => 'a@b.com',
|
2019-07-09 21:07:44 +00:00
|
|
|
'payment_method' => 'dummy_payment_gateway',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertNotWPError( $order_id1 );
|
|
|
|
$this->assertEquals( $coupon_data_store->get_tentative_usage_count( $coupon1->get_id() ), 1 );
|
|
|
|
$this->assertEquals( $coupon_data_store->get_tentative_usage_count( $coupon2->get_id() ), 1 );
|
|
|
|
|
|
|
|
WC()->cart->empty_cart();
|
|
|
|
WC()->cart->add_to_cart( $product->get_id(), 1 );
|
|
|
|
WC()->cart->add_discount( $coupon_code1 );
|
|
|
|
WC()->cart->add_discount( $coupon_code2 );
|
|
|
|
|
|
|
|
$order2_id = $checkout->create_order(
|
|
|
|
array(
|
2020-04-21 10:07:21 +00:00
|
|
|
'billing_email' => 'a@b.com',
|
2019-07-09 21:07:44 +00:00
|
|
|
'payment_method' => 'dummy_payment_gateway',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertWPError( $order2_id );
|
|
|
|
$this->assertEquals( $coupon_data_store->get_tentative_usage_count( $coupon1->get_id() ), 1 );
|
|
|
|
$this->assertEquals( $coupon_data_store->get_tentative_usage_count( $coupon2->get_id() ), 1 );
|
|
|
|
}
|
|
|
|
|
2020-03-11 15:17:24 +00:00
|
|
|
/**
|
|
|
|
* Test when `usage_count` meta is deleted for some reason.
|
|
|
|
*
|
|
|
|
* @throws Exception When unable to create order.
|
|
|
|
*/
|
|
|
|
public function test_create_order_with_usage_limit_deleted() {
|
|
|
|
$coupon_code = 'coupon4one';
|
|
|
|
$coupon_data_store = WC_Data_Store::load( 'coupon' );
|
|
|
|
$coupon = WC_Helper_Coupon::create_coupon(
|
|
|
|
$coupon_code,
|
|
|
|
array( 'usage_limit' => 1 )
|
|
|
|
);
|
|
|
|
|
|
|
|
delete_post_meta( $coupon->get_id(), 'usage_count' );
|
|
|
|
|
|
|
|
$product = WC_Helper_Product::create_simple_product( true );
|
|
|
|
WC()->cart->add_to_cart( $product->get_id(), 1 );
|
|
|
|
WC()->cart->add_discount( $coupon->get_code() );
|
|
|
|
$checkout = WC_Checkout::instance();
|
|
|
|
$order_id = $checkout->create_order(
|
|
|
|
array(
|
|
|
|
'billing_email' => 'a@b.com',
|
|
|
|
'payment_method' => 'dummy_payment_gateway',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertNotWPError( $order_id );
|
|
|
|
$this->assertEquals( $coupon_data_store->get_tentative_usage_count( $coupon->get_id() ), 1 );
|
|
|
|
}
|
|
|
|
|
2020-04-02 15:04:57 +00:00
|
|
|
/**
|
|
|
|
* Test usage limit for guest users uasge limit per user is set.
|
|
|
|
*
|
|
|
|
* @throws Exception When unable to create order.
|
|
|
|
*/
|
|
|
|
public function test_usage_limit_per_user_for_guest() {
|
|
|
|
wp_set_current_user( 0 );
|
|
|
|
wc_clear_notices();
|
|
|
|
$coupon_code = 'coupon4one';
|
|
|
|
$coupon = WC_Helper_Coupon::create_coupon(
|
|
|
|
$coupon_code,
|
|
|
|
array( 'usage_limit_per_user' => 1 )
|
|
|
|
);
|
|
|
|
$product = WC_Helper_Product::create_simple_product( true );
|
|
|
|
WC()->cart->add_to_cart( $product->get_id(), 1 );
|
|
|
|
WC()->cart->add_discount( $coupon->get_code() );
|
|
|
|
$checkout = WC_Checkout::instance();
|
|
|
|
$posted_data = array(
|
|
|
|
'billing_email' => 'a@b.com',
|
|
|
|
'payment_method' => 'dummy_payment_gateway',
|
|
|
|
);
|
|
|
|
$order_id = $checkout->create_order( $posted_data );
|
|
|
|
$this->assertNotWPError( $order_id );
|
|
|
|
|
|
|
|
WC()->cart->add_to_cart( $product->get_id(), 1 );
|
|
|
|
WC()->cart->add_discount( $coupon->get_code() );
|
|
|
|
WC()->cart->check_customer_coupons( $posted_data );
|
|
|
|
$this->assertTrue( wc_has_notice( $coupon->get_coupon_error( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED ), 'error' ) );
|
|
|
|
}
|
|
|
|
|
2019-07-09 21:07:44 +00:00
|
|
|
/**
|
|
|
|
* Helper function to return 0.01.
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function __return_0_01() {
|
|
|
|
return 0.01;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|