Test adding a variation with 'any' attributes

This commit is contained in:
Jonathan Sadowski 2020-04-24 14:20:21 -05:00
parent 790c8ae8ae
commit 3f47608228
1 changed files with 30 additions and 2 deletions

View File

@ -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.
*/