2016-11-09 12:21:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Data Store Tests: Tests WC_Coupon's WC_Data_Store.
|
|
|
|
* @package WooCommerce\Tests\Coupon
|
|
|
|
*/
|
|
|
|
class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure the coupon store loads.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-09 12:21:18 +00:00
|
|
|
*/
|
2018-01-22 04:13:13 +00:00
|
|
|
public function test_coupon_store_loads() {
|
2016-11-09 12:21:18 +00:00
|
|
|
$store = new WC_Data_Store( 'coupon' );
|
|
|
|
$this->assertTrue( is_callable( array( $store, 'read' ) ) );
|
|
|
|
$this->assertEquals( 'WC_Coupon_Data_Store_CPT', $store->get_current_class_name() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test coupon create.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-09 12:21:18 +00:00
|
|
|
*/
|
2018-01-22 04:13:13 +00:00
|
|
|
public function test_coupon_create() {
|
2018-01-22 04:01:09 +00:00
|
|
|
$code = 'coupon-' . time();
|
2018-01-22 04:10:14 +00:00
|
|
|
$coupon = new WC_Coupon();
|
2016-11-09 12:21:18 +00:00
|
|
|
$coupon->set_code( $code );
|
|
|
|
$coupon->set_description( 'This is a test comment.' );
|
|
|
|
$coupon->save();
|
|
|
|
|
|
|
|
$this->assertEquals( $code, $coupon->get_code() );
|
|
|
|
$this->assertNotEquals( 0, $coupon->get_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test coupon deletion.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-09 12:21:18 +00:00
|
|
|
*/
|
2018-01-22 04:13:13 +00:00
|
|
|
public function test_coupon_delete() {
|
2018-01-22 04:01:09 +00:00
|
|
|
$coupon = WC_Helper_Coupon::create_coupon();
|
2016-11-09 12:21:18 +00:00
|
|
|
$coupon_id = $coupon->get_id();
|
|
|
|
$this->assertNotEquals( 0, $coupon_id );
|
|
|
|
$coupon->delete( true );
|
|
|
|
$coupon = new WC_Coupon( $coupon_id );
|
|
|
|
$this->assertEquals( 0, $coupon->get_id() );
|
|
|
|
}
|
|
|
|
|
2017-09-10 02:47:25 +00:00
|
|
|
/**
|
|
|
|
* Test coupon accurately cleans up object cache upon deletion.
|
|
|
|
*/
|
|
|
|
public function test_coupon_cache_deletion() {
|
|
|
|
$coupon = WC_Helper_Coupon::create_coupon( 'test' );
|
|
|
|
$coupon->delete( true );
|
|
|
|
|
|
|
|
$cache_name = WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $coupon->get_code();
|
2018-01-22 04:01:09 +00:00
|
|
|
$ids = wp_cache_get( $cache_name, 'coupons' );
|
2017-09-10 02:47:25 +00:00
|
|
|
|
|
|
|
$this->assertEquals( false, $ids, sprintf( 'Object cache for %s was not removed upon deletion of coupon.', $cache_name ) );
|
|
|
|
}
|
|
|
|
|
2016-11-09 12:21:18 +00:00
|
|
|
/**
|
|
|
|
* Test coupon update.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-09 12:21:18 +00:00
|
|
|
*/
|
2018-01-22 04:13:13 +00:00
|
|
|
public function test_coupon_update() {
|
2018-01-22 04:01:09 +00:00
|
|
|
$coupon = WC_Helper_Coupon::create_coupon();
|
2016-11-09 12:21:18 +00:00
|
|
|
$coupon_id = $coupon->get_id();
|
|
|
|
$this->assertEquals( 'dummycoupon', $coupon->get_code() );
|
|
|
|
$coupon->set_code( 'dummycoupon2' );
|
|
|
|
$coupon->save();
|
|
|
|
$coupon = new WC_Coupon( $coupon->get_id() );
|
|
|
|
$this->assertEquals( 'dummycoupon2', $coupon->get_code() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test coupon reading from the DB.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-09 12:21:18 +00:00
|
|
|
*/
|
2018-01-22 04:13:13 +00:00
|
|
|
public function test_coupon_read() {
|
2018-01-22 04:01:09 +00:00
|
|
|
$code = 'coupon-' . time();
|
2018-01-22 04:10:14 +00:00
|
|
|
$coupon = new WC_Coupon();
|
2016-11-09 12:21:18 +00:00
|
|
|
$coupon->set_code( $code );
|
|
|
|
$coupon->set_description( 'This is a test coupon.' );
|
2018-02-22 20:06:53 +00:00
|
|
|
$coupon->set_amount( '' );
|
2016-11-09 12:21:18 +00:00
|
|
|
$coupon->set_usage_count( 5 );
|
|
|
|
$coupon->save();
|
|
|
|
$coupon_id = $coupon->get_id();
|
|
|
|
|
|
|
|
$coupon_read = new WC_Coupon( $coupon_id );
|
|
|
|
|
|
|
|
$this->assertEquals( 5, $coupon_read->get_usage_count() );
|
|
|
|
$this->assertEquals( $code, $coupon_read->get_code() );
|
2018-02-22 20:06:53 +00:00
|
|
|
$this->assertEquals( 0, $coupon->get_amount() );
|
2016-11-09 12:21:18 +00:00
|
|
|
$this->assertEquals( 'This is a test coupon.', $coupon_read->get_description() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test coupon saving.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-09 12:21:18 +00:00
|
|
|
*/
|
2018-01-22 04:13:13 +00:00
|
|
|
public function test_coupon_save() {
|
2018-01-22 04:01:09 +00:00
|
|
|
$coupon = WC_Helper_Coupon::create_coupon();
|
2016-11-09 12:21:18 +00:00
|
|
|
$coupon_id = $coupon->get_id();
|
|
|
|
$coupon->set_code( 'dummycoupon2' );
|
|
|
|
$coupon->save();
|
|
|
|
$coupon = new WC_Coupon( $coupon_id ); // Read from DB to retest
|
|
|
|
$this->assertEquals( 'dummycoupon2', $coupon->get_code() );
|
|
|
|
$this->assertEquals( $coupon_id, $coupon->get_id() );
|
|
|
|
|
2018-01-22 04:10:14 +00:00
|
|
|
$new_coupon = new WC_Coupon();
|
2016-11-09 12:21:18 +00:00
|
|
|
$new_coupon->set_code( 'dummycoupon3' );
|
|
|
|
$new_coupon->save();
|
|
|
|
$new_coupon_id = $new_coupon->get_id();
|
|
|
|
$this->assertEquals( 'dummycoupon3', $new_coupon->get_code() );
|
|
|
|
$this->assertNotEquals( 0, $new_coupon_id );
|
|
|
|
}
|
|
|
|
|
2017-03-13 21:24:05 +00:00
|
|
|
/**
|
|
|
|
* Test coupon date saving/loading.
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-22 04:13:13 +00:00
|
|
|
public function test_coupon_date_saving() {
|
2017-03-13 21:24:05 +00:00
|
|
|
$expiry_date = time() - 10;
|
|
|
|
|
2017-03-21 15:38:35 +00:00
|
|
|
$coupon = WC_Helper_Coupon::create_coupon( 'coupon-' . time() );
|
2017-03-13 21:24:05 +00:00
|
|
|
$coupon->set_date_expires( $expiry_date );
|
|
|
|
$coupon->save();
|
|
|
|
|
|
|
|
$coupon_read = new WC_Coupon( $coupon->get_id() );
|
|
|
|
|
2017-03-14 15:10:31 +00:00
|
|
|
$this->assertEquals( date( 'Y-m-d', $expiry_date ), date( 'Y-m-d', $coupon_read->get_date_expires()->getTimestamp() ) );
|
2017-03-13 21:24:05 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 12:21:18 +00:00
|
|
|
/**
|
|
|
|
* Test coupon increase, decrease, user usage count methods.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-09 12:21:18 +00:00
|
|
|
*/
|
2018-01-22 04:13:13 +00:00
|
|
|
public function test_coupon_usage_magic_methods() {
|
2016-11-09 12:21:18 +00:00
|
|
|
$coupon = WC_Helper_Coupon::create_coupon();
|
|
|
|
$user_id = 1;
|
|
|
|
|
|
|
|
$this->assertEquals( 0, $coupon->get_usage_count() );
|
|
|
|
$this->assertEmpty( $coupon->get_used_by() );
|
|
|
|
|
2017-03-08 23:27:37 +00:00
|
|
|
$coupon->increase_usage_count( 'woo@woo.local' );
|
2016-11-09 12:21:18 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 1, $coupon->get_usage_count() );
|
|
|
|
$this->assertEquals( array( 'woo@woo.local' ), $coupon->get_used_by() );
|
|
|
|
|
2017-03-08 23:27:37 +00:00
|
|
|
$coupon->increase_usage_count( $user_id );
|
|
|
|
$coupon->increase_usage_count( $user_id );
|
2016-11-09 12:21:18 +00:00
|
|
|
|
|
|
|
$data_store = WC_Data_Store::load( 'coupon' );
|
|
|
|
$this->assertEquals( 2, $data_store->get_usage_by_user_id( $coupon, $user_id ) );
|
|
|
|
|
2017-03-08 23:27:37 +00:00
|
|
|
$coupon->decrease_usage_count( 'woo@woo.local' );
|
|
|
|
$coupon->decrease_usage_count( $user_id );
|
2016-11-09 12:21:18 +00:00
|
|
|
$this->assertEquals( 1, $coupon->get_usage_count() );
|
|
|
|
$this->assertEquals( array( 1 ), $coupon->get_used_by() );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|