• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql-woocommerce / 23675172456

28 Mar 2026 02:10AM UTC coverage: 70.983% (-18.4%) from 89.424%
23675172456

Pull #1003

github

web-flow
Merge 05339093d into 6fb7b226f
Pull Request #1003: devops: WC email template tests, COT cursor HPOS fix, checkout account auth

71 of 81 new or added lines in 5 files covered. (87.65%)

3346 existing lines in 124 files now uncovered.

12576 of 17717 relevant lines covered (70.98%)

55.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/includes/mutation/class-payment-method-set-default.php
1
<?php
2
/**
3
 * Mutation - setDefaultPaymentMethod
4
 *
5
 * Registers mutation for changing user's preferred payment method.
6
 *
7
 * @package WPGraphQL\WooCommerce\Mutation
8
 * @since 0.12.4
9
 */
10

11
namespace WPGraphQL\WooCommerce\Mutation;
12

13
use GraphQL\Error\UserError;
14
use WC_Payment_Tokens;
15

16
/**
17
 * Class Payment_Method_Set_Default
18
 */
19
class Payment_Method_Set_Default {
20
        /**
21
         * Registers mutation
22
         *
23
         * @return void
24
         */
25
        public static function register_mutation() {
26
                register_graphql_mutation(
110✔
27
                        'setDefaultPaymentMethod',
110✔
28
                        [
110✔
29
                                'inputFields'         => self::get_input_fields(),
110✔
30
                                'outputFields'        => self::get_output_fields(),
110✔
31
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
110✔
32
                        ]
110✔
33
                );
110✔
34
        }
35

36
        /**
37
         * Defines the mutation input field configuration
38
         *
39
         * @return array
40
         */
41
        public static function get_input_fields() {
42
                return [
110✔
43
                        'tokenId' => [
110✔
44
                                'type'        => [ 'non_null' => 'Integer' ],
110✔
45
                                'description' => __( 'Token ID of the payment token being deleted.', 'wp-graphql-woocommerce' ),
110✔
46
                        ],
110✔
47
                ];
110✔
48
        }
49

50
        /**
51
         * Defines the mutation output field configuration
52
         *
53
         * @return array
54
         */
55
        public static function get_output_fields() {
56
                return [
110✔
57
                        'status' => [
110✔
58
                                'type'        => 'String',
110✔
59
                                'description' => __( 'Status of the request', 'wp-graphql-woocommerce' ),
110✔
60
                                'resolve'     => static function ( $payload ) {
110✔
UNCOV
61
                                        return ! empty( $payload['status'] ) ? $payload['status'] : 'FAILED';
×
62
                                },
110✔
63
                        ],
110✔
64
                        'token'  => [
110✔
65
                                'type'        => 'PaymentTokenInterface',
110✔
66
                                'description' => __( 'Preferred payment method token', 'wp-graphql-woocommerce' ),
110✔
67
                                'resolve'     => static function ( $payload ) {
110✔
UNCOV
68
                                        return ! empty( $payload['token'] ) ? $payload['token'] : null;
×
69
                                },
110✔
70
                        ],
110✔
71
                ];
110✔
72
        }
73

74
        /**
75
         * Defines the mutation data modification closure.
76
         *
77
         * @return callable
78
         */
79
        public static function mutate_and_get_payload() {
80
                return static function ( $input ) {
110✔
UNCOV
81
                        global $wp;
×
UNCOV
82
                        if ( ! is_user_logged_in() ) {
×
UNCOV
83
                                throw new UserError( __( 'Must be authenticated to set a default payment method', 'wp-graphql-woocommerce' ) );
×
84
                        }
85

UNCOV
86
                        $token_id = $input['tokenId'];
×
UNCOV
87
                        $token    = WC_Payment_Tokens::get( $token_id );
×
88

UNCOV
89
                        if ( is_null( $token ) || get_current_user_id() !== $token->get_user_id() ) {
×
UNCOV
90
                                throw new UserError( __( 'Invalid payment method.', 'wp-graphql-woocommerce' ) );
×
91
                        }
92

UNCOV
93
                        WC_Payment_Tokens::set_users_default( $token->get_user_id(), intval( $token_id ) );
×
UNCOV
94
                        wc_add_notice( __( 'This payment method was successfully set as your default.', 'wp-graphql-woocommerce' ) );
×
95

UNCOV
96
                        return [
×
UNCOV
97
                                'status' => 'SUCCESS',
×
UNCOV
98
                                'token'  => WC_Payment_Tokens::get( $token_id ),
×
UNCOV
99
                        ];
×
100
                };
110✔
101
        }
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc