Add test to check for notice when invalid attribute is provided for a variant

This commit is contained in:
Jonathan Sadowski 2020-04-24 13:52:16 -05:00
parent f8066a8141
commit 3d0bfd8ee2
1 changed files with 30 additions and 0 deletions

View File

@ -2094,6 +2094,36 @@ class WC_Tests_Cart extends WC_Unit_Test_Case {
$this->assertEquals( 2, WC()->cart->get_cart_contents_count() ); $this->assertEquals( 2, WC()->cart->get_cart_contents_count() );
} }
/**
* 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() {
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_pop( $variations );
// Attempt adding variation with add_to_cart_action, specifying a different colour.
$_REQUEST['add-to-cart'] = $variation['variation_id'];
$_REQUEST['attribute_pa_colour'] = 'green';
WC_Form_Handler::add_to_cart_action( false );
$notices = WC()->session->get( 'wc_notices', array() );
// Reset filter / REQUEST variables.
unset( $_REQUEST['add-to-cart'] );
unset( $_REQUEST['attribute_pa_colour'] );
remove_filter( 'woocommerce_add_to_cart_redirect', '__return_false' );
// Check that the notices contain an error message about an invalid colour.
$this->assertArrayHasKey( 'error', $notices );
$this->assertCount( 1, $notices['error'] );
$this->assertEquals( 'Invalid value posted for colour', $notices['error'][0]['notice'] );
}
/** /**
* Helper function. Adds 1.5 taxable fees to cart. * Helper function. Adds 1.5 taxable fees to cart.
*/ */