@@ -27,6 +27,16 @@ class APITest extends WPTestCase
2727 */
2828 private $ api ;
2929
30+ /**
31+ * Holds the current timestamp, defined in setUp to fix
32+ * it for all tests.
33+ *
34+ * @since 2.8.3
35+ *
36+ * @var int
37+ */
38+ private $ now = 0 ;
39+
3040 /**
3141 * Performs actions before each test.
3242 *
@@ -36,6 +46,9 @@ public function setUp(): void
3646 {
3747 parent ::setUp ();
3848
49+ // Set the current timestamp to the start of the test.
50+ $ this ->now = strtotime ( 'now ' );
51+
3952 // Activate Plugin, to include the Plugin's constants in tests.
4053 activate_plugins ('convertkit/wp-convertkit.php ' );
4154
@@ -77,7 +90,7 @@ public function testAccessTokenRefreshedAndSavedWhenExpired()
7790
7891 // Filter requests to mock the token expiry and refreshing the token.
7992 add_filter ( 'pre_http_request ' , array ( $ this , 'mockAccessTokenExpiredResponse ' ), 10 , 3 );
80- add_filter ( 'pre_http_request ' , array ( $ this , 'mockRefreshTokenResponse ' ), 10 , 3 );
93+ add_filter ( 'pre_http_request ' , array ( $ this , 'mockTokenResponse ' ), 10 , 3 );
8194
8295 // Run request, which will trigger the above filters as if the token expired and refreshes automatically.
8396 $ result = $ this ->api ->get_account ();
@@ -88,6 +101,46 @@ public function testAccessTokenRefreshedAndSavedWhenExpired()
88101 $ this ->assertEquals ( $ settings ->get_refresh_token (), $ _ENV ['CONVERTKIT_OAUTH_REFRESH_TOKEN ' ] );
89102 }
90103
104+ /**
105+ * Test that a WordPress Cron event is created when an access token is obtained.
106+ *
107+ * @since 2.8.3
108+ */
109+ public function testCronEventCreatedWhenAccessTokenObtained ()
110+ {
111+ // Mock request as if the API returned an access and refresh token when a request
112+ // was made to refresh the token.
113+ add_filter ( 'pre_http_request ' , array ( $ this , 'mockTokenResponse ' ), 10 , 3 );
114+
115+ // Run request, as if the access token was obtained successfully.
116+ $ result = $ this ->api ->get_access_token ( 'mockAuthCode ' );
117+
118+ // Confirm the Cron event to refresh the access token was created, and the timestamp to
119+ // run the refresh token call matches the expiry of the access token.
120+ $ nextScheduledTimestamp = wp_next_scheduled ( 'convertkit_refresh_token ' );
121+ $ this ->assertEquals ( $ nextScheduledTimestamp , $ this ->now + 10000 );
122+ }
123+
124+ /**
125+ * Test that a WordPress Cron event is created when an access token is refreshed.
126+ *
127+ * @since 2.8.3
128+ */
129+ public function testCronEventCreatedWhenTokenRefreshed ()
130+ {
131+ // Mock request as if the API returned an access and refresh token when a request
132+ // was made to refresh the token.
133+ add_filter ( 'pre_http_request ' , array ( $ this , 'mockTokenResponse ' ), 10 , 3 );
134+
135+ // Run request, as if the token was refreshed.
136+ $ result = $ this ->api ->refresh_token ();
137+
138+ // Confirm the Cron event to refresh the access token was created, and the timestamp to
139+ // run the refresh token call matches the expiry of the access token.
140+ $ nextScheduledTimestamp = wp_next_scheduled ( 'convertkit_refresh_token ' );
141+ $ this ->assertEquals ( $ nextScheduledTimestamp , $ this ->now + 10000 );
142+ }
143+
91144 /**
92145 * Mocks an API response as if the Access Token expired.
93146 *
@@ -138,15 +191,15 @@ public function mockAccessTokenExpiredResponse( $response, $parsed_args, $url )
138191 * @param string $url Request URL.
139192 * @return mixed
140193 */
141- public function mockRefreshTokenResponse ( $ response , $ parsed_args , $ url )
194+ public function mockTokenResponse ( $ response , $ parsed_args , $ url )
142195 {
143196 // Only mock requests made to the /token endpoint.
144197 if ( strpos ( $ url , 'https://api.kit.com/oauth/token ' ) === false ) {
145198 return $ response ;
146199 }
147200
148201 // Remove this filter, so we don't end up in a loop when retrying the request.
149- remove_filter ( 'pre_http_request ' , array ( $ this , 'mockRefreshTokenResponse ' ) );
202+ remove_filter ( 'pre_http_request ' , array ( $ this , 'mockTokenResponse ' ) );
150203
151204 // Return a mock access and refresh token for this API request, as calling
152205 // refresh_token results in a new access and refresh token being provided,
@@ -158,7 +211,7 @@ public function mockRefreshTokenResponse( $response, $parsed_args, $url )
158211 'access_token ' => $ _ENV ['CONVERTKIT_OAUTH_ACCESS_TOKEN ' ],
159212 'refresh_token ' => $ _ENV ['CONVERTKIT_OAUTH_REFRESH_TOKEN ' ],
160213 'token_type ' => 'bearer ' ,
161- 'created_at ' => strtotime ( ' now ' ) ,
214+ 'created_at ' => $ this -> now ,
162215 'expires_in ' => 10000 ,
163216 'scope ' => 'public ' ,
164217 )
0 commit comments