@@ -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