From 3f47608228ad7b4ecac6c44d7c3902464d2af869 Mon Sep 17 00:00:00 2001 From: Jonathan Sadowski Date: Fri, 24 Apr 2020 14:20:21 -0500 Subject: [PATCH] Test adding a variation with 'any' attributes --- tests/legacy/unit-tests/cart/cart.php | 32 +++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/tests/legacy/unit-tests/cart/cart.php b/tests/legacy/unit-tests/cart/cart.php index 8768395d352..4a04dabfd11 100644 --- a/tests/legacy/unit-tests/cart/cart.php +++ b/tests/legacy/unit-tests/cart/cart.php @@ -2055,7 +2055,7 @@ class WC_Tests_Cart extends WC_Unit_Test_Case { * Test that adding a variation with URL parameter increases the quantity appropriately * as described in issue 24000. */ - public function test_add_variation_with_url() { + public function test_add_variation_by_url() { add_filter( 'woocommerce_add_to_cart_redirect', '__return_false' ); update_option( 'woocommerce_cart_redirect_after_add', 'no' ); WC()->cart->empty_cart(); @@ -2105,7 +2105,7 @@ class WC_Tests_Cart extends WC_Unit_Test_Case { * Test that adding a variation via URL parameter fails when specifying a value for the attribute * that differs from a value belonging to that variant. */ - public function test_add_variation_with_invalid_attribute() { + public function test_add_variation_by_url_with_invalid_attribute() { add_filter( 'woocommerce_add_to_cart_redirect', '__return_false' ); update_option( 'woocommerce_cart_redirect_after_add', 'no' ); WC()->cart->empty_cart(); @@ -2131,6 +2131,34 @@ class WC_Tests_Cart extends WC_Unit_Test_Case { $this->assertEquals( 'Invalid value posted for colour', $notices['error'][0]['notice'] ); } + /** + * Test that adding a variation via URL parameter succeeds when some attributes belong to the + * variation and others are specificed via URL parameter. + */ + public function test_add_variation_by_url_with_valid_attribute() { + add_filter( 'woocommerce_add_to_cart_redirect', '__return_false' ); + update_option( 'woocommerce_cart_redirect_after_add', 'no' ); + WC()->cart->empty_cart(); + + $product = WC_Helper_Product::create_variation_product(); + $variations = $product->get_available_variations(); + $variation = array_shift( $variations ); + + // Attempt adding variation with add_to_cart_action, specifying attributes not defined in the variation. + $_REQUEST['add-to-cart'] = $variation['variation_id']; + $_REQUEST['attribute_pa_colour'] = 'red'; + $_REQUEST['attribute_pa_number'] = '1'; + WC_Form_Handler::add_to_cart_action( false ); + $notices = WC()->session->get( 'wc_notices', array() ); + + // Check if the item is in the cart. + $this->assertCount( 1, WC()->cart->get_cart_contents() ); + $this->assertEquals( 1, WC()->cart->get_cart_contents_count() ); + + // Check that there are no error notices. + $this->assertArrayNotHasKey( 'error', $notices ); + } + /** * Helper function. Adds 1.5 taxable fees to cart. */