Skip to content

Commit b905af9

Browse files
committed
CartMutationsTest updated.
1 parent b366aee commit b905af9

File tree

3 files changed

+115
-5
lines changed

3 files changed

+115
-5
lines changed

includes/mutation/class-cart-apply-coupon.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ public static function mutate_and_get_payload() {
6969
return function( $input, AppContext $context, ResolveInfo $info ) {
7070
Cart_Mutation::check_session_token();
7171

72-
// Retrieve product database ID if relay ID provided.
73-
if ( empty( $input['code'] ) ) {
74-
throw new UserError( __( 'No coupon code provided', 'wp-graphql-woocommerce' ) );
75-
}
76-
7772
// Get the coupon.
7873
$the_coupon = new \WC_Coupon( $input['code'] );
7974

tests/_support/Helper/crud-helpers/coupon.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public function create( $args = array(), $save = true ) {
3232
)
3333
);
3434

35+
codecept_debug( $coupon );
36+
3537
// Set meta data.
3638
if ( ! empty( $args['meta_data'] ) ) {
3739
$coupon->set_meta_data( $args['meta_data'] );

tests/wpunit/CartMutationsTest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,119 @@ public function testApplyCouponMutation() {
826826
$this->assertTrue( $old_total > $new_total );
827827
}
828828

829+
public function testApplyCouponMutationWithInvalidCoupons() {
830+
$cart = WC()->cart;
831+
832+
// Create products.
833+
$product_id = $this->product->create_simple();
834+
835+
// Create invalid coupon codes.
836+
$coupon_id = $this->coupon->create(
837+
array( 'product_ids' => array( $product_id ) )
838+
);
839+
$expired_coupon_code = wc_get_coupon_code_by_id(
840+
$this->coupon->create(
841+
array(
842+
'product_ids' => array( $product_id ),
843+
'date_expires' => time() - 20,
844+
)
845+
)
846+
);
847+
$applied_coupon_code = wc_get_coupon_code_by_id(
848+
$this->coupon->create(
849+
array( 'product_ids' => array( $product_id ) )
850+
)
851+
);
852+
853+
// Add items to carts.
854+
$cart_item_key = $cart->add_to_cart( $product_id, 1 );
855+
$cart->apply_coupon( $applied_coupon_code );
856+
857+
$old_total = \WC()->cart->get_cart_contents_total();
858+
859+
$mutation = '
860+
mutation ( $input: ApplyCouponInput! ) {
861+
applyCoupon( input: $input ) {
862+
clientMutationId
863+
}
864+
}
865+
';
866+
867+
/**
868+
* Assertion One
869+
*
870+
* Can't pass coupon ID as coupon "code". Mutation should fail.
871+
*/
872+
$variables = array(
873+
'input' => array(
874+
'clientMutationId' => 'someId',
875+
'code' => $coupon_id,
876+
),
877+
);
878+
$actual = graphql(
879+
array(
880+
'query' => $mutation,
881+
'variables' => $variables,
882+
)
883+
);
884+
885+
// use --debug flag to view.
886+
codecept_debug( $actual );
887+
888+
$this->assertNotEmpty( $actual['errors'] );
889+
$this->assertEmpty( $actual['data']['applyCoupon'] );
890+
891+
/**
892+
* Assertion Two
893+
*
894+
* Can't pass expired coupon code. Mutation should fail.
895+
*/
896+
$variables = array(
897+
'input' => array(
898+
'clientMutationId' => 'someId',
899+
'code' => $expired_coupon_code,
900+
),
901+
);
902+
$actual = graphql(
903+
array(
904+
'query' => $mutation,
905+
'variables' => $variables,
906+
)
907+
);
908+
909+
// use --debug flag to view.
910+
codecept_debug( $actual );
911+
912+
$this->assertNotEmpty( $actual['errors'] );
913+
$this->assertEmpty( $actual['data']['applyCoupon'] );
914+
915+
/**
916+
* Assertion Three
917+
*
918+
* Can't pass coupon already applied to the cart. Mutation should fail.
919+
*/
920+
$variables = array(
921+
'input' => array(
922+
'clientMutationId' => 'someId',
923+
'code' => $applied_coupon_code,
924+
),
925+
);
926+
$actual = graphql(
927+
array(
928+
'query' => $mutation,
929+
'variables' => $variables,
930+
)
931+
);
932+
933+
// use --debug flag to view.
934+
codecept_debug( $actual );
935+
936+
$this->assertNotEmpty( $actual['errors'] );
937+
$this->assertEmpty( $actual['data']['applyCoupon'] );
938+
939+
$this->assertEquals( $old_total, \WC()->cart->get_cart_contents_total() );
940+
}
941+
829942
public function testRemoveCouponMutation() {
830943
$cart = WC()->cart;
831944

0 commit comments

Comments
 (0)