@@ -82,12 +82,60 @@ public function testAccessTokenRefreshedAndSavedWhenExpired()
8282 // Run request, which will trigger the above filters as if the token expired and refreshes automatically.
8383 $ result = $ this ->api ->get_account ();
8484
85+ // Confirm no WP_Error is returned.
86+ $ this ->assertNotInstanceOf ( 'WP_Error ' , $ result );
87+ $ this ->assertIsArray ( $ result );
88+
8589 // Confirm "new" tokens now exist in the Plugin's settings, which confirms the `convertkit_api_refresh_token` hook was called when
8690 // the tokens were refreshed.
8791 $ this ->assertEquals ( $ settings ->get_access_token (), $ _ENV ['CONVERTKIT_OAUTH_ACCESS_TOKEN ' ] );
8892 $ this ->assertEquals ( $ settings ->get_refresh_token (), $ _ENV ['CONVERTKIT_OAUTH_REFRESH_TOKEN ' ] );
8993 }
9094
95+ /**
96+ * Test that the Access Token, Refresh Token and Token Expiry are deleted from the Plugin's settings
97+ * when the Access Token used is invalid.
98+ *
99+ * @since 3.1.0
100+ */
101+ public function testAccessTokenDeletedWhenInvalid ()
102+ {
103+ // Save an invalid access token and refresh token in the Plugin's settings.
104+ $ settings = new \ConvertKit_Settings ();
105+ $ settings ->save (
106+ array (
107+ 'access_token ' => 'invalidAccessToken ' ,
108+ 'refresh_token ' => $ _ENV ['CONVERTKIT_OAUTH_REFRESH_TOKEN ' ],
109+ 'token_expires ' => time () + 10000 ,
110+ )
111+ );
112+
113+ // Confirm the tokens saved.
114+ $ this ->assertEquals ( $ settings ->get_access_token (), 'invalidAccessToken ' );
115+ $ this ->assertEquals ( $ settings ->get_refresh_token (), $ _ENV ['CONVERTKIT_OAUTH_REFRESH_TOKEN ' ] );
116+
117+ // Initialize the API using the invalid refresh token.
118+ $ api = new \ConvertKit_API_V4 (
119+ $ _ENV ['CONVERTKIT_OAUTH_CLIENT_ID ' ],
120+ $ _ENV ['KIT_OAUTH_REDIRECT_URI ' ],
121+ $ settings ->get_access_token (),
122+ $ settings ->get_refresh_token ()
123+ );
124+
125+ // Run request.
126+ $ result = $ api ->get_account ();
127+
128+ // Confirm a WP_Error is returned.
129+ $ this ->assertInstanceOf ( 'WP_Error ' , $ result );
130+ $ this ->assertEquals ( $ result ->get_error_code (), 'convertkit_api_error ' );
131+ $ this ->assertEquals ( $ result ->get_error_message (), 'The access token is invalid ' );
132+
133+ // Confirm tokens removed from the Plugin's settings, which confirms the `convertkit_api_access_token_invalid` hook was called when the tokens were deleted.
134+ $ this ->assertEmpty ( $ settings ->get_access_token () );
135+ $ this ->assertEmpty ( $ settings ->get_refresh_token () );
136+ $ this ->assertEmpty ( $ settings ->get_token_expiry () );
137+ }
138+
91139 /**
92140 * Test that a WordPress Cron event is created when an access token is obtained.
93141 *
0 commit comments