diff --git a/includes/mutation/class-cart-add-item.php b/includes/mutation/class-cart-add-item.php index 0cde7291e..4888d2c3c 100644 --- a/includes/mutation/class-cart-add-item.php +++ b/includes/mutation/class-cart-add-item.php @@ -107,8 +107,14 @@ public static function mutate_and_get_payload() { // Add item to cart and get item key. $cart_item_key = \WC()->cart->add_to_cart( ...$cart_item_args ); - if ( empty( $cart_item_key ) ) { - throw new UserError( __( 'Failed to add cart item. Please check input.', 'wp-graphql-woocommerce' ) ); + if ( false === $cart_item_key ) { + $notices = \WC()->session->get( 'wc_notices' ); + if ( ! empty( $notices['error'] ) ) { + $cart_error_messages = implode( ' ', array_column( $notices['error'], 'notice' ) ); + throw new UserError( $cart_error_messages ); + } else { + throw new UserError( __( 'Failed to add cart item. Please check input.', 'wp-graphql-woocommerce' ) ); + } } // Return payload. diff --git a/tests/wpunit/CartMutationsTest.php b/tests/wpunit/CartMutationsTest.php index 651f9a0ed..53a09e6dd 100644 --- a/tests/wpunit/CartMutationsTest.php +++ b/tests/wpunit/CartMutationsTest.php @@ -23,7 +23,17 @@ public function tearDown() { \WC()->cart->empty_cart( true ); parent::tearDown(); - } + } + + private function graphql( $query, $operation_name = null, $variables = null ) { + // Run GraphQL request. + $results = graphql( compact( 'query', 'operation_name', 'variables' ) ); + + // use --debug flag to view. + codecept_debug( $results ); + + return $results; + } private function addToCart( $input ) { $mutation = ' @@ -53,15 +63,7 @@ private function addToCart( $input ) { } '; - $actual = graphql( - array( - 'query' => $mutation, - 'operation_name' => 'addToCart', - 'variables' => array( 'input' => $input ), - ) - ); - - return $actual; + return $this->graphql( $mutation, 'addToCart', compact( 'input' ) ); } private function removeItemsFromCart( $input ) { @@ -92,15 +94,7 @@ private function removeItemsFromCart( $input ) { } '; - $actual = graphql( - array( - 'query' => $mutation, - 'operation_name' => 'removeItemsFromCart', - 'variables' => array( 'input' => $input ), - ) - ); - - return $actual; + return $this->graphql( $mutation, 'removeItemsFromCart', compact( 'input' ) ); } private function restoreItems( $input ) { @@ -129,17 +123,9 @@ private function restoreItems( $input ) { } } } - '; - - $actual = graphql( - array( - 'query' => $mutation, - 'operation_name' => 'restoreCartItems', - 'variables' => array( 'input' => $input ), - ) - ); + '; - return $actual; + return $this->graphql( $mutation, 'restoreCartItems', compact( 'input' ) ); } // tests @@ -153,9 +139,6 @@ public function testAddToCartMutationWithProduct() { ) ); - // use --debug flag to view. - codecept_debug( $actual ); - // Retrieve cart item key. $this->assertArrayHasKey('data', $actual ); $this->assertArrayHasKey('addToCart', $actual['data'] ); @@ -202,9 +185,6 @@ public function testAddToCartMutationWithProductVariation() { ) ); - // use --debug flag to view. - codecept_debug( $actual ); - // Retrieve cart item key. $this->assertArrayHasKey('data', $actual ); $this->assertArrayHasKey('addToCart', $actual['data'] ); @@ -297,26 +277,21 @@ public function testUpdateCartItemQuantitiesMutation() { } '; - $actual = graphql( - array( - 'query' => $mutation, - 'operation_name' => 'updateItemQuantities', - 'variables' => array( - 'input' => array( - 'clientMutationId' => 'someId', - 'items' => array( - array( 'key' => $key_1, 'quantity' => 4 ), - array( 'key' => $key_2, 'quantity' => 2 ), - array( 'key' => $key_3, 'quantity' => 0 ), - ), - ), - ), - ) + $actual = $this->graphql( + $mutation, + 'updateItemQuantities', + array( + 'input' => array( + 'clientMutationId' => 'someId', + 'items' => array( + array( 'key' => $key_1, 'quantity' => 4 ), + array( 'key' => $key_2, 'quantity' => 2 ), + array( 'key' => $key_3, 'quantity' => 0 ), + ), + ), + ) ); - // use --debug flag to view. - codecept_debug( $actual ); - // Check cart item data. $expected = array( 'data' => array( @@ -365,9 +340,6 @@ public function testRemoveItemsFromCartMutation() { ) ); - // use --debug flag to view. - codecept_debug( $actual ); - $expected = array( 'data' => array( 'removeItemsFromCart' => array( @@ -425,9 +397,6 @@ public function testRemoveItemsFromCartMutationWithMultipleItems() { ) ); - // use --debug flag to view. - codecept_debug( $actual ); - $expected = array( 'data' => array( 'removeItemsFromCart' => array( @@ -486,9 +455,6 @@ public function testRemoveItemsFromCartMutationUsingAllField() { ) ); - // use --debug flag to view. - codecept_debug( $actual ); - $expected = array( 'data' => array( 'removeItemsFromCart' => array( @@ -537,9 +503,6 @@ public function testRestoreCartItemsMutation() { ) ); - // use --debug flag to view. - codecept_debug( $actual ); - $expected = array( 'data' => array( 'restoreCartItems' => array( @@ -604,9 +567,6 @@ public function testRestoreCartItemsMutationWithMultipleItems() { ) ); - // use --debug flag to view. - codecept_debug( $actual ); - $expected = array( 'data' => array( 'restoreCartItems' => array( @@ -666,16 +626,7 @@ public function testEmptyCartMutation() { $variables = array( 'input' => array( 'clientMutationId' => 'someId' ), ); - $actual = graphql( - array( - 'query' => $mutation, - 'operation_name' => 'emptyCart', - 'variables' => $variables, - ) - ); - - // use --debug flag to view. - codecept_debug( $actual ); + $actual = $this->graphql( $mutation, 'emptyCart', $variables ); $expected = array( 'data' => array( @@ -771,16 +722,7 @@ public function testApplyCouponMutation() { 'code' => $coupon_code, ), ); - $actual = graphql( - array( - 'query' => $mutation, - 'operation_name' => 'applyCoupon', - 'variables' => $variables, - ) - ); - - // use --debug flag to view. - codecept_debug( $actual ); + $actual = $this->graphql( $mutation, 'applyCoupon', $variables ); // Get updated cart item. $cart_item = WC()->cart->get_cart_item( $cart_item_key ); @@ -877,15 +819,7 @@ public function testApplyCouponMutationWithInvalidCoupons() { 'code' => $coupon_id, ), ); - $actual = graphql( - array( - 'query' => $mutation, - 'variables' => $variables, - ) - ); - - // use --debug flag to view. - codecept_debug( $actual ); + $actual = $this->graphql( $mutation, null, $variables ); $this->assertNotEmpty( $actual['errors'] ); $this->assertEmpty( $actual['data']['applyCoupon'] ); @@ -901,15 +835,7 @@ public function testApplyCouponMutationWithInvalidCoupons() { 'code' => $expired_coupon_code, ), ); - $actual = graphql( - array( - 'query' => $mutation, - 'variables' => $variables, - ) - ); - - // use --debug flag to view. - codecept_debug( $actual ); + $actual = $this->graphql( $mutation, null, $variables ); $this->assertNotEmpty( $actual['errors'] ); $this->assertEmpty( $actual['data']['applyCoupon'] ); @@ -925,15 +851,7 @@ public function testApplyCouponMutationWithInvalidCoupons() { 'code' => $applied_coupon_code, ), ); - $actual = graphql( - array( - 'query' => $mutation, - 'variables' => $variables, - ) - ); - - // use --debug flag to view. - codecept_debug( $actual ); + $actual = $this->graphql( $mutation, null, $variables ); $this->assertNotEmpty( $actual['errors'] ); $this->assertEmpty( $actual['data']['applyCoupon'] ); @@ -995,16 +913,7 @@ public function testRemoveCouponMutation() { 'codes' => array( $coupon_code ), ), ); - $actual = graphql( - array( - 'query' => $mutation, - 'operation_name' => 'removeCoupons', - 'variables' => $variables, - ) - ); - - // use --debug flag to view. - codecept_debug( $actual ); + $actual = $this->graphql( $mutation, 'removeCoupons', $variables ); // Get updated cart item. $cart_item = \WC()->cart->get_cart_item( $cart_item_key ); @@ -1078,30 +987,12 @@ public function testAddFeeMutation() { 'amount' => 49.99, ), ); - $actual = graphql( - array( - 'query' => $mutation, - 'operation_name' => 'addFee', - 'variables' => $variables, - ) - ); - - // use --debug flag to view. - codecept_debug( $actual ); + $actual = $this->graphql( $mutation, 'addFee', $variables ); $this->assertArrayHasKey('errors', $actual ); wp_set_current_user( $this->shop_manager ); - $actual = graphql( - array( - 'query' => $mutation, - 'operation_name' => 'addFee', - 'variables' => $variables, - ) - ); - - // use --debug flag to view. - codecept_debug( $actual ); + $actual = $this->graphql( $mutation, 'addFee', $variables ); $expected = array( 'data' => array( @@ -1113,5 +1004,25 @@ public function testAddFeeMutation() { ); $this->assertEquals( $expected, $actual ); - } + } + + public function testAddToCartMutationErrors() { + // Create products. + $product_id = $this->product->create_simple( + array( + 'manage_stock' => true, + 'stock_quantity' => 1, + ) + ); + + $failed = $this->addToCart( + array( + 'clientMutationId' => 'someId', + 'productId' => $product_id, + 'quantity' => 5, + ) + ); + + $this->assertArrayHasKey( 'errors', $failed ); + } }