2015-03-02 16:06:03 +00:00
< ? php
2015-03-06 15:32:40 +00:00
2015-03-02 16:06:03 +00:00
/**
2015-11-03 13:31:20 +00:00
* Class Functions .
2015-03-06 15:32:40 +00:00
* @ package WooCommerce\Tests\Cart
2015-03-02 16:06:03 +00:00
*/
2016-03-23 12:14:13 +00:00
class WC_Tests_Cart_Functions extends WC_Unit_Test_Case {
2015-03-02 16:06:03 +00:00
2015-10-28 18:09:53 +00:00
/**
2015-11-03 13:31:20 +00:00
* Helper method to get the checkout URL .
2015-10-28 18:09:53 +00:00
*
* @ since 2.5 . 0
*
* @ return string
*/
private function get_checkout_url () {
// Get the checkout URL
$checkout_page_id = wc_get_page_id ( 'checkout' );
$checkout_url = '' ;
// Check if there is a checkout page
if ( $checkout_page_id ) {
// Get the permalink
$checkout_url = get_permalink ( $checkout_page_id );
// Force SSL if needed
if ( is_ssl () || 'yes' === get_option ( 'woocommerce_force_ssl_checkout' ) ) {
$checkout_url = str_replace ( 'http:' , 'https:' , $checkout_url );
}
// Allow filtering of checkout URL
$checkout_url = apply_filters ( 'woocommerce_get_checkout_url' , $checkout_url );
}
return $checkout_url ;
}
/**
2015-11-03 13:31:20 +00:00
* Test get_checkout_url over HTTP .
2015-10-28 18:09:53 +00:00
*
* @ since 2.5 . 0
*/
public function test_get_checkout_url_regular () {
2016-06-30 11:23:41 +00:00
// Make sure pages exist
WC_Install :: create_pages ();
2015-10-28 18:09:53 +00:00
// Get the original setting
$o_setting = get_option ( 'woocommerce_force_ssl_checkout' );
// Force SSL checkout
update_option ( 'woocommerce_force_ssl_checkout' , 'no' );
$this -> assertEquals ( $this -> get_checkout_url (), wc_get_checkout_url () );
// Restore option
update_option ( 'woocommerce_force_ssl_checkout' , $o_setting );
}
/**
2015-11-03 13:31:20 +00:00
* Test get_checkout_url over HTTP .
2015-10-28 18:09:53 +00:00
*
* @ since 2.5 . 0
*/
public function test_get_checkout_url_ssl () {
2016-06-30 11:23:41 +00:00
// Make sure pages exist
WC_Install :: create_pages ();
2015-10-28 18:09:53 +00:00
// Get the original setting
$o_setting = get_option ( 'woocommerce_force_ssl_checkout' );
// Force SSL checkout
update_option ( 'woocommerce_force_ssl_checkout' , 'yes' );
$this -> assertEquals ( $this -> get_checkout_url (), wc_get_checkout_url () );
// Restore option
update_option ( 'woocommerce_force_ssl_checkout' , $o_setting );
}
2015-03-02 16:06:03 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test wc_empty_cart () .
2015-03-02 16:06:03 +00:00
*
* @ since 2.3 . 0
*/
public function test_wc_empty_cart () {
// Create dummy product
2016-03-23 12:14:13 +00:00
$product = WC_Helper_Product :: create_simple_product ();
2015-03-02 16:06:03 +00:00
// Add the product to the cart
WC () -> cart -> add_to_cart ( $product -> id , 1 );
// Empty the cart
wc_empty_cart ();
// Check if the cart is empty
$this -> assertEquals ( 0 , WC () -> cart -> get_cart_contents_count () );
2015-03-02 16:11:42 +00:00
// Delete the previously created product
2016-03-23 12:14:13 +00:00
WC_Helper_Product :: delete_product ( $product -> id );
2015-03-02 16:06:03 +00:00
}
2015-04-14 14:37:10 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test wc_format_list_of_items () .
2015-04-14 14:37:10 +00:00
*
* @ since 2.4
*/
public function test_wc_format_list_of_items () {
$items = array ( 'Title 1' , 'Title 2' );
2016-01-21 22:19:03 +00:00
$this -> assertEquals ( 'Title 1 and Title 2' , wc_format_list_of_items ( $items ) );
2015-04-14 14:37:10 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_cart_totals_subtotal_html () .
2015-04-14 14:37:10 +00:00
*
* @ todo test with taxes incl ./ excl .
* @ since 2.4
*/
public function test_wc_cart_totals_subtotal_html () {
2016-03-23 12:14:13 +00:00
$product = WC_Helper_Product :: create_simple_product ();
2015-04-14 14:37:10 +00:00
WC () -> cart -> add_to_cart ( $product -> id , 1 );
$this -> expectOutputString ( wc_price ( $product -> price ), wc_cart_totals_subtotal_html () );
2016-03-23 12:14:13 +00:00
WC_Helper_Product :: delete_product ( $product -> id );
2015-04-14 14:37:10 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Test wc_cart_totals_coupon_label () .
2015-04-14 14:37:10 +00:00
*
* @ since 2.4
*/
public function test_wc_cart_totals_coupon_label () {
2016-03-23 12:14:13 +00:00
$coupon = WC_Helper_Coupon :: create_coupon ();
2015-04-14 14:37:10 +00:00
2016-02-23 16:01:40 +00:00
$this -> expectOutputString ( apply_filters ( 'woocommerce_cart_totals_coupon_label' , 'Coupon: ' . $coupon -> get_code () ), wc_cart_totals_coupon_label ( $coupon ) );
2015-04-14 14:37:10 +00:00
2016-06-13 14:01:58 +00:00
WC_Helper_Coupon :: delete_coupon ( $coupon -> get_id () );
2015-04-14 14:37:10 +00:00
}
2015-10-28 18:09:53 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test get_cart_url method .
2015-10-28 18:09:53 +00:00
*
* @ since 2.5 . 0
*/
public function test_wc_get_cart_url () {
$cart_page_url = wc_get_page_permalink ( 'cart' );
$this -> assertEquals ( apply_filters ( 'woocommerce_get_cart_url' , $cart_page_url ? $cart_page_url : '' ), wc_get_cart_url () );
}
2016-08-01 10:42:19 +00:00
/**
* Test wc_add_to_cart_message
*/
public function test_wc_add_to_cart_message () {
$product = WC_Helper_Product :: create_simple_product ();
$message = wc_add_to_cart_message ( array ( $product -> id => 1 ), false , true );
$this -> assertEquals ( '<a href="http://example.org" class="button wc-forward">View Cart</a> “Dummy Product” has been added to your cart.' , $message );
$message = wc_add_to_cart_message ( array ( $product -> id => 3 ), false , true );
$this -> assertEquals ( '<a href="http://example.org" class="button wc-forward">View Cart</a> “Dummy Product” has been added to your cart.' , $message );
$message = wc_add_to_cart_message ( array ( $product -> id => 1 ), true , true );
$this -> assertEquals ( '<a href="http://example.org" class="button wc-forward">View Cart</a> “Dummy Product” has been added to your cart.' , $message );
$message = wc_add_to_cart_message ( array ( $product -> id => 3 ), true , true );
$this -> assertEquals ( '<a href="http://example.org" class="button wc-forward">View Cart</a> 3 × “Dummy Product” have been added to your cart.' , $message );
$message = wc_add_to_cart_message ( $product -> id , false , true );
$this -> assertEquals ( '<a href="http://example.org" class="button wc-forward">View Cart</a> “Dummy Product” has been added to your cart.' , $message );
$message = wc_add_to_cart_message ( $product -> id , true , true );
$this -> assertEquals ( '<a href="http://example.org" class="button wc-forward">View Cart</a> “Dummy Product” has been added to your cart.' , $message );
}
2015-03-02 16:06:03 +00:00
}