diff --git a/tests/unit-tests/cart/cart.php b/tests/unit-tests/cart/cart.php index 4756f04209c..bc0e3a8b583 100644 --- a/tests/unit-tests/cart/cart.php +++ b/tests/unit-tests/cart/cart.php @@ -1061,4 +1061,48 @@ class WC_Tests_Cart extends WC_Unit_Test_Case { WC_Helper_Coupon::delete_coupon( $iu_coupon1->get_code() ); WC_Helper_Coupon::delete_coupon( $iu_coupon2->get_code() ); } + + public function test_clone_cart() { + $cart = wc()->cart; + $new_cart = clone $cart; + $is_identical_cart = $cart === $new_cart; + + // Cloned carts should not be identical. + $this->assertFalse( $is_identical_cart, 'Cloned cart not identical to original cart' ); + } + + public function test_cloned_cart_session() { + // PHP 5.2 does not include support for ReflectionProperty::setAccessible(). + if ( version_compare( '5.3', PHP_VERSION, '>' ) ) { + $this->markTestSkipped( 'Test requires PHP 5.3 and above to use ReflectionProperty::setAccessible()' ); + } + + $cart = wc()->cart; + $new_cart = clone $cart; + + // Allow accessing protected properties. + $reflected_cart = new ReflectionClass( $cart ); + $cart_session = $reflected_cart->getProperty( 'session' ); + $cart_session->setAccessible( true ); + $reflected_new_cart = new ReflectionClass( $new_cart ); + $new_cart_session = $reflected_new_cart->getProperty( 'session' ); + $new_cart_session->setAccessible( true ); + + // Ensure that cloned properties are not identical. + $identical_sessions = $cart_session->getValue( $cart ) === $new_cart_session->getValue( $new_cart ); + $this->assertFalse( $identical_sessions, 'Cloned cart sessions should not be identical to original cart' ); + } + + public function test_cloned_cart_fees() { + $cart = wc()->cart; + $new_cart = clone $cart; + + // Get the properties from each object. + $cart_fees = $cart->fees_api(); + $new_cart_fees = $new_cart->fees_api(); + + // Ensure that cloned properties are not identical. + $identical_fees = $cart_fees === $new_cart_fees; + $this->assertFalse( $identical_fees, 'Cloned cart fees should not be identical to original cart.' ); + } }